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;
234 int audio_clock_serial;
235 double audio_diff_cum; /* used for AV difference average computation */
236 double audio_diff_avg_coef;
237 double audio_diff_threshold;
238 int audio_diff_avg_count;
241 int audio_hw_buf_size;
242 uint8_t silence_buf[SDL_AUDIO_MIN_BUFFER_SIZE];
245 unsigned int audio_buf_size; /* in bytes */
246 unsigned int audio_buf1_size;
247 int audio_buf_index; /* in bytes */
248 int audio_write_buf_size;
249 struct AudioParams audio_src;
251 struct AudioParams audio_filter_src;
253 struct AudioParams audio_tgt;
254 struct SwrContext *swr_ctx;
255 int frame_drops_early;
256 int frame_drops_late;
259 SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
261 int16_t sample_array[SAMPLE_ARRAY_SIZE];
262 int sample_array_index;
266 FFTSample *rdft_data;
268 double last_vis_time;
271 AVStream *subtitle_st;
272 PacketQueue subtitleq;
275 double frame_last_returned_time;
276 double frame_last_filter_delay;
280 double max_frame_duration; // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity
282 struct SwsContext *img_convert_ctx;
284 struct SwsContext *sub_convert_ctx;
285 SDL_Rect last_display_rect;
289 int width, height, xleft, ytop;
294 AVFilterContext *in_video_filter; // the first filter in the video chain
295 AVFilterContext *out_video_filter; // the last filter in the video chain
296 AVFilterContext *in_audio_filter; // the first filter in the audio chain
297 AVFilterContext *out_audio_filter; // the last filter in the audio chain
298 AVFilterGraph *agraph; // audio filter graph
301 int last_video_stream, last_audio_stream, last_subtitle_stream;
303 SDL_cond *continue_read_thread;
306 /* options specified by the user */
307 static AVInputFormat *file_iformat;
308 static const char *input_filename;
309 static const char *window_title;
310 static int fs_screen_width;
311 static int fs_screen_height;
312 static int default_width = 640;
313 static int default_height = 480;
314 static int screen_width = 0;
315 static int screen_height = 0;
316 static int audio_disable;
317 static int video_disable;
318 static int subtitle_disable;
319 static const char* wanted_stream_spec[AVMEDIA_TYPE_NB] = {0};
320 static int seek_by_bytes = -1;
321 static int display_disable;
322 static int show_status = 1;
323 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
324 static int64_t start_time = AV_NOPTS_VALUE;
325 static int64_t duration = AV_NOPTS_VALUE;
327 static int genpts = 0;
328 static int lowres = 0;
329 static int decoder_reorder_pts = -1;
331 static int exit_on_keydown;
332 static int exit_on_mousedown;
334 static int framedrop = -1;
335 static int infinite_buffer = -1;
336 static enum ShowMode show_mode = SHOW_MODE_NONE;
337 static const char *audio_codec_name;
338 static const char *subtitle_codec_name;
339 static const char *video_codec_name;
340 double rdftspeed = 0.02;
341 static int64_t cursor_last_shown;
342 static int cursor_hidden = 0;
344 static const char **vfilters_list = NULL;
345 static int nb_vfilters = 0;
346 static char *afilters = NULL;
348 static int autorotate = 1;
350 /* current context */
351 static int is_full_screen;
352 static int64_t audio_callback_time;
354 static AVPacket flush_pkt;
356 #define FF_ALLOC_EVENT (SDL_USEREVENT)
357 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
359 static SDL_Surface *screen;
362 static int opt_add_vfilter(void *optctx, const char *opt, const char *arg)
364 GROW_ARRAY(vfilters_list, nb_vfilters);
365 vfilters_list[nb_vfilters - 1] = arg;
371 int cmp_audio_fmts(enum AVSampleFormat fmt1, int64_t channel_count1,
372 enum AVSampleFormat fmt2, int64_t channel_count2)
374 /* If channel count == 1, planar and non-planar formats are the same */
375 if (channel_count1 == 1 && channel_count2 == 1)
376 return av_get_packed_sample_fmt(fmt1) != av_get_packed_sample_fmt(fmt2);
378 return channel_count1 != channel_count2 || fmt1 != fmt2;
382 int64_t get_valid_channel_layout(int64_t channel_layout, int channels)
384 if (channel_layout && av_get_channel_layout_nb_channels(channel_layout) == channels)
385 return channel_layout;
390 static void free_picture(Frame *vp);
392 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
394 MyAVPacketList *pkt1;
396 if (q->abort_request)
399 pkt1 = av_malloc(sizeof(MyAVPacketList));
404 if (pkt == &flush_pkt)
406 pkt1->serial = q->serial;
411 q->last_pkt->next = pkt1;
414 q->size += pkt1->pkt.size + sizeof(*pkt1);
415 /* XXX: should duplicate packet data in DV case */
416 SDL_CondSignal(q->cond);
420 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
424 /* duplicate the packet */
425 if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
428 SDL_LockMutex(q->mutex);
429 ret = packet_queue_put_private(q, pkt);
430 SDL_UnlockMutex(q->mutex);
432 if (pkt != &flush_pkt && ret < 0)
438 static int packet_queue_put_nullpacket(PacketQueue *q, int stream_index)
440 AVPacket pkt1, *pkt = &pkt1;
444 pkt->stream_index = stream_index;
445 return packet_queue_put(q, pkt);
448 /* packet queue handling */
449 static void packet_queue_init(PacketQueue *q)
451 memset(q, 0, sizeof(PacketQueue));
452 q->mutex = SDL_CreateMutex();
453 q->cond = SDL_CreateCond();
454 q->abort_request = 1;
457 static void packet_queue_flush(PacketQueue *q)
459 MyAVPacketList *pkt, *pkt1;
461 SDL_LockMutex(q->mutex);
462 for (pkt = q->first_pkt; pkt; pkt = pkt1) {
464 av_free_packet(&pkt->pkt);
471 SDL_UnlockMutex(q->mutex);
474 static void packet_queue_destroy(PacketQueue *q)
476 packet_queue_flush(q);
477 SDL_DestroyMutex(q->mutex);
478 SDL_DestroyCond(q->cond);
481 static void packet_queue_abort(PacketQueue *q)
483 SDL_LockMutex(q->mutex);
485 q->abort_request = 1;
487 SDL_CondSignal(q->cond);
489 SDL_UnlockMutex(q->mutex);
492 static void packet_queue_start(PacketQueue *q)
494 SDL_LockMutex(q->mutex);
495 q->abort_request = 0;
496 packet_queue_put_private(q, &flush_pkt);
497 SDL_UnlockMutex(q->mutex);
500 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
501 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
503 MyAVPacketList *pkt1;
506 SDL_LockMutex(q->mutex);
509 if (q->abort_request) {
516 q->first_pkt = pkt1->next;
520 q->size -= pkt1->pkt.size + sizeof(*pkt1);
523 *serial = pkt1->serial;
531 SDL_CondWait(q->cond, q->mutex);
534 SDL_UnlockMutex(q->mutex);
538 static void decoder_init(Decoder *d, AVCodecContext *avctx, PacketQueue *queue, SDL_cond *empty_queue_cond) {
539 memset(d, 0, sizeof(Decoder));
542 d->empty_queue_cond = empty_queue_cond;
543 d->start_pts = AV_NOPTS_VALUE;
546 static int decoder_decode_frame(Decoder *d, AVFrame *frame, AVSubtitle *sub) {
552 if (d->queue->abort_request)
555 if (!d->packet_pending || d->queue->serial != d->pkt_serial) {
558 if (d->queue->nb_packets == 0)
559 SDL_CondSignal(d->empty_queue_cond);
560 if (packet_queue_get(d->queue, &pkt, 1, &d->pkt_serial) < 0)
562 if (pkt.data == flush_pkt.data) {
563 avcodec_flush_buffers(d->avctx);
565 d->next_pts = d->start_pts;
566 d->next_pts_tb = d->start_pts_tb;
568 } while (pkt.data == flush_pkt.data || d->queue->serial != d->pkt_serial);
569 av_free_packet(&d->pkt);
570 d->pkt_temp = d->pkt = pkt;
571 d->packet_pending = 1;
574 switch (d->avctx->codec_type) {
575 case AVMEDIA_TYPE_VIDEO:
576 ret = avcodec_decode_video2(d->avctx, frame, &got_frame, &d->pkt_temp);
578 if (decoder_reorder_pts == -1) {
579 frame->pts = av_frame_get_best_effort_timestamp(frame);
580 } else if (decoder_reorder_pts) {
581 frame->pts = frame->pkt_pts;
583 frame->pts = frame->pkt_dts;
587 case AVMEDIA_TYPE_AUDIO:
588 ret = avcodec_decode_audio4(d->avctx, frame, &got_frame, &d->pkt_temp);
590 AVRational tb = (AVRational){1, frame->sample_rate};
591 if (frame->pts != AV_NOPTS_VALUE)
592 frame->pts = av_rescale_q(frame->pts, d->avctx->time_base, tb);
593 else if (frame->pkt_pts != AV_NOPTS_VALUE)
594 frame->pts = av_rescale_q(frame->pkt_pts, av_codec_get_pkt_timebase(d->avctx), tb);
595 else if (d->next_pts != AV_NOPTS_VALUE)
596 frame->pts = av_rescale_q(d->next_pts, d->next_pts_tb, tb);
597 if (frame->pts != AV_NOPTS_VALUE) {
598 d->next_pts = frame->pts + frame->nb_samples;
603 case AVMEDIA_TYPE_SUBTITLE:
604 ret = avcodec_decode_subtitle2(d->avctx, sub, &got_frame, &d->pkt_temp);
609 d->packet_pending = 0;
612 d->pkt_temp.pts = AV_NOPTS_VALUE;
613 if (d->pkt_temp.data) {
614 if (d->avctx->codec_type != AVMEDIA_TYPE_AUDIO)
615 ret = d->pkt_temp.size;
616 d->pkt_temp.data += ret;
617 d->pkt_temp.size -= ret;
618 if (d->pkt_temp.size <= 0)
619 d->packet_pending = 0;
622 d->packet_pending = 0;
623 d->finished = d->pkt_serial;
627 } while (!got_frame && !d->finished);
632 static void decoder_destroy(Decoder *d) {
633 av_free_packet(&d->pkt);
636 static void frame_queue_unref_item(Frame *vp)
638 av_frame_unref(vp->frame);
639 avsubtitle_free(&vp->sub);
642 static int frame_queue_init(FrameQueue *f, PacketQueue *pktq, int max_size, int keep_last)
645 memset(f, 0, sizeof(FrameQueue));
646 if (!(f->mutex = SDL_CreateMutex()))
647 return AVERROR(ENOMEM);
648 if (!(f->cond = SDL_CreateCond()))
649 return AVERROR(ENOMEM);
651 f->max_size = FFMIN(max_size, FRAME_QUEUE_SIZE);
652 f->keep_last = !!keep_last;
653 for (i = 0; i < f->max_size; i++)
654 if (!(f->queue[i].frame = av_frame_alloc()))
655 return AVERROR(ENOMEM);
659 static void frame_queue_destory(FrameQueue *f)
662 for (i = 0; i < f->max_size; i++) {
663 Frame *vp = &f->queue[i];
664 frame_queue_unref_item(vp);
665 av_frame_free(&vp->frame);
668 SDL_DestroyMutex(f->mutex);
669 SDL_DestroyCond(f->cond);
672 static void frame_queue_signal(FrameQueue *f)
674 SDL_LockMutex(f->mutex);
675 SDL_CondSignal(f->cond);
676 SDL_UnlockMutex(f->mutex);
679 static Frame *frame_queue_peek(FrameQueue *f)
681 return &f->queue[(f->rindex + f->rindex_shown) % f->max_size];
684 static Frame *frame_queue_peek_next(FrameQueue *f)
686 return &f->queue[(f->rindex + f->rindex_shown + 1) % f->max_size];
689 static Frame *frame_queue_peek_last(FrameQueue *f)
691 return &f->queue[f->rindex];
694 static Frame *frame_queue_peek_writable(FrameQueue *f)
696 /* wait until we have space to put a new frame */
697 SDL_LockMutex(f->mutex);
698 while (f->size >= f->max_size &&
699 !f->pktq->abort_request) {
700 SDL_CondWait(f->cond, f->mutex);
702 SDL_UnlockMutex(f->mutex);
704 if (f->pktq->abort_request)
707 return &f->queue[f->windex];
710 static Frame *frame_queue_peek_readable(FrameQueue *f)
712 /* wait until we have a readable a new frame */
713 SDL_LockMutex(f->mutex);
714 while (f->size - f->rindex_shown <= 0 &&
715 !f->pktq->abort_request) {
716 SDL_CondWait(f->cond, f->mutex);
718 SDL_UnlockMutex(f->mutex);
720 if (f->pktq->abort_request)
723 return &f->queue[(f->rindex + f->rindex_shown) % f->max_size];
726 static void frame_queue_push(FrameQueue *f)
728 if (++f->windex == f->max_size)
730 SDL_LockMutex(f->mutex);
732 SDL_CondSignal(f->cond);
733 SDL_UnlockMutex(f->mutex);
736 static void frame_queue_next(FrameQueue *f)
738 if (f->keep_last && !f->rindex_shown) {
742 frame_queue_unref_item(&f->queue[f->rindex]);
743 if (++f->rindex == f->max_size)
745 SDL_LockMutex(f->mutex);
747 SDL_CondSignal(f->cond);
748 SDL_UnlockMutex(f->mutex);
751 /* jump back to the previous frame if available by resetting rindex_shown */
752 static int frame_queue_prev(FrameQueue *f)
754 int ret = f->rindex_shown;
759 /* return the number of undisplayed frames in the queue */
760 static int frame_queue_nb_remaining(FrameQueue *f)
762 return f->size - f->rindex_shown;
765 /* return last shown position */
766 static int64_t frame_queue_last_pos(FrameQueue *f)
768 Frame *fp = &f->queue[f->rindex];
769 if (f->rindex_shown && fp->serial == f->pktq->serial)
775 static void decoder_abort(Decoder *d, FrameQueue *fq)
777 packet_queue_abort(d->queue);
778 frame_queue_signal(fq);
779 SDL_WaitThread(d->decoder_tid, NULL);
780 d->decoder_tid = NULL;
781 packet_queue_flush(d->queue);
784 static inline void fill_rectangle(SDL_Surface *screen,
785 int x, int y, int w, int h, int color, int update)
792 SDL_FillRect(screen, &rect, color);
793 if (update && w > 0 && h > 0)
794 SDL_UpdateRect(screen, x, y, w, h);
797 /* draw only the border of a rectangle */
798 static void fill_border(int xleft, int ytop, int width, int height, int x, int y, int w, int h, int color, int update)
802 /* fill the background */
806 w2 = width - (x + w);
812 h2 = height - (y + h);
815 fill_rectangle(screen,
819 fill_rectangle(screen,
820 xleft + width - w2, ytop,
823 fill_rectangle(screen,
827 fill_rectangle(screen,
828 xleft + w1, ytop + height - h2,
833 #define ALPHA_BLEND(a, oldp, newp, s)\
834 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
840 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
842 int x, y, Y, U, V, A;
843 uint8_t *lum, *cb, *cr;
844 int dstx, dsty, dstw, dsth;
845 const AVPicture *src = &rect->pict;
847 dstw = av_clip(rect->w, 0, imgw);
848 dsth = av_clip(rect->h, 0, imgh);
849 dstx = av_clip(rect->x, 0, imgw - dstw);
850 dsty = av_clip(rect->y, 0, imgh - dsth);
851 lum = dst->data[0] + dstx + dsty * dst->linesize[0];
852 cb = dst->data[1] + dstx/2 + (dsty >> 1) * dst->linesize[1];
853 cr = dst->data[2] + dstx/2 + (dsty >> 1) * dst->linesize[2];
855 for (y = 0; y<dsth; y++) {
856 for (x = 0; x<dstw; x++) {
857 Y = src->data[0][x + y*src->linesize[0]];
858 A = src->data[3][x + y*src->linesize[3]];
859 lum[0] = ALPHA_BLEND(A, lum[0], Y, 0);
862 lum += dst->linesize[0] - dstw;
865 for (y = 0; y<dsth/2; y++) {
866 for (x = 0; x<dstw/2; x++) {
867 U = src->data[1][x + y*src->linesize[1]];
868 V = src->data[2][x + y*src->linesize[2]];
869 A = src->data[3][2*x + 2*y *src->linesize[3]]
870 + src->data[3][2*x + 1 + 2*y *src->linesize[3]]
871 + src->data[3][2*x + 1 + (2*y+1)*src->linesize[3]]
872 + src->data[3][2*x + (2*y+1)*src->linesize[3]];
873 cb[0] = ALPHA_BLEND(A>>2, cb[0], U, 0);
874 cr[0] = ALPHA_BLEND(A>>2, cr[0], V, 0);
878 cb += dst->linesize[1] - dstw/2;
879 cr += dst->linesize[2] - dstw/2;
883 static void free_picture(Frame *vp)
886 SDL_FreeYUVOverlay(vp->bmp);
891 static void calculate_display_rect(SDL_Rect *rect,
892 int scr_xleft, int scr_ytop, int scr_width, int scr_height,
893 int pic_width, int pic_height, AVRational pic_sar)
896 int width, height, x, y;
898 if (pic_sar.num == 0)
901 aspect_ratio = av_q2d(pic_sar);
903 if (aspect_ratio <= 0.0)
905 aspect_ratio *= (float)pic_width / (float)pic_height;
907 /* XXX: we suppose the screen has a 1.0 pixel ratio */
909 width = ((int)rint(height * aspect_ratio)) & ~1;
910 if (width > scr_width) {
912 height = ((int)rint(width / aspect_ratio)) & ~1;
914 x = (scr_width - width) / 2;
915 y = (scr_height - height) / 2;
916 rect->x = scr_xleft + x;
917 rect->y = scr_ytop + y;
918 rect->w = FFMAX(width, 1);
919 rect->h = FFMAX(height, 1);
922 static void video_image_display(VideoState *is)
930 vp = frame_queue_peek(&is->pictq);
932 if (is->subtitle_st) {
933 if (frame_queue_nb_remaining(&is->subpq) > 0) {
934 sp = frame_queue_peek(&is->subpq);
936 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
937 SDL_LockYUVOverlay (vp->bmp);
939 pict.data[0] = vp->bmp->pixels[0];
940 pict.data[1] = vp->bmp->pixels[2];
941 pict.data[2] = vp->bmp->pixels[1];
943 pict.linesize[0] = vp->bmp->pitches[0];
944 pict.linesize[1] = vp->bmp->pitches[2];
945 pict.linesize[2] = vp->bmp->pitches[1];
947 for (i = 0; i < sp->sub.num_rects; i++)
948 blend_subrect(&pict, sp->sub.rects[i],
949 vp->bmp->w, vp->bmp->h);
951 SDL_UnlockYUVOverlay (vp->bmp);
956 calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp->width, vp->height, vp->sar);
958 SDL_DisplayYUVOverlay(vp->bmp, &rect);
960 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) {
961 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
962 fill_border(is->xleft, is->ytop, is->width, is->height, rect.x, rect.y, rect.w, rect.h, bgcolor, 1);
963 is->last_display_rect = rect;
968 static inline int compute_mod(int a, int b)
970 return a < 0 ? a%b + b : a%b;
973 static void video_audio_display(VideoState *s)
975 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
976 int ch, channels, h, h2, bgcolor, fgcolor;
978 int rdft_bits, nb_freq;
980 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
982 nb_freq = 1 << (rdft_bits - 1);
984 /* compute display index : center on currently output samples */
985 channels = s->audio_tgt.channels;
986 nb_display_channels = channels;
988 int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
990 delay = s->audio_write_buf_size;
993 /* to be more precise, we take into account the time spent since
994 the last buffer computation */
995 if (audio_callback_time) {
996 time_diff = av_gettime_relative() - audio_callback_time;
997 delay -= (time_diff * s->audio_tgt.freq) / 1000000;
1000 delay += 2 * data_used;
1001 if (delay < data_used)
1004 i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
1005 if (s->show_mode == SHOW_MODE_WAVES) {
1007 for (i = 0; i < 1000; i += channels) {
1008 int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
1009 int a = s->sample_array[idx];
1010 int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
1011 int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
1012 int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
1014 if (h < score && (b ^ c) < 0) {
1021 s->last_i_start = i_start;
1023 i_start = s->last_i_start;
1026 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
1027 if (s->show_mode == SHOW_MODE_WAVES) {
1028 fill_rectangle(screen,
1029 s->xleft, s->ytop, s->width, s->height,
1032 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
1034 /* total height for one channel */
1035 h = s->height / nb_display_channels;
1036 /* graph height / 2 */
1038 for (ch = 0; ch < nb_display_channels; ch++) {
1040 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
1041 for (x = 0; x < s->width; x++) {
1042 y = (s->sample_array[i] * h2) >> 15;
1049 fill_rectangle(screen,
1050 s->xleft + x, ys, 1, y,
1053 if (i >= SAMPLE_ARRAY_SIZE)
1054 i -= SAMPLE_ARRAY_SIZE;
1058 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
1060 for (ch = 1; ch < nb_display_channels; ch++) {
1061 y = s->ytop + ch * h;
1062 fill_rectangle(screen,
1063 s->xleft, y, s->width, 1,
1066 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
1068 nb_display_channels= FFMIN(nb_display_channels, 2);
1069 if (rdft_bits != s->rdft_bits) {
1070 av_rdft_end(s->rdft);
1071 av_free(s->rdft_data);
1072 s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
1073 s->rdft_bits = rdft_bits;
1074 s->rdft_data = av_malloc_array(nb_freq, 4 *sizeof(*s->rdft_data));
1076 if (!s->rdft || !s->rdft_data){
1077 av_log(NULL, AV_LOG_ERROR, "Failed to allocate buffers for RDFT, switching to waves display\n");
1078 s->show_mode = SHOW_MODE_WAVES;
1081 for (ch = 0; ch < nb_display_channels; ch++) {
1082 data[ch] = s->rdft_data + 2 * nb_freq * ch;
1084 for (x = 0; x < 2 * nb_freq; x++) {
1085 double w = (x-nb_freq) * (1.0 / nb_freq);
1086 data[ch][x] = s->sample_array[i] * (1.0 - w * w);
1088 if (i >= SAMPLE_ARRAY_SIZE)
1089 i -= SAMPLE_ARRAY_SIZE;
1091 av_rdft_calc(s->rdft, data[ch]);
1093 /* Least efficient way to do this, we should of course
1094 * directly access it but it is more than fast enough. */
1095 for (y = 0; y < s->height; y++) {
1096 double w = 1 / sqrt(nb_freq);
1097 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]));
1098 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
1099 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
1102 fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
1104 fill_rectangle(screen,
1105 s->xpos, s->height-y, 1, 1,
1109 SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
1112 if (s->xpos >= s->width)
1117 static void stream_close(VideoState *is)
1119 /* XXX: use a special url_shutdown call to abort parse cleanly */
1120 is->abort_request = 1;
1121 SDL_WaitThread(is->read_tid, NULL);
1122 packet_queue_destroy(&is->videoq);
1123 packet_queue_destroy(&is->audioq);
1124 packet_queue_destroy(&is->subtitleq);
1126 /* free all pictures */
1127 frame_queue_destory(&is->pictq);
1128 frame_queue_destory(&is->sampq);
1129 frame_queue_destory(&is->subpq);
1130 SDL_DestroyCond(is->continue_read_thread);
1131 #if !CONFIG_AVFILTER
1132 sws_freeContext(is->img_convert_ctx);
1134 sws_freeContext(is->sub_convert_ctx);
1138 static void do_exit(VideoState *is)
1143 av_lockmgr_register(NULL);
1146 av_freep(&vfilters_list);
1148 avformat_network_deinit();
1152 av_log(NULL, AV_LOG_QUIET, "%s", "");
1156 static void sigterm_handler(int sig)
1161 static void set_default_window_size(int width, int height, AVRational sar)
1164 calculate_display_rect(&rect, 0, 0, INT_MAX, height, width, height, sar);
1165 default_width = rect.w;
1166 default_height = rect.h;
1169 static int video_open(VideoState *is, int force_set_video_mode, Frame *vp)
1171 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
1174 if (is_full_screen) flags |= SDL_FULLSCREEN;
1175 else flags |= SDL_RESIZABLE;
1177 if (vp && vp->width)
1178 set_default_window_size(vp->width, vp->height, vp->sar);
1180 if (is_full_screen && fs_screen_width) {
1181 w = fs_screen_width;
1182 h = fs_screen_height;
1183 } else if (!is_full_screen && screen_width) {
1190 w = FFMIN(16383, w);
1191 if (screen && is->width == screen->w && screen->w == w
1192 && is->height== screen->h && screen->h == h && !force_set_video_mode)
1194 screen = SDL_SetVideoMode(w, h, 0, flags);
1196 av_log(NULL, AV_LOG_FATAL, "SDL: could not set video mode - exiting\n");
1200 window_title = input_filename;
1201 SDL_WM_SetCaption(window_title, window_title);
1203 is->width = screen->w;
1204 is->height = screen->h;
1209 /* display the current picture, if any */
1210 static void video_display(VideoState *is)
1213 video_open(is, 0, NULL);
1214 if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
1215 video_audio_display(is);
1216 else if (is->video_st)
1217 video_image_display(is);
1220 static double get_clock(Clock *c)
1222 if (*c->queue_serial != c->serial)
1227 double time = av_gettime_relative() / 1000000.0;
1228 return c->pts_drift + time - (time - c->last_updated) * (1.0 - c->speed);
1232 static void set_clock_at(Clock *c, double pts, int serial, double time)
1235 c->last_updated = time;
1236 c->pts_drift = c->pts - time;
1240 static void set_clock(Clock *c, double pts, int serial)
1242 double time = av_gettime_relative() / 1000000.0;
1243 set_clock_at(c, pts, serial, time);
1246 static void set_clock_speed(Clock *c, double speed)
1248 set_clock(c, get_clock(c), c->serial);
1252 static void init_clock(Clock *c, int *queue_serial)
1256 c->queue_serial = queue_serial;
1257 set_clock(c, NAN, -1);
1260 static void sync_clock_to_slave(Clock *c, Clock *slave)
1262 double clock = get_clock(c);
1263 double slave_clock = get_clock(slave);
1264 if (!isnan(slave_clock) && (isnan(clock) || fabs(clock - slave_clock) > AV_NOSYNC_THRESHOLD))
1265 set_clock(c, slave_clock, slave->serial);
1268 static int get_master_sync_type(VideoState *is) {
1269 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
1271 return AV_SYNC_VIDEO_MASTER;
1273 return AV_SYNC_AUDIO_MASTER;
1274 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
1276 return AV_SYNC_AUDIO_MASTER;
1278 return AV_SYNC_EXTERNAL_CLOCK;
1280 return AV_SYNC_EXTERNAL_CLOCK;
1284 /* get the current master clock value */
1285 static double get_master_clock(VideoState *is)
1289 switch (get_master_sync_type(is)) {
1290 case AV_SYNC_VIDEO_MASTER:
1291 val = get_clock(&is->vidclk);
1293 case AV_SYNC_AUDIO_MASTER:
1294 val = get_clock(&is->audclk);
1297 val = get_clock(&is->extclk);
1303 static void check_external_clock_speed(VideoState *is) {
1304 if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
1305 is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
1306 set_clock_speed(&is->extclk, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->extclk.speed - EXTERNAL_CLOCK_SPEED_STEP));
1307 } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
1308 (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
1309 set_clock_speed(&is->extclk, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->extclk.speed + EXTERNAL_CLOCK_SPEED_STEP));
1311 double speed = is->extclk.speed;
1313 set_clock_speed(&is->extclk, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
1317 /* seek in the stream */
1318 static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
1320 if (!is->seek_req) {
1323 is->seek_flags &= ~AVSEEK_FLAG_BYTE;
1325 is->seek_flags |= AVSEEK_FLAG_BYTE;
1327 SDL_CondSignal(is->continue_read_thread);
1331 /* pause or resume the video */
1332 static void stream_toggle_pause(VideoState *is)
1335 is->frame_timer += av_gettime_relative() / 1000000.0 - is->vidclk.last_updated;
1336 if (is->read_pause_return != AVERROR(ENOSYS)) {
1337 is->vidclk.paused = 0;
1339 set_clock(&is->vidclk, get_clock(&is->vidclk), is->vidclk.serial);
1341 set_clock(&is->extclk, get_clock(&is->extclk), is->extclk.serial);
1342 is->paused = is->audclk.paused = is->vidclk.paused = is->extclk.paused = !is->paused;
1345 static void toggle_pause(VideoState *is)
1347 stream_toggle_pause(is);
1351 static void step_to_next_frame(VideoState *is)
1353 /* if the stream is paused unpause it, then step */
1355 stream_toggle_pause(is);
1359 static double compute_target_delay(double delay, VideoState *is)
1361 double sync_threshold, diff = 0;
1363 /* update delay to follow master synchronisation source */
1364 if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
1365 /* if video is slave, we try to correct big delays by
1366 duplicating or deleting a frame */
1367 diff = get_clock(&is->vidclk) - get_master_clock(is);
1369 /* skip or repeat frame. We take into account the
1370 delay to compute the threshold. I still don't know
1371 if it is the best guess */
1372 sync_threshold = FFMAX(AV_SYNC_THRESHOLD_MIN, FFMIN(AV_SYNC_THRESHOLD_MAX, delay));
1373 if (!isnan(diff) && fabs(diff) < is->max_frame_duration) {
1374 if (diff <= -sync_threshold)
1375 delay = FFMAX(0, delay + diff);
1376 else if (diff >= sync_threshold && delay > AV_SYNC_FRAMEDUP_THRESHOLD)
1377 delay = delay + diff;
1378 else if (diff >= sync_threshold)
1383 av_log(NULL, AV_LOG_TRACE, "video: delay=%0.3f A-V=%f\n",
1389 static double vp_duration(VideoState *is, Frame *vp, Frame *nextvp) {
1390 if (vp->serial == nextvp->serial) {
1391 double duration = nextvp->pts - vp->pts;
1392 if (isnan(duration) || duration <= 0 || duration > is->max_frame_duration)
1393 return vp->duration;
1401 static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
1402 /* update current video pts */
1403 set_clock(&is->vidclk, pts, serial);
1404 sync_clock_to_slave(&is->extclk, &is->vidclk);
1407 /* called to display each frame */
1408 static void video_refresh(void *opaque, double *remaining_time)
1410 VideoState *is = opaque;
1415 if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
1416 check_external_clock_speed(is);
1418 if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
1419 time = av_gettime_relative() / 1000000.0;
1420 if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
1422 is->last_vis_time = time;
1424 *remaining_time = FFMIN(*remaining_time, is->last_vis_time + rdftspeed - time);
1429 if (is->force_refresh)
1430 redisplay = frame_queue_prev(&is->pictq);
1432 if (frame_queue_nb_remaining(&is->pictq) == 0) {
1433 // nothing to do, no picture to display in the queue
1435 double last_duration, duration, delay;
1438 /* dequeue the picture */
1439 lastvp = frame_queue_peek_last(&is->pictq);
1440 vp = frame_queue_peek(&is->pictq);
1442 if (vp->serial != is->videoq.serial) {
1443 frame_queue_next(&is->pictq);
1448 if (lastvp->serial != vp->serial && !redisplay)
1449 is->frame_timer = av_gettime_relative() / 1000000.0;
1454 /* compute nominal last_duration */
1455 last_duration = vp_duration(is, lastvp, vp);
1459 delay = compute_target_delay(last_duration, is);
1461 time= av_gettime_relative()/1000000.0;
1462 if (time < is->frame_timer + delay && !redisplay) {
1463 *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
1467 is->frame_timer += delay;
1468 if (delay > 0 && time - is->frame_timer > AV_SYNC_THRESHOLD_MAX)
1469 is->frame_timer = time;
1471 SDL_LockMutex(is->pictq.mutex);
1472 if (!redisplay && !isnan(vp->pts))
1473 update_video_pts(is, vp->pts, vp->pos, vp->serial);
1474 SDL_UnlockMutex(is->pictq.mutex);
1476 if (frame_queue_nb_remaining(&is->pictq) > 1) {
1477 Frame *nextvp = frame_queue_peek_next(&is->pictq);
1478 duration = vp_duration(is, vp, nextvp);
1479 if(!is->step && (redisplay || framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
1481 is->frame_drops_late++;
1482 frame_queue_next(&is->pictq);
1488 if (is->subtitle_st) {
1489 while (frame_queue_nb_remaining(&is->subpq) > 0) {
1490 sp = frame_queue_peek(&is->subpq);
1492 if (frame_queue_nb_remaining(&is->subpq) > 1)
1493 sp2 = frame_queue_peek_next(&is->subpq);
1497 if (sp->serial != is->subtitleq.serial
1498 || (is->vidclk.pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1499 || (sp2 && is->vidclk.pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1501 frame_queue_next(&is->subpq);
1509 /* display picture */
1510 if (!display_disable && is->show_mode == SHOW_MODE_VIDEO)
1513 frame_queue_next(&is->pictq);
1515 if (is->step && !is->paused)
1516 stream_toggle_pause(is);
1519 is->force_refresh = 0;
1521 static int64_t last_time;
1523 int aqsize, vqsize, sqsize;
1526 cur_time = av_gettime_relative();
1527 if (!last_time || (cur_time - last_time) >= 30000) {
1532 aqsize = is->audioq.size;
1534 vqsize = is->videoq.size;
1535 if (is->subtitle_st)
1536 sqsize = is->subtitleq.size;
1538 if (is->audio_st && is->video_st)
1539 av_diff = get_clock(&is->audclk) - get_clock(&is->vidclk);
1540 else if (is->video_st)
1541 av_diff = get_master_clock(is) - get_clock(&is->vidclk);
1542 else if (is->audio_st)
1543 av_diff = get_master_clock(is) - get_clock(&is->audclk);
1544 av_log(NULL, AV_LOG_INFO,
1545 "%7.2f %s:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
1546 get_master_clock(is),
1547 (is->audio_st && is->video_st) ? "A-V" : (is->video_st ? "M-V" : (is->audio_st ? "M-A" : " ")),
1549 is->frame_drops_early + is->frame_drops_late,
1553 is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1554 is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1556 last_time = cur_time;
1561 /* allocate a picture (needs to do that in main thread to avoid
1562 potential locking problems */
1563 static void alloc_picture(VideoState *is)
1568 vp = &is->pictq.queue[is->pictq.windex];
1572 video_open(is, 0, vp);
1574 vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1577 bufferdiff = vp->bmp ? FFMAX(vp->bmp->pixels[0], vp->bmp->pixels[1]) - FFMIN(vp->bmp->pixels[0], vp->bmp->pixels[1]) : 0;
1578 if (!vp->bmp || vp->bmp->pitches[0] < vp->width || bufferdiff < (int64_t)vp->height * vp->bmp->pitches[0]) {
1579 /* SDL allocates a buffer smaller than requested if the video
1580 * overlay hardware is unable to support the requested size. */
1581 av_log(NULL, AV_LOG_FATAL,
1582 "Error: the video system does not support an image\n"
1583 "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1584 "to reduce the image size.\n", vp->width, vp->height );
1588 SDL_LockMutex(is->pictq.mutex);
1590 SDL_CondSignal(is->pictq.cond);
1591 SDL_UnlockMutex(is->pictq.mutex);
1594 static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
1595 int i, width, height;
1597 for (i = 0; i < 3; i++) {
1604 if (bmp->pitches[i] > width) {
1605 maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
1606 for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
1612 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, double duration, int64_t pos, int serial)
1616 #if defined(DEBUG_SYNC) && 0
1617 printf("frame_type=%c pts=%0.3f\n",
1618 av_get_picture_type_char(src_frame->pict_type), pts);
1621 if (!(vp = frame_queue_peek_writable(&is->pictq)))
1624 vp->sar = src_frame->sample_aspect_ratio;
1626 /* alloc or resize hardware picture buffer */
1627 if (!vp->bmp || vp->reallocate || !vp->allocated ||
1628 vp->width != src_frame->width ||
1629 vp->height != src_frame->height) {
1634 vp->width = src_frame->width;
1635 vp->height = src_frame->height;
1637 /* the allocation must be done in the main thread to avoid
1638 locking problems. */
1639 event.type = FF_ALLOC_EVENT;
1640 event.user.data1 = is;
1641 SDL_PushEvent(&event);
1643 /* wait until the picture is allocated */
1644 SDL_LockMutex(is->pictq.mutex);
1645 while (!vp->allocated && !is->videoq.abort_request) {
1646 SDL_CondWait(is->pictq.cond, is->pictq.mutex);
1648 /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1649 if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1650 while (!vp->allocated && !is->abort_request) {
1651 SDL_CondWait(is->pictq.cond, is->pictq.mutex);
1654 SDL_UnlockMutex(is->pictq.mutex);
1656 if (is->videoq.abort_request)
1660 /* if the frame is not skipped, then display it */
1662 AVPicture pict = { { 0 } };
1664 /* get a pointer on the bitmap */
1665 SDL_LockYUVOverlay (vp->bmp);
1667 pict.data[0] = vp->bmp->pixels[0];
1668 pict.data[1] = vp->bmp->pixels[2];
1669 pict.data[2] = vp->bmp->pixels[1];
1671 pict.linesize[0] = vp->bmp->pitches[0];
1672 pict.linesize[1] = vp->bmp->pitches[2];
1673 pict.linesize[2] = vp->bmp->pitches[1];
1676 // FIXME use direct rendering
1677 av_picture_copy(&pict, (AVPicture *)src_frame,
1678 src_frame->format, vp->width, vp->height);
1680 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1681 is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1682 vp->width, vp->height, src_frame->format, vp->width, vp->height,
1683 AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1684 if (!is->img_convert_ctx) {
1685 av_log(NULL, AV_LOG_FATAL, "Cannot initialize the conversion context\n");
1688 sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1689 0, vp->height, pict.data, pict.linesize);
1691 /* workaround SDL PITCH_WORKAROUND */
1692 duplicate_right_border_pixels(vp->bmp);
1693 /* update the bitmap content */
1694 SDL_UnlockYUVOverlay(vp->bmp);
1697 vp->duration = duration;
1699 vp->serial = serial;
1701 /* now we can update the picture count */
1702 frame_queue_push(&is->pictq);
1707 static int get_video_frame(VideoState *is, AVFrame *frame)
1711 if ((got_picture = decoder_decode_frame(&is->viddec, frame, NULL)) < 0)
1717 if (frame->pts != AV_NOPTS_VALUE)
1718 dpts = av_q2d(is->video_st->time_base) * frame->pts;
1720 frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
1722 is->viddec_width = frame->width;
1723 is->viddec_height = frame->height;
1725 if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
1726 if (frame->pts != AV_NOPTS_VALUE) {
1727 double diff = dpts - get_master_clock(is);
1728 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD &&
1729 diff - is->frame_last_filter_delay < 0 &&
1730 is->viddec.pkt_serial == is->vidclk.serial &&
1731 is->videoq.nb_packets) {
1732 is->frame_drops_early++;
1733 av_frame_unref(frame);
1744 static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
1745 AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
1748 int nb_filters = graph->nb_filters;
1749 AVFilterInOut *outputs = NULL, *inputs = NULL;
1752 outputs = avfilter_inout_alloc();
1753 inputs = avfilter_inout_alloc();
1754 if (!outputs || !inputs) {
1755 ret = AVERROR(ENOMEM);
1759 outputs->name = av_strdup("in");
1760 outputs->filter_ctx = source_ctx;
1761 outputs->pad_idx = 0;
1762 outputs->next = NULL;
1764 inputs->name = av_strdup("out");
1765 inputs->filter_ctx = sink_ctx;
1766 inputs->pad_idx = 0;
1767 inputs->next = NULL;
1769 if ((ret = avfilter_graph_parse_ptr(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
1772 if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
1776 /* Reorder the filters to ensure that inputs of the custom filters are merged first */
1777 for (i = 0; i < graph->nb_filters - nb_filters; i++)
1778 FFSWAP(AVFilterContext*, graph->filters[i], graph->filters[i + nb_filters]);
1780 ret = avfilter_graph_config(graph, NULL);
1782 avfilter_inout_free(&outputs);
1783 avfilter_inout_free(&inputs);
1787 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame)
1789 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
1790 char sws_flags_str[128];
1791 char buffersrc_args[256];
1793 AVFilterContext *filt_src = NULL, *filt_out = NULL, *last_filter = NULL;
1794 AVCodecContext *codec = is->video_st->codec;
1795 AVRational fr = av_guess_frame_rate(is->ic, is->video_st, NULL);
1797 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1798 snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
1799 graph->scale_sws_opts = av_strdup(sws_flags_str);
1801 snprintf(buffersrc_args, sizeof(buffersrc_args),
1802 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
1803 frame->width, frame->height, frame->format,
1804 is->video_st->time_base.num, is->video_st->time_base.den,
1805 codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
1806 if (fr.num && fr.den)
1807 av_strlcatf(buffersrc_args, sizeof(buffersrc_args), ":frame_rate=%d/%d", fr.num, fr.den);
1809 if ((ret = avfilter_graph_create_filter(&filt_src,
1810 avfilter_get_by_name("buffer"),
1811 "ffplay_buffer", buffersrc_args, NULL,
1815 ret = avfilter_graph_create_filter(&filt_out,
1816 avfilter_get_by_name("buffersink"),
1817 "ffplay_buffersink", NULL, NULL, graph);
1821 if ((ret = av_opt_set_int_list(filt_out, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
1824 last_filter = filt_out;
1826 /* Note: this macro adds a filter before the lastly added filter, so the
1827 * processing order of the filters is in reverse */
1828 #define INSERT_FILT(name, arg) do { \
1829 AVFilterContext *filt_ctx; \
1831 ret = avfilter_graph_create_filter(&filt_ctx, \
1832 avfilter_get_by_name(name), \
1833 "ffplay_" name, arg, NULL, graph); \
1837 ret = avfilter_link(filt_ctx, 0, last_filter, 0); \
1841 last_filter = filt_ctx; \
1844 /* SDL YUV code is not handling odd width/height for some driver
1845 * combinations, therefore we crop the picture to an even width/height. */
1846 INSERT_FILT("crop", "floor(in_w/2)*2:floor(in_h/2)*2");
1849 double theta = get_rotation(is->video_st);
1851 if (fabs(theta - 90) < 1.0) {
1852 INSERT_FILT("transpose", "clock");
1853 } else if (fabs(theta - 180) < 1.0) {
1854 INSERT_FILT("hflip", NULL);
1855 INSERT_FILT("vflip", NULL);
1856 } else if (fabs(theta - 270) < 1.0) {
1857 INSERT_FILT("transpose", "cclock");
1858 } else if (fabs(theta) > 1.0) {
1859 char rotate_buf[64];
1860 snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta);
1861 INSERT_FILT("rotate", rotate_buf);
1865 if ((ret = configure_filtergraph(graph, vfilters, filt_src, last_filter)) < 0)
1868 is->in_video_filter = filt_src;
1869 is->out_video_filter = filt_out;
1875 static int configure_audio_filters(VideoState *is, const char *afilters, int force_output_format)
1877 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
1878 int sample_rates[2] = { 0, -1 };
1879 int64_t channel_layouts[2] = { 0, -1 };
1880 int channels[2] = { 0, -1 };
1881 AVFilterContext *filt_asrc = NULL, *filt_asink = NULL;
1882 char aresample_swr_opts[512] = "";
1883 AVDictionaryEntry *e = NULL;
1884 char asrc_args[256];
1887 avfilter_graph_free(&is->agraph);
1888 if (!(is->agraph = avfilter_graph_alloc()))
1889 return AVERROR(ENOMEM);
1891 while ((e = av_dict_get(swr_opts, "", e, AV_DICT_IGNORE_SUFFIX)))
1892 av_strlcatf(aresample_swr_opts, sizeof(aresample_swr_opts), "%s=%s:", e->key, e->value);
1893 if (strlen(aresample_swr_opts))
1894 aresample_swr_opts[strlen(aresample_swr_opts)-1] = '\0';
1895 av_opt_set(is->agraph, "aresample_swr_opts", aresample_swr_opts, 0);
1897 ret = snprintf(asrc_args, sizeof(asrc_args),
1898 "sample_rate=%d:sample_fmt=%s:channels=%d:time_base=%d/%d",
1899 is->audio_filter_src.freq, av_get_sample_fmt_name(is->audio_filter_src.fmt),
1900 is->audio_filter_src.channels,
1901 1, is->audio_filter_src.freq);
1902 if (is->audio_filter_src.channel_layout)
1903 snprintf(asrc_args + ret, sizeof(asrc_args) - ret,
1904 ":channel_layout=0x%"PRIx64, is->audio_filter_src.channel_layout);
1906 ret = avfilter_graph_create_filter(&filt_asrc,
1907 avfilter_get_by_name("abuffer"), "ffplay_abuffer",
1908 asrc_args, NULL, is->agraph);
1913 ret = avfilter_graph_create_filter(&filt_asink,
1914 avfilter_get_by_name("abuffersink"), "ffplay_abuffersink",
1915 NULL, NULL, is->agraph);
1919 if ((ret = av_opt_set_int_list(filt_asink, "sample_fmts", sample_fmts, AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
1921 if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
1924 if (force_output_format) {
1925 channel_layouts[0] = is->audio_tgt.channel_layout;
1926 channels [0] = is->audio_tgt.channels;
1927 sample_rates [0] = is->audio_tgt.freq;
1928 if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 0, AV_OPT_SEARCH_CHILDREN)) < 0)
1930 if ((ret = av_opt_set_int_list(filt_asink, "channel_layouts", channel_layouts, -1, AV_OPT_SEARCH_CHILDREN)) < 0)
1932 if ((ret = av_opt_set_int_list(filt_asink, "channel_counts" , channels , -1, AV_OPT_SEARCH_CHILDREN)) < 0)
1934 if ((ret = av_opt_set_int_list(filt_asink, "sample_rates" , sample_rates , -1, AV_OPT_SEARCH_CHILDREN)) < 0)
1939 if ((ret = configure_filtergraph(is->agraph, afilters, filt_asrc, filt_asink)) < 0)
1942 is->in_audio_filter = filt_asrc;
1943 is->out_audio_filter = filt_asink;
1947 avfilter_graph_free(&is->agraph);
1950 #endif /* CONFIG_AVFILTER */
1952 static int audio_thread(void *arg)
1954 VideoState *is = arg;
1955 AVFrame *frame = av_frame_alloc();
1958 int last_serial = -1;
1959 int64_t dec_channel_layout;
1967 return AVERROR(ENOMEM);
1970 if ((got_frame = decoder_decode_frame(&is->auddec, frame, NULL)) < 0)
1974 tb = (AVRational){1, frame->sample_rate};
1977 dec_channel_layout = get_valid_channel_layout(frame->channel_layout, av_frame_get_channels(frame));
1980 cmp_audio_fmts(is->audio_filter_src.fmt, is->audio_filter_src.channels,
1981 frame->format, av_frame_get_channels(frame)) ||
1982 is->audio_filter_src.channel_layout != dec_channel_layout ||
1983 is->audio_filter_src.freq != frame->sample_rate ||
1984 is->auddec.pkt_serial != last_serial;
1987 char buf1[1024], buf2[1024];
1988 av_get_channel_layout_string(buf1, sizeof(buf1), -1, is->audio_filter_src.channel_layout);
1989 av_get_channel_layout_string(buf2, sizeof(buf2), -1, dec_channel_layout);
1990 av_log(NULL, AV_LOG_DEBUG,
1991 "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",
1992 is->audio_filter_src.freq, is->audio_filter_src.channels, av_get_sample_fmt_name(is->audio_filter_src.fmt), buf1, last_serial,
1993 frame->sample_rate, av_frame_get_channels(frame), av_get_sample_fmt_name(frame->format), buf2, is->auddec.pkt_serial);
1995 is->audio_filter_src.fmt = frame->format;
1996 is->audio_filter_src.channels = av_frame_get_channels(frame);
1997 is->audio_filter_src.channel_layout = dec_channel_layout;
1998 is->audio_filter_src.freq = frame->sample_rate;
1999 last_serial = is->auddec.pkt_serial;
2001 if ((ret = configure_audio_filters(is, afilters, 1)) < 0)
2005 if ((ret = av_buffersrc_add_frame(is->in_audio_filter, frame)) < 0)
2008 while ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, frame, 0)) >= 0) {
2009 tb = is->out_audio_filter->inputs[0]->time_base;
2011 if (!(af = frame_queue_peek_writable(&is->sampq)))
2014 af->pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(tb);
2015 af->pos = av_frame_get_pkt_pos(frame);
2016 af->serial = is->auddec.pkt_serial;
2017 af->duration = av_q2d((AVRational){frame->nb_samples, frame->sample_rate});
2019 av_frame_move_ref(af->frame, frame);
2020 frame_queue_push(&is->sampq);
2023 if (is->audioq.serial != is->auddec.pkt_serial)
2026 if (ret == AVERROR_EOF)
2027 is->auddec.finished = is->auddec.pkt_serial;
2030 } while (ret >= 0 || ret == AVERROR(EAGAIN) || ret == AVERROR_EOF);
2033 avfilter_graph_free(&is->agraph);
2035 av_frame_free(&frame);
2039 static void decoder_start(Decoder *d, int (*fn)(void *), void *arg)
2041 packet_queue_start(d->queue);
2042 d->decoder_tid = SDL_CreateThread(fn, arg);
2045 static int video_thread(void *arg)
2047 VideoState *is = arg;
2048 AVFrame *frame = av_frame_alloc();
2052 AVRational tb = is->video_st->time_base;
2053 AVRational frame_rate = av_guess_frame_rate(is->ic, is->video_st, NULL);
2056 AVFilterGraph *graph = avfilter_graph_alloc();
2057 AVFilterContext *filt_out = NULL, *filt_in = NULL;
2060 enum AVPixelFormat last_format = -2;
2061 int last_serial = -1;
2062 int last_vfilter_idx = 0;
2064 av_frame_free(&frame);
2065 return AVERROR(ENOMEM);
2072 avfilter_graph_free(&graph);
2074 return AVERROR(ENOMEM);
2078 ret = get_video_frame(is, frame);
2085 if ( last_w != frame->width
2086 || last_h != frame->height
2087 || last_format != frame->format
2088 || last_serial != is->viddec.pkt_serial
2089 || last_vfilter_idx != is->vfilter_idx) {
2090 av_log(NULL, AV_LOG_DEBUG,
2091 "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
2093 (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
2094 frame->width, frame->height,
2095 (const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), is->viddec.pkt_serial);
2096 avfilter_graph_free(&graph);
2097 graph = avfilter_graph_alloc();
2098 if ((ret = configure_video_filters(graph, is, vfilters_list ? vfilters_list[is->vfilter_idx] : NULL, frame)) < 0) {
2100 event.type = FF_QUIT_EVENT;
2101 event.user.data1 = is;
2102 SDL_PushEvent(&event);
2105 filt_in = is->in_video_filter;
2106 filt_out = is->out_video_filter;
2107 last_w = frame->width;
2108 last_h = frame->height;
2109 last_format = frame->format;
2110 last_serial = is->viddec.pkt_serial;
2111 last_vfilter_idx = is->vfilter_idx;
2112 frame_rate = filt_out->inputs[0]->frame_rate;
2115 ret = av_buffersrc_add_frame(filt_in, frame);
2120 is->frame_last_returned_time = av_gettime_relative() / 1000000.0;
2122 ret = av_buffersink_get_frame_flags(filt_out, frame, 0);
2124 if (ret == AVERROR_EOF)
2125 is->viddec.finished = is->viddec.pkt_serial;
2130 is->frame_last_filter_delay = av_gettime_relative() / 1000000.0 - is->frame_last_returned_time;
2131 if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
2132 is->frame_last_filter_delay = 0;
2133 tb = filt_out->inputs[0]->time_base;
2135 duration = (frame_rate.num && frame_rate.den ? av_q2d((AVRational){frame_rate.den, frame_rate.num}) : 0);
2136 pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(tb);
2137 ret = queue_picture(is, frame, pts, duration, av_frame_get_pkt_pos(frame), is->viddec.pkt_serial);
2138 av_frame_unref(frame);
2148 avfilter_graph_free(&graph);
2150 av_frame_free(&frame);
2154 static int subtitle_thread(void *arg)
2156 VideoState *is = arg;
2163 if (!(sp = frame_queue_peek_writable(&is->subpq)))
2166 if ((got_subtitle = decoder_decode_frame(&is->subdec, NULL, &sp->sub)) < 0)
2171 if (got_subtitle && sp->sub.format == 0) {
2172 if (sp->sub.pts != AV_NOPTS_VALUE)
2173 pts = sp->sub.pts / (double)AV_TIME_BASE;
2175 sp->serial = is->subdec.pkt_serial;
2177 for (i = 0; i < sp->sub.num_rects; i++)
2179 int in_w = sp->sub.rects[i]->w;
2180 int in_h = sp->sub.rects[i]->h;
2181 int subw = is->subdec.avctx->width ? is->subdec.avctx->width : is->viddec_width;
2182 int subh = is->subdec.avctx->height ? is->subdec.avctx->height : is->viddec_height;
2183 int out_w = is->viddec_width ? in_w * is->viddec_width / subw : in_w;
2184 int out_h = is->viddec_height ? in_h * is->viddec_height / subh : in_h;
2187 //can not use avpicture_alloc as it is not compatible with avsubtitle_free()
2188 av_image_fill_linesizes(newpic.linesize, AV_PIX_FMT_YUVA420P, out_w);
2189 newpic.data[0] = av_malloc(newpic.linesize[0] * out_h);
2190 newpic.data[3] = av_malloc(newpic.linesize[3] * out_h);
2191 newpic.data[1] = av_malloc(newpic.linesize[1] * ((out_h+1)/2));
2192 newpic.data[2] = av_malloc(newpic.linesize[2] * ((out_h+1)/2));
2194 is->sub_convert_ctx = sws_getCachedContext(is->sub_convert_ctx,
2195 in_w, in_h, AV_PIX_FMT_PAL8, out_w, out_h,
2196 AV_PIX_FMT_YUVA420P, sws_flags, NULL, NULL, NULL);
2197 if (!is->sub_convert_ctx || !newpic.data[0] || !newpic.data[3] ||
2198 !newpic.data[1] || !newpic.data[2]
2200 av_log(NULL, AV_LOG_FATAL, "Cannot initialize the sub conversion context\n");
2203 sws_scale(is->sub_convert_ctx,
2204 (void*)sp->sub.rects[i]->pict.data, sp->sub.rects[i]->pict.linesize,
2205 0, in_h, newpic.data, newpic.linesize);
2207 av_free(sp->sub.rects[i]->pict.data[0]);
2208 av_free(sp->sub.rects[i]->pict.data[1]);
2209 sp->sub.rects[i]->pict = newpic;
2210 sp->sub.rects[i]->w = out_w;
2211 sp->sub.rects[i]->h = out_h;
2212 sp->sub.rects[i]->x = sp->sub.rects[i]->x * out_w / in_w;
2213 sp->sub.rects[i]->y = sp->sub.rects[i]->y * out_h / in_h;
2216 /* now we can update the picture count */
2217 frame_queue_push(&is->subpq);
2218 } else if (got_subtitle) {
2219 avsubtitle_free(&sp->sub);
2225 /* copy samples for viewing in editor window */
2226 static void update_sample_display(VideoState *is, short *samples, int samples_size)
2230 size = samples_size / sizeof(short);
2232 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
2235 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
2237 is->sample_array_index += len;
2238 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
2239 is->sample_array_index = 0;
2244 /* return the wanted number of samples to get better sync if sync_type is video
2245 * or external master clock */
2246 static int synchronize_audio(VideoState *is, int nb_samples)
2248 int wanted_nb_samples = nb_samples;
2250 /* if not master, then we try to remove or add samples to correct the clock */
2251 if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
2252 double diff, avg_diff;
2253 int min_nb_samples, max_nb_samples;
2255 diff = get_clock(&is->audclk) - get_master_clock(is);
2257 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
2258 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
2259 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
2260 /* not enough measures to have a correct estimate */
2261 is->audio_diff_avg_count++;
2263 /* estimate the A-V difference */
2264 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
2266 if (fabs(avg_diff) >= is->audio_diff_threshold) {
2267 wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
2268 min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2269 max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2270 wanted_nb_samples = av_clip(wanted_nb_samples, min_nb_samples, max_nb_samples);
2272 av_log(NULL, AV_LOG_TRACE, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n",
2273 diff, avg_diff, wanted_nb_samples - nb_samples,
2274 is->audio_clock, is->audio_diff_threshold);
2277 /* too big difference : may be initial PTS errors, so
2279 is->audio_diff_avg_count = 0;
2280 is->audio_diff_cum = 0;
2284 return wanted_nb_samples;
2288 * Decode one audio frame and return its uncompressed size.
2290 * The processed audio frame is decoded, converted if required, and
2291 * stored in is->audio_buf, with size in bytes given by the return
2294 static int audio_decode_frame(VideoState *is)
2296 int data_size, resampled_data_size;
2297 int64_t dec_channel_layout;
2298 av_unused double audio_clock0;
2299 int wanted_nb_samples;
2306 if (!(af = frame_queue_peek_readable(&is->sampq)))
2308 frame_queue_next(&is->sampq);
2309 } while (af->serial != is->audioq.serial);
2311 data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(af->frame),
2312 af->frame->nb_samples,
2313 af->frame->format, 1);
2315 dec_channel_layout =
2316 (af->frame->channel_layout && av_frame_get_channels(af->frame) == av_get_channel_layout_nb_channels(af->frame->channel_layout)) ?
2317 af->frame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(af->frame));
2318 wanted_nb_samples = synchronize_audio(is, af->frame->nb_samples);
2320 if (af->frame->format != is->audio_src.fmt ||
2321 dec_channel_layout != is->audio_src.channel_layout ||
2322 af->frame->sample_rate != is->audio_src.freq ||
2323 (wanted_nb_samples != af->frame->nb_samples && !is->swr_ctx)) {
2324 swr_free(&is->swr_ctx);
2325 is->swr_ctx = swr_alloc_set_opts(NULL,
2326 is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2327 dec_channel_layout, af->frame->format, af->frame->sample_rate,
2329 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2330 av_log(NULL, AV_LOG_ERROR,
2331 "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2332 af->frame->sample_rate, av_get_sample_fmt_name(af->frame->format), av_frame_get_channels(af->frame),
2333 is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
2334 swr_free(&is->swr_ctx);
2337 is->audio_src.channel_layout = dec_channel_layout;
2338 is->audio_src.channels = av_frame_get_channels(af->frame);
2339 is->audio_src.freq = af->frame->sample_rate;
2340 is->audio_src.fmt = af->frame->format;
2344 const uint8_t **in = (const uint8_t **)af->frame->extended_data;
2345 uint8_t **out = &is->audio_buf1;
2346 int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / af->frame->sample_rate + 256;
2347 int out_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
2350 av_log(NULL, AV_LOG_ERROR, "av_samples_get_buffer_size() failed\n");
2353 if (wanted_nb_samples != af->frame->nb_samples) {
2354 if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - af->frame->nb_samples) * is->audio_tgt.freq / af->frame->sample_rate,
2355 wanted_nb_samples * is->audio_tgt.freq / af->frame->sample_rate) < 0) {
2356 av_log(NULL, AV_LOG_ERROR, "swr_set_compensation() failed\n");
2360 av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
2361 if (!is->audio_buf1)
2362 return AVERROR(ENOMEM);
2363 len2 = swr_convert(is->swr_ctx, out, out_count, in, af->frame->nb_samples);
2365 av_log(NULL, AV_LOG_ERROR, "swr_convert() failed\n");
2368 if (len2 == out_count) {
2369 av_log(NULL, AV_LOG_WARNING, "audio buffer is probably too small\n");
2370 if (swr_init(is->swr_ctx) < 0)
2371 swr_free(&is->swr_ctx);
2373 is->audio_buf = is->audio_buf1;
2374 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2376 is->audio_buf = af->frame->data[0];
2377 resampled_data_size = data_size;
2380 audio_clock0 = is->audio_clock;
2381 /* update the audio clock with the pts */
2382 if (!isnan(af->pts))
2383 is->audio_clock = af->pts + (double) af->frame->nb_samples / af->frame->sample_rate;
2385 is->audio_clock = NAN;
2386 is->audio_clock_serial = af->serial;
2389 static double last_clock;
2390 printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
2391 is->audio_clock - last_clock,
2392 is->audio_clock, audio_clock0);
2393 last_clock = is->audio_clock;
2396 return resampled_data_size;
2399 /* prepare a new audio buffer */
2400 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2402 VideoState *is = opaque;
2403 int audio_size, len1;
2405 audio_callback_time = av_gettime_relative();
2408 if (is->audio_buf_index >= is->audio_buf_size) {
2409 audio_size = audio_decode_frame(is);
2410 if (audio_size < 0) {
2411 /* if error, just output silence */
2412 is->audio_buf = is->silence_buf;
2413 is->audio_buf_size = sizeof(is->silence_buf) / is->audio_tgt.frame_size * is->audio_tgt.frame_size;
2415 if (is->show_mode != SHOW_MODE_VIDEO)
2416 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2417 is->audio_buf_size = audio_size;
2419 is->audio_buf_index = 0;
2421 len1 = is->audio_buf_size - is->audio_buf_index;
2424 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2427 is->audio_buf_index += len1;
2429 is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2430 /* Let's assume the audio driver that is used by SDL has two periods. */
2431 if (!isnan(is->audio_clock)) {
2432 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);
2433 sync_clock_to_slave(&is->extclk, &is->audclk);
2437 static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
2439 SDL_AudioSpec wanted_spec, spec;
2441 static const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
2442 static const int next_sample_rates[] = {0, 44100, 48000, 96000, 192000};
2443 int next_sample_rate_idx = FF_ARRAY_ELEMS(next_sample_rates) - 1;
2445 env = SDL_getenv("SDL_AUDIO_CHANNELS");
2447 wanted_nb_channels = atoi(env);
2448 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2450 if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
2451 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2452 wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2454 wanted_nb_channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2455 wanted_spec.channels = wanted_nb_channels;
2456 wanted_spec.freq = wanted_sample_rate;
2457 if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2458 av_log(NULL, AV_LOG_ERROR, "Invalid sample rate or channel count!\n");
2461 while (next_sample_rate_idx && next_sample_rates[next_sample_rate_idx] >= wanted_spec.freq)
2462 next_sample_rate_idx--;
2463 wanted_spec.format = AUDIO_S16SYS;
2464 wanted_spec.silence = 0;
2465 wanted_spec.samples = FFMAX(SDL_AUDIO_MIN_BUFFER_SIZE, 2 << av_log2(wanted_spec.freq / SDL_AUDIO_MAX_CALLBACKS_PER_SEC));
2466 wanted_spec.callback = sdl_audio_callback;
2467 wanted_spec.userdata = opaque;
2468 while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2469 av_log(NULL, AV_LOG_WARNING, "SDL_OpenAudio (%d channels, %d Hz): %s\n",
2470 wanted_spec.channels, wanted_spec.freq, SDL_GetError());
2471 wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
2472 if (!wanted_spec.channels) {
2473 wanted_spec.freq = next_sample_rates[next_sample_rate_idx--];
2474 wanted_spec.channels = wanted_nb_channels;
2475 if (!wanted_spec.freq) {
2476 av_log(NULL, AV_LOG_ERROR,
2477 "No more combinations to try, audio open failed\n");
2481 wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
2483 if (spec.format != AUDIO_S16SYS) {
2484 av_log(NULL, AV_LOG_ERROR,
2485 "SDL advised audio format %d is not supported!\n", spec.format);
2488 if (spec.channels != wanted_spec.channels) {
2489 wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2490 if (!wanted_channel_layout) {
2491 av_log(NULL, AV_LOG_ERROR,
2492 "SDL advised channel count %d is not supported!\n", spec.channels);
2497 audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
2498 audio_hw_params->freq = spec.freq;
2499 audio_hw_params->channel_layout = wanted_channel_layout;
2500 audio_hw_params->channels = spec.channels;
2501 audio_hw_params->frame_size = av_samples_get_buffer_size(NULL, audio_hw_params->channels, 1, audio_hw_params->fmt, 1);
2502 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);
2503 if (audio_hw_params->bytes_per_sec <= 0 || audio_hw_params->frame_size <= 0) {
2504 av_log(NULL, AV_LOG_ERROR, "av_samples_get_buffer_size failed\n");
2510 /* open a given stream. Return 0 if OK */
2511 static int stream_component_open(VideoState *is, int stream_index)
2513 AVFormatContext *ic = is->ic;
2514 AVCodecContext *avctx;
2516 const char *forced_codec_name = NULL;
2518 AVDictionaryEntry *t = NULL;
2519 int sample_rate, nb_channels;
2520 int64_t channel_layout;
2522 int stream_lowres = lowres;
2524 if (stream_index < 0 || stream_index >= ic->nb_streams)
2526 avctx = ic->streams[stream_index]->codec;
2528 codec = avcodec_find_decoder(avctx->codec_id);
2530 switch(avctx->codec_type){
2531 case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; forced_codec_name = audio_codec_name; break;
2532 case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
2533 case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; forced_codec_name = video_codec_name; break;
2535 if (forced_codec_name)
2536 codec = avcodec_find_decoder_by_name(forced_codec_name);
2538 if (forced_codec_name) av_log(NULL, AV_LOG_WARNING,
2539 "No codec could be found with name '%s'\n", forced_codec_name);
2540 else av_log(NULL, AV_LOG_WARNING,
2541 "No codec could be found with id %d\n", avctx->codec_id);
2545 avctx->codec_id = codec->id;
2546 if(stream_lowres > av_codec_get_max_lowres(codec)){
2547 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2548 av_codec_get_max_lowres(codec));
2549 stream_lowres = av_codec_get_max_lowres(codec);
2551 av_codec_set_lowres(avctx, stream_lowres);
2553 if(stream_lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
2555 avctx->flags2 |= AV_CODEC_FLAG2_FAST;
2556 if(codec->capabilities & AV_CODEC_CAP_DR1)
2557 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2559 opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
2560 if (!av_dict_get(opts, "threads", NULL, 0))
2561 av_dict_set(&opts, "threads", "auto", 0);
2563 av_dict_set_int(&opts, "lowres", stream_lowres, 0);
2564 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
2565 av_dict_set(&opts, "refcounted_frames", "1", 0);
2566 if ((ret = avcodec_open2(avctx, codec, &opts)) < 0) {
2569 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2570 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2571 ret = AVERROR_OPTION_NOT_FOUND;
2576 ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2577 switch (avctx->codec_type) {
2578 case AVMEDIA_TYPE_AUDIO:
2583 is->audio_filter_src.freq = avctx->sample_rate;
2584 is->audio_filter_src.channels = avctx->channels;
2585 is->audio_filter_src.channel_layout = get_valid_channel_layout(avctx->channel_layout, avctx->channels);
2586 is->audio_filter_src.fmt = avctx->sample_fmt;
2587 if ((ret = configure_audio_filters(is, afilters, 0)) < 0)
2589 link = is->out_audio_filter->inputs[0];
2590 sample_rate = link->sample_rate;
2591 nb_channels = link->channels;
2592 channel_layout = link->channel_layout;
2595 sample_rate = avctx->sample_rate;
2596 nb_channels = avctx->channels;
2597 channel_layout = avctx->channel_layout;
2600 /* prepare audio output */
2601 if ((ret = audio_open(is, channel_layout, nb_channels, sample_rate, &is->audio_tgt)) < 0)
2603 is->audio_hw_buf_size = ret;
2604 is->audio_src = is->audio_tgt;
2605 is->audio_buf_size = 0;
2606 is->audio_buf_index = 0;
2608 /* init averaging filter */
2609 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2610 is->audio_diff_avg_count = 0;
2611 /* since we do not have a precise anough audio fifo fullness,
2612 we correct audio sync only if larger than this threshold */
2613 is->audio_diff_threshold = (double)(is->audio_hw_buf_size) / is->audio_tgt.bytes_per_sec;
2615 is->audio_stream = stream_index;
2616 is->audio_st = ic->streams[stream_index];
2618 decoder_init(&is->auddec, avctx, &is->audioq, is->continue_read_thread);
2619 if ((is->ic->iformat->flags & (AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK)) && !is->ic->iformat->read_seek) {
2620 is->auddec.start_pts = is->audio_st->start_time;
2621 is->auddec.start_pts_tb = is->audio_st->time_base;
2623 decoder_start(&is->auddec, audio_thread, is);
2626 case AVMEDIA_TYPE_VIDEO:
2627 is->video_stream = stream_index;
2628 is->video_st = ic->streams[stream_index];
2630 is->viddec_width = avctx->width;
2631 is->viddec_height = avctx->height;
2633 decoder_init(&is->viddec, avctx, &is->videoq, is->continue_read_thread);
2634 decoder_start(&is->viddec, video_thread, is);
2635 is->queue_attachments_req = 1;
2637 case AVMEDIA_TYPE_SUBTITLE:
2638 is->subtitle_stream = stream_index;
2639 is->subtitle_st = ic->streams[stream_index];
2641 decoder_init(&is->subdec, avctx, &is->subtitleq, is->continue_read_thread);
2642 decoder_start(&is->subdec, subtitle_thread, is);
2649 av_dict_free(&opts);
2654 static void stream_component_close(VideoState *is, int stream_index)
2656 AVFormatContext *ic = is->ic;
2657 AVCodecContext *avctx;
2659 if (stream_index < 0 || stream_index >= ic->nb_streams)
2661 avctx = ic->streams[stream_index]->codec;
2663 switch (avctx->codec_type) {
2664 case AVMEDIA_TYPE_AUDIO:
2665 decoder_abort(&is->auddec, &is->sampq);
2667 decoder_destroy(&is->auddec);
2668 swr_free(&is->swr_ctx);
2669 av_freep(&is->audio_buf1);
2670 is->audio_buf1_size = 0;
2671 is->audio_buf = NULL;
2674 av_rdft_end(is->rdft);
2675 av_freep(&is->rdft_data);
2680 case AVMEDIA_TYPE_VIDEO:
2681 decoder_abort(&is->viddec, &is->pictq);
2682 decoder_destroy(&is->viddec);
2684 case AVMEDIA_TYPE_SUBTITLE:
2685 decoder_abort(&is->subdec, &is->subpq);
2686 decoder_destroy(&is->subdec);
2692 ic->streams[stream_index]->discard = AVDISCARD_ALL;
2693 avcodec_close(avctx);
2694 switch (avctx->codec_type) {
2695 case AVMEDIA_TYPE_AUDIO:
2696 is->audio_st = NULL;
2697 is->audio_stream = -1;
2699 case AVMEDIA_TYPE_VIDEO:
2700 is->video_st = NULL;
2701 is->video_stream = -1;
2703 case AVMEDIA_TYPE_SUBTITLE:
2704 is->subtitle_st = NULL;
2705 is->subtitle_stream = -1;
2712 static int decode_interrupt_cb(void *ctx)
2714 VideoState *is = ctx;
2715 return is->abort_request;
2718 static int is_realtime(AVFormatContext *s)
2720 if( !strcmp(s->iformat->name, "rtp")
2721 || !strcmp(s->iformat->name, "rtsp")
2722 || !strcmp(s->iformat->name, "sdp")
2726 if(s->pb && ( !strncmp(s->filename, "rtp:", 4)
2727 || !strncmp(s->filename, "udp:", 4)
2734 /* this thread gets the stream from the disk or the network */
2735 static int read_thread(void *arg)
2737 VideoState *is = arg;
2738 AVFormatContext *ic = NULL;
2740 int st_index[AVMEDIA_TYPE_NB];
2741 AVPacket pkt1, *pkt = &pkt1;
2742 int64_t stream_start_time;
2743 int pkt_in_play_range = 0;
2744 AVDictionaryEntry *t;
2745 AVDictionary **opts;
2746 int orig_nb_streams;
2747 SDL_mutex *wait_mutex = SDL_CreateMutex();
2748 int scan_all_pmts_set = 0;
2751 memset(st_index, -1, sizeof(st_index));
2752 is->last_video_stream = is->video_stream = -1;
2753 is->last_audio_stream = is->audio_stream = -1;
2754 is->last_subtitle_stream = is->subtitle_stream = -1;
2757 ic = avformat_alloc_context();
2759 av_log(NULL, AV_LOG_FATAL, "Could not allocate context.\n");
2760 ret = AVERROR(ENOMEM);
2763 ic->interrupt_callback.callback = decode_interrupt_cb;
2764 ic->interrupt_callback.opaque = is;
2765 if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
2766 av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
2767 scan_all_pmts_set = 1;
2769 err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2771 print_error(is->filename, err);
2775 if (scan_all_pmts_set)
2776 av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
2778 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2779 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2780 ret = AVERROR_OPTION_NOT_FOUND;
2786 ic->flags |= AVFMT_FLAG_GENPTS;
2788 av_format_inject_global_side_data(ic);
2790 opts = setup_find_stream_info_opts(ic, codec_opts);
2791 orig_nb_streams = ic->nb_streams;
2793 err = avformat_find_stream_info(ic, opts);
2795 for (i = 0; i < orig_nb_streams; i++)
2796 av_dict_free(&opts[i]);
2800 av_log(NULL, AV_LOG_WARNING,
2801 "%s: could not find codec parameters\n", is->filename);
2807 ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use avio_feof() to test for the end
2809 if (seek_by_bytes < 0)
2810 seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT) && strcmp("ogg", ic->iformat->name);
2812 is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
2814 if (!window_title && (t = av_dict_get(ic->metadata, "title", NULL, 0)))
2815 window_title = av_asprintf("%s - %s", t->value, input_filename);
2817 /* if seeking requested, we execute it */
2818 if (start_time != AV_NOPTS_VALUE) {
2821 timestamp = start_time;
2822 /* add the stream start time */
2823 if (ic->start_time != AV_NOPTS_VALUE)
2824 timestamp += ic->start_time;
2825 ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2827 av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
2828 is->filename, (double)timestamp / AV_TIME_BASE);
2832 is->realtime = is_realtime(ic);
2835 av_dump_format(ic, 0, is->filename, 0);
2837 for (i = 0; i < ic->nb_streams; i++) {
2838 AVStream *st = ic->streams[i];
2839 enum AVMediaType type = st->codec->codec_type;
2840 st->discard = AVDISCARD_ALL;
2841 if (wanted_stream_spec[type] && st_index[type] == -1)
2842 if (avformat_match_stream_specifier(ic, st, wanted_stream_spec[type]) > 0)
2845 for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
2846 if (wanted_stream_spec[i] && st_index[i] == -1) {
2847 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));
2848 st_index[i] = INT_MAX;
2853 st_index[AVMEDIA_TYPE_VIDEO] =
2854 av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2855 st_index[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2857 st_index[AVMEDIA_TYPE_AUDIO] =
2858 av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
2859 st_index[AVMEDIA_TYPE_AUDIO],
2860 st_index[AVMEDIA_TYPE_VIDEO],
2862 if (!video_disable && !subtitle_disable)
2863 st_index[AVMEDIA_TYPE_SUBTITLE] =
2864 av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
2865 st_index[AVMEDIA_TYPE_SUBTITLE],
2866 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2867 st_index[AVMEDIA_TYPE_AUDIO] :
2868 st_index[AVMEDIA_TYPE_VIDEO]),
2871 is->show_mode = show_mode;
2872 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2873 AVStream *st = ic->streams[st_index[AVMEDIA_TYPE_VIDEO]];
2874 AVCodecContext *avctx = st->codec;
2875 AVRational sar = av_guess_sample_aspect_ratio(ic, st, NULL);
2877 set_default_window_size(avctx->width, avctx->height, sar);
2880 /* open the streams */
2881 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2882 stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
2886 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2887 ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
2889 if (is->show_mode == SHOW_MODE_NONE)
2890 is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
2892 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2893 stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
2896 if (is->video_stream < 0 && is->audio_stream < 0) {
2897 av_log(NULL, AV_LOG_FATAL, "Failed to open file '%s' or configure filtergraph\n",
2903 if (infinite_buffer < 0 && is->realtime)
2904 infinite_buffer = 1;
2907 if (is->abort_request)
2909 if (is->paused != is->last_paused) {
2910 is->last_paused = is->paused;
2912 is->read_pause_return = av_read_pause(ic);
2916 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
2918 (!strcmp(ic->iformat->name, "rtsp") ||
2919 (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
2920 /* wait 10 ms to avoid trying to get another packet */
2927 int64_t seek_target = is->seek_pos;
2928 int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
2929 int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
2930 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
2931 // of the seek_pos/seek_rel variables
2933 ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
2935 av_log(NULL, AV_LOG_ERROR,
2936 "%s: error while seeking\n", is->ic->filename);
2938 if (is->audio_stream >= 0) {
2939 packet_queue_flush(&is->audioq);
2940 packet_queue_put(&is->audioq, &flush_pkt);
2942 if (is->subtitle_stream >= 0) {
2943 packet_queue_flush(&is->subtitleq);
2944 packet_queue_put(&is->subtitleq, &flush_pkt);
2946 if (is->video_stream >= 0) {
2947 packet_queue_flush(&is->videoq);
2948 packet_queue_put(&is->videoq, &flush_pkt);
2950 if (is->seek_flags & AVSEEK_FLAG_BYTE) {
2951 set_clock(&is->extclk, NAN, 0);
2953 set_clock(&is->extclk, seek_target / (double)AV_TIME_BASE, 0);
2957 is->queue_attachments_req = 1;
2960 step_to_next_frame(is);
2962 if (is->queue_attachments_req) {
2963 if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
2965 if ((ret = av_copy_packet(©, &is->video_st->attached_pic)) < 0)
2967 packet_queue_put(&is->videoq, ©);
2968 packet_queue_put_nullpacket(&is->videoq, is->video_stream);
2970 is->queue_attachments_req = 0;
2973 /* if the queue are full, no need to read more */
2974 if (infinite_buffer<1 &&
2975 (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
2976 || ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
2977 && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request
2978 || (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2979 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
2981 SDL_LockMutex(wait_mutex);
2982 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2983 SDL_UnlockMutex(wait_mutex);
2987 (!is->audio_st || (is->auddec.finished == is->audioq.serial && frame_queue_nb_remaining(&is->sampq) == 0)) &&
2988 (!is->video_st || (is->viddec.finished == is->videoq.serial && frame_queue_nb_remaining(&is->pictq) == 0))) {
2989 if (loop != 1 && (!loop || --loop)) {
2990 stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
2991 } else if (autoexit) {
2996 ret = av_read_frame(ic, pkt);
2998 if ((ret == AVERROR_EOF || avio_feof(ic->pb)) && !is->eof) {
2999 if (is->video_stream >= 0)
3000 packet_queue_put_nullpacket(&is->videoq, is->video_stream);
3001 if (is->audio_stream >= 0)
3002 packet_queue_put_nullpacket(&is->audioq, is->audio_stream);
3003 if (is->subtitle_stream >= 0)
3004 packet_queue_put_nullpacket(&is->subtitleq, is->subtitle_stream);
3007 if (ic->pb && ic->pb->error)
3009 SDL_LockMutex(wait_mutex);
3010 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
3011 SDL_UnlockMutex(wait_mutex);
3016 /* check if packet is in play range specified by user, then queue, otherwise discard */
3017 stream_start_time = ic->streams[pkt->stream_index]->start_time;
3018 pkt_ts = pkt->pts == AV_NOPTS_VALUE ? pkt->dts : pkt->pts;
3019 pkt_in_play_range = duration == AV_NOPTS_VALUE ||
3020 (pkt_ts - (stream_start_time != AV_NOPTS_VALUE ? stream_start_time : 0)) *
3021 av_q2d(ic->streams[pkt->stream_index]->time_base) -
3022 (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
3023 <= ((double)duration / 1000000);
3024 if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
3025 packet_queue_put(&is->audioq, pkt);
3026 } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
3027 && !(is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
3028 packet_queue_put(&is->videoq, pkt);
3029 } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
3030 packet_queue_put(&is->subtitleq, pkt);
3032 av_free_packet(pkt);
3035 /* wait until the end */
3036 while (!is->abort_request) {
3042 /* close each stream */
3043 if (is->audio_stream >= 0)
3044 stream_component_close(is, is->audio_stream);
3045 if (is->video_stream >= 0)
3046 stream_component_close(is, is->video_stream);
3047 if (is->subtitle_stream >= 0)
3048 stream_component_close(is, is->subtitle_stream);
3050 avformat_close_input(&ic);
3057 event.type = FF_QUIT_EVENT;
3058 event.user.data1 = is;
3059 SDL_PushEvent(&event);
3061 SDL_DestroyMutex(wait_mutex);
3065 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
3069 is = av_mallocz(sizeof(VideoState));
3072 av_strlcpy(is->filename, filename, sizeof(is->filename));
3073 is->iformat = iformat;
3077 /* start video display */
3078 if (frame_queue_init(&is->pictq, &is->videoq, VIDEO_PICTURE_QUEUE_SIZE, 1) < 0)
3080 if (frame_queue_init(&is->subpq, &is->subtitleq, SUBPICTURE_QUEUE_SIZE, 0) < 0)
3082 if (frame_queue_init(&is->sampq, &is->audioq, SAMPLE_QUEUE_SIZE, 1) < 0)
3085 packet_queue_init(&is->videoq);
3086 packet_queue_init(&is->audioq);
3087 packet_queue_init(&is->subtitleq);
3089 is->continue_read_thread = SDL_CreateCond();
3091 init_clock(&is->vidclk, &is->videoq.serial);
3092 init_clock(&is->audclk, &is->audioq.serial);
3093 init_clock(&is->extclk, &is->extclk.serial);
3094 is->audio_clock_serial = -1;
3095 is->av_sync_type = av_sync_type;
3096 is->read_tid = SDL_CreateThread(read_thread, is);
3097 if (!is->read_tid) {
3105 static void stream_cycle_channel(VideoState *is, int codec_type)
3107 AVFormatContext *ic = is->ic;
3108 int start_index, stream_index;
3111 AVProgram *p = NULL;
3112 int nb_streams = is->ic->nb_streams;
3114 if (codec_type == AVMEDIA_TYPE_VIDEO) {
3115 start_index = is->last_video_stream;
3116 old_index = is->video_stream;
3117 } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
3118 start_index = is->last_audio_stream;
3119 old_index = is->audio_stream;
3121 start_index = is->last_subtitle_stream;
3122 old_index = is->subtitle_stream;
3124 stream_index = start_index;
3126 if (codec_type != AVMEDIA_TYPE_VIDEO && is->video_stream != -1) {
3127 p = av_find_program_from_stream(ic, NULL, is->video_stream);
3129 nb_streams = p->nb_stream_indexes;
3130 for (start_index = 0; start_index < nb_streams; start_index++)
3131 if (p->stream_index[start_index] == stream_index)
3133 if (start_index == nb_streams)
3135 stream_index = start_index;
3140 if (++stream_index >= nb_streams)
3142 if (codec_type == AVMEDIA_TYPE_SUBTITLE)
3145 is->last_subtitle_stream = -1;
3148 if (start_index == -1)
3152 if (stream_index == start_index)
3154 st = is->ic->streams[p ? p->stream_index[stream_index] : stream_index];
3155 if (st->codec->codec_type == codec_type) {
3156 /* check that parameters are OK */
3157 switch (codec_type) {
3158 case AVMEDIA_TYPE_AUDIO:
3159 if (st->codec->sample_rate != 0 &&
3160 st->codec->channels != 0)
3163 case AVMEDIA_TYPE_VIDEO:
3164 case AVMEDIA_TYPE_SUBTITLE:
3172 if (p && stream_index != -1)
3173 stream_index = p->stream_index[stream_index];
3174 av_log(NULL, AV_LOG_INFO, "Switch %s stream from #%d to #%d\n",
3175 av_get_media_type_string(codec_type),
3179 stream_component_close(is, old_index);
3180 stream_component_open(is, stream_index);
3184 static void toggle_full_screen(VideoState *is)
3186 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
3187 /* OS X needs to reallocate the SDL overlays */
3189 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
3190 is->pictq.queue[i].reallocate = 1;
3192 is_full_screen = !is_full_screen;
3193 video_open(is, 1, NULL);
3196 static void toggle_audio_display(VideoState *is)
3198 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
3199 int next = is->show_mode;
3201 next = (next + 1) % SHOW_MODE_NB;
3202 } while (next != is->show_mode && (next == SHOW_MODE_VIDEO && !is->video_st || next != SHOW_MODE_VIDEO && !is->audio_st));
3203 if (is->show_mode != next) {
3204 fill_rectangle(screen,
3205 is->xleft, is->ytop, is->width, is->height,
3207 is->force_refresh = 1;
3208 is->show_mode = next;
3212 static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) {
3213 double remaining_time = 0.0;
3215 while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
3216 if (!cursor_hidden && av_gettime_relative() - cursor_last_shown > CURSOR_HIDE_DELAY) {
3220 if (remaining_time > 0.0)
3221 av_usleep((int64_t)(remaining_time * 1000000.0));
3222 remaining_time = REFRESH_RATE;
3223 if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh))
3224 video_refresh(is, &remaining_time);
3229 static void seek_chapter(VideoState *is, int incr)
3231 int64_t pos = get_master_clock(is) * AV_TIME_BASE;
3234 if (!is->ic->nb_chapters)
3237 /* find the current chapter */
3238 for (i = 0; i < is->ic->nb_chapters; i++) {
3239 AVChapter *ch = is->ic->chapters[i];
3240 if (av_compare_ts(pos, AV_TIME_BASE_Q, ch->start, ch->time_base) < 0) {
3248 if (i >= is->ic->nb_chapters)
3251 av_log(NULL, AV_LOG_VERBOSE, "Seeking to chapter %d.\n", i);
3252 stream_seek(is, av_rescale_q(is->ic->chapters[i]->start, is->ic->chapters[i]->time_base,
3253 AV_TIME_BASE_Q), 0, 0);
3256 /* handle an event sent by the GUI */
3257 static void event_loop(VideoState *cur_stream)
3260 double incr, pos, frac;
3264 refresh_loop_wait_event(cur_stream, &event);
3265 switch (event.type) {
3267 if (exit_on_keydown) {
3268 do_exit(cur_stream);
3271 switch (event.key.keysym.sym) {
3274 do_exit(cur_stream);
3277 toggle_full_screen(cur_stream);
3278 cur_stream->force_refresh = 1;
3282 toggle_pause(cur_stream);
3284 case SDLK_s: // S: Step to next frame
3285 step_to_next_frame(cur_stream);
3288 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
3291 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
3294 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
3295 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
3296 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
3299 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
3303 if (cur_stream->show_mode == SHOW_MODE_VIDEO && cur_stream->vfilter_idx < nb_vfilters - 1) {
3304 if (++cur_stream->vfilter_idx >= nb_vfilters)
3305 cur_stream->vfilter_idx = 0;
3307 cur_stream->vfilter_idx = 0;
3308 toggle_audio_display(cur_stream);
3311 toggle_audio_display(cur_stream);
3315 if (cur_stream->ic->nb_chapters <= 1) {
3319 seek_chapter(cur_stream, 1);
3322 if (cur_stream->ic->nb_chapters <= 1) {
3326 seek_chapter(cur_stream, -1);
3340 if (seek_by_bytes) {
3342 if (pos < 0 && cur_stream->video_stream >= 0)
3343 pos = frame_queue_last_pos(&cur_stream->pictq);
3344 if (pos < 0 && cur_stream->audio_stream >= 0)
3345 pos = frame_queue_last_pos(&cur_stream->sampq);
3347 pos = avio_tell(cur_stream->ic->pb);
3348 if (cur_stream->ic->bit_rate)
3349 incr *= cur_stream->ic->bit_rate / 8.0;
3353 stream_seek(cur_stream, pos, incr, 1);
3355 pos = get_master_clock(cur_stream);
3357 pos = (double)cur_stream->seek_pos / AV_TIME_BASE;
3359 if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
3360 pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
3361 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
3368 case SDL_VIDEOEXPOSE:
3369 cur_stream->force_refresh = 1;
3371 case SDL_MOUSEBUTTONDOWN:
3372 if (exit_on_mousedown) {
3373 do_exit(cur_stream);
3376 case SDL_MOUSEMOTION:
3377 if (cursor_hidden) {
3381 cursor_last_shown = av_gettime_relative();
3382 if (event.type == SDL_MOUSEBUTTONDOWN) {
3385 if (event.motion.state != SDL_PRESSED)
3389 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
3390 uint64_t size = avio_size(cur_stream->ic->pb);
3391 stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
3395 int tns, thh, tmm, tss;
3396 tns = cur_stream->ic->duration / 1000000LL;
3398 tmm = (tns % 3600) / 60;
3400 frac = x / cur_stream->width;
3403 mm = (ns % 3600) / 60;
3405 av_log(NULL, AV_LOG_INFO,
3406 "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
3407 hh, mm, ss, thh, tmm, tss);
3408 ts = frac * cur_stream->ic->duration;
3409 if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
3410 ts += cur_stream->ic->start_time;
3411 stream_seek(cur_stream, ts, 0, 0);
3414 case SDL_VIDEORESIZE:
3415 screen = SDL_SetVideoMode(FFMIN(16383, event.resize.w), event.resize.h, 0,
3416 SDL_HWSURFACE|(is_full_screen?SDL_FULLSCREEN:SDL_RESIZABLE)|SDL_ASYNCBLIT|SDL_HWACCEL);
3418 av_log(NULL, AV_LOG_FATAL, "Failed to set video mode\n");
3419 do_exit(cur_stream);
3421 screen_width = cur_stream->width = screen->w;
3422 screen_height = cur_stream->height = screen->h;
3423 cur_stream->force_refresh = 1;
3427 do_exit(cur_stream);
3429 case FF_ALLOC_EVENT:
3430 alloc_picture(event.user.data1);
3438 static int opt_frame_size(void *optctx, const char *opt, const char *arg)
3440 av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
3441 return opt_default(NULL, "video_size", arg);
3444 static int opt_width(void *optctx, const char *opt, const char *arg)
3446 screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3450 static int opt_height(void *optctx, const char *opt, const char *arg)
3452 screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3456 static int opt_format(void *optctx, const char *opt, const char *arg)
3458 file_iformat = av_find_input_format(arg);
3459 if (!file_iformat) {
3460 av_log(NULL, AV_LOG_FATAL, "Unknown input format: %s\n", arg);
3461 return AVERROR(EINVAL);
3466 static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
3468 av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3469 return opt_default(NULL, "pixel_format", arg);
3472 static int opt_sync(void *optctx, const char *opt, const char *arg)
3474 if (!strcmp(arg, "audio"))
3475 av_sync_type = AV_SYNC_AUDIO_MASTER;
3476 else if (!strcmp(arg, "video"))
3477 av_sync_type = AV_SYNC_VIDEO_MASTER;
3478 else if (!strcmp(arg, "ext"))
3479 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3481 av_log(NULL, AV_LOG_ERROR, "Unknown value for %s: %s\n", opt, arg);
3487 static int opt_seek(void *optctx, const char *opt, const char *arg)
3489 start_time = parse_time_or_die(opt, arg, 1);
3493 static int opt_duration(void *optctx, const char *opt, const char *arg)
3495 duration = parse_time_or_die(opt, arg, 1);
3499 static int opt_show_mode(void *optctx, const char *opt, const char *arg)
3501 show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3502 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3503 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
3504 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3508 static void opt_input_file(void *optctx, const char *filename)
3510 if (input_filename) {
3511 av_log(NULL, AV_LOG_FATAL,
3512 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3513 filename, input_filename);
3516 if (!strcmp(filename, "-"))
3518 input_filename = filename;
3521 static int opt_codec(void *optctx, const char *opt, const char *arg)
3523 const char *spec = strchr(opt, ':');
3525 av_log(NULL, AV_LOG_ERROR,
3526 "No media specifier was specified in '%s' in option '%s'\n",
3528 return AVERROR(EINVAL);
3532 case 'a' : audio_codec_name = arg; break;
3533 case 's' : subtitle_codec_name = arg; break;
3534 case 'v' : video_codec_name = arg; break;
3536 av_log(NULL, AV_LOG_ERROR,
3537 "Invalid media specifier '%s' in option '%s'\n", spec, opt);
3538 return AVERROR(EINVAL);
3545 static const OptionDef options[] = {
3546 #include "cmdutils_common_opts.h"
3547 { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
3548 { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
3549 { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3550 { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
3551 { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
3552 { "vn", OPT_BOOL, { &video_disable }, "disable video" },
3553 { "sn", OPT_BOOL, { &subtitle_disable }, "disable subtitling" },
3554 { "ast", OPT_STRING | HAS_ARG | OPT_EXPERT, { &wanted_stream_spec[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_specifier" },
3555 { "vst", OPT_STRING | HAS_ARG | OPT_EXPERT, { &wanted_stream_spec[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_specifier" },
3556 { "sst", OPT_STRING | HAS_ARG | OPT_EXPERT, { &wanted_stream_spec[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_specifier" },
3557 { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
3558 { "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
3559 { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3560 { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
3561 { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
3562 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
3563 { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
3564 { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
3565 { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
3566 { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3567 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
3568 { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3569 { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
3570 { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
3571 { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
3572 { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
3573 { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
3574 { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
3575 { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
3577 { "vf", OPT_EXPERT | HAS_ARG, { .func_arg = opt_add_vfilter }, "set video filters", "filter_graph" },
3578 { "af", OPT_STRING | HAS_ARG, { &afilters }, "set audio filters", "filter_graph" },
3580 { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
3581 { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3582 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
3583 { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
3584 { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
3585 { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &audio_codec_name }, "force audio decoder", "decoder_name" },
3586 { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
3587 { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &video_codec_name }, "force video decoder", "decoder_name" },
3588 { "autorotate", OPT_BOOL, { &autorotate }, "automatically rotate video", "" },
3592 static void show_usage(void)
3594 av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3595 av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3596 av_log(NULL, AV_LOG_INFO, "\n");
3599 void show_help_default(const char *opt, const char *arg)
3601 av_log_set_callback(log_callback_help);
3603 show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
3604 show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3606 show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3607 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3608 #if !CONFIG_AVFILTER
3609 show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3611 show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
3613 printf("\nWhile playing:\n"
3615 "f toggle full screen\n"
3617 "a cycle audio channel in the current program\n"
3618 "v cycle video channel\n"
3619 "t cycle subtitle channel in the current program\n"
3621 "w cycle video filters or show modes\n"
3622 "s activate frame-step mode\n"
3623 "left/right seek backward/forward 10 seconds\n"
3624 "down/up seek backward/forward 1 minute\n"
3625 "page down/page up seek backward/forward 10 minutes\n"
3626 "mouse click seek to percentage in file corresponding to fraction of width\n"
3630 static int lockmgr(void **mtx, enum AVLockOp op)
3633 case AV_LOCK_CREATE:
3634 *mtx = SDL_CreateMutex();
3638 case AV_LOCK_OBTAIN:
3639 return !!SDL_LockMutex(*mtx);
3640 case AV_LOCK_RELEASE:
3641 return !!SDL_UnlockMutex(*mtx);
3642 case AV_LOCK_DESTROY:
3643 SDL_DestroyMutex(*mtx);
3649 /* Called from the main */
3650 int main(int argc, char **argv)
3654 char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
3656 av_log_set_flags(AV_LOG_SKIP_REPEATED);
3657 parse_loglevel(argc, argv, options);
3659 /* register all codecs, demux and protocols */
3661 avdevice_register_all();
3664 avfilter_register_all();
3667 avformat_network_init();
3671 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
3672 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
3674 show_banner(argc, argv, options);
3676 parse_options(NULL, argc, argv, options, opt_input_file);
3678 if (!input_filename) {
3680 av_log(NULL, AV_LOG_FATAL, "An input file must be specified\n");
3681 av_log(NULL, AV_LOG_FATAL,
3682 "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3686 if (display_disable) {
3689 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3691 flags &= ~SDL_INIT_AUDIO;
3692 if (display_disable)
3693 SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
3694 #if !defined(_WIN32) && !defined(__APPLE__)
3695 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3697 if (SDL_Init (flags)) {
3698 av_log(NULL, AV_LOG_FATAL, "Could not initialize SDL - %s\n", SDL_GetError());
3699 av_log(NULL, AV_LOG_FATAL, "(Did you set the DISPLAY variable?)\n");
3703 if (!display_disable) {
3704 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3705 fs_screen_width = vi->current_w;
3706 fs_screen_height = vi->current_h;
3709 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3710 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3711 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3713 if (av_lockmgr_register(lockmgr)) {
3714 av_log(NULL, AV_LOG_FATAL, "Could not initialize lock manager!\n");
3718 av_init_packet(&flush_pkt);
3719 flush_pkt.data = (uint8_t *)&flush_pkt;
3721 is = stream_open(input_filename, file_iformat);
3723 av_log(NULL, AV_LOG_FATAL, "Failed to initialize VideoState!\n");