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
33 #include "libavutil/avstring.h"
34 #include "libavutil/colorspace.h"
35 #include "libavutil/eval.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/dict.h"
40 #include "libavutil/parseutils.h"
41 #include "libavutil/samplefmt.h"
42 #include "libavutil/avassert.h"
43 #include "libavutil/time.h"
44 #include "libavformat/avformat.h"
45 #include "libavdevice/avdevice.h"
46 #include "libswscale/swscale.h"
47 #include "libavutil/opt.h"
48 #include "libavcodec/avfft.h"
49 #include "libswresample/swresample.h"
52 # include "libavfilter/avcodec.h"
53 # include "libavfilter/avfilter.h"
54 # include "libavfilter/buffersink.h"
55 # include "libavfilter/buffersrc.h"
59 #include <SDL_thread.h>
65 const char program_name[] = "ffplay";
66 const int program_birth_year = 2003;
68 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
71 /* Minimum SDL audio buffer size, in samples. */
72 #define SDL_AUDIO_MIN_BUFFER_SIZE 512
73 /* Calculate actual buffer size keeping in mind not cause too frequent audio callbacks */
74 #define SDL_AUDIO_MAX_CALLBACKS_PER_SEC 30
76 /* no AV sync correction is done if below the minimum AV sync threshold */
77 #define AV_SYNC_THRESHOLD_MIN 0.04
78 /* AV sync correction is done if above the maximum AV sync threshold */
79 #define AV_SYNC_THRESHOLD_MAX 0.1
80 /* If a frame duration is longer than this, it will not be duplicated to compensate AV sync */
81 #define AV_SYNC_FRAMEDUP_THRESHOLD 0.1
82 /* no AV correction is done if too big error */
83 #define AV_NOSYNC_THRESHOLD 10.0
85 /* maximum audio speed change to get correct sync */
86 #define SAMPLE_CORRECTION_PERCENT_MAX 10
88 /* external clock speed adjustment constants for realtime sources based on buffer fullness */
89 #define EXTERNAL_CLOCK_SPEED_MIN 0.900
90 #define EXTERNAL_CLOCK_SPEED_MAX 1.010
91 #define EXTERNAL_CLOCK_SPEED_STEP 0.001
93 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
94 #define AUDIO_DIFF_AVG_NB 20
96 /* polls for possible required screen refresh at least this often, should be less than 1/fps */
97 #define REFRESH_RATE 0.01
99 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
100 /* TODO: We assume that a decoded and resampled frame fits into this buffer */
101 #define SAMPLE_ARRAY_SIZE (8 * 65536)
103 #define CURSOR_HIDE_DELAY 1000000
105 static int64_t sws_flags = SWS_BICUBIC;
107 typedef struct MyAVPacketList {
109 struct MyAVPacketList *next;
113 typedef struct PacketQueue {
114 MyAVPacketList *first_pkt, *last_pkt;
123 #define VIDEO_PICTURE_QUEUE_SIZE 3
124 #define SUBPICTURE_QUEUE_SIZE 16
125 #define SAMPLE_QUEUE_SIZE 9
126 #define FRAME_QUEUE_SIZE FFMAX(SAMPLE_QUEUE_SIZE, FFMAX(VIDEO_PICTURE_QUEUE_SIZE, SUBPICTURE_QUEUE_SIZE))
128 typedef struct AudioParams {
131 int64_t channel_layout;
132 enum AVSampleFormat fmt;
137 typedef struct Clock {
138 double pts; /* clock base */
139 double pts_drift; /* clock base minus time at which we updated the clock */
142 int serial; /* clock is based on a packet with this serial */
144 int *queue_serial; /* pointer to the current packet queue serial, used for obsolete clock detection */
147 /* Common struct for handling all types of decoded data and allocated render buffers. */
148 typedef struct Frame {
152 double pts; /* presentation timestamp for the frame */
153 double duration; /* estimated duration of the frame */
154 int64_t pos; /* byte position of the frame in the input file */
163 typedef struct FrameQueue {
164 Frame queue[FRAME_QUEUE_SIZE];
177 AV_SYNC_AUDIO_MASTER, /* default choice */
178 AV_SYNC_VIDEO_MASTER,
179 AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
182 typedef struct Decoder {
186 AVCodecContext *avctx;
190 SDL_cond *empty_queue_cond;
192 AVRational start_pts_tb;
194 AVRational next_pts_tb;
195 SDL_Thread *decoder_tid;
198 typedef struct VideoState {
199 SDL_Thread *read_tid;
200 AVInputFormat *iformat;
205 int queue_attachments_req;
210 int read_pause_return;
231 int audio_clock_serial;
232 double audio_diff_cum; /* used for AV difference average computation */
233 double audio_diff_avg_coef;
234 double audio_diff_threshold;
235 int audio_diff_avg_count;
238 int audio_hw_buf_size;
239 uint8_t silence_buf[SDL_AUDIO_MIN_BUFFER_SIZE];
242 unsigned int audio_buf_size; /* in bytes */
243 unsigned int audio_buf1_size;
244 int audio_buf_index; /* in bytes */
245 int audio_write_buf_size;
246 struct AudioParams audio_src;
248 struct AudioParams audio_filter_src;
250 struct AudioParams audio_tgt;
251 struct SwrContext *swr_ctx;
252 int frame_drops_early;
253 int frame_drops_late;
256 SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
258 int16_t sample_array[SAMPLE_ARRAY_SIZE];
259 int sample_array_index;
263 FFTSample *rdft_data;
265 double last_vis_time;
268 AVStream *subtitle_st;
269 PacketQueue subtitleq;
272 double frame_last_returned_time;
273 double frame_last_filter_delay;
277 double max_frame_duration; // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity
279 struct SwsContext *img_convert_ctx;
281 SDL_Rect last_display_rect;
285 int width, height, xleft, ytop;
290 AVFilterContext *in_video_filter; // the first filter in the video chain
291 AVFilterContext *out_video_filter; // the last filter in the video chain
292 AVFilterContext *in_audio_filter; // the first filter in the audio chain
293 AVFilterContext *out_audio_filter; // the last filter in the audio chain
294 AVFilterGraph *agraph; // audio filter graph
297 int last_video_stream, last_audio_stream, last_subtitle_stream;
299 SDL_cond *continue_read_thread;
302 /* options specified by the user */
303 static AVInputFormat *file_iformat;
304 static const char *input_filename;
305 static const char *window_title;
306 static int fs_screen_width;
307 static int fs_screen_height;
308 static int default_width = 640;
309 static int default_height = 480;
310 static int screen_width = 0;
311 static int screen_height = 0;
312 static int audio_disable;
313 static int video_disable;
314 static int subtitle_disable;
315 static const char* wanted_stream_spec[AVMEDIA_TYPE_NB] = {0};
316 static int seek_by_bytes = -1;
317 static int display_disable;
318 static int show_status = 1;
319 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
320 static int64_t start_time = AV_NOPTS_VALUE;
321 static int64_t duration = AV_NOPTS_VALUE;
323 static int genpts = 0;
324 static int lowres = 0;
325 static int decoder_reorder_pts = -1;
327 static int exit_on_keydown;
328 static int exit_on_mousedown;
330 static int framedrop = -1;
331 static int infinite_buffer = -1;
332 static enum ShowMode show_mode = SHOW_MODE_NONE;
333 static const char *audio_codec_name;
334 static const char *subtitle_codec_name;
335 static const char *video_codec_name;
336 double rdftspeed = 0.02;
337 static int64_t cursor_last_shown;
338 static int cursor_hidden = 0;
340 static const char **vfilters_list = NULL;
341 static int nb_vfilters = 0;
342 static char *afilters = NULL;
344 static int autorotate = 1;
346 /* current context */
347 static int is_full_screen;
348 static int64_t audio_callback_time;
350 static AVPacket flush_pkt;
352 #define FF_ALLOC_EVENT (SDL_USEREVENT)
353 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
355 static SDL_Surface *screen;
358 static int opt_add_vfilter(void *optctx, const char *opt, const char *arg)
360 GROW_ARRAY(vfilters_list, nb_vfilters);
361 vfilters_list[nb_vfilters - 1] = arg;
367 int cmp_audio_fmts(enum AVSampleFormat fmt1, int64_t channel_count1,
368 enum AVSampleFormat fmt2, int64_t channel_count2)
370 /* If channel count == 1, planar and non-planar formats are the same */
371 if (channel_count1 == 1 && channel_count2 == 1)
372 return av_get_packed_sample_fmt(fmt1) != av_get_packed_sample_fmt(fmt2);
374 return channel_count1 != channel_count2 || fmt1 != fmt2;
378 int64_t get_valid_channel_layout(int64_t channel_layout, int channels)
380 if (channel_layout && av_get_channel_layout_nb_channels(channel_layout) == channels)
381 return channel_layout;
386 static void free_picture(Frame *vp);
388 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
390 MyAVPacketList *pkt1;
392 if (q->abort_request)
395 pkt1 = av_malloc(sizeof(MyAVPacketList));
400 if (pkt == &flush_pkt)
402 pkt1->serial = q->serial;
407 q->last_pkt->next = pkt1;
410 q->size += pkt1->pkt.size + sizeof(*pkt1);
411 /* XXX: should duplicate packet data in DV case */
412 SDL_CondSignal(q->cond);
416 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
420 /* duplicate the packet */
421 if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
424 SDL_LockMutex(q->mutex);
425 ret = packet_queue_put_private(q, pkt);
426 SDL_UnlockMutex(q->mutex);
428 if (pkt != &flush_pkt && ret < 0)
434 static int packet_queue_put_nullpacket(PacketQueue *q, int stream_index)
436 AVPacket pkt1, *pkt = &pkt1;
440 pkt->stream_index = stream_index;
441 return packet_queue_put(q, pkt);
444 /* packet queue handling */
445 static void packet_queue_init(PacketQueue *q)
447 memset(q, 0, sizeof(PacketQueue));
448 q->mutex = SDL_CreateMutex();
449 q->cond = SDL_CreateCond();
450 q->abort_request = 1;
453 static void packet_queue_flush(PacketQueue *q)
455 MyAVPacketList *pkt, *pkt1;
457 SDL_LockMutex(q->mutex);
458 for (pkt = q->first_pkt; pkt; pkt = pkt1) {
460 av_free_packet(&pkt->pkt);
467 SDL_UnlockMutex(q->mutex);
470 static void packet_queue_destroy(PacketQueue *q)
472 packet_queue_flush(q);
473 SDL_DestroyMutex(q->mutex);
474 SDL_DestroyCond(q->cond);
477 static void packet_queue_abort(PacketQueue *q)
479 SDL_LockMutex(q->mutex);
481 q->abort_request = 1;
483 SDL_CondSignal(q->cond);
485 SDL_UnlockMutex(q->mutex);
488 static void packet_queue_start(PacketQueue *q)
490 SDL_LockMutex(q->mutex);
491 q->abort_request = 0;
492 packet_queue_put_private(q, &flush_pkt);
493 SDL_UnlockMutex(q->mutex);
496 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
497 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
499 MyAVPacketList *pkt1;
502 SDL_LockMutex(q->mutex);
505 if (q->abort_request) {
512 q->first_pkt = pkt1->next;
516 q->size -= pkt1->pkt.size + sizeof(*pkt1);
519 *serial = pkt1->serial;
527 SDL_CondWait(q->cond, q->mutex);
530 SDL_UnlockMutex(q->mutex);
534 static void decoder_init(Decoder *d, AVCodecContext *avctx, PacketQueue *queue, SDL_cond *empty_queue_cond) {
535 memset(d, 0, sizeof(Decoder));
538 d->empty_queue_cond = empty_queue_cond;
539 d->start_pts = AV_NOPTS_VALUE;
542 static int decoder_decode_frame(Decoder *d, AVFrame *frame, AVSubtitle *sub) {
548 if (d->queue->abort_request)
551 if (!d->packet_pending || d->queue->serial != d->pkt_serial) {
554 if (d->queue->nb_packets == 0)
555 SDL_CondSignal(d->empty_queue_cond);
556 if (packet_queue_get(d->queue, &pkt, 1, &d->pkt_serial) < 0)
558 if (pkt.data == flush_pkt.data) {
559 avcodec_flush_buffers(d->avctx);
561 d->next_pts = d->start_pts;
562 d->next_pts_tb = d->start_pts_tb;
564 } while (pkt.data == flush_pkt.data || d->queue->serial != d->pkt_serial);
565 av_free_packet(&d->pkt);
566 d->pkt_temp = d->pkt = pkt;
567 d->packet_pending = 1;
570 switch (d->avctx->codec_type) {
571 case AVMEDIA_TYPE_VIDEO:
572 ret = avcodec_decode_video2(d->avctx, frame, &got_frame, &d->pkt_temp);
574 if (decoder_reorder_pts == -1) {
575 frame->pts = av_frame_get_best_effort_timestamp(frame);
576 } else if (decoder_reorder_pts) {
577 frame->pts = frame->pkt_pts;
579 frame->pts = frame->pkt_dts;
583 case AVMEDIA_TYPE_AUDIO:
584 ret = avcodec_decode_audio4(d->avctx, frame, &got_frame, &d->pkt_temp);
586 AVRational tb = (AVRational){1, frame->sample_rate};
587 if (frame->pts != AV_NOPTS_VALUE)
588 frame->pts = av_rescale_q(frame->pts, d->avctx->time_base, tb);
589 else if (frame->pkt_pts != AV_NOPTS_VALUE)
590 frame->pts = av_rescale_q(frame->pkt_pts, av_codec_get_pkt_timebase(d->avctx), tb);
591 else if (d->next_pts != AV_NOPTS_VALUE)
592 frame->pts = av_rescale_q(d->next_pts, d->next_pts_tb, tb);
593 if (frame->pts != AV_NOPTS_VALUE) {
594 d->next_pts = frame->pts + frame->nb_samples;
599 case AVMEDIA_TYPE_SUBTITLE:
600 ret = avcodec_decode_subtitle2(d->avctx, sub, &got_frame, &d->pkt_temp);
605 d->packet_pending = 0;
608 d->pkt_temp.pts = AV_NOPTS_VALUE;
609 if (d->pkt_temp.data) {
610 if (d->avctx->codec_type != AVMEDIA_TYPE_AUDIO)
611 ret = d->pkt_temp.size;
612 d->pkt_temp.data += ret;
613 d->pkt_temp.size -= ret;
614 if (d->pkt_temp.size <= 0)
615 d->packet_pending = 0;
618 d->packet_pending = 0;
619 d->finished = d->pkt_serial;
623 } while (!got_frame && !d->finished);
628 static void decoder_destroy(Decoder *d) {
629 av_free_packet(&d->pkt);
632 static void frame_queue_unref_item(Frame *vp)
634 av_frame_unref(vp->frame);
635 avsubtitle_free(&vp->sub);
638 static int frame_queue_init(FrameQueue *f, PacketQueue *pktq, int max_size, int keep_last)
641 memset(f, 0, sizeof(FrameQueue));
642 if (!(f->mutex = SDL_CreateMutex()))
643 return AVERROR(ENOMEM);
644 if (!(f->cond = SDL_CreateCond()))
645 return AVERROR(ENOMEM);
647 f->max_size = FFMIN(max_size, FRAME_QUEUE_SIZE);
648 f->keep_last = !!keep_last;
649 for (i = 0; i < f->max_size; i++)
650 if (!(f->queue[i].frame = av_frame_alloc()))
651 return AVERROR(ENOMEM);
655 static void frame_queue_destory(FrameQueue *f)
658 for (i = 0; i < f->max_size; i++) {
659 Frame *vp = &f->queue[i];
660 frame_queue_unref_item(vp);
661 av_frame_free(&vp->frame);
664 SDL_DestroyMutex(f->mutex);
665 SDL_DestroyCond(f->cond);
668 static void frame_queue_signal(FrameQueue *f)
670 SDL_LockMutex(f->mutex);
671 SDL_CondSignal(f->cond);
672 SDL_UnlockMutex(f->mutex);
675 static Frame *frame_queue_peek(FrameQueue *f)
677 return &f->queue[(f->rindex + f->rindex_shown) % f->max_size];
680 static Frame *frame_queue_peek_next(FrameQueue *f)
682 return &f->queue[(f->rindex + f->rindex_shown + 1) % f->max_size];
685 static Frame *frame_queue_peek_last(FrameQueue *f)
687 return &f->queue[f->rindex];
690 static Frame *frame_queue_peek_writable(FrameQueue *f)
692 /* wait until we have space to put a new frame */
693 SDL_LockMutex(f->mutex);
694 while (f->size >= f->max_size &&
695 !f->pktq->abort_request) {
696 SDL_CondWait(f->cond, f->mutex);
698 SDL_UnlockMutex(f->mutex);
700 if (f->pktq->abort_request)
703 return &f->queue[f->windex];
706 static Frame *frame_queue_peek_readable(FrameQueue *f)
708 /* wait until we have a readable a new frame */
709 SDL_LockMutex(f->mutex);
710 while (f->size - f->rindex_shown <= 0 &&
711 !f->pktq->abort_request) {
712 SDL_CondWait(f->cond, f->mutex);
714 SDL_UnlockMutex(f->mutex);
716 if (f->pktq->abort_request)
719 return &f->queue[(f->rindex + f->rindex_shown) % f->max_size];
722 static void frame_queue_push(FrameQueue *f)
724 if (++f->windex == f->max_size)
726 SDL_LockMutex(f->mutex);
728 SDL_CondSignal(f->cond);
729 SDL_UnlockMutex(f->mutex);
732 static void frame_queue_next(FrameQueue *f)
734 if (f->keep_last && !f->rindex_shown) {
738 frame_queue_unref_item(&f->queue[f->rindex]);
739 if (++f->rindex == f->max_size)
741 SDL_LockMutex(f->mutex);
743 SDL_CondSignal(f->cond);
744 SDL_UnlockMutex(f->mutex);
747 /* jump back to the previous frame if available by resetting rindex_shown */
748 static int frame_queue_prev(FrameQueue *f)
750 int ret = f->rindex_shown;
755 /* return the number of undisplayed frames in the queue */
756 static int frame_queue_nb_remaining(FrameQueue *f)
758 return f->size - f->rindex_shown;
761 /* return last shown position */
762 static int64_t frame_queue_last_pos(FrameQueue *f)
764 Frame *fp = &f->queue[f->rindex];
765 if (f->rindex_shown && fp->serial == f->pktq->serial)
771 static void decoder_abort(Decoder *d, FrameQueue *fq)
773 packet_queue_abort(d->queue);
774 frame_queue_signal(fq);
775 SDL_WaitThread(d->decoder_tid, NULL);
776 d->decoder_tid = NULL;
777 packet_queue_flush(d->queue);
780 static inline void fill_rectangle(SDL_Surface *screen,
781 int x, int y, int w, int h, int color, int update)
788 SDL_FillRect(screen, &rect, color);
789 if (update && w > 0 && h > 0)
790 SDL_UpdateRect(screen, x, y, w, h);
793 /* draw only the border of a rectangle */
794 static void fill_border(int xleft, int ytop, int width, int height, int x, int y, int w, int h, int color, int update)
798 /* fill the background */
802 w2 = width - (x + w);
808 h2 = height - (y + h);
811 fill_rectangle(screen,
815 fill_rectangle(screen,
816 xleft + width - w2, ytop,
819 fill_rectangle(screen,
823 fill_rectangle(screen,
824 xleft + w1, ytop + height - h2,
829 #define ALPHA_BLEND(a, oldp, newp, s)\
830 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
832 #define RGBA_IN(r, g, b, a, s)\
834 unsigned int v = ((const uint32_t *)(s))[0];\
835 a = (v >> 24) & 0xff;\
836 r = (v >> 16) & 0xff;\
837 g = (v >> 8) & 0xff;\
841 #define YUVA_IN(y, u, v, a, s, pal)\
843 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
844 a = (val >> 24) & 0xff;\
845 y = (val >> 16) & 0xff;\
846 u = (val >> 8) & 0xff;\
850 #define YUVA_OUT(d, y, u, v, a)\
852 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
858 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
860 int wrap, wrap3, width2, skip2;
861 int y, u, v, a, u1, v1, a1, w, h;
862 uint8_t *lum, *cb, *cr;
865 int dstx, dsty, dstw, dsth;
867 dstw = av_clip(rect->w, 0, imgw);
868 dsth = av_clip(rect->h, 0, imgh);
869 dstx = av_clip(rect->x, 0, imgw - dstw);
870 dsty = av_clip(rect->y, 0, imgh - dsth);
871 lum = dst->data[0] + dsty * dst->linesize[0];
872 cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
873 cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
875 width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
877 wrap = dst->linesize[0];
878 wrap3 = rect->pict.linesize[0];
879 p = rect->pict.data[0];
880 pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
888 YUVA_IN(y, u, v, a, p, pal);
889 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
890 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
891 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
897 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
898 YUVA_IN(y, u, v, a, p, pal);
902 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
904 YUVA_IN(y, u, v, a, p + BPP, pal);
908 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
909 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
910 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
917 YUVA_IN(y, u, v, a, p, pal);
918 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
919 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
920 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
924 p += wrap3 - dstw * BPP;
925 lum += wrap - dstw - dstx;
926 cb += dst->linesize[1] - width2 - skip2;
927 cr += dst->linesize[2] - width2 - skip2;
929 for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
935 YUVA_IN(y, u, v, a, p, pal);
939 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
942 YUVA_IN(y, u, v, a, p, pal);
946 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
947 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
948 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
954 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
955 YUVA_IN(y, u, v, a, p, pal);
959 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
961 YUVA_IN(y, u, v, a, p + BPP, pal);
965 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
969 YUVA_IN(y, u, v, a, p, pal);
973 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
975 YUVA_IN(y, u, v, a, p + BPP, pal);
979 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
981 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
982 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
986 p += -wrap3 + 2 * BPP;
990 YUVA_IN(y, u, v, a, p, pal);
994 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
997 YUVA_IN(y, u, v, a, p, pal);
1001 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
1002 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
1003 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
1009 p += wrap3 + (wrap3 - dstw * BPP);
1010 lum += wrap + (wrap - dstw - dstx);
1011 cb += dst->linesize[1] - width2 - skip2;
1012 cr += dst->linesize[2] - width2 - skip2;
1014 /* handle odd height */
1021 YUVA_IN(y, u, v, a, p, pal);
1022 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
1023 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
1024 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
1030 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
1031 YUVA_IN(y, u, v, a, p, pal);
1035 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
1037 YUVA_IN(y, u, v, a, p + BPP, pal);
1041 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
1042 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
1043 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
1050 YUVA_IN(y, u, v, a, p, pal);
1051 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
1052 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
1053 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
1058 static void free_picture(Frame *vp)
1061 SDL_FreeYUVOverlay(vp->bmp);
1066 static void calculate_display_rect(SDL_Rect *rect,
1067 int scr_xleft, int scr_ytop, int scr_width, int scr_height,
1068 int pic_width, int pic_height, AVRational pic_sar)
1071 int width, height, x, y;
1073 if (pic_sar.num == 0)
1076 aspect_ratio = av_q2d(pic_sar);
1078 if (aspect_ratio <= 0.0)
1080 aspect_ratio *= (float)pic_width / (float)pic_height;
1082 /* XXX: we suppose the screen has a 1.0 pixel ratio */
1083 height = scr_height;
1084 width = ((int)rint(height * aspect_ratio)) & ~1;
1085 if (width > scr_width) {
1087 height = ((int)rint(width / aspect_ratio)) & ~1;
1089 x = (scr_width - width) / 2;
1090 y = (scr_height - height) / 2;
1091 rect->x = scr_xleft + x;
1092 rect->y = scr_ytop + y;
1093 rect->w = FFMAX(width, 1);
1094 rect->h = FFMAX(height, 1);
1097 static void video_image_display(VideoState *is)
1105 vp = frame_queue_peek(&is->pictq);
1107 if (is->subtitle_st) {
1108 if (frame_queue_nb_remaining(&is->subpq) > 0) {
1109 sp = frame_queue_peek(&is->subpq);
1111 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
1112 SDL_LockYUVOverlay (vp->bmp);
1114 pict.data[0] = vp->bmp->pixels[0];
1115 pict.data[1] = vp->bmp->pixels[2];
1116 pict.data[2] = vp->bmp->pixels[1];
1118 pict.linesize[0] = vp->bmp->pitches[0];
1119 pict.linesize[1] = vp->bmp->pitches[2];
1120 pict.linesize[2] = vp->bmp->pitches[1];
1122 for (i = 0; i < sp->sub.num_rects; i++)
1123 blend_subrect(&pict, sp->sub.rects[i],
1124 vp->bmp->w, vp->bmp->h);
1126 SDL_UnlockYUVOverlay (vp->bmp);
1131 calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp->width, vp->height, vp->sar);
1133 SDL_DisplayYUVOverlay(vp->bmp, &rect);
1135 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) {
1136 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
1137 fill_border(is->xleft, is->ytop, is->width, is->height, rect.x, rect.y, rect.w, rect.h, bgcolor, 1);
1138 is->last_display_rect = rect;
1143 static inline int compute_mod(int a, int b)
1145 return a < 0 ? a%b + b : a%b;
1148 static void video_audio_display(VideoState *s)
1150 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
1151 int ch, channels, h, h2, bgcolor, fgcolor;
1153 int rdft_bits, nb_freq;
1155 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
1157 nb_freq = 1 << (rdft_bits - 1);
1159 /* compute display index : center on currently output samples */
1160 channels = s->audio_tgt.channels;
1161 nb_display_channels = channels;
1163 int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
1165 delay = s->audio_write_buf_size;
1168 /* to be more precise, we take into account the time spent since
1169 the last buffer computation */
1170 if (audio_callback_time) {
1171 time_diff = av_gettime_relative() - audio_callback_time;
1172 delay -= (time_diff * s->audio_tgt.freq) / 1000000;
1175 delay += 2 * data_used;
1176 if (delay < data_used)
1179 i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
1180 if (s->show_mode == SHOW_MODE_WAVES) {
1182 for (i = 0; i < 1000; i += channels) {
1183 int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
1184 int a = s->sample_array[idx];
1185 int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
1186 int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
1187 int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
1189 if (h < score && (b ^ c) < 0) {
1196 s->last_i_start = i_start;
1198 i_start = s->last_i_start;
1201 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
1202 if (s->show_mode == SHOW_MODE_WAVES) {
1203 fill_rectangle(screen,
1204 s->xleft, s->ytop, s->width, s->height,
1207 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
1209 /* total height for one channel */
1210 h = s->height / nb_display_channels;
1211 /* graph height / 2 */
1213 for (ch = 0; ch < nb_display_channels; ch++) {
1215 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
1216 for (x = 0; x < s->width; x++) {
1217 y = (s->sample_array[i] * h2) >> 15;
1224 fill_rectangle(screen,
1225 s->xleft + x, ys, 1, y,
1228 if (i >= SAMPLE_ARRAY_SIZE)
1229 i -= SAMPLE_ARRAY_SIZE;
1233 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
1235 for (ch = 1; ch < nb_display_channels; ch++) {
1236 y = s->ytop + ch * h;
1237 fill_rectangle(screen,
1238 s->xleft, y, s->width, 1,
1241 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
1243 nb_display_channels= FFMIN(nb_display_channels, 2);
1244 if (rdft_bits != s->rdft_bits) {
1245 av_rdft_end(s->rdft);
1246 av_free(s->rdft_data);
1247 s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
1248 s->rdft_bits = rdft_bits;
1249 s->rdft_data = av_malloc_array(nb_freq, 4 *sizeof(*s->rdft_data));
1251 if (!s->rdft || !s->rdft_data){
1252 av_log(NULL, AV_LOG_ERROR, "Failed to allocate buffers for RDFT, switching to waves display\n");
1253 s->show_mode = SHOW_MODE_WAVES;
1256 for (ch = 0; ch < nb_display_channels; ch++) {
1257 data[ch] = s->rdft_data + 2 * nb_freq * ch;
1259 for (x = 0; x < 2 * nb_freq; x++) {
1260 double w = (x-nb_freq) * (1.0 / nb_freq);
1261 data[ch][x] = s->sample_array[i] * (1.0 - w * w);
1263 if (i >= SAMPLE_ARRAY_SIZE)
1264 i -= SAMPLE_ARRAY_SIZE;
1266 av_rdft_calc(s->rdft, data[ch]);
1268 /* Least efficient way to do this, we should of course
1269 * directly access it but it is more than fast enough. */
1270 for (y = 0; y < s->height; y++) {
1271 double w = 1 / sqrt(nb_freq);
1272 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]));
1273 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
1274 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
1277 fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
1279 fill_rectangle(screen,
1280 s->xpos, s->height-y, 1, 1,
1284 SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
1287 if (s->xpos >= s->width)
1292 static void stream_close(VideoState *is)
1294 /* XXX: use a special url_shutdown call to abort parse cleanly */
1295 is->abort_request = 1;
1296 SDL_WaitThread(is->read_tid, NULL);
1297 packet_queue_destroy(&is->videoq);
1298 packet_queue_destroy(&is->audioq);
1299 packet_queue_destroy(&is->subtitleq);
1301 /* free all pictures */
1302 frame_queue_destory(&is->pictq);
1303 frame_queue_destory(&is->sampq);
1304 frame_queue_destory(&is->subpq);
1305 SDL_DestroyCond(is->continue_read_thread);
1306 #if !CONFIG_AVFILTER
1307 sws_freeContext(is->img_convert_ctx);
1312 static void do_exit(VideoState *is)
1317 av_lockmgr_register(NULL);
1320 av_freep(&vfilters_list);
1322 avformat_network_deinit();
1326 av_log(NULL, AV_LOG_QUIET, "%s", "");
1330 static void sigterm_handler(int sig)
1335 static void set_default_window_size(int width, int height, AVRational sar)
1338 calculate_display_rect(&rect, 0, 0, INT_MAX, height, width, height, sar);
1339 default_width = rect.w;
1340 default_height = rect.h;
1343 static int video_open(VideoState *is, int force_set_video_mode, Frame *vp)
1345 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
1348 if (is_full_screen) flags |= SDL_FULLSCREEN;
1349 else flags |= SDL_RESIZABLE;
1351 if (vp && vp->width)
1352 set_default_window_size(vp->width, vp->height, vp->sar);
1354 if (is_full_screen && fs_screen_width) {
1355 w = fs_screen_width;
1356 h = fs_screen_height;
1357 } else if (!is_full_screen && screen_width) {
1364 w = FFMIN(16383, w);
1365 if (screen && is->width == screen->w && screen->w == w
1366 && is->height== screen->h && screen->h == h && !force_set_video_mode)
1368 screen = SDL_SetVideoMode(w, h, 0, flags);
1370 av_log(NULL, AV_LOG_FATAL, "SDL: could not set video mode - exiting\n");
1374 window_title = input_filename;
1375 SDL_WM_SetCaption(window_title, window_title);
1377 is->width = screen->w;
1378 is->height = screen->h;
1383 /* display the current picture, if any */
1384 static void video_display(VideoState *is)
1387 video_open(is, 0, NULL);
1388 if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
1389 video_audio_display(is);
1390 else if (is->video_st)
1391 video_image_display(is);
1394 static double get_clock(Clock *c)
1396 if (*c->queue_serial != c->serial)
1401 double time = av_gettime_relative() / 1000000.0;
1402 return c->pts_drift + time - (time - c->last_updated) * (1.0 - c->speed);
1406 static void set_clock_at(Clock *c, double pts, int serial, double time)
1409 c->last_updated = time;
1410 c->pts_drift = c->pts - time;
1414 static void set_clock(Clock *c, double pts, int serial)
1416 double time = av_gettime_relative() / 1000000.0;
1417 set_clock_at(c, pts, serial, time);
1420 static void set_clock_speed(Clock *c, double speed)
1422 set_clock(c, get_clock(c), c->serial);
1426 static void init_clock(Clock *c, int *queue_serial)
1430 c->queue_serial = queue_serial;
1431 set_clock(c, NAN, -1);
1434 static void sync_clock_to_slave(Clock *c, Clock *slave)
1436 double clock = get_clock(c);
1437 double slave_clock = get_clock(slave);
1438 if (!isnan(slave_clock) && (isnan(clock) || fabs(clock - slave_clock) > AV_NOSYNC_THRESHOLD))
1439 set_clock(c, slave_clock, slave->serial);
1442 static int get_master_sync_type(VideoState *is) {
1443 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
1445 return AV_SYNC_VIDEO_MASTER;
1447 return AV_SYNC_AUDIO_MASTER;
1448 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
1450 return AV_SYNC_AUDIO_MASTER;
1452 return AV_SYNC_EXTERNAL_CLOCK;
1454 return AV_SYNC_EXTERNAL_CLOCK;
1458 /* get the current master clock value */
1459 static double get_master_clock(VideoState *is)
1463 switch (get_master_sync_type(is)) {
1464 case AV_SYNC_VIDEO_MASTER:
1465 val = get_clock(&is->vidclk);
1467 case AV_SYNC_AUDIO_MASTER:
1468 val = get_clock(&is->audclk);
1471 val = get_clock(&is->extclk);
1477 static void check_external_clock_speed(VideoState *is) {
1478 if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
1479 is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
1480 set_clock_speed(&is->extclk, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->extclk.speed - EXTERNAL_CLOCK_SPEED_STEP));
1481 } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
1482 (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
1483 set_clock_speed(&is->extclk, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->extclk.speed + EXTERNAL_CLOCK_SPEED_STEP));
1485 double speed = is->extclk.speed;
1487 set_clock_speed(&is->extclk, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
1491 /* seek in the stream */
1492 static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
1494 if (!is->seek_req) {
1497 is->seek_flags &= ~AVSEEK_FLAG_BYTE;
1499 is->seek_flags |= AVSEEK_FLAG_BYTE;
1501 SDL_CondSignal(is->continue_read_thread);
1505 /* pause or resume the video */
1506 static void stream_toggle_pause(VideoState *is)
1509 is->frame_timer += av_gettime_relative() / 1000000.0 - is->vidclk.last_updated;
1510 if (is->read_pause_return != AVERROR(ENOSYS)) {
1511 is->vidclk.paused = 0;
1513 set_clock(&is->vidclk, get_clock(&is->vidclk), is->vidclk.serial);
1515 set_clock(&is->extclk, get_clock(&is->extclk), is->extclk.serial);
1516 is->paused = is->audclk.paused = is->vidclk.paused = is->extclk.paused = !is->paused;
1519 static void toggle_pause(VideoState *is)
1521 stream_toggle_pause(is);
1525 static void step_to_next_frame(VideoState *is)
1527 /* if the stream is paused unpause it, then step */
1529 stream_toggle_pause(is);
1533 static double compute_target_delay(double delay, VideoState *is)
1535 double sync_threshold, diff = 0;
1537 /* update delay to follow master synchronisation source */
1538 if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
1539 /* if video is slave, we try to correct big delays by
1540 duplicating or deleting a frame */
1541 diff = get_clock(&is->vidclk) - get_master_clock(is);
1543 /* skip or repeat frame. We take into account the
1544 delay to compute the threshold. I still don't know
1545 if it is the best guess */
1546 sync_threshold = FFMAX(AV_SYNC_THRESHOLD_MIN, FFMIN(AV_SYNC_THRESHOLD_MAX, delay));
1547 if (!isnan(diff) && fabs(diff) < is->max_frame_duration) {
1548 if (diff <= -sync_threshold)
1549 delay = FFMAX(0, delay + diff);
1550 else if (diff >= sync_threshold && delay > AV_SYNC_FRAMEDUP_THRESHOLD)
1551 delay = delay + diff;
1552 else if (diff >= sync_threshold)
1557 av_log(NULL, AV_LOG_TRACE, "video: delay=%0.3f A-V=%f\n",
1563 static double vp_duration(VideoState *is, Frame *vp, Frame *nextvp) {
1564 if (vp->serial == nextvp->serial) {
1565 double duration = nextvp->pts - vp->pts;
1566 if (isnan(duration) || duration <= 0 || duration > is->max_frame_duration)
1567 return vp->duration;
1575 static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
1576 /* update current video pts */
1577 set_clock(&is->vidclk, pts, serial);
1578 sync_clock_to_slave(&is->extclk, &is->vidclk);
1581 /* called to display each frame */
1582 static void video_refresh(void *opaque, double *remaining_time)
1584 VideoState *is = opaque;
1589 if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
1590 check_external_clock_speed(is);
1592 if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
1593 time = av_gettime_relative() / 1000000.0;
1594 if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
1596 is->last_vis_time = time;
1598 *remaining_time = FFMIN(*remaining_time, is->last_vis_time + rdftspeed - time);
1603 if (is->force_refresh)
1604 redisplay = frame_queue_prev(&is->pictq);
1606 if (frame_queue_nb_remaining(&is->pictq) == 0) {
1607 // nothing to do, no picture to display in the queue
1609 double last_duration, duration, delay;
1612 /* dequeue the picture */
1613 lastvp = frame_queue_peek_last(&is->pictq);
1614 vp = frame_queue_peek(&is->pictq);
1616 if (vp->serial != is->videoq.serial) {
1617 frame_queue_next(&is->pictq);
1622 if (lastvp->serial != vp->serial && !redisplay)
1623 is->frame_timer = av_gettime_relative() / 1000000.0;
1628 /* compute nominal last_duration */
1629 last_duration = vp_duration(is, lastvp, vp);
1633 delay = compute_target_delay(last_duration, is);
1635 time= av_gettime_relative()/1000000.0;
1636 if (time < is->frame_timer + delay && !redisplay) {
1637 *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
1641 is->frame_timer += delay;
1642 if (delay > 0 && time - is->frame_timer > AV_SYNC_THRESHOLD_MAX)
1643 is->frame_timer = time;
1645 SDL_LockMutex(is->pictq.mutex);
1646 if (!redisplay && !isnan(vp->pts))
1647 update_video_pts(is, vp->pts, vp->pos, vp->serial);
1648 SDL_UnlockMutex(is->pictq.mutex);
1650 if (frame_queue_nb_remaining(&is->pictq) > 1) {
1651 Frame *nextvp = frame_queue_peek_next(&is->pictq);
1652 duration = vp_duration(is, vp, nextvp);
1653 if(!is->step && (redisplay || framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
1655 is->frame_drops_late++;
1656 frame_queue_next(&is->pictq);
1662 if (is->subtitle_st) {
1663 while (frame_queue_nb_remaining(&is->subpq) > 0) {
1664 sp = frame_queue_peek(&is->subpq);
1666 if (frame_queue_nb_remaining(&is->subpq) > 1)
1667 sp2 = frame_queue_peek_next(&is->subpq);
1671 if (sp->serial != is->subtitleq.serial
1672 || (is->vidclk.pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1673 || (sp2 && is->vidclk.pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1675 frame_queue_next(&is->subpq);
1683 /* display picture */
1684 if (!display_disable && is->show_mode == SHOW_MODE_VIDEO)
1687 frame_queue_next(&is->pictq);
1689 if (is->step && !is->paused)
1690 stream_toggle_pause(is);
1693 is->force_refresh = 0;
1695 static int64_t last_time;
1697 int aqsize, vqsize, sqsize;
1700 cur_time = av_gettime_relative();
1701 if (!last_time || (cur_time - last_time) >= 30000) {
1706 aqsize = is->audioq.size;
1708 vqsize = is->videoq.size;
1709 if (is->subtitle_st)
1710 sqsize = is->subtitleq.size;
1712 if (is->audio_st && is->video_st)
1713 av_diff = get_clock(&is->audclk) - get_clock(&is->vidclk);
1714 else if (is->video_st)
1715 av_diff = get_master_clock(is) - get_clock(&is->vidclk);
1716 else if (is->audio_st)
1717 av_diff = get_master_clock(is) - get_clock(&is->audclk);
1718 av_log(NULL, AV_LOG_INFO,
1719 "%7.2f %s:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
1720 get_master_clock(is),
1721 (is->audio_st && is->video_st) ? "A-V" : (is->video_st ? "M-V" : (is->audio_st ? "M-A" : " ")),
1723 is->frame_drops_early + is->frame_drops_late,
1727 is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1728 is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1730 last_time = cur_time;
1735 /* allocate a picture (needs to do that in main thread to avoid
1736 potential locking problems */
1737 static void alloc_picture(VideoState *is)
1742 vp = &is->pictq.queue[is->pictq.windex];
1746 video_open(is, 0, vp);
1748 vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1751 bufferdiff = vp->bmp ? FFMAX(vp->bmp->pixels[0], vp->bmp->pixels[1]) - FFMIN(vp->bmp->pixels[0], vp->bmp->pixels[1]) : 0;
1752 if (!vp->bmp || vp->bmp->pitches[0] < vp->width || bufferdiff < (int64_t)vp->height * vp->bmp->pitches[0]) {
1753 /* SDL allocates a buffer smaller than requested if the video
1754 * overlay hardware is unable to support the requested size. */
1755 av_log(NULL, AV_LOG_FATAL,
1756 "Error: the video system does not support an image\n"
1757 "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1758 "to reduce the image size.\n", vp->width, vp->height );
1762 SDL_LockMutex(is->pictq.mutex);
1764 SDL_CondSignal(is->pictq.cond);
1765 SDL_UnlockMutex(is->pictq.mutex);
1768 static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
1769 int i, width, height;
1771 for (i = 0; i < 3; i++) {
1778 if (bmp->pitches[i] > width) {
1779 maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
1780 for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
1786 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, double duration, int64_t pos, int serial)
1790 #if defined(DEBUG_SYNC) && 0
1791 printf("frame_type=%c pts=%0.3f\n",
1792 av_get_picture_type_char(src_frame->pict_type), pts);
1795 if (!(vp = frame_queue_peek_writable(&is->pictq)))
1798 vp->sar = src_frame->sample_aspect_ratio;
1800 /* alloc or resize hardware picture buffer */
1801 if (!vp->bmp || vp->reallocate || !vp->allocated ||
1802 vp->width != src_frame->width ||
1803 vp->height != src_frame->height) {
1808 vp->width = src_frame->width;
1809 vp->height = src_frame->height;
1811 /* the allocation must be done in the main thread to avoid
1812 locking problems. */
1813 event.type = FF_ALLOC_EVENT;
1814 event.user.data1 = is;
1815 SDL_PushEvent(&event);
1817 /* wait until the picture is allocated */
1818 SDL_LockMutex(is->pictq.mutex);
1819 while (!vp->allocated && !is->videoq.abort_request) {
1820 SDL_CondWait(is->pictq.cond, is->pictq.mutex);
1822 /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1823 if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1824 while (!vp->allocated && !is->abort_request) {
1825 SDL_CondWait(is->pictq.cond, is->pictq.mutex);
1828 SDL_UnlockMutex(is->pictq.mutex);
1830 if (is->videoq.abort_request)
1834 /* if the frame is not skipped, then display it */
1836 AVPicture pict = { { 0 } };
1838 /* get a pointer on the bitmap */
1839 SDL_LockYUVOverlay (vp->bmp);
1841 pict.data[0] = vp->bmp->pixels[0];
1842 pict.data[1] = vp->bmp->pixels[2];
1843 pict.data[2] = vp->bmp->pixels[1];
1845 pict.linesize[0] = vp->bmp->pitches[0];
1846 pict.linesize[1] = vp->bmp->pitches[2];
1847 pict.linesize[2] = vp->bmp->pitches[1];
1850 // FIXME use direct rendering
1851 av_picture_copy(&pict, (AVPicture *)src_frame,
1852 src_frame->format, vp->width, vp->height);
1854 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1855 is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1856 vp->width, vp->height, src_frame->format, vp->width, vp->height,
1857 AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1858 if (!is->img_convert_ctx) {
1859 av_log(NULL, AV_LOG_FATAL, "Cannot initialize the conversion context\n");
1862 sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1863 0, vp->height, pict.data, pict.linesize);
1865 /* workaround SDL PITCH_WORKAROUND */
1866 duplicate_right_border_pixels(vp->bmp);
1867 /* update the bitmap content */
1868 SDL_UnlockYUVOverlay(vp->bmp);
1871 vp->duration = duration;
1873 vp->serial = serial;
1875 /* now we can update the picture count */
1876 frame_queue_push(&is->pictq);
1881 static int get_video_frame(VideoState *is, AVFrame *frame)
1885 if ((got_picture = decoder_decode_frame(&is->viddec, frame, NULL)) < 0)
1891 if (frame->pts != AV_NOPTS_VALUE)
1892 dpts = av_q2d(is->video_st->time_base) * frame->pts;
1894 frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
1896 if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
1897 if (frame->pts != AV_NOPTS_VALUE) {
1898 double diff = dpts - get_master_clock(is);
1899 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD &&
1900 diff - is->frame_last_filter_delay < 0 &&
1901 is->viddec.pkt_serial == is->vidclk.serial &&
1902 is->videoq.nb_packets) {
1903 is->frame_drops_early++;
1904 av_frame_unref(frame);
1915 static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
1916 AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
1919 int nb_filters = graph->nb_filters;
1920 AVFilterInOut *outputs = NULL, *inputs = NULL;
1923 outputs = avfilter_inout_alloc();
1924 inputs = avfilter_inout_alloc();
1925 if (!outputs || !inputs) {
1926 ret = AVERROR(ENOMEM);
1930 outputs->name = av_strdup("in");
1931 outputs->filter_ctx = source_ctx;
1932 outputs->pad_idx = 0;
1933 outputs->next = NULL;
1935 inputs->name = av_strdup("out");
1936 inputs->filter_ctx = sink_ctx;
1937 inputs->pad_idx = 0;
1938 inputs->next = NULL;
1940 if ((ret = avfilter_graph_parse_ptr(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
1943 if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
1947 /* Reorder the filters to ensure that inputs of the custom filters are merged first */
1948 for (i = 0; i < graph->nb_filters - nb_filters; i++)
1949 FFSWAP(AVFilterContext*, graph->filters[i], graph->filters[i + nb_filters]);
1951 ret = avfilter_graph_config(graph, NULL);
1953 avfilter_inout_free(&outputs);
1954 avfilter_inout_free(&inputs);
1958 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame)
1960 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
1961 char sws_flags_str[128];
1962 char buffersrc_args[256];
1964 AVFilterContext *filt_src = NULL, *filt_out = NULL, *last_filter = NULL;
1965 AVCodecContext *codec = is->video_st->codec;
1966 AVRational fr = av_guess_frame_rate(is->ic, is->video_st, NULL);
1968 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1969 snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
1970 graph->scale_sws_opts = av_strdup(sws_flags_str);
1972 snprintf(buffersrc_args, sizeof(buffersrc_args),
1973 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
1974 frame->width, frame->height, frame->format,
1975 is->video_st->time_base.num, is->video_st->time_base.den,
1976 codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
1977 if (fr.num && fr.den)
1978 av_strlcatf(buffersrc_args, sizeof(buffersrc_args), ":frame_rate=%d/%d", fr.num, fr.den);
1980 if ((ret = avfilter_graph_create_filter(&filt_src,
1981 avfilter_get_by_name("buffer"),
1982 "ffplay_buffer", buffersrc_args, NULL,
1986 ret = avfilter_graph_create_filter(&filt_out,
1987 avfilter_get_by_name("buffersink"),
1988 "ffplay_buffersink", NULL, NULL, graph);
1992 if ((ret = av_opt_set_int_list(filt_out, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
1995 last_filter = filt_out;
1997 /* Note: this macro adds a filter before the lastly added filter, so the
1998 * processing order of the filters is in reverse */
1999 #define INSERT_FILT(name, arg) do { \
2000 AVFilterContext *filt_ctx; \
2002 ret = avfilter_graph_create_filter(&filt_ctx, \
2003 avfilter_get_by_name(name), \
2004 "ffplay_" name, arg, NULL, graph); \
2008 ret = avfilter_link(filt_ctx, 0, last_filter, 0); \
2012 last_filter = filt_ctx; \
2015 /* SDL YUV code is not handling odd width/height for some driver
2016 * combinations, therefore we crop the picture to an even width/height. */
2017 INSERT_FILT("crop", "floor(in_w/2)*2:floor(in_h/2)*2");
2020 double theta = get_rotation(is->video_st);
2022 if (fabs(theta - 90) < 1.0) {
2023 INSERT_FILT("transpose", "clock");
2024 } else if (fabs(theta - 180) < 1.0) {
2025 INSERT_FILT("hflip", NULL);
2026 INSERT_FILT("vflip", NULL);
2027 } else if (fabs(theta - 270) < 1.0) {
2028 INSERT_FILT("transpose", "cclock");
2029 } else if (fabs(theta) > 1.0) {
2030 char rotate_buf[64];
2031 snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta);
2032 INSERT_FILT("rotate", rotate_buf);
2036 if ((ret = configure_filtergraph(graph, vfilters, filt_src, last_filter)) < 0)
2039 is->in_video_filter = filt_src;
2040 is->out_video_filter = filt_out;
2046 static int configure_audio_filters(VideoState *is, const char *afilters, int force_output_format)
2048 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
2049 int sample_rates[2] = { 0, -1 };
2050 int64_t channel_layouts[2] = { 0, -1 };
2051 int channels[2] = { 0, -1 };
2052 AVFilterContext *filt_asrc = NULL, *filt_asink = NULL;
2053 char aresample_swr_opts[512] = "";
2054 AVDictionaryEntry *e = NULL;
2055 char asrc_args[256];
2058 avfilter_graph_free(&is->agraph);
2059 if (!(is->agraph = avfilter_graph_alloc()))
2060 return AVERROR(ENOMEM);
2062 while ((e = av_dict_get(swr_opts, "", e, AV_DICT_IGNORE_SUFFIX)))
2063 av_strlcatf(aresample_swr_opts, sizeof(aresample_swr_opts), "%s=%s:", e->key, e->value);
2064 if (strlen(aresample_swr_opts))
2065 aresample_swr_opts[strlen(aresample_swr_opts)-1] = '\0';
2066 av_opt_set(is->agraph, "aresample_swr_opts", aresample_swr_opts, 0);
2068 ret = snprintf(asrc_args, sizeof(asrc_args),
2069 "sample_rate=%d:sample_fmt=%s:channels=%d:time_base=%d/%d",
2070 is->audio_filter_src.freq, av_get_sample_fmt_name(is->audio_filter_src.fmt),
2071 is->audio_filter_src.channels,
2072 1, is->audio_filter_src.freq);
2073 if (is->audio_filter_src.channel_layout)
2074 snprintf(asrc_args + ret, sizeof(asrc_args) - ret,
2075 ":channel_layout=0x%"PRIx64, is->audio_filter_src.channel_layout);
2077 ret = avfilter_graph_create_filter(&filt_asrc,
2078 avfilter_get_by_name("abuffer"), "ffplay_abuffer",
2079 asrc_args, NULL, is->agraph);
2084 ret = avfilter_graph_create_filter(&filt_asink,
2085 avfilter_get_by_name("abuffersink"), "ffplay_abuffersink",
2086 NULL, NULL, is->agraph);
2090 if ((ret = av_opt_set_int_list(filt_asink, "sample_fmts", sample_fmts, AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
2092 if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
2095 if (force_output_format) {
2096 channel_layouts[0] = is->audio_tgt.channel_layout;
2097 channels [0] = is->audio_tgt.channels;
2098 sample_rates [0] = is->audio_tgt.freq;
2099 if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 0, AV_OPT_SEARCH_CHILDREN)) < 0)
2101 if ((ret = av_opt_set_int_list(filt_asink, "channel_layouts", channel_layouts, -1, AV_OPT_SEARCH_CHILDREN)) < 0)
2103 if ((ret = av_opt_set_int_list(filt_asink, "channel_counts" , channels , -1, AV_OPT_SEARCH_CHILDREN)) < 0)
2105 if ((ret = av_opt_set_int_list(filt_asink, "sample_rates" , sample_rates , -1, AV_OPT_SEARCH_CHILDREN)) < 0)
2110 if ((ret = configure_filtergraph(is->agraph, afilters, filt_asrc, filt_asink)) < 0)
2113 is->in_audio_filter = filt_asrc;
2114 is->out_audio_filter = filt_asink;
2118 avfilter_graph_free(&is->agraph);
2121 #endif /* CONFIG_AVFILTER */
2123 static int audio_thread(void *arg)
2125 VideoState *is = arg;
2126 AVFrame *frame = av_frame_alloc();
2129 int last_serial = -1;
2130 int64_t dec_channel_layout;
2138 return AVERROR(ENOMEM);
2141 if ((got_frame = decoder_decode_frame(&is->auddec, frame, NULL)) < 0)
2145 tb = (AVRational){1, frame->sample_rate};
2148 dec_channel_layout = get_valid_channel_layout(frame->channel_layout, av_frame_get_channels(frame));
2151 cmp_audio_fmts(is->audio_filter_src.fmt, is->audio_filter_src.channels,
2152 frame->format, av_frame_get_channels(frame)) ||
2153 is->audio_filter_src.channel_layout != dec_channel_layout ||
2154 is->audio_filter_src.freq != frame->sample_rate ||
2155 is->auddec.pkt_serial != last_serial;
2158 char buf1[1024], buf2[1024];
2159 av_get_channel_layout_string(buf1, sizeof(buf1), -1, is->audio_filter_src.channel_layout);
2160 av_get_channel_layout_string(buf2, sizeof(buf2), -1, dec_channel_layout);
2161 av_log(NULL, AV_LOG_DEBUG,
2162 "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",
2163 is->audio_filter_src.freq, is->audio_filter_src.channels, av_get_sample_fmt_name(is->audio_filter_src.fmt), buf1, last_serial,
2164 frame->sample_rate, av_frame_get_channels(frame), av_get_sample_fmt_name(frame->format), buf2, is->auddec.pkt_serial);
2166 is->audio_filter_src.fmt = frame->format;
2167 is->audio_filter_src.channels = av_frame_get_channels(frame);
2168 is->audio_filter_src.channel_layout = dec_channel_layout;
2169 is->audio_filter_src.freq = frame->sample_rate;
2170 last_serial = is->auddec.pkt_serial;
2172 if ((ret = configure_audio_filters(is, afilters, 1)) < 0)
2176 if ((ret = av_buffersrc_add_frame(is->in_audio_filter, frame)) < 0)
2179 while ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, frame, 0)) >= 0) {
2180 tb = is->out_audio_filter->inputs[0]->time_base;
2182 if (!(af = frame_queue_peek_writable(&is->sampq)))
2185 af->pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(tb);
2186 af->pos = av_frame_get_pkt_pos(frame);
2187 af->serial = is->auddec.pkt_serial;
2188 af->duration = av_q2d((AVRational){frame->nb_samples, frame->sample_rate});
2190 av_frame_move_ref(af->frame, frame);
2191 frame_queue_push(&is->sampq);
2194 if (is->audioq.serial != is->auddec.pkt_serial)
2197 if (ret == AVERROR_EOF)
2198 is->auddec.finished = is->auddec.pkt_serial;
2201 } while (ret >= 0 || ret == AVERROR(EAGAIN) || ret == AVERROR_EOF);
2204 avfilter_graph_free(&is->agraph);
2206 av_frame_free(&frame);
2210 static void decoder_start(Decoder *d, int (*fn)(void *), void *arg)
2212 packet_queue_start(d->queue);
2213 d->decoder_tid = SDL_CreateThread(fn, arg);
2216 static int video_thread(void *arg)
2218 VideoState *is = arg;
2219 AVFrame *frame = av_frame_alloc();
2223 AVRational tb = is->video_st->time_base;
2224 AVRational frame_rate = av_guess_frame_rate(is->ic, is->video_st, NULL);
2227 AVFilterGraph *graph = avfilter_graph_alloc();
2228 AVFilterContext *filt_out = NULL, *filt_in = NULL;
2231 enum AVPixelFormat last_format = -2;
2232 int last_serial = -1;
2233 int last_vfilter_idx = 0;
2235 av_frame_free(&frame);
2236 return AVERROR(ENOMEM);
2243 avfilter_graph_free(&graph);
2245 return AVERROR(ENOMEM);
2249 ret = get_video_frame(is, frame);
2256 if ( last_w != frame->width
2257 || last_h != frame->height
2258 || last_format != frame->format
2259 || last_serial != is->viddec.pkt_serial
2260 || last_vfilter_idx != is->vfilter_idx) {
2261 av_log(NULL, AV_LOG_DEBUG,
2262 "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
2264 (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
2265 frame->width, frame->height,
2266 (const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), is->viddec.pkt_serial);
2267 avfilter_graph_free(&graph);
2268 graph = avfilter_graph_alloc();
2269 if ((ret = configure_video_filters(graph, is, vfilters_list ? vfilters_list[is->vfilter_idx] : NULL, frame)) < 0) {
2271 event.type = FF_QUIT_EVENT;
2272 event.user.data1 = is;
2273 SDL_PushEvent(&event);
2276 filt_in = is->in_video_filter;
2277 filt_out = is->out_video_filter;
2278 last_w = frame->width;
2279 last_h = frame->height;
2280 last_format = frame->format;
2281 last_serial = is->viddec.pkt_serial;
2282 last_vfilter_idx = is->vfilter_idx;
2283 frame_rate = filt_out->inputs[0]->frame_rate;
2286 ret = av_buffersrc_add_frame(filt_in, frame);
2291 is->frame_last_returned_time = av_gettime_relative() / 1000000.0;
2293 ret = av_buffersink_get_frame_flags(filt_out, frame, 0);
2295 if (ret == AVERROR_EOF)
2296 is->viddec.finished = is->viddec.pkt_serial;
2301 is->frame_last_filter_delay = av_gettime_relative() / 1000000.0 - is->frame_last_returned_time;
2302 if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
2303 is->frame_last_filter_delay = 0;
2304 tb = filt_out->inputs[0]->time_base;
2306 duration = (frame_rate.num && frame_rate.den ? av_q2d((AVRational){frame_rate.den, frame_rate.num}) : 0);
2307 pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(tb);
2308 ret = queue_picture(is, frame, pts, duration, av_frame_get_pkt_pos(frame), is->viddec.pkt_serial);
2309 av_frame_unref(frame);
2319 avfilter_graph_free(&graph);
2321 av_frame_free(&frame);
2325 static int subtitle_thread(void *arg)
2327 VideoState *is = arg;
2332 int r, g, b, y, u, v, a;
2335 if (!(sp = frame_queue_peek_writable(&is->subpq)))
2338 if ((got_subtitle = decoder_decode_frame(&is->subdec, NULL, &sp->sub)) < 0)
2343 if (got_subtitle && sp->sub.format == 0) {
2344 if (sp->sub.pts != AV_NOPTS_VALUE)
2345 pts = sp->sub.pts / (double)AV_TIME_BASE;
2347 sp->serial = is->subdec.pkt_serial;
2349 for (i = 0; i < sp->sub.num_rects; i++)
2351 for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
2353 RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
2354 y = RGB_TO_Y_CCIR(r, g, b);
2355 u = RGB_TO_U_CCIR(r, g, b, 0);
2356 v = RGB_TO_V_CCIR(r, g, b, 0);
2357 YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
2361 /* now we can update the picture count */
2362 frame_queue_push(&is->subpq);
2363 } else if (got_subtitle) {
2364 avsubtitle_free(&sp->sub);
2370 /* copy samples for viewing in editor window */
2371 static void update_sample_display(VideoState *is, short *samples, int samples_size)
2375 size = samples_size / sizeof(short);
2377 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
2380 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
2382 is->sample_array_index += len;
2383 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
2384 is->sample_array_index = 0;
2389 /* return the wanted number of samples to get better sync if sync_type is video
2390 * or external master clock */
2391 static int synchronize_audio(VideoState *is, int nb_samples)
2393 int wanted_nb_samples = nb_samples;
2395 /* if not master, then we try to remove or add samples to correct the clock */
2396 if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
2397 double diff, avg_diff;
2398 int min_nb_samples, max_nb_samples;
2400 diff = get_clock(&is->audclk) - get_master_clock(is);
2402 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
2403 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
2404 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
2405 /* not enough measures to have a correct estimate */
2406 is->audio_diff_avg_count++;
2408 /* estimate the A-V difference */
2409 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
2411 if (fabs(avg_diff) >= is->audio_diff_threshold) {
2412 wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
2413 min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2414 max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2415 wanted_nb_samples = av_clip(wanted_nb_samples, min_nb_samples, max_nb_samples);
2417 av_log(NULL, AV_LOG_TRACE, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n",
2418 diff, avg_diff, wanted_nb_samples - nb_samples,
2419 is->audio_clock, is->audio_diff_threshold);
2422 /* too big difference : may be initial PTS errors, so
2424 is->audio_diff_avg_count = 0;
2425 is->audio_diff_cum = 0;
2429 return wanted_nb_samples;
2433 * Decode one audio frame and return its uncompressed size.
2435 * The processed audio frame is decoded, converted if required, and
2436 * stored in is->audio_buf, with size in bytes given by the return
2439 static int audio_decode_frame(VideoState *is)
2441 int data_size, resampled_data_size;
2442 int64_t dec_channel_layout;
2443 av_unused double audio_clock0;
2444 int wanted_nb_samples;
2451 if (!(af = frame_queue_peek_readable(&is->sampq)))
2453 frame_queue_next(&is->sampq);
2454 } while (af->serial != is->audioq.serial);
2456 data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(af->frame),
2457 af->frame->nb_samples,
2458 af->frame->format, 1);
2460 dec_channel_layout =
2461 (af->frame->channel_layout && av_frame_get_channels(af->frame) == av_get_channel_layout_nb_channels(af->frame->channel_layout)) ?
2462 af->frame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(af->frame));
2463 wanted_nb_samples = synchronize_audio(is, af->frame->nb_samples);
2465 if (af->frame->format != is->audio_src.fmt ||
2466 dec_channel_layout != is->audio_src.channel_layout ||
2467 af->frame->sample_rate != is->audio_src.freq ||
2468 (wanted_nb_samples != af->frame->nb_samples && !is->swr_ctx)) {
2469 swr_free(&is->swr_ctx);
2470 is->swr_ctx = swr_alloc_set_opts(NULL,
2471 is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2472 dec_channel_layout, af->frame->format, af->frame->sample_rate,
2474 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2475 av_log(NULL, AV_LOG_ERROR,
2476 "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2477 af->frame->sample_rate, av_get_sample_fmt_name(af->frame->format), av_frame_get_channels(af->frame),
2478 is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
2479 swr_free(&is->swr_ctx);
2482 is->audio_src.channel_layout = dec_channel_layout;
2483 is->audio_src.channels = av_frame_get_channels(af->frame);
2484 is->audio_src.freq = af->frame->sample_rate;
2485 is->audio_src.fmt = af->frame->format;
2489 const uint8_t **in = (const uint8_t **)af->frame->extended_data;
2490 uint8_t **out = &is->audio_buf1;
2491 int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / af->frame->sample_rate + 256;
2492 int out_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
2495 av_log(NULL, AV_LOG_ERROR, "av_samples_get_buffer_size() failed\n");
2498 if (wanted_nb_samples != af->frame->nb_samples) {
2499 if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - af->frame->nb_samples) * is->audio_tgt.freq / af->frame->sample_rate,
2500 wanted_nb_samples * is->audio_tgt.freq / af->frame->sample_rate) < 0) {
2501 av_log(NULL, AV_LOG_ERROR, "swr_set_compensation() failed\n");
2505 av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
2506 if (!is->audio_buf1)
2507 return AVERROR(ENOMEM);
2508 len2 = swr_convert(is->swr_ctx, out, out_count, in, af->frame->nb_samples);
2510 av_log(NULL, AV_LOG_ERROR, "swr_convert() failed\n");
2513 if (len2 == out_count) {
2514 av_log(NULL, AV_LOG_WARNING, "audio buffer is probably too small\n");
2515 if (swr_init(is->swr_ctx) < 0)
2516 swr_free(&is->swr_ctx);
2518 is->audio_buf = is->audio_buf1;
2519 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2521 is->audio_buf = af->frame->data[0];
2522 resampled_data_size = data_size;
2525 audio_clock0 = is->audio_clock;
2526 /* update the audio clock with the pts */
2527 if (!isnan(af->pts))
2528 is->audio_clock = af->pts + (double) af->frame->nb_samples / af->frame->sample_rate;
2530 is->audio_clock = NAN;
2531 is->audio_clock_serial = af->serial;
2534 static double last_clock;
2535 printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
2536 is->audio_clock - last_clock,
2537 is->audio_clock, audio_clock0);
2538 last_clock = is->audio_clock;
2541 return resampled_data_size;
2544 /* prepare a new audio buffer */
2545 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2547 VideoState *is = opaque;
2548 int audio_size, len1;
2550 audio_callback_time = av_gettime_relative();
2553 if (is->audio_buf_index >= is->audio_buf_size) {
2554 audio_size = audio_decode_frame(is);
2555 if (audio_size < 0) {
2556 /* if error, just output silence */
2557 is->audio_buf = is->silence_buf;
2558 is->audio_buf_size = sizeof(is->silence_buf) / is->audio_tgt.frame_size * is->audio_tgt.frame_size;
2560 if (is->show_mode != SHOW_MODE_VIDEO)
2561 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2562 is->audio_buf_size = audio_size;
2564 is->audio_buf_index = 0;
2566 len1 = is->audio_buf_size - is->audio_buf_index;
2569 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2572 is->audio_buf_index += len1;
2574 is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2575 /* Let's assume the audio driver that is used by SDL has two periods. */
2576 if (!isnan(is->audio_clock)) {
2577 set_clock_at(&is->audclk, is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / is->audio_tgt.bytes_per_sec, is->audio_clock_serial, audio_callback_time / 1000000.0);
2578 sync_clock_to_slave(&is->extclk, &is->audclk);
2582 static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
2584 SDL_AudioSpec wanted_spec, spec;
2586 static const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
2587 static const int next_sample_rates[] = {0, 44100, 48000, 96000, 192000};
2588 int next_sample_rate_idx = FF_ARRAY_ELEMS(next_sample_rates) - 1;
2590 env = SDL_getenv("SDL_AUDIO_CHANNELS");
2592 wanted_nb_channels = atoi(env);
2593 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2595 if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
2596 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2597 wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2599 wanted_nb_channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2600 wanted_spec.channels = wanted_nb_channels;
2601 wanted_spec.freq = wanted_sample_rate;
2602 if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2603 av_log(NULL, AV_LOG_ERROR, "Invalid sample rate or channel count!\n");
2606 while (next_sample_rate_idx && next_sample_rates[next_sample_rate_idx] >= wanted_spec.freq)
2607 next_sample_rate_idx--;
2608 wanted_spec.format = AUDIO_S16SYS;
2609 wanted_spec.silence = 0;
2610 wanted_spec.samples = FFMAX(SDL_AUDIO_MIN_BUFFER_SIZE, 2 << av_log2(wanted_spec.freq / SDL_AUDIO_MAX_CALLBACKS_PER_SEC));
2611 wanted_spec.callback = sdl_audio_callback;
2612 wanted_spec.userdata = opaque;
2613 while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2614 av_log(NULL, AV_LOG_WARNING, "SDL_OpenAudio (%d channels, %d Hz): %s\n",
2615 wanted_spec.channels, wanted_spec.freq, SDL_GetError());
2616 wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
2617 if (!wanted_spec.channels) {
2618 wanted_spec.freq = next_sample_rates[next_sample_rate_idx--];
2619 wanted_spec.channels = wanted_nb_channels;
2620 if (!wanted_spec.freq) {
2621 av_log(NULL, AV_LOG_ERROR,
2622 "No more combinations to try, audio open failed\n");
2626 wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
2628 if (spec.format != AUDIO_S16SYS) {
2629 av_log(NULL, AV_LOG_ERROR,
2630 "SDL advised audio format %d is not supported!\n", spec.format);
2633 if (spec.channels != wanted_spec.channels) {
2634 wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2635 if (!wanted_channel_layout) {
2636 av_log(NULL, AV_LOG_ERROR,
2637 "SDL advised channel count %d is not supported!\n", spec.channels);
2642 audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
2643 audio_hw_params->freq = spec.freq;
2644 audio_hw_params->channel_layout = wanted_channel_layout;
2645 audio_hw_params->channels = spec.channels;
2646 audio_hw_params->frame_size = av_samples_get_buffer_size(NULL, audio_hw_params->channels, 1, audio_hw_params->fmt, 1);
2647 audio_hw_params->bytes_per_sec = av_samples_get_buffer_size(NULL, audio_hw_params->channels, audio_hw_params->freq, audio_hw_params->fmt, 1);
2648 if (audio_hw_params->bytes_per_sec <= 0 || audio_hw_params->frame_size <= 0) {
2649 av_log(NULL, AV_LOG_ERROR, "av_samples_get_buffer_size failed\n");
2655 /* open a given stream. Return 0 if OK */
2656 static int stream_component_open(VideoState *is, int stream_index)
2658 AVFormatContext *ic = is->ic;
2659 AVCodecContext *avctx;
2661 const char *forced_codec_name = NULL;
2663 AVDictionaryEntry *t = NULL;
2664 int sample_rate, nb_channels;
2665 int64_t channel_layout;
2667 int stream_lowres = lowres;
2669 if (stream_index < 0 || stream_index >= ic->nb_streams)
2671 avctx = ic->streams[stream_index]->codec;
2673 codec = avcodec_find_decoder(avctx->codec_id);
2675 switch(avctx->codec_type){
2676 case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; forced_codec_name = audio_codec_name; break;
2677 case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
2678 case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; forced_codec_name = video_codec_name; break;
2680 if (forced_codec_name)
2681 codec = avcodec_find_decoder_by_name(forced_codec_name);
2683 if (forced_codec_name) av_log(NULL, AV_LOG_WARNING,
2684 "No codec could be found with name '%s'\n", forced_codec_name);
2685 else av_log(NULL, AV_LOG_WARNING,
2686 "No codec could be found with id %d\n", avctx->codec_id);
2690 avctx->codec_id = codec->id;
2691 if(stream_lowres > av_codec_get_max_lowres(codec)){
2692 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2693 av_codec_get_max_lowres(codec));
2694 stream_lowres = av_codec_get_max_lowres(codec);
2696 av_codec_set_lowres(avctx, stream_lowres);
2698 if(stream_lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
2700 avctx->flags2 |= AV_CODEC_FLAG2_FAST;
2701 if(codec->capabilities & AV_CODEC_CAP_DR1)
2702 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2704 opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
2705 if (!av_dict_get(opts, "threads", NULL, 0))
2706 av_dict_set(&opts, "threads", "auto", 0);
2708 av_dict_set_int(&opts, "lowres", stream_lowres, 0);
2709 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
2710 av_dict_set(&opts, "refcounted_frames", "1", 0);
2711 if ((ret = avcodec_open2(avctx, codec, &opts)) < 0) {
2714 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2715 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2716 ret = AVERROR_OPTION_NOT_FOUND;
2721 ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2722 switch (avctx->codec_type) {
2723 case AVMEDIA_TYPE_AUDIO:
2728 is->audio_filter_src.freq = avctx->sample_rate;
2729 is->audio_filter_src.channels = avctx->channels;
2730 is->audio_filter_src.channel_layout = get_valid_channel_layout(avctx->channel_layout, avctx->channels);
2731 is->audio_filter_src.fmt = avctx->sample_fmt;
2732 if ((ret = configure_audio_filters(is, afilters, 0)) < 0)
2734 link = is->out_audio_filter->inputs[0];
2735 sample_rate = link->sample_rate;
2736 nb_channels = link->channels;
2737 channel_layout = link->channel_layout;
2740 sample_rate = avctx->sample_rate;
2741 nb_channels = avctx->channels;
2742 channel_layout = avctx->channel_layout;
2745 /* prepare audio output */
2746 if ((ret = audio_open(is, channel_layout, nb_channels, sample_rate, &is->audio_tgt)) < 0)
2748 is->audio_hw_buf_size = ret;
2749 is->audio_src = is->audio_tgt;
2750 is->audio_buf_size = 0;
2751 is->audio_buf_index = 0;
2753 /* init averaging filter */
2754 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2755 is->audio_diff_avg_count = 0;
2756 /* since we do not have a precise anough audio fifo fullness,
2757 we correct audio sync only if larger than this threshold */
2758 is->audio_diff_threshold = (double)(is->audio_hw_buf_size) / is->audio_tgt.bytes_per_sec;
2760 is->audio_stream = stream_index;
2761 is->audio_st = ic->streams[stream_index];
2763 decoder_init(&is->auddec, avctx, &is->audioq, is->continue_read_thread);
2764 if ((is->ic->iformat->flags & (AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK)) && !is->ic->iformat->read_seek) {
2765 is->auddec.start_pts = is->audio_st->start_time;
2766 is->auddec.start_pts_tb = is->audio_st->time_base;
2768 decoder_start(&is->auddec, audio_thread, is);
2771 case AVMEDIA_TYPE_VIDEO:
2772 is->video_stream = stream_index;
2773 is->video_st = ic->streams[stream_index];
2775 decoder_init(&is->viddec, avctx, &is->videoq, is->continue_read_thread);
2776 decoder_start(&is->viddec, video_thread, is);
2777 is->queue_attachments_req = 1;
2779 case AVMEDIA_TYPE_SUBTITLE:
2780 is->subtitle_stream = stream_index;
2781 is->subtitle_st = ic->streams[stream_index];
2783 decoder_init(&is->subdec, avctx, &is->subtitleq, is->continue_read_thread);
2784 decoder_start(&is->subdec, subtitle_thread, is);
2791 av_dict_free(&opts);
2796 static void stream_component_close(VideoState *is, int stream_index)
2798 AVFormatContext *ic = is->ic;
2799 AVCodecContext *avctx;
2801 if (stream_index < 0 || stream_index >= ic->nb_streams)
2803 avctx = ic->streams[stream_index]->codec;
2805 switch (avctx->codec_type) {
2806 case AVMEDIA_TYPE_AUDIO:
2807 decoder_abort(&is->auddec, &is->sampq);
2809 decoder_destroy(&is->auddec);
2810 swr_free(&is->swr_ctx);
2811 av_freep(&is->audio_buf1);
2812 is->audio_buf1_size = 0;
2813 is->audio_buf = NULL;
2816 av_rdft_end(is->rdft);
2817 av_freep(&is->rdft_data);
2822 case AVMEDIA_TYPE_VIDEO:
2823 decoder_abort(&is->viddec, &is->pictq);
2824 decoder_destroy(&is->viddec);
2826 case AVMEDIA_TYPE_SUBTITLE:
2827 decoder_abort(&is->subdec, &is->subpq);
2828 decoder_destroy(&is->subdec);
2834 ic->streams[stream_index]->discard = AVDISCARD_ALL;
2835 avcodec_close(avctx);
2836 switch (avctx->codec_type) {
2837 case AVMEDIA_TYPE_AUDIO:
2838 is->audio_st = NULL;
2839 is->audio_stream = -1;
2841 case AVMEDIA_TYPE_VIDEO:
2842 is->video_st = NULL;
2843 is->video_stream = -1;
2845 case AVMEDIA_TYPE_SUBTITLE:
2846 is->subtitle_st = NULL;
2847 is->subtitle_stream = -1;
2854 static int decode_interrupt_cb(void *ctx)
2856 VideoState *is = ctx;
2857 return is->abort_request;
2860 static int is_realtime(AVFormatContext *s)
2862 if( !strcmp(s->iformat->name, "rtp")
2863 || !strcmp(s->iformat->name, "rtsp")
2864 || !strcmp(s->iformat->name, "sdp")
2868 if(s->pb && ( !strncmp(s->filename, "rtp:", 4)
2869 || !strncmp(s->filename, "udp:", 4)
2876 /* this thread gets the stream from the disk or the network */
2877 static int read_thread(void *arg)
2879 VideoState *is = arg;
2880 AVFormatContext *ic = NULL;
2882 int st_index[AVMEDIA_TYPE_NB];
2883 AVPacket pkt1, *pkt = &pkt1;
2884 int64_t stream_start_time;
2885 int pkt_in_play_range = 0;
2886 AVDictionaryEntry *t;
2887 AVDictionary **opts;
2888 int orig_nb_streams;
2889 SDL_mutex *wait_mutex = SDL_CreateMutex();
2890 int scan_all_pmts_set = 0;
2893 memset(st_index, -1, sizeof(st_index));
2894 is->last_video_stream = is->video_stream = -1;
2895 is->last_audio_stream = is->audio_stream = -1;
2896 is->last_subtitle_stream = is->subtitle_stream = -1;
2899 ic = avformat_alloc_context();
2901 av_log(NULL, AV_LOG_FATAL, "Could not allocate context.\n");
2902 ret = AVERROR(ENOMEM);
2905 ic->interrupt_callback.callback = decode_interrupt_cb;
2906 ic->interrupt_callback.opaque = is;
2907 if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
2908 av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
2909 scan_all_pmts_set = 1;
2911 err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2913 print_error(is->filename, err);
2917 if (scan_all_pmts_set)
2918 av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
2920 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2921 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2922 ret = AVERROR_OPTION_NOT_FOUND;
2928 ic->flags |= AVFMT_FLAG_GENPTS;
2930 av_format_inject_global_side_data(ic);
2932 opts = setup_find_stream_info_opts(ic, codec_opts);
2933 orig_nb_streams = ic->nb_streams;
2935 err = avformat_find_stream_info(ic, opts);
2937 for (i = 0; i < orig_nb_streams; i++)
2938 av_dict_free(&opts[i]);
2942 av_log(NULL, AV_LOG_WARNING,
2943 "%s: could not find codec parameters\n", is->filename);
2949 ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use avio_feof() to test for the end
2951 if (seek_by_bytes < 0)
2952 seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT) && strcmp("ogg", ic->iformat->name);
2954 is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
2956 if (!window_title && (t = av_dict_get(ic->metadata, "title", NULL, 0)))
2957 window_title = av_asprintf("%s - %s", t->value, input_filename);
2959 /* if seeking requested, we execute it */
2960 if (start_time != AV_NOPTS_VALUE) {
2963 timestamp = start_time;
2964 /* add the stream start time */
2965 if (ic->start_time != AV_NOPTS_VALUE)
2966 timestamp += ic->start_time;
2967 ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2969 av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
2970 is->filename, (double)timestamp / AV_TIME_BASE);
2974 is->realtime = is_realtime(ic);
2977 av_dump_format(ic, 0, is->filename, 0);
2979 for (i = 0; i < ic->nb_streams; i++) {
2980 AVStream *st = ic->streams[i];
2981 enum AVMediaType type = st->codec->codec_type;
2982 st->discard = AVDISCARD_ALL;
2983 if (wanted_stream_spec[type] && st_index[type] == -1)
2984 if (avformat_match_stream_specifier(ic, st, wanted_stream_spec[type]) > 0)
2987 for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
2988 if (wanted_stream_spec[i] && st_index[i] == -1) {
2989 av_log(NULL, AV_LOG_ERROR, "Stream specifier %s does not match any %s stream\n", wanted_stream_spec[i], av_get_media_type_string(i));
2990 st_index[i] = INT_MAX;
2995 st_index[AVMEDIA_TYPE_VIDEO] =
2996 av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2997 st_index[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2999 st_index[AVMEDIA_TYPE_AUDIO] =
3000 av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
3001 st_index[AVMEDIA_TYPE_AUDIO],
3002 st_index[AVMEDIA_TYPE_VIDEO],
3004 if (!video_disable && !subtitle_disable)
3005 st_index[AVMEDIA_TYPE_SUBTITLE] =
3006 av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
3007 st_index[AVMEDIA_TYPE_SUBTITLE],
3008 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
3009 st_index[AVMEDIA_TYPE_AUDIO] :
3010 st_index[AVMEDIA_TYPE_VIDEO]),
3013 is->show_mode = show_mode;
3014 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
3015 AVStream *st = ic->streams[st_index[AVMEDIA_TYPE_VIDEO]];
3016 AVCodecContext *avctx = st->codec;
3017 AVRational sar = av_guess_sample_aspect_ratio(ic, st, NULL);
3019 set_default_window_size(avctx->width, avctx->height, sar);
3022 /* open the streams */
3023 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
3024 stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
3028 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
3029 ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
3031 if (is->show_mode == SHOW_MODE_NONE)
3032 is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
3034 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
3035 stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
3038 if (is->video_stream < 0 && is->audio_stream < 0) {
3039 av_log(NULL, AV_LOG_FATAL, "Failed to open file '%s' or configure filtergraph\n",
3045 if (infinite_buffer < 0 && is->realtime)
3046 infinite_buffer = 1;
3049 if (is->abort_request)
3051 if (is->paused != is->last_paused) {
3052 is->last_paused = is->paused;
3054 is->read_pause_return = av_read_pause(ic);
3058 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
3060 (!strcmp(ic->iformat->name, "rtsp") ||
3061 (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
3062 /* wait 10 ms to avoid trying to get another packet */
3069 int64_t seek_target = is->seek_pos;
3070 int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
3071 int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
3072 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
3073 // of the seek_pos/seek_rel variables
3075 ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
3077 av_log(NULL, AV_LOG_ERROR,
3078 "%s: error while seeking\n", is->ic->filename);
3080 if (is->audio_stream >= 0) {
3081 packet_queue_flush(&is->audioq);
3082 packet_queue_put(&is->audioq, &flush_pkt);
3084 if (is->subtitle_stream >= 0) {
3085 packet_queue_flush(&is->subtitleq);
3086 packet_queue_put(&is->subtitleq, &flush_pkt);
3088 if (is->video_stream >= 0) {
3089 packet_queue_flush(&is->videoq);
3090 packet_queue_put(&is->videoq, &flush_pkt);
3092 if (is->seek_flags & AVSEEK_FLAG_BYTE) {
3093 set_clock(&is->extclk, NAN, 0);
3095 set_clock(&is->extclk, seek_target / (double)AV_TIME_BASE, 0);
3099 is->queue_attachments_req = 1;
3102 step_to_next_frame(is);
3104 if (is->queue_attachments_req) {
3105 if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
3107 if ((ret = av_copy_packet(©, &is->video_st->attached_pic)) < 0)
3109 packet_queue_put(&is->videoq, ©);
3110 packet_queue_put_nullpacket(&is->videoq, is->video_stream);
3112 is->queue_attachments_req = 0;
3115 /* if the queue are full, no need to read more */
3116 if (infinite_buffer<1 &&
3117 (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
3118 || ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
3119 && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request
3120 || (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))
3121 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
3123 SDL_LockMutex(wait_mutex);
3124 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
3125 SDL_UnlockMutex(wait_mutex);
3129 (!is->audio_st || (is->auddec.finished == is->audioq.serial && frame_queue_nb_remaining(&is->sampq) == 0)) &&
3130 (!is->video_st || (is->viddec.finished == is->videoq.serial && frame_queue_nb_remaining(&is->pictq) == 0))) {
3131 if (loop != 1 && (!loop || --loop)) {
3132 stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
3133 } else if (autoexit) {
3138 ret = av_read_frame(ic, pkt);
3140 if ((ret == AVERROR_EOF || avio_feof(ic->pb)) && !is->eof) {
3141 if (is->video_stream >= 0)
3142 packet_queue_put_nullpacket(&is->videoq, is->video_stream);
3143 if (is->audio_stream >= 0)
3144 packet_queue_put_nullpacket(&is->audioq, is->audio_stream);
3145 if (is->subtitle_stream >= 0)
3146 packet_queue_put_nullpacket(&is->subtitleq, is->subtitle_stream);
3149 if (ic->pb && ic->pb->error)
3151 SDL_LockMutex(wait_mutex);
3152 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
3153 SDL_UnlockMutex(wait_mutex);
3158 /* check if packet is in play range specified by user, then queue, otherwise discard */
3159 stream_start_time = ic->streams[pkt->stream_index]->start_time;
3160 pkt_ts = pkt->pts == AV_NOPTS_VALUE ? pkt->dts : pkt->pts;
3161 pkt_in_play_range = duration == AV_NOPTS_VALUE ||
3162 (pkt_ts - (stream_start_time != AV_NOPTS_VALUE ? stream_start_time : 0)) *
3163 av_q2d(ic->streams[pkt->stream_index]->time_base) -
3164 (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
3165 <= ((double)duration / 1000000);
3166 if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
3167 packet_queue_put(&is->audioq, pkt);
3168 } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
3169 && !(is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
3170 packet_queue_put(&is->videoq, pkt);
3171 } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
3172 packet_queue_put(&is->subtitleq, pkt);
3174 av_free_packet(pkt);
3177 /* wait until the end */
3178 while (!is->abort_request) {
3184 /* close each stream */
3185 if (is->audio_stream >= 0)
3186 stream_component_close(is, is->audio_stream);
3187 if (is->video_stream >= 0)
3188 stream_component_close(is, is->video_stream);
3189 if (is->subtitle_stream >= 0)
3190 stream_component_close(is, is->subtitle_stream);
3192 avformat_close_input(&ic);
3199 event.type = FF_QUIT_EVENT;
3200 event.user.data1 = is;
3201 SDL_PushEvent(&event);
3203 SDL_DestroyMutex(wait_mutex);
3207 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
3211 is = av_mallocz(sizeof(VideoState));
3214 av_strlcpy(is->filename, filename, sizeof(is->filename));
3215 is->iformat = iformat;
3219 /* start video display */
3220 if (frame_queue_init(&is->pictq, &is->videoq, VIDEO_PICTURE_QUEUE_SIZE, 1) < 0)
3222 if (frame_queue_init(&is->subpq, &is->subtitleq, SUBPICTURE_QUEUE_SIZE, 0) < 0)
3224 if (frame_queue_init(&is->sampq, &is->audioq, SAMPLE_QUEUE_SIZE, 1) < 0)
3227 packet_queue_init(&is->videoq);
3228 packet_queue_init(&is->audioq);
3229 packet_queue_init(&is->subtitleq);
3231 is->continue_read_thread = SDL_CreateCond();
3233 init_clock(&is->vidclk, &is->videoq.serial);
3234 init_clock(&is->audclk, &is->audioq.serial);
3235 init_clock(&is->extclk, &is->extclk.serial);
3236 is->audio_clock_serial = -1;
3237 is->av_sync_type = av_sync_type;
3238 is->read_tid = SDL_CreateThread(read_thread, is);
3239 if (!is->read_tid) {
3247 static void stream_cycle_channel(VideoState *is, int codec_type)
3249 AVFormatContext *ic = is->ic;
3250 int start_index, stream_index;
3253 AVProgram *p = NULL;
3254 int nb_streams = is->ic->nb_streams;
3256 if (codec_type == AVMEDIA_TYPE_VIDEO) {
3257 start_index = is->last_video_stream;
3258 old_index = is->video_stream;
3259 } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
3260 start_index = is->last_audio_stream;
3261 old_index = is->audio_stream;
3263 start_index = is->last_subtitle_stream;
3264 old_index = is->subtitle_stream;
3266 stream_index = start_index;
3268 if (codec_type != AVMEDIA_TYPE_VIDEO && is->video_stream != -1) {
3269 p = av_find_program_from_stream(ic, NULL, is->video_stream);
3271 nb_streams = p->nb_stream_indexes;
3272 for (start_index = 0; start_index < nb_streams; start_index++)
3273 if (p->stream_index[start_index] == stream_index)
3275 if (start_index == nb_streams)
3277 stream_index = start_index;
3282 if (++stream_index >= nb_streams)
3284 if (codec_type == AVMEDIA_TYPE_SUBTITLE)
3287 is->last_subtitle_stream = -1;
3290 if (start_index == -1)
3294 if (stream_index == start_index)
3296 st = is->ic->streams[p ? p->stream_index[stream_index] : stream_index];
3297 if (st->codec->codec_type == codec_type) {
3298 /* check that parameters are OK */
3299 switch (codec_type) {
3300 case AVMEDIA_TYPE_AUDIO:
3301 if (st->codec->sample_rate != 0 &&
3302 st->codec->channels != 0)
3305 case AVMEDIA_TYPE_VIDEO:
3306 case AVMEDIA_TYPE_SUBTITLE:
3314 if (p && stream_index != -1)
3315 stream_index = p->stream_index[stream_index];
3316 av_log(NULL, AV_LOG_INFO, "Switch %s stream from #%d to #%d\n",
3317 av_get_media_type_string(codec_type),
3321 stream_component_close(is, old_index);
3322 stream_component_open(is, stream_index);
3326 static void toggle_full_screen(VideoState *is)
3328 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
3329 /* OS X needs to reallocate the SDL overlays */
3331 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
3332 is->pictq.queue[i].reallocate = 1;
3334 is_full_screen = !is_full_screen;
3335 video_open(is, 1, NULL);
3338 static void toggle_audio_display(VideoState *is)
3340 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
3341 int next = is->show_mode;
3343 next = (next + 1) % SHOW_MODE_NB;
3344 } while (next != is->show_mode && (next == SHOW_MODE_VIDEO && !is->video_st || next != SHOW_MODE_VIDEO && !is->audio_st));
3345 if (is->show_mode != next) {
3346 fill_rectangle(screen,
3347 is->xleft, is->ytop, is->width, is->height,
3349 is->force_refresh = 1;
3350 is->show_mode = next;
3354 static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) {
3355 double remaining_time = 0.0;
3357 while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
3358 if (!cursor_hidden && av_gettime_relative() - cursor_last_shown > CURSOR_HIDE_DELAY) {
3362 if (remaining_time > 0.0)
3363 av_usleep((int64_t)(remaining_time * 1000000.0));
3364 remaining_time = REFRESH_RATE;
3365 if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh))
3366 video_refresh(is, &remaining_time);
3371 static void seek_chapter(VideoState *is, int incr)
3373 int64_t pos = get_master_clock(is) * AV_TIME_BASE;
3376 if (!is->ic->nb_chapters)
3379 /* find the current chapter */
3380 for (i = 0; i < is->ic->nb_chapters; i++) {
3381 AVChapter *ch = is->ic->chapters[i];
3382 if (av_compare_ts(pos, AV_TIME_BASE_Q, ch->start, ch->time_base) < 0) {
3390 if (i >= is->ic->nb_chapters)
3393 av_log(NULL, AV_LOG_VERBOSE, "Seeking to chapter %d.\n", i);
3394 stream_seek(is, av_rescale_q(is->ic->chapters[i]->start, is->ic->chapters[i]->time_base,
3395 AV_TIME_BASE_Q), 0, 0);
3398 /* handle an event sent by the GUI */
3399 static void event_loop(VideoState *cur_stream)
3402 double incr, pos, frac;
3406 refresh_loop_wait_event(cur_stream, &event);
3407 switch (event.type) {
3409 if (exit_on_keydown) {
3410 do_exit(cur_stream);
3413 switch (event.key.keysym.sym) {
3416 do_exit(cur_stream);
3419 toggle_full_screen(cur_stream);
3420 cur_stream->force_refresh = 1;
3424 toggle_pause(cur_stream);
3426 case SDLK_s: // S: Step to next frame
3427 step_to_next_frame(cur_stream);
3430 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
3433 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
3436 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
3437 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
3438 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
3441 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
3445 if (cur_stream->show_mode == SHOW_MODE_VIDEO && cur_stream->vfilter_idx < nb_vfilters - 1) {
3446 if (++cur_stream->vfilter_idx >= nb_vfilters)
3447 cur_stream->vfilter_idx = 0;
3449 cur_stream->vfilter_idx = 0;
3450 toggle_audio_display(cur_stream);
3453 toggle_audio_display(cur_stream);
3457 if (cur_stream->ic->nb_chapters <= 1) {
3461 seek_chapter(cur_stream, 1);
3464 if (cur_stream->ic->nb_chapters <= 1) {
3468 seek_chapter(cur_stream, -1);
3482 if (seek_by_bytes) {
3484 if (pos < 0 && cur_stream->video_stream >= 0)
3485 pos = frame_queue_last_pos(&cur_stream->pictq);
3486 if (pos < 0 && cur_stream->audio_stream >= 0)
3487 pos = frame_queue_last_pos(&cur_stream->sampq);
3489 pos = avio_tell(cur_stream->ic->pb);
3490 if (cur_stream->ic->bit_rate)
3491 incr *= cur_stream->ic->bit_rate / 8.0;
3495 stream_seek(cur_stream, pos, incr, 1);
3497 pos = get_master_clock(cur_stream);
3499 pos = (double)cur_stream->seek_pos / AV_TIME_BASE;
3501 if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
3502 pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
3503 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
3510 case SDL_VIDEOEXPOSE:
3511 cur_stream->force_refresh = 1;
3513 case SDL_MOUSEBUTTONDOWN:
3514 if (exit_on_mousedown) {
3515 do_exit(cur_stream);
3518 case SDL_MOUSEMOTION:
3519 if (cursor_hidden) {
3523 cursor_last_shown = av_gettime_relative();
3524 if (event.type == SDL_MOUSEBUTTONDOWN) {
3527 if (event.motion.state != SDL_PRESSED)
3531 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
3532 uint64_t size = avio_size(cur_stream->ic->pb);
3533 stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
3537 int tns, thh, tmm, tss;
3538 tns = cur_stream->ic->duration / 1000000LL;
3540 tmm = (tns % 3600) / 60;
3542 frac = x / cur_stream->width;
3545 mm = (ns % 3600) / 60;
3547 av_log(NULL, AV_LOG_INFO,
3548 "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
3549 hh, mm, ss, thh, tmm, tss);
3550 ts = frac * cur_stream->ic->duration;
3551 if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
3552 ts += cur_stream->ic->start_time;
3553 stream_seek(cur_stream, ts, 0, 0);
3556 case SDL_VIDEORESIZE:
3557 screen = SDL_SetVideoMode(FFMIN(16383, event.resize.w), event.resize.h, 0,
3558 SDL_HWSURFACE|(is_full_screen?SDL_FULLSCREEN:SDL_RESIZABLE)|SDL_ASYNCBLIT|SDL_HWACCEL);
3560 av_log(NULL, AV_LOG_FATAL, "Failed to set video mode\n");
3561 do_exit(cur_stream);
3563 screen_width = cur_stream->width = screen->w;
3564 screen_height = cur_stream->height = screen->h;
3565 cur_stream->force_refresh = 1;
3569 do_exit(cur_stream);
3571 case FF_ALLOC_EVENT:
3572 alloc_picture(event.user.data1);
3580 static int opt_frame_size(void *optctx, const char *opt, const char *arg)
3582 av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
3583 return opt_default(NULL, "video_size", arg);
3586 static int opt_width(void *optctx, const char *opt, const char *arg)
3588 screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3592 static int opt_height(void *optctx, const char *opt, const char *arg)
3594 screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3598 static int opt_format(void *optctx, const char *opt, const char *arg)
3600 file_iformat = av_find_input_format(arg);
3601 if (!file_iformat) {
3602 av_log(NULL, AV_LOG_FATAL, "Unknown input format: %s\n", arg);
3603 return AVERROR(EINVAL);
3608 static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
3610 av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3611 return opt_default(NULL, "pixel_format", arg);
3614 static int opt_sync(void *optctx, const char *opt, const char *arg)
3616 if (!strcmp(arg, "audio"))
3617 av_sync_type = AV_SYNC_AUDIO_MASTER;
3618 else if (!strcmp(arg, "video"))
3619 av_sync_type = AV_SYNC_VIDEO_MASTER;
3620 else if (!strcmp(arg, "ext"))
3621 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3623 av_log(NULL, AV_LOG_ERROR, "Unknown value for %s: %s\n", opt, arg);
3629 static int opt_seek(void *optctx, const char *opt, const char *arg)
3631 start_time = parse_time_or_die(opt, arg, 1);
3635 static int opt_duration(void *optctx, const char *opt, const char *arg)
3637 duration = parse_time_or_die(opt, arg, 1);
3641 static int opt_show_mode(void *optctx, const char *opt, const char *arg)
3643 show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3644 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3645 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
3646 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3650 static void opt_input_file(void *optctx, const char *filename)
3652 if (input_filename) {
3653 av_log(NULL, AV_LOG_FATAL,
3654 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3655 filename, input_filename);
3658 if (!strcmp(filename, "-"))
3660 input_filename = filename;
3663 static int opt_codec(void *optctx, const char *opt, const char *arg)
3665 const char *spec = strchr(opt, ':');
3667 av_log(NULL, AV_LOG_ERROR,
3668 "No media specifier was specified in '%s' in option '%s'\n",
3670 return AVERROR(EINVAL);
3674 case 'a' : audio_codec_name = arg; break;
3675 case 's' : subtitle_codec_name = arg; break;
3676 case 'v' : video_codec_name = arg; break;
3678 av_log(NULL, AV_LOG_ERROR,
3679 "Invalid media specifier '%s' in option '%s'\n", spec, opt);
3680 return AVERROR(EINVAL);
3687 static const OptionDef options[] = {
3688 #include "cmdutils_common_opts.h"
3689 { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
3690 { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
3691 { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3692 { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
3693 { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
3694 { "vn", OPT_BOOL, { &video_disable }, "disable video" },
3695 { "sn", OPT_BOOL, { &subtitle_disable }, "disable subtitling" },
3696 { "ast", OPT_STRING | HAS_ARG | OPT_EXPERT, { &wanted_stream_spec[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_specifier" },
3697 { "vst", OPT_STRING | HAS_ARG | OPT_EXPERT, { &wanted_stream_spec[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_specifier" },
3698 { "sst", OPT_STRING | HAS_ARG | OPT_EXPERT, { &wanted_stream_spec[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_specifier" },
3699 { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
3700 { "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
3701 { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3702 { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
3703 { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
3704 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
3705 { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
3706 { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
3707 { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
3708 { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3709 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
3710 { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3711 { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
3712 { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
3713 { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
3714 { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
3715 { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
3716 { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
3717 { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
3719 { "vf", OPT_EXPERT | HAS_ARG, { .func_arg = opt_add_vfilter }, "set video filters", "filter_graph" },
3720 { "af", OPT_STRING | HAS_ARG, { &afilters }, "set audio filters", "filter_graph" },
3722 { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
3723 { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3724 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
3725 { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
3726 { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
3727 { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &audio_codec_name }, "force audio decoder", "decoder_name" },
3728 { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
3729 { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &video_codec_name }, "force video decoder", "decoder_name" },
3730 { "autorotate", OPT_BOOL, { &autorotate }, "automatically rotate video", "" },
3734 static void show_usage(void)
3736 av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3737 av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3738 av_log(NULL, AV_LOG_INFO, "\n");
3741 void show_help_default(const char *opt, const char *arg)
3743 av_log_set_callback(log_callback_help);
3745 show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
3746 show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3748 show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3749 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3750 #if !CONFIG_AVFILTER
3751 show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3753 show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
3755 printf("\nWhile playing:\n"
3757 "f toggle full screen\n"
3759 "a cycle audio channel in the current program\n"
3760 "v cycle video channel\n"
3761 "t cycle subtitle channel in the current program\n"
3763 "w cycle video filters or show modes\n"
3764 "s activate frame-step mode\n"
3765 "left/right seek backward/forward 10 seconds\n"
3766 "down/up seek backward/forward 1 minute\n"
3767 "page down/page up seek backward/forward 10 minutes\n"
3768 "mouse click seek to percentage in file corresponding to fraction of width\n"
3772 static int lockmgr(void **mtx, enum AVLockOp op)
3775 case AV_LOCK_CREATE:
3776 *mtx = SDL_CreateMutex();
3780 case AV_LOCK_OBTAIN:
3781 return !!SDL_LockMutex(*mtx);
3782 case AV_LOCK_RELEASE:
3783 return !!SDL_UnlockMutex(*mtx);
3784 case AV_LOCK_DESTROY:
3785 SDL_DestroyMutex(*mtx);
3791 /* Called from the main */
3792 int main(int argc, char **argv)
3796 char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
3798 av_log_set_flags(AV_LOG_SKIP_REPEATED);
3799 parse_loglevel(argc, argv, options);
3801 /* register all codecs, demux and protocols */
3803 avdevice_register_all();
3806 avfilter_register_all();
3809 avformat_network_init();
3813 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
3814 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
3816 show_banner(argc, argv, options);
3818 parse_options(NULL, argc, argv, options, opt_input_file);
3820 if (!input_filename) {
3822 av_log(NULL, AV_LOG_FATAL, "An input file must be specified\n");
3823 av_log(NULL, AV_LOG_FATAL,
3824 "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3828 if (display_disable) {
3831 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3833 flags &= ~SDL_INIT_AUDIO;
3834 if (display_disable)
3835 SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
3836 #if !defined(_WIN32) && !defined(__APPLE__)
3837 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3839 if (SDL_Init (flags)) {
3840 av_log(NULL, AV_LOG_FATAL, "Could not initialize SDL - %s\n", SDL_GetError());
3841 av_log(NULL, AV_LOG_FATAL, "(Did you set the DISPLAY variable?)\n");
3845 if (!display_disable) {
3846 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3847 fs_screen_width = vi->current_w;
3848 fs_screen_height = vi->current_h;
3851 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3852 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3853 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3855 if (av_lockmgr_register(lockmgr)) {
3856 av_log(NULL, AV_LOG_FATAL, "Could not initialize lock manager!\n");
3860 av_init_packet(&flush_pkt);
3861 flush_pkt.data = (uint8_t *)&flush_pkt;
3863 is = stream_open(input_filename, file_iformat);
3865 av_log(NULL, AV_LOG_FATAL, "Failed to initialize VideoState!\n");