2 * Copyright (c) 2003 Fabrice Bellard
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * simple media player based on the FFmpeg libraries
31 #include "libavutil/avstring.h"
32 #include "libavutil/colorspace.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/parseutils.h"
38 #include "libavutil/samplefmt.h"
39 #include "libavutil/avassert.h"
40 #include "libavutil/time.h"
41 #include "libavformat/avformat.h"
42 #include "libavdevice/avdevice.h"
43 #include "libswscale/swscale.h"
44 #include "libavutil/opt.h"
45 #include "libavcodec/avfft.h"
46 #include "libswresample/swresample.h"
49 # include "libavfilter/avcodec.h"
50 # include "libavfilter/avfilter.h"
51 # include "libavfilter/avfiltergraph.h"
52 # include "libavfilter/buffersink.h"
53 # include "libavfilter/buffersrc.h"
57 #include <SDL_thread.h>
63 const char program_name[] = "ffplay";
64 const int program_birth_year = 2003;
66 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
69 /* SDL audio buffer size, in samples. Should be small to have precise
70 A/V sync as SDL does not have hardware buffer fullness info. */
71 #define SDL_AUDIO_BUFFER_SIZE 1024
73 /* no AV sync correction is done if below the AV sync threshold */
74 #define AV_SYNC_THRESHOLD 0.01
75 /* no AV correction is done if too big error */
76 #define AV_NOSYNC_THRESHOLD 10.0
78 /* maximum audio speed change to get correct sync */
79 #define SAMPLE_CORRECTION_PERCENT_MAX 10
81 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
82 #define AUDIO_DIFF_AVG_NB 20
84 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
85 /* TODO: We assume that a decoded and resampled frame fits into this buffer */
86 #define SAMPLE_ARRAY_SIZE (8 * 65536)
88 static int sws_flags = SWS_BICUBIC;
90 typedef struct MyAVPacketList {
92 struct MyAVPacketList *next;
96 typedef struct PacketQueue {
97 MyAVPacketList *first_pkt, *last_pkt;
106 #define VIDEO_PICTURE_QUEUE_SIZE 4
107 #define SUBPICTURE_QUEUE_SIZE 4
109 typedef struct VideoPicture {
110 double pts; // presentation timestamp for this picture
111 int64_t pos; // byte position in file
114 int width, height; /* source height & width */
115 AVRational sample_aspect_ratio;
121 AVFilterBufferRef *picref;
125 typedef struct SubPicture {
126 double pts; /* presentation time stamp for this picture */
130 typedef struct AudioParams {
134 enum AVSampleFormat fmt;
138 AV_SYNC_AUDIO_MASTER, /* default choice */
139 AV_SYNC_VIDEO_MASTER,
140 AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
143 typedef struct VideoState {
144 SDL_Thread *read_tid;
145 SDL_Thread *video_tid;
146 SDL_Thread *refresh_tid;
147 AVInputFormat *iformat;
153 int que_attachments_req;
158 int read_pause_return;
164 double external_clock; ///< external clock base
165 double external_clock_drift; ///< external clock base - time (av_gettime) at which we updated external_clock
166 int64_t external_clock_time; ///< last reference time
169 double audio_diff_cum; /* used for AV difference average computation */
170 double audio_diff_avg_coef;
171 double audio_diff_threshold;
172 int audio_diff_avg_count;
175 int audio_hw_buf_size;
176 DECLARE_ALIGNED(16,uint8_t,audio_buf2)[AVCODEC_MAX_AUDIO_FRAME_SIZE * 4];
177 uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
180 unsigned int audio_buf_size; /* in bytes */
181 int audio_buf_index; /* in bytes */
182 int audio_write_buf_size;
183 AVPacket audio_pkt_temp;
185 int audio_pkt_temp_serial;
186 struct AudioParams audio_src;
187 struct AudioParams audio_tgt;
188 struct SwrContext *swr_ctx;
189 double audio_current_pts;
190 double audio_current_pts_drift;
191 int frame_drops_early;
192 int frame_drops_late;
196 SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
198 int16_t sample_array[SAMPLE_ARRAY_SIZE];
199 int sample_array_index;
203 FFTSample *rdft_data;
206 SDL_Thread *subtitle_tid;
208 int subtitle_stream_changed;
209 AVStream *subtitle_st;
210 PacketQueue subtitleq;
211 SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
212 int subpq_size, subpq_rindex, subpq_windex;
213 SDL_mutex *subpq_mutex;
214 SDL_cond *subpq_cond;
217 double frame_last_pts;
218 double frame_last_duration;
219 double frame_last_dropped_pts;
220 double frame_last_returned_time;
221 double frame_last_filter_delay;
222 int64_t frame_last_dropped_pos;
223 double video_clock; // pts of last decoded frame / predicted pts of next decoded frame
227 double video_current_pts; // current displayed pts (different from video_clock if frame fifos are used)
228 double video_current_pts_drift; // video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
229 int64_t video_current_pos; // current displayed file pos
230 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
231 int pictq_size, pictq_rindex, pictq_windex;
232 SDL_mutex *pictq_mutex;
233 SDL_cond *pictq_cond;
235 struct SwsContext *img_convert_ctx;
239 int width, height, xleft, ytop;
243 AVFilterContext *in_video_filter; // the first filter in the video chain
244 AVFilterContext *out_video_filter; // the last filter in the video chain
246 FrameBuffer *buffer_pool;
250 int last_video_stream, last_audio_stream, last_subtitle_stream;
252 SDL_cond *continue_read_thread;
255 /* options specified by the user */
256 static AVInputFormat *file_iformat;
257 static const char *input_filename;
258 static const char *window_title;
259 static int fs_screen_width;
260 static int fs_screen_height;
261 static int screen_width = 0;
262 static int screen_height = 0;
263 static int audio_disable;
264 static int video_disable;
265 static int wanted_stream[AVMEDIA_TYPE_NB] = {
266 [AVMEDIA_TYPE_AUDIO] = -1,
267 [AVMEDIA_TYPE_VIDEO] = -1,
268 [AVMEDIA_TYPE_SUBTITLE] = -1,
270 static int seek_by_bytes = -1;
271 static int display_disable;
272 static int show_status = 1;
273 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
274 static int64_t start_time = AV_NOPTS_VALUE;
275 static int64_t duration = AV_NOPTS_VALUE;
276 static int workaround_bugs = 1;
278 static int genpts = 0;
279 static int lowres = 0;
280 static int idct = FF_IDCT_AUTO;
281 static enum AVDiscard skip_frame = AVDISCARD_DEFAULT;
282 static enum AVDiscard skip_idct = AVDISCARD_DEFAULT;
283 static enum AVDiscard skip_loop_filter = AVDISCARD_DEFAULT;
284 static int error_concealment = 3;
285 static int decoder_reorder_pts = -1;
287 static int exit_on_keydown;
288 static int exit_on_mousedown;
290 static int framedrop = -1;
291 static int infinite_buffer = -1;
292 static enum ShowMode show_mode = SHOW_MODE_NONE;
293 static const char *audio_codec_name;
294 static const char *subtitle_codec_name;
295 static const char *video_codec_name;
296 static int rdftspeed = 20;
298 static char *vfilters = NULL;
301 /* current context */
302 static int is_full_screen;
303 static int64_t audio_callback_time;
305 static AVPacket flush_pkt;
307 #define FF_ALLOC_EVENT (SDL_USEREVENT)
308 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
309 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
311 static SDL_Surface *screen;
313 static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
315 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
317 MyAVPacketList *pkt1;
319 if (q->abort_request)
322 pkt1 = av_malloc(sizeof(MyAVPacketList));
327 if (pkt == &flush_pkt)
329 pkt1->serial = q->serial;
334 q->last_pkt->next = pkt1;
337 q->size += pkt1->pkt.size + sizeof(*pkt1);
338 /* XXX: should duplicate packet data in DV case */
339 SDL_CondSignal(q->cond);
343 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
347 /* duplicate the packet */
348 if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
351 SDL_LockMutex(q->mutex);
352 ret = packet_queue_put_private(q, pkt);
353 SDL_UnlockMutex(q->mutex);
355 if (pkt != &flush_pkt && ret < 0)
361 /* packet queue handling */
362 static void packet_queue_init(PacketQueue *q)
364 memset(q, 0, sizeof(PacketQueue));
365 q->mutex = SDL_CreateMutex();
366 q->cond = SDL_CreateCond();
367 q->abort_request = 1;
370 static void packet_queue_flush(PacketQueue *q)
372 MyAVPacketList *pkt, *pkt1;
374 SDL_LockMutex(q->mutex);
375 for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
377 av_free_packet(&pkt->pkt);
384 SDL_UnlockMutex(q->mutex);
387 static void packet_queue_destroy(PacketQueue *q)
389 packet_queue_flush(q);
390 SDL_DestroyMutex(q->mutex);
391 SDL_DestroyCond(q->cond);
394 static void packet_queue_abort(PacketQueue *q)
396 SDL_LockMutex(q->mutex);
398 q->abort_request = 1;
400 SDL_CondSignal(q->cond);
402 SDL_UnlockMutex(q->mutex);
405 static void packet_queue_start(PacketQueue *q)
407 SDL_LockMutex(q->mutex);
408 q->abort_request = 0;
409 packet_queue_put_private(q, &flush_pkt);
410 SDL_UnlockMutex(q->mutex);
413 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
414 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
416 MyAVPacketList *pkt1;
419 SDL_LockMutex(q->mutex);
422 if (q->abort_request) {
429 q->first_pkt = pkt1->next;
433 q->size -= pkt1->pkt.size + sizeof(*pkt1);
436 *serial = pkt1->serial;
444 SDL_CondWait(q->cond, q->mutex);
447 SDL_UnlockMutex(q->mutex);
451 static inline void fill_rectangle(SDL_Surface *screen,
452 int x, int y, int w, int h, int color)
459 SDL_FillRect(screen, &rect, color);
462 #define ALPHA_BLEND(a, oldp, newp, s)\
463 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
465 #define RGBA_IN(r, g, b, a, s)\
467 unsigned int v = ((const uint32_t *)(s))[0];\
468 a = (v >> 24) & 0xff;\
469 r = (v >> 16) & 0xff;\
470 g = (v >> 8) & 0xff;\
474 #define YUVA_IN(y, u, v, a, s, pal)\
476 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
477 a = (val >> 24) & 0xff;\
478 y = (val >> 16) & 0xff;\
479 u = (val >> 8) & 0xff;\
483 #define YUVA_OUT(d, y, u, v, a)\
485 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
491 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
493 int wrap, wrap3, width2, skip2;
494 int y, u, v, a, u1, v1, a1, w, h;
495 uint8_t *lum, *cb, *cr;
498 int dstx, dsty, dstw, dsth;
500 dstw = av_clip(rect->w, 0, imgw);
501 dsth = av_clip(rect->h, 0, imgh);
502 dstx = av_clip(rect->x, 0, imgw - dstw);
503 dsty = av_clip(rect->y, 0, imgh - dsth);
504 lum = dst->data[0] + dsty * dst->linesize[0];
505 cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
506 cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
508 width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
510 wrap = dst->linesize[0];
511 wrap3 = rect->pict.linesize[0];
512 p = rect->pict.data[0];
513 pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
521 YUVA_IN(y, u, v, a, p, pal);
522 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
523 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
524 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
530 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
531 YUVA_IN(y, u, v, a, p, pal);
535 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
537 YUVA_IN(y, u, v, a, p + BPP, pal);
541 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
542 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
543 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
550 YUVA_IN(y, u, v, a, p, pal);
551 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
552 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
553 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
557 p += wrap3 - dstw * BPP;
558 lum += wrap - dstw - dstx;
559 cb += dst->linesize[1] - width2 - skip2;
560 cr += dst->linesize[2] - width2 - skip2;
562 for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
568 YUVA_IN(y, u, v, a, p, pal);
572 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
575 YUVA_IN(y, u, v, a, p, pal);
579 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
580 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
581 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
587 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
588 YUVA_IN(y, u, v, a, p, pal);
592 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
594 YUVA_IN(y, u, v, a, p + BPP, pal);
598 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
602 YUVA_IN(y, u, v, a, p, pal);
606 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
608 YUVA_IN(y, u, v, a, p + BPP, pal);
612 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
614 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
615 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
619 p += -wrap3 + 2 * BPP;
623 YUVA_IN(y, u, v, a, p, pal);
627 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
630 YUVA_IN(y, u, v, a, p, pal);
634 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
635 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
636 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
642 p += wrap3 + (wrap3 - dstw * BPP);
643 lum += wrap + (wrap - dstw - dstx);
644 cb += dst->linesize[1] - width2 - skip2;
645 cr += dst->linesize[2] - width2 - skip2;
647 /* handle odd height */
654 YUVA_IN(y, u, v, a, p, pal);
655 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
656 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
657 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
663 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
664 YUVA_IN(y, u, v, a, p, pal);
668 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
670 YUVA_IN(y, u, v, a, p + BPP, pal);
674 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
675 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
676 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
683 YUVA_IN(y, u, v, a, p, pal);
684 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
685 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
686 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
691 static void free_subpicture(SubPicture *sp)
693 avsubtitle_free(&sp->sub);
696 static void calculate_display_rect(SDL_Rect *rect, int scr_xleft, int scr_ytop, int scr_width, int scr_height, VideoPicture *vp)
699 int width, height, x, y;
701 if (vp->sample_aspect_ratio.num == 0)
704 aspect_ratio = av_q2d(vp->sample_aspect_ratio);
706 if (aspect_ratio <= 0.0)
708 aspect_ratio *= (float)vp->width / (float)vp->height;
710 /* XXX: we suppose the screen has a 1.0 pixel ratio */
712 width = ((int)rint(height * aspect_ratio)) & ~1;
713 if (width > scr_width) {
715 height = ((int)rint(width / aspect_ratio)) & ~1;
717 x = (scr_width - width) / 2;
718 y = (scr_height - height) / 2;
719 rect->x = scr_xleft + x;
720 rect->y = scr_ytop + y;
721 rect->w = FFMAX(width, 1);
722 rect->h = FFMAX(height, 1);
725 static void video_image_display(VideoState *is)
733 vp = &is->pictq[is->pictq_rindex];
735 if (is->subtitle_st) {
736 if (is->subpq_size > 0) {
737 sp = &is->subpq[is->subpq_rindex];
739 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
740 SDL_LockYUVOverlay (vp->bmp);
742 pict.data[0] = vp->bmp->pixels[0];
743 pict.data[1] = vp->bmp->pixels[2];
744 pict.data[2] = vp->bmp->pixels[1];
746 pict.linesize[0] = vp->bmp->pitches[0];
747 pict.linesize[1] = vp->bmp->pitches[2];
748 pict.linesize[2] = vp->bmp->pitches[1];
750 for (i = 0; i < sp->sub.num_rects; i++)
751 blend_subrect(&pict, sp->sub.rects[i],
752 vp->bmp->w, vp->bmp->h);
754 SDL_UnlockYUVOverlay (vp->bmp);
759 calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp);
761 SDL_DisplayYUVOverlay(vp->bmp, &rect);
765 static inline int compute_mod(int a, int b)
767 return a < 0 ? a%b + b : a%b;
770 static void video_audio_display(VideoState *s)
772 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
773 int ch, channels, h, h2, bgcolor, fgcolor;
775 int rdft_bits, nb_freq;
777 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
779 nb_freq = 1 << (rdft_bits - 1);
781 /* compute display index : center on currently output samples */
782 channels = s->audio_tgt.channels;
783 nb_display_channels = channels;
785 int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
787 delay = s->audio_write_buf_size;
790 /* to be more precise, we take into account the time spent since
791 the last buffer computation */
792 if (audio_callback_time) {
793 time_diff = av_gettime() - audio_callback_time;
794 delay -= (time_diff * s->audio_tgt.freq) / 1000000;
797 delay += 2 * data_used;
798 if (delay < data_used)
801 i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
802 if (s->show_mode == SHOW_MODE_WAVES) {
804 for (i = 0; i < 1000; i += channels) {
805 int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
806 int a = s->sample_array[idx];
807 int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
808 int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
809 int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
811 if (h < score && (b ^ c) < 0) {
818 s->last_i_start = i_start;
820 i_start = s->last_i_start;
823 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
824 if (s->show_mode == SHOW_MODE_WAVES) {
825 fill_rectangle(screen,
826 s->xleft, s->ytop, s->width, s->height,
829 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
831 /* total height for one channel */
832 h = s->height / nb_display_channels;
833 /* graph height / 2 */
835 for (ch = 0; ch < nb_display_channels; ch++) {
837 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
838 for (x = 0; x < s->width; x++) {
839 y = (s->sample_array[i] * h2) >> 15;
846 fill_rectangle(screen,
847 s->xleft + x, ys, 1, y,
850 if (i >= SAMPLE_ARRAY_SIZE)
851 i -= SAMPLE_ARRAY_SIZE;
855 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
857 for (ch = 1; ch < nb_display_channels; ch++) {
858 y = s->ytop + ch * h;
859 fill_rectangle(screen,
860 s->xleft, y, s->width, 1,
863 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
865 nb_display_channels= FFMIN(nb_display_channels, 2);
866 if (rdft_bits != s->rdft_bits) {
867 av_rdft_end(s->rdft);
868 av_free(s->rdft_data);
869 s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
870 s->rdft_bits = rdft_bits;
871 s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data));
875 for (ch = 0; ch < nb_display_channels; ch++) {
876 data[ch] = s->rdft_data + 2 * nb_freq * ch;
878 for (x = 0; x < 2 * nb_freq; x++) {
879 double w = (x-nb_freq) * (1.0 / nb_freq);
880 data[ch][x] = s->sample_array[i] * (1.0 - w * w);
882 if (i >= SAMPLE_ARRAY_SIZE)
883 i -= SAMPLE_ARRAY_SIZE;
885 av_rdft_calc(s->rdft, data[ch]);
887 // least efficient way to do this, we should of course directly access it but its more than fast enough
888 for (y = 0; y < s->height; y++) {
889 double w = 1 / sqrt(nb_freq);
890 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]));
891 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
892 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
895 fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
897 fill_rectangle(screen,
898 s->xpos, s->height-y, 1, 1,
902 SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
905 if (s->xpos >= s->width)
910 static void stream_close(VideoState *is)
914 /* XXX: use a special url_shutdown call to abort parse cleanly */
915 is->abort_request = 1;
916 SDL_WaitThread(is->read_tid, NULL);
917 SDL_WaitThread(is->refresh_tid, NULL);
918 packet_queue_destroy(&is->videoq);
919 packet_queue_destroy(&is->audioq);
920 packet_queue_destroy(&is->subtitleq);
922 /* free all pictures */
923 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
926 avfilter_unref_bufferp(&vp->picref);
929 SDL_FreeYUVOverlay(vp->bmp);
933 SDL_DestroyMutex(is->pictq_mutex);
934 SDL_DestroyCond(is->pictq_cond);
935 SDL_DestroyMutex(is->subpq_mutex);
936 SDL_DestroyCond(is->subpq_cond);
937 SDL_DestroyCond(is->continue_read_thread);
939 if (is->img_convert_ctx)
940 sws_freeContext(is->img_convert_ctx);
945 static void do_exit(VideoState *is)
950 av_lockmgr_register(NULL);
956 avformat_network_deinit();
960 av_log(NULL, AV_LOG_QUIET, "%s", "");
964 static void sigterm_handler(int sig)
969 static int video_open(VideoState *is, int force_set_video_mode)
971 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
973 VideoPicture *vp = &is->pictq[is->pictq_rindex];
976 if (is_full_screen) flags |= SDL_FULLSCREEN;
977 else flags |= SDL_RESIZABLE;
979 if (is_full_screen && fs_screen_width) {
981 h = fs_screen_height;
982 } else if (!is_full_screen && screen_width) {
985 } else if (vp->width) {
986 calculate_display_rect(&rect, 0, 0, INT_MAX, vp->height, vp);
993 if (screen && is->width == screen->w && screen->w == w
994 && is->height== screen->h && screen->h == h && !force_set_video_mode)
996 screen = SDL_SetVideoMode(w, h, 0, flags);
998 fprintf(stderr, "SDL: could not set video mode - exiting\n");
1002 window_title = input_filename;
1003 SDL_WM_SetCaption(window_title, window_title);
1005 is->width = screen->w;
1006 is->height = screen->h;
1011 /* display the current picture, if any */
1012 static void video_display(VideoState *is)
1016 if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
1017 video_audio_display(is);
1018 else if (is->video_st)
1019 video_image_display(is);
1022 static int refresh_thread(void *opaque)
1024 VideoState *is= opaque;
1025 while (!is->abort_request) {
1027 event.type = FF_REFRESH_EVENT;
1028 event.user.data1 = opaque;
1029 if (!is->refresh && (!is->paused || is->force_refresh)) {
1031 SDL_PushEvent(&event);
1033 //FIXME ideally we should wait the correct time but SDLs event passing is so slow it would be silly
1034 av_usleep(is->audio_st && is->show_mode != SHOW_MODE_VIDEO ? rdftspeed*1000 : 5000);
1039 /* get the current audio clock value */
1040 static double get_audio_clock(VideoState *is)
1043 return is->audio_current_pts;
1045 return is->audio_current_pts_drift + av_gettime() / 1000000.0;
1049 /* get the current video clock value */
1050 static double get_video_clock(VideoState *is)
1053 return is->video_current_pts;
1055 return is->video_current_pts_drift + av_gettime() / 1000000.0;
1059 /* get the current external clock value */
1060 static double get_external_clock(VideoState *is)
1063 return is->external_clock;
1065 return is->external_clock_drift + av_gettime() / 1000000.0;
1069 static int get_master_sync_type(VideoState *is) {
1070 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
1072 return AV_SYNC_VIDEO_MASTER;
1074 return AV_SYNC_AUDIO_MASTER;
1075 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
1077 return AV_SYNC_AUDIO_MASTER;
1079 return AV_SYNC_VIDEO_MASTER;
1081 return AV_SYNC_EXTERNAL_CLOCK;
1085 /* get the current master clock value */
1086 static double get_master_clock(VideoState *is)
1090 switch (get_master_sync_type(is)) {
1091 case AV_SYNC_VIDEO_MASTER:
1092 val = get_video_clock(is);
1094 case AV_SYNC_AUDIO_MASTER:
1095 val = get_audio_clock(is);
1098 val = get_external_clock(is);
1104 static void update_external_clock_pts(VideoState *is, double pts)
1106 is->external_clock_time = av_gettime();
1107 is->external_clock = pts;
1108 is->external_clock_drift = pts - is->external_clock_time / 1000000.0;
1111 static void check_external_clock_sync(VideoState *is, double pts) {
1112 if (fabs(get_external_clock(is) - pts) > AV_NOSYNC_THRESHOLD) {
1113 update_external_clock_pts(is, pts);
1117 /* seek in the stream */
1118 static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
1120 if (!is->seek_req) {
1123 is->seek_flags &= ~AVSEEK_FLAG_BYTE;
1125 is->seek_flags |= AVSEEK_FLAG_BYTE;
1130 /* pause or resume the video */
1131 static void stream_toggle_pause(VideoState *is)
1134 is->frame_timer += av_gettime() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
1135 if (is->read_pause_return != AVERROR(ENOSYS)) {
1136 is->video_current_pts = is->video_current_pts_drift + av_gettime() / 1000000.0;
1138 is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
1140 update_external_clock_pts(is, get_external_clock(is));
1141 is->paused = !is->paused;
1144 static double compute_target_delay(double delay, VideoState *is)
1146 double sync_threshold, diff;
1148 /* update delay to follow master synchronisation source */
1149 if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) ||
1150 is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
1151 /* if video is slave, we try to correct big delays by
1152 duplicating or deleting a frame */
1153 diff = get_video_clock(is) - get_master_clock(is);
1155 /* skip or repeat frame. We take into account the
1156 delay to compute the threshold. I still don't know
1157 if it is the best guess */
1158 sync_threshold = FFMAX(AV_SYNC_THRESHOLD, delay);
1159 if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
1160 if (diff <= -sync_threshold)
1162 else if (diff >= sync_threshold)
1167 av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
1173 static void pictq_next_picture(VideoState *is) {
1174 /* update queue size and signal for next picture */
1175 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
1176 is->pictq_rindex = 0;
1178 SDL_LockMutex(is->pictq_mutex);
1180 SDL_CondSignal(is->pictq_cond);
1181 SDL_UnlockMutex(is->pictq_mutex);
1184 static void pictq_prev_picture(VideoState *is) {
1185 VideoPicture *prevvp;
1186 /* update queue size and signal for the previous picture */
1187 prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE];
1188 if (prevvp->allocated && !prevvp->skip) {
1189 SDL_LockMutex(is->pictq_mutex);
1190 if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE - 1) {
1191 if (--is->pictq_rindex == -1)
1192 is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1;
1195 SDL_CondSignal(is->pictq_cond);
1196 SDL_UnlockMutex(is->pictq_mutex);
1200 static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
1201 double time = av_gettime() / 1000000.0;
1202 /* update current video pts */
1203 is->video_current_pts = pts;
1204 is->video_current_pts_drift = is->video_current_pts - time;
1205 is->video_current_pos = pos;
1206 is->frame_last_pts = pts;
1207 if (is->videoq.serial == serial)
1208 check_external_clock_sync(is, is->video_current_pts);
1211 /* called to display each frame */
1212 static void video_refresh(void *opaque)
1214 VideoState *is = opaque;
1218 SubPicture *sp, *sp2;
1221 if (is->force_refresh)
1222 pictq_prev_picture(is);
1224 if (is->pictq_size == 0) {
1225 SDL_LockMutex(is->pictq_mutex);
1226 if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
1227 update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, 0);
1228 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1230 SDL_UnlockMutex(is->pictq_mutex);
1231 // nothing to do, no picture to display in the que
1233 double last_duration, duration, delay;
1234 /* dequeue the picture */
1235 vp = &is->pictq[is->pictq_rindex];
1238 pictq_next_picture(is);
1245 /* compute nominal last_duration */
1246 last_duration = vp->pts - is->frame_last_pts;
1247 if (last_duration > 0 && last_duration < 10.0) {
1248 /* if duration of the last frame was sane, update last_duration in video state */
1249 is->frame_last_duration = last_duration;
1251 delay = compute_target_delay(is->frame_last_duration, is);
1253 time= av_gettime()/1000000.0;
1254 if (time < is->frame_timer + delay)
1258 is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
1260 SDL_LockMutex(is->pictq_mutex);
1261 update_video_pts(is, vp->pts, vp->pos, vp->serial);
1262 SDL_UnlockMutex(is->pictq_mutex);
1264 if (is->pictq_size > 1) {
1265 VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
1266 duration = nextvp->pts - vp->pts;
1267 if((framedrop>0 || (framedrop && is->av_sync_type != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
1268 is->frame_drops_late++;
1269 pictq_next_picture(is);
1274 if (is->subtitle_st) {
1275 if (is->subtitle_stream_changed) {
1276 SDL_LockMutex(is->subpq_mutex);
1278 while (is->subpq_size) {
1279 free_subpicture(&is->subpq[is->subpq_rindex]);
1281 /* update queue size and signal for next picture */
1282 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1283 is->subpq_rindex = 0;
1287 is->subtitle_stream_changed = 0;
1289 SDL_CondSignal(is->subpq_cond);
1290 SDL_UnlockMutex(is->subpq_mutex);
1292 if (is->subpq_size > 0) {
1293 sp = &is->subpq[is->subpq_rindex];
1295 if (is->subpq_size > 1)
1296 sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
1300 if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1301 || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1303 free_subpicture(sp);
1305 /* update queue size and signal for next picture */
1306 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1307 is->subpq_rindex = 0;
1309 SDL_LockMutex(is->subpq_mutex);
1311 SDL_CondSignal(is->subpq_cond);
1312 SDL_UnlockMutex(is->subpq_mutex);
1319 /* display picture */
1320 if (!display_disable)
1323 pictq_next_picture(is);
1325 } else if (is->audio_st) {
1326 /* draw the next audio frame */
1328 /* if only audio stream, then display the audio bars (better
1329 than nothing, just to test the implementation */
1331 /* display picture */
1332 if (!display_disable)
1335 is->force_refresh = 0;
1337 static int64_t last_time;
1339 int aqsize, vqsize, sqsize;
1342 cur_time = av_gettime();
1343 if (!last_time || (cur_time - last_time) >= 30000) {
1348 aqsize = is->audioq.size;
1350 vqsize = is->videoq.size;
1351 if (is->subtitle_st)
1352 sqsize = is->subtitleq.size;
1354 if (is->audio_st && is->video_st)
1355 av_diff = get_audio_clock(is) - get_video_clock(is);
1356 printf("%7.2f A-V:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
1357 get_master_clock(is),
1359 is->frame_drops_early + is->frame_drops_late,
1363 is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1364 is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1366 last_time = cur_time;
1371 /* allocate a picture (needs to do that in main thread to avoid
1372 potential locking problems */
1373 static void alloc_picture(VideoState *is)
1377 vp = &is->pictq[is->pictq_windex];
1380 SDL_FreeYUVOverlay(vp->bmp);
1383 avfilter_unref_bufferp(&vp->picref);
1388 vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1391 if (!vp->bmp || vp->bmp->pitches[0] < vp->width) {
1392 /* SDL allocates a buffer smaller than requested if the video
1393 * overlay hardware is unable to support the requested size. */
1394 fprintf(stderr, "Error: the video system does not support an image\n"
1395 "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1396 "to reduce the image size.\n", vp->width, vp->height );
1400 SDL_LockMutex(is->pictq_mutex);
1402 SDL_CondSignal(is->pictq_cond);
1403 SDL_UnlockMutex(is->pictq_mutex);
1406 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts1, int64_t pos, int serial)
1409 double frame_delay, pts = pts1;
1411 /* compute the exact PTS for the picture if it is omitted in the stream
1412 * pts1 is the dts of the pkt / pts of the frame */
1414 /* update video clock with pts, if present */
1415 is->video_clock = pts;
1417 pts = is->video_clock;
1419 /* update video clock for next frame */
1420 frame_delay = av_q2d(is->video_st->codec->time_base);
1421 /* for MPEG2, the frame can be repeated, so we update the
1422 clock accordingly */
1423 frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
1424 is->video_clock += frame_delay;
1426 #if defined(DEBUG_SYNC) && 0
1427 printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
1428 av_get_picture_type_char(src_frame->pict_type), pts, pts1);
1431 /* wait until we have space to put a new picture */
1432 SDL_LockMutex(is->pictq_mutex);
1434 /* keep the last already displayed picture in the queue */
1435 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 2 &&
1436 !is->videoq.abort_request) {
1437 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1439 SDL_UnlockMutex(is->pictq_mutex);
1441 if (is->videoq.abort_request)
1444 vp = &is->pictq[is->pictq_windex];
1447 vp->sample_aspect_ratio = ((AVFilterBufferRef *)src_frame->opaque)->video->sample_aspect_ratio;
1449 vp->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, src_frame);
1452 /* alloc or resize hardware picture buffer */
1453 if (!vp->bmp || vp->reallocate || !vp->allocated ||
1454 vp->width != src_frame->width ||
1455 vp->height != src_frame->height) {
1460 vp->width = src_frame->width;
1461 vp->height = src_frame->height;
1463 /* the allocation must be done in the main thread to avoid
1464 locking problems. */
1465 event.type = FF_ALLOC_EVENT;
1466 event.user.data1 = is;
1467 SDL_PushEvent(&event);
1469 /* wait until the picture is allocated */
1470 SDL_LockMutex(is->pictq_mutex);
1471 while (!vp->allocated && !is->videoq.abort_request) {
1472 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1474 /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1475 if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1476 while (!vp->allocated) {
1477 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1480 SDL_UnlockMutex(is->pictq_mutex);
1482 if (is->videoq.abort_request)
1486 /* if the frame is not skipped, then display it */
1488 AVPicture pict = { { 0 } };
1490 avfilter_unref_bufferp(&vp->picref);
1491 vp->picref = src_frame->opaque;
1494 /* get a pointer on the bitmap */
1495 SDL_LockYUVOverlay (vp->bmp);
1497 pict.data[0] = vp->bmp->pixels[0];
1498 pict.data[1] = vp->bmp->pixels[2];
1499 pict.data[2] = vp->bmp->pixels[1];
1501 pict.linesize[0] = vp->bmp->pitches[0];
1502 pict.linesize[1] = vp->bmp->pitches[2];
1503 pict.linesize[2] = vp->bmp->pitches[1];
1506 // FIXME use direct rendering
1507 av_picture_copy(&pict, (AVPicture *)src_frame,
1508 src_frame->format, vp->width, vp->height);
1510 sws_flags = av_get_int(sws_opts, "sws_flags", NULL);
1511 is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1512 vp->width, vp->height, src_frame->format, vp->width, vp->height,
1513 AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1514 if (is->img_convert_ctx == NULL) {
1515 fprintf(stderr, "Cannot initialize the conversion context\n");
1518 sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1519 0, vp->height, pict.data, pict.linesize);
1521 /* update the bitmap content */
1522 SDL_UnlockYUVOverlay(vp->bmp);
1527 vp->serial = serial;
1529 /* now we can update the picture count */
1530 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
1531 is->pictq_windex = 0;
1532 SDL_LockMutex(is->pictq_mutex);
1534 SDL_UnlockMutex(is->pictq_mutex);
1539 static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt, int *serial)
1543 if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
1546 if (pkt->data == flush_pkt.data) {
1547 avcodec_flush_buffers(is->video_st->codec);
1549 SDL_LockMutex(is->pictq_mutex);
1550 // Make sure there are no long delay timers (ideally we should just flush the que but thats harder)
1551 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
1552 is->pictq[i].skip = 1;
1554 while (is->pictq_size && !is->videoq.abort_request) {
1555 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1557 is->video_current_pos = -1;
1558 is->frame_last_pts = AV_NOPTS_VALUE;
1559 is->frame_last_duration = 0;
1560 is->frame_timer = (double)av_gettime() / 1000000.0;
1561 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1562 SDL_UnlockMutex(is->pictq_mutex);
1567 if(avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt) < 0)
1573 if (decoder_reorder_pts == -1) {
1574 *pts = av_frame_get_best_effort_timestamp(frame);
1575 } else if (decoder_reorder_pts) {
1576 *pts = frame->pkt_pts;
1578 *pts = frame->pkt_dts;
1581 if (*pts == AV_NOPTS_VALUE) {
1585 if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) || is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK) &&
1586 (framedrop>0 || (framedrop && is->av_sync_type != AV_SYNC_VIDEO_MASTER))) {
1587 SDL_LockMutex(is->pictq_mutex);
1588 if (is->frame_last_pts != AV_NOPTS_VALUE && *pts) {
1589 double clockdiff = get_video_clock(is) - get_master_clock(is);
1590 double dpts = av_q2d(is->video_st->time_base) * *pts;
1591 double ptsdiff = dpts - is->frame_last_pts;
1592 if (fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
1593 ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
1594 clockdiff + ptsdiff - is->frame_last_filter_delay < 0) {
1595 is->frame_last_dropped_pos = pkt->pos;
1596 is->frame_last_dropped_pts = dpts;
1597 is->frame_drops_early++;
1601 SDL_UnlockMutex(is->pictq_mutex);
1610 static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
1611 AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
1614 AVFilterInOut *outputs = NULL, *inputs = NULL;
1617 outputs = avfilter_inout_alloc();
1618 inputs = avfilter_inout_alloc();
1619 if (!outputs || !inputs) {
1620 ret = AVERROR(ENOMEM);
1624 outputs->name = av_strdup("in");
1625 outputs->filter_ctx = source_ctx;
1626 outputs->pad_idx = 0;
1627 outputs->next = NULL;
1629 inputs->name = av_strdup("out");
1630 inputs->filter_ctx = sink_ctx;
1631 inputs->pad_idx = 0;
1632 inputs->next = NULL;
1634 if ((ret = avfilter_graph_parse(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
1637 if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
1641 return avfilter_graph_config(graph, NULL);
1643 avfilter_inout_free(&outputs);
1644 avfilter_inout_free(&inputs);
1648 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters)
1650 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
1651 char sws_flags_str[128];
1652 char buffersrc_args[256];
1654 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
1655 AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_format, *filt_crop;
1656 AVCodecContext *codec = is->video_st->codec;
1658 snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%d", sws_flags);
1659 graph->scale_sws_opts = av_strdup(sws_flags_str);
1661 snprintf(buffersrc_args, sizeof(buffersrc_args),
1662 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
1663 codec->width, codec->height, codec->pix_fmt,
1664 is->video_st->time_base.num, is->video_st->time_base.den,
1665 codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
1667 if ((ret = avfilter_graph_create_filter(&filt_src,
1668 avfilter_get_by_name("buffer"),
1669 "ffplay_buffer", buffersrc_args, NULL,
1673 buffersink_params->pixel_fmts = pix_fmts;
1674 ret = avfilter_graph_create_filter(&filt_out,
1675 avfilter_get_by_name("ffbuffersink"),
1676 "ffplay_buffersink", NULL, buffersink_params, graph);
1677 av_freep(&buffersink_params);
1681 /* SDL YUV code is not handling odd width/height for some driver
1682 * combinations, therefore we crop the picture to an even width/height. */
1683 if ((ret = avfilter_graph_create_filter(&filt_crop,
1684 avfilter_get_by_name("crop"),
1685 "ffplay_crop", "floor(in_w/2)*2:floor(in_h/2)*2", NULL, graph)) < 0)
1687 if ((ret = avfilter_graph_create_filter(&filt_format,
1688 avfilter_get_by_name("format"),
1689 "format", "yuv420p", NULL, graph)) < 0)
1691 if ((ret = avfilter_link(filt_crop, 0, filt_format, 0)) < 0)
1693 if ((ret = avfilter_link(filt_format, 0, filt_out, 0)) < 0)
1696 if ((ret = configure_filtergraph(graph, vfilters, filt_src, filt_crop)) < 0)
1699 is->in_video_filter = filt_src;
1700 is->out_video_filter = filt_out;
1705 #endif /* CONFIG_AVFILTER */
1707 static int video_thread(void *arg)
1709 AVPacket pkt = { 0 };
1710 VideoState *is = arg;
1711 AVFrame *frame = avcodec_alloc_frame();
1712 int64_t pts_int = AV_NOPTS_VALUE, pos = -1;
1718 AVCodecContext *codec = is->video_st->codec;
1719 AVFilterGraph *graph = avfilter_graph_alloc();
1720 AVFilterContext *filt_out = NULL, *filt_in = NULL;
1723 enum AVPixelFormat last_format = -2;
1725 if (codec->codec->capabilities & CODEC_CAP_DR1) {
1727 codec->get_buffer = codec_get_buffer;
1728 codec->release_buffer = codec_release_buffer;
1729 codec->opaque = &is->buffer_pool;
1735 AVFilterBufferRef *picref;
1738 while (is->paused && !is->videoq.abort_request)
1741 avcodec_get_frame_defaults(frame);
1742 av_free_packet(&pkt);
1744 ret = get_video_frame(is, frame, &pts_int, &pkt, &serial);
1752 if ( last_w != is->video_st->codec->width
1753 || last_h != is->video_st->codec->height
1754 || last_format != is->video_st->codec->pix_fmt) {
1755 av_log(NULL, AV_LOG_INFO, "Frame changed from size:%dx%d to size:%dx%d\n",
1756 last_w, last_h, is->video_st->codec->width, is->video_st->codec->height);
1757 avfilter_graph_free(&graph);
1758 graph = avfilter_graph_alloc();
1759 if ((ret = configure_video_filters(graph, is, vfilters)) < 0) {
1761 event.type = FF_QUIT_EVENT;
1762 event.user.data1 = is;
1763 SDL_PushEvent(&event);
1764 av_free_packet(&pkt);
1767 filt_in = is->in_video_filter;
1768 filt_out = is->out_video_filter;
1769 last_w = is->video_st->codec->width;
1770 last_h = is->video_st->codec->height;
1771 last_format = is->video_st->codec->pix_fmt;
1774 frame->pts = pts_int;
1775 frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
1776 if (is->use_dr1 && frame->opaque) {
1777 FrameBuffer *buf = frame->opaque;
1778 AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
1779 frame->data, frame->linesize,
1780 AV_PERM_READ | AV_PERM_PRESERVE,
1781 frame->width, frame->height,
1784 avfilter_copy_frame_props(fb, frame);
1785 fb->buf->priv = buf;
1786 fb->buf->free = filter_release_buffer;
1789 av_buffersrc_add_ref(filt_in, fb, AV_BUFFERSRC_FLAG_NO_COPY);
1792 av_buffersrc_write_frame(filt_in, frame);
1794 av_free_packet(&pkt);
1797 is->frame_last_returned_time = av_gettime() / 1000000.0;
1799 ret = av_buffersink_get_buffer_ref(filt_out, &picref, 0);
1805 is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
1806 if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
1807 is->frame_last_filter_delay = 0;
1809 avfilter_copy_buf_props(frame, picref);
1811 pts_int = picref->pts;
1812 tb = filt_out->inputs[0]->time_base;
1814 frame->opaque = picref;
1816 if (av_cmp_q(tb, is->video_st->time_base)) {
1817 av_unused int64_t pts1 = pts_int;
1818 pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
1819 av_dlog(NULL, "video_thread(): "
1820 "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
1821 tb.num, tb.den, pts1,
1822 is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
1824 pts = pts_int * av_q2d(is->video_st->time_base);
1825 ret = queue_picture(is, frame, pts, pos, serial);
1828 pts = pts_int * av_q2d(is->video_st->time_base);
1829 ret = queue_picture(is, frame, pts, pkt.pos, serial);
1836 stream_toggle_pause(is);
1839 avcodec_flush_buffers(is->video_st->codec);
1841 avfilter_graph_free(&graph);
1843 av_free_packet(&pkt);
1844 avcodec_free_frame(&frame);
1848 static int subtitle_thread(void *arg)
1850 VideoState *is = arg;
1852 AVPacket pkt1, *pkt = &pkt1;
1856 int r, g, b, y, u, v, a;
1859 while (is->paused && !is->subtitleq.abort_request) {
1862 if (packet_queue_get(&is->subtitleq, pkt, 1, NULL) < 0)
1865 if (pkt->data == flush_pkt.data) {
1866 avcodec_flush_buffers(is->subtitle_st->codec);
1869 SDL_LockMutex(is->subpq_mutex);
1870 while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
1871 !is->subtitleq.abort_request) {
1872 SDL_CondWait(is->subpq_cond, is->subpq_mutex);
1874 SDL_UnlockMutex(is->subpq_mutex);
1876 if (is->subtitleq.abort_request)
1879 sp = &is->subpq[is->subpq_windex];
1881 /* NOTE: ipts is the PTS of the _first_ picture beginning in
1882 this packet, if any */
1884 if (pkt->pts != AV_NOPTS_VALUE)
1885 pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
1887 avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
1888 &got_subtitle, pkt);
1889 if (got_subtitle && sp->sub.format == 0) {
1890 if (sp->sub.pts != AV_NOPTS_VALUE)
1891 pts = sp->sub.pts / (double)AV_TIME_BASE;
1894 for (i = 0; i < sp->sub.num_rects; i++)
1896 for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
1898 RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
1899 y = RGB_TO_Y_CCIR(r, g, b);
1900 u = RGB_TO_U_CCIR(r, g, b, 0);
1901 v = RGB_TO_V_CCIR(r, g, b, 0);
1902 YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
1906 /* now we can update the picture count */
1907 if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
1908 is->subpq_windex = 0;
1909 SDL_LockMutex(is->subpq_mutex);
1911 SDL_UnlockMutex(is->subpq_mutex);
1913 av_free_packet(pkt);
1918 /* copy samples for viewing in editor window */
1919 static void update_sample_display(VideoState *is, short *samples, int samples_size)
1923 size = samples_size / sizeof(short);
1925 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
1928 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
1930 is->sample_array_index += len;
1931 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
1932 is->sample_array_index = 0;
1937 /* return the wanted number of samples to get better sync if sync_type is video
1938 * or external master clock */
1939 static int synchronize_audio(VideoState *is, int nb_samples)
1941 int wanted_nb_samples = nb_samples;
1943 /* if not master, then we try to remove or add samples to correct the clock */
1944 if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||
1945 is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
1946 double diff, avg_diff;
1947 int min_nb_samples, max_nb_samples;
1949 diff = get_audio_clock(is) - get_master_clock(is);
1951 if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
1952 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
1953 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
1954 /* not enough measures to have a correct estimate */
1955 is->audio_diff_avg_count++;
1957 /* estimate the A-V difference */
1958 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
1960 if (fabs(avg_diff) >= is->audio_diff_threshold) {
1961 wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
1962 min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
1963 max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
1964 wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
1966 av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
1967 diff, avg_diff, wanted_nb_samples - nb_samples,
1968 is->audio_clock, is->video_clock, is->audio_diff_threshold);
1971 /* too big difference : may be initial PTS errors, so
1973 is->audio_diff_avg_count = 0;
1974 is->audio_diff_cum = 0;
1978 return wanted_nb_samples;
1981 /* decode one audio frame and returns its uncompressed size */
1982 static int audio_decode_frame(VideoState *is, double *pts_ptr)
1984 AVPacket *pkt_temp = &is->audio_pkt_temp;
1985 AVPacket *pkt = &is->audio_pkt;
1986 AVCodecContext *dec = is->audio_st->codec;
1987 int len1, len2, data_size, resampled_data_size;
1988 int64_t dec_channel_layout;
1992 int flush_complete = 0;
1993 int wanted_nb_samples;
1996 /* NOTE: the audio packet can contain several frames */
1997 while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) {
1999 if (!(is->frame = avcodec_alloc_frame()))
2000 return AVERROR(ENOMEM);
2002 avcodec_get_frame_defaults(is->frame);
2010 len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
2012 /* if error, we skip the frame */
2017 pkt_temp->data += len1;
2018 pkt_temp->size -= len1;
2021 /* stop sending empty packets if the decoder is finished */
2022 if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
2026 data_size = av_samples_get_buffer_size(NULL, is->frame->channels,
2027 is->frame->nb_samples,
2028 is->frame->format, 1);
2030 dec_channel_layout =
2031 (is->frame->channel_layout && is->frame->channels == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
2032 is->frame->channel_layout : av_get_default_channel_layout(is->frame->channels);
2033 wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
2035 if (is->frame->format != is->audio_src.fmt ||
2036 dec_channel_layout != is->audio_src.channel_layout ||
2037 is->frame->sample_rate != is->audio_src.freq ||
2038 (wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) {
2039 swr_free(&is->swr_ctx);
2040 is->swr_ctx = swr_alloc_set_opts(NULL,
2041 is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2042 dec_channel_layout, is->frame->format, is->frame->sample_rate,
2044 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2045 fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2046 is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), (int)is->frame->channels,
2047 is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
2050 is->audio_src.channel_layout = dec_channel_layout;
2051 is->audio_src.channels = is->frame->channels;
2052 is->audio_src.freq = is->frame->sample_rate;
2053 is->audio_src.fmt = is->frame->format;
2057 const uint8_t **in = (const uint8_t **)is->frame->extended_data;
2058 uint8_t *out[] = {is->audio_buf2};
2059 int out_count = sizeof(is->audio_buf2) / is->audio_tgt.channels / av_get_bytes_per_sample(is->audio_tgt.fmt);
2060 if (wanted_nb_samples != is->frame->nb_samples) {
2061 if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
2062 wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
2063 fprintf(stderr, "swr_set_compensation() failed\n");
2067 len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
2069 fprintf(stderr, "swr_convert() failed\n");
2072 if (len2 == out_count) {
2073 fprintf(stderr, "warning: audio buffer is probably too small\n");
2074 swr_init(is->swr_ctx);
2076 is->audio_buf = is->audio_buf2;
2077 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2079 is->audio_buf = is->frame->data[0];
2080 resampled_data_size = data_size;
2083 /* if no pts, then compute it */
2084 pts = is->audio_clock;
2086 is->audio_clock += (double)data_size /
2087 (is->frame->channels * is->frame->sample_rate * av_get_bytes_per_sample(is->frame->format));
2090 static double last_clock;
2091 printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
2092 is->audio_clock - last_clock,
2093 is->audio_clock, pts);
2094 last_clock = is->audio_clock;
2097 return resampled_data_size;
2100 /* free the current packet */
2102 av_free_packet(pkt);
2103 memset(pkt_temp, 0, sizeof(*pkt_temp));
2105 if (is->paused || is->audioq.abort_request) {
2109 if (is->audioq.nb_packets == 0)
2110 SDL_CondSignal(is->continue_read_thread);
2112 /* read next packet */
2113 if ((new_packet = packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
2116 if (pkt->data == flush_pkt.data) {
2117 avcodec_flush_buffers(dec);
2123 /* if update the audio clock with the pts */
2124 if (pkt->pts != AV_NOPTS_VALUE) {
2125 is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
2130 /* prepare a new audio buffer */
2131 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2133 VideoState *is = opaque;
2134 int audio_size, len1;
2136 int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
2139 audio_callback_time = av_gettime();
2142 if (is->audio_buf_index >= is->audio_buf_size) {
2143 audio_size = audio_decode_frame(is, &pts);
2144 if (audio_size < 0) {
2145 /* if error, just output silence */
2146 is->audio_buf = is->silence_buf;
2147 is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
2149 if (is->show_mode != SHOW_MODE_VIDEO)
2150 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2151 is->audio_buf_size = audio_size;
2153 is->audio_buf_index = 0;
2155 len1 = is->audio_buf_size - is->audio_buf_index;
2158 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2161 is->audio_buf_index += len1;
2163 bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2164 is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2165 /* Let's assume the audio driver that is used by SDL has two periods. */
2166 is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec;
2167 is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
2168 if (is->audioq.serial == is->audio_pkt_temp_serial)
2169 check_external_clock_sync(is, is->audio_current_pts);
2172 static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
2174 SDL_AudioSpec wanted_spec, spec;
2176 const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
2178 env = SDL_getenv("SDL_AUDIO_CHANNELS");
2180 wanted_nb_channels = atoi(env);
2181 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2183 if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
2184 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2185 wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2187 wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2188 wanted_spec.freq = wanted_sample_rate;
2189 if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2190 fprintf(stderr, "Invalid sample rate or channel count!\n");
2193 wanted_spec.format = AUDIO_S16SYS;
2194 wanted_spec.silence = 0;
2195 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
2196 wanted_spec.callback = sdl_audio_callback;
2197 wanted_spec.userdata = opaque;
2198 while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2199 fprintf(stderr, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError());
2200 wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
2201 if (!wanted_spec.channels) {
2202 fprintf(stderr, "No more channel combinations to try, audio open failed\n");
2205 wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
2207 if (spec.format != AUDIO_S16SYS) {
2208 fprintf(stderr, "SDL advised audio format %d is not supported!\n", spec.format);
2211 if (spec.channels != wanted_spec.channels) {
2212 wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2213 if (!wanted_channel_layout) {
2214 fprintf(stderr, "SDL advised channel count %d is not supported!\n", spec.channels);
2219 audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
2220 audio_hw_params->freq = spec.freq;
2221 audio_hw_params->channel_layout = wanted_channel_layout;
2222 audio_hw_params->channels = spec.channels;
2226 /* open a given stream. Return 0 if OK */
2227 static int stream_component_open(VideoState *is, int stream_index)
2229 AVFormatContext *ic = is->ic;
2230 AVCodecContext *avctx;
2233 AVDictionaryEntry *t = NULL;
2235 if (stream_index < 0 || stream_index >= ic->nb_streams)
2237 avctx = ic->streams[stream_index]->codec;
2239 codec = avcodec_find_decoder(avctx->codec_id);
2240 opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
2242 switch(avctx->codec_type){
2243 case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; if(audio_codec_name ) codec= avcodec_find_decoder_by_name( audio_codec_name); break;
2244 case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; if(subtitle_codec_name) codec= avcodec_find_decoder_by_name(subtitle_codec_name); break;
2245 case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; if(video_codec_name ) codec= avcodec_find_decoder_by_name( video_codec_name); break;
2250 avctx->workaround_bugs = workaround_bugs;
2251 avctx->lowres = lowres;
2252 if(avctx->lowres > codec->max_lowres){
2253 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2255 avctx->lowres= codec->max_lowres;
2257 avctx->idct_algo = idct;
2258 avctx->skip_frame = skip_frame;
2259 avctx->skip_idct = skip_idct;
2260 avctx->skip_loop_filter = skip_loop_filter;
2261 avctx->error_concealment = error_concealment;
2263 if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
2264 if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
2265 if(codec->capabilities & CODEC_CAP_DR1)
2266 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2268 if (!av_dict_get(opts, "threads", NULL, 0))
2269 av_dict_set(&opts, "threads", "auto", 0);
2271 avcodec_open2(avctx, codec, &opts) < 0)
2273 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2274 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2275 return AVERROR_OPTION_NOT_FOUND;
2278 /* prepare audio output */
2279 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2280 int audio_hw_buf_size = audio_open(is, avctx->channel_layout, avctx->channels, avctx->sample_rate, &is->audio_src);
2281 if (audio_hw_buf_size < 0)
2283 is->audio_hw_buf_size = audio_hw_buf_size;
2284 is->audio_tgt = is->audio_src;
2287 ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2288 switch (avctx->codec_type) {
2289 case AVMEDIA_TYPE_AUDIO:
2290 is->audio_stream = stream_index;
2291 is->audio_st = ic->streams[stream_index];
2292 is->audio_buf_size = 0;
2293 is->audio_buf_index = 0;
2295 /* init averaging filter */
2296 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2297 is->audio_diff_avg_count = 0;
2298 /* since we do not have a precise anough audio fifo fullness,
2299 we correct audio sync only if larger than this threshold */
2300 is->audio_diff_threshold = 2.0 * is->audio_hw_buf_size / av_samples_get_buffer_size(NULL, is->audio_tgt.channels, is->audio_tgt.freq, is->audio_tgt.fmt, 1);
2302 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
2303 memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
2304 packet_queue_start(&is->audioq);
2307 case AVMEDIA_TYPE_VIDEO:
2308 is->video_stream = stream_index;
2309 is->video_st = ic->streams[stream_index];
2311 packet_queue_start(&is->videoq);
2312 is->video_tid = SDL_CreateThread(video_thread, is);
2314 case AVMEDIA_TYPE_SUBTITLE:
2315 is->subtitle_stream = stream_index;
2316 is->subtitle_st = ic->streams[stream_index];
2317 packet_queue_start(&is->subtitleq);
2319 is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
2327 static void stream_component_close(VideoState *is, int stream_index)
2329 AVFormatContext *ic = is->ic;
2330 AVCodecContext *avctx;
2332 if (stream_index < 0 || stream_index >= ic->nb_streams)
2334 avctx = ic->streams[stream_index]->codec;
2336 switch (avctx->codec_type) {
2337 case AVMEDIA_TYPE_AUDIO:
2338 packet_queue_abort(&is->audioq);
2342 packet_queue_flush(&is->audioq);
2343 av_free_packet(&is->audio_pkt);
2344 swr_free(&is->swr_ctx);
2345 av_freep(&is->audio_buf1);
2346 is->audio_buf = NULL;
2347 avcodec_free_frame(&is->frame);
2350 av_rdft_end(is->rdft);
2351 av_freep(&is->rdft_data);
2356 case AVMEDIA_TYPE_VIDEO:
2357 packet_queue_abort(&is->videoq);
2359 /* note: we also signal this mutex to make sure we deblock the
2360 video thread in all cases */
2361 SDL_LockMutex(is->pictq_mutex);
2362 SDL_CondSignal(is->pictq_cond);
2363 SDL_UnlockMutex(is->pictq_mutex);
2365 SDL_WaitThread(is->video_tid, NULL);
2367 packet_queue_flush(&is->videoq);
2369 case AVMEDIA_TYPE_SUBTITLE:
2370 packet_queue_abort(&is->subtitleq);
2372 /* note: we also signal this mutex to make sure we deblock the
2373 video thread in all cases */
2374 SDL_LockMutex(is->subpq_mutex);
2375 is->subtitle_stream_changed = 1;
2377 SDL_CondSignal(is->subpq_cond);
2378 SDL_UnlockMutex(is->subpq_mutex);
2380 SDL_WaitThread(is->subtitle_tid, NULL);
2382 packet_queue_flush(&is->subtitleq);
2388 ic->streams[stream_index]->discard = AVDISCARD_ALL;
2389 avcodec_close(avctx);
2391 free_buffer_pool(&is->buffer_pool);
2393 switch (avctx->codec_type) {
2394 case AVMEDIA_TYPE_AUDIO:
2395 is->audio_st = NULL;
2396 is->audio_stream = -1;
2398 case AVMEDIA_TYPE_VIDEO:
2399 is->video_st = NULL;
2400 is->video_stream = -1;
2402 case AVMEDIA_TYPE_SUBTITLE:
2403 is->subtitle_st = NULL;
2404 is->subtitle_stream = -1;
2411 static int decode_interrupt_cb(void *ctx)
2413 VideoState *is = ctx;
2414 return is->abort_request;
2417 static int is_realtime(AVFormatContext *s)
2419 if( !strcmp(s->iformat->name, "rtp")
2420 || !strcmp(s->iformat->name, "rtsp")
2421 || !strcmp(s->iformat->name, "sdp")
2425 if(s->pb && ( !strncmp(s->filename, "rtp:", 4)
2426 || !strncmp(s->filename, "udp:", 4)
2433 /* this thread gets the stream from the disk or the network */
2434 static int read_thread(void *arg)
2436 VideoState *is = arg;
2437 AVFormatContext *ic = NULL;
2439 int st_index[AVMEDIA_TYPE_NB];
2440 AVPacket pkt1, *pkt = &pkt1;
2442 int pkt_in_play_range = 0;
2443 AVDictionaryEntry *t;
2444 AVDictionary **opts;
2445 int orig_nb_streams;
2446 SDL_mutex *wait_mutex = SDL_CreateMutex();
2448 memset(st_index, -1, sizeof(st_index));
2449 is->last_video_stream = is->video_stream = -1;
2450 is->last_audio_stream = is->audio_stream = -1;
2451 is->last_subtitle_stream = is->subtitle_stream = -1;
2453 ic = avformat_alloc_context();
2454 ic->interrupt_callback.callback = decode_interrupt_cb;
2455 ic->interrupt_callback.opaque = is;
2456 err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2458 print_error(is->filename, err);
2462 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2463 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2464 ret = AVERROR_OPTION_NOT_FOUND;
2470 ic->flags |= AVFMT_FLAG_GENPTS;
2472 opts = setup_find_stream_info_opts(ic, codec_opts);
2473 orig_nb_streams = ic->nb_streams;
2475 err = avformat_find_stream_info(ic, opts);
2477 fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
2481 for (i = 0; i < orig_nb_streams; i++)
2482 av_dict_free(&opts[i]);
2486 ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
2488 if (seek_by_bytes < 0)
2489 seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT);
2491 /* if seeking requested, we execute it */
2492 if (start_time != AV_NOPTS_VALUE) {
2495 timestamp = start_time;
2496 /* add the stream start time */
2497 if (ic->start_time != AV_NOPTS_VALUE)
2498 timestamp += ic->start_time;
2499 ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2501 fprintf(stderr, "%s: could not seek to position %0.3f\n",
2502 is->filename, (double)timestamp / AV_TIME_BASE);
2506 for (i = 0; i < ic->nb_streams; i++)
2507 ic->streams[i]->discard = AVDISCARD_ALL;
2509 st_index[AVMEDIA_TYPE_VIDEO] =
2510 av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2511 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2513 st_index[AVMEDIA_TYPE_AUDIO] =
2514 av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
2515 wanted_stream[AVMEDIA_TYPE_AUDIO],
2516 st_index[AVMEDIA_TYPE_VIDEO],
2519 st_index[AVMEDIA_TYPE_SUBTITLE] =
2520 av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
2521 wanted_stream[AVMEDIA_TYPE_SUBTITLE],
2522 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2523 st_index[AVMEDIA_TYPE_AUDIO] :
2524 st_index[AVMEDIA_TYPE_VIDEO]),
2527 av_dump_format(ic, 0, is->filename, 0);
2530 is->show_mode = show_mode;
2532 /* open the streams */
2533 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2534 stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
2538 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2539 ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
2541 is->refresh_tid = SDL_CreateThread(refresh_thread, is);
2542 if (is->show_mode == SHOW_MODE_NONE)
2543 is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
2545 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2546 stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
2549 if (is->video_stream < 0 && is->audio_stream < 0) {
2550 fprintf(stderr, "%s: could not open codecs\n", is->filename);
2555 if (infinite_buffer < 0 && is_realtime(ic))
2556 infinite_buffer = 1;
2559 if (is->abort_request)
2561 if (is->paused != is->last_paused) {
2562 is->last_paused = is->paused;
2564 is->read_pause_return = av_read_pause(ic);
2568 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
2570 (!strcmp(ic->iformat->name, "rtsp") ||
2571 (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
2572 /* wait 10 ms to avoid trying to get another packet */
2579 int64_t seek_target = is->seek_pos;
2580 int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
2581 int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
2582 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
2583 // of the seek_pos/seek_rel variables
2585 ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
2587 fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
2589 if (is->audio_stream >= 0) {
2590 packet_queue_flush(&is->audioq);
2591 packet_queue_put(&is->audioq, &flush_pkt);
2593 if (is->subtitle_stream >= 0) {
2594 packet_queue_flush(&is->subtitleq);
2595 packet_queue_put(&is->subtitleq, &flush_pkt);
2597 if (is->video_stream >= 0) {
2598 packet_queue_flush(&is->videoq);
2599 packet_queue_put(&is->videoq, &flush_pkt);
2602 update_external_clock_pts(is, (seek_target + ic->start_time) / (double)AV_TIME_BASE);
2606 if (is->que_attachments_req) {
2607 avformat_queue_attached_pictures(ic);
2608 is->que_attachments_req = 0;
2611 /* if the queue are full, no need to read more */
2612 if (infinite_buffer<1 &&
2613 (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
2614 || ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
2615 && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request)
2616 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
2618 SDL_LockMutex(wait_mutex);
2619 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2620 SDL_UnlockMutex(wait_mutex);
2624 if (is->video_stream >= 0) {
2625 av_init_packet(pkt);
2628 pkt->stream_index = is->video_stream;
2629 packet_queue_put(&is->videoq, pkt);
2631 if (is->audio_stream >= 0 &&
2632 is->audio_st->codec->codec->capabilities & CODEC_CAP_DELAY) {
2633 av_init_packet(pkt);
2636 pkt->stream_index = is->audio_stream;
2637 packet_queue_put(&is->audioq, pkt);
2640 if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
2641 if (loop != 1 && (!loop || --loop)) {
2642 stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
2643 } else if (autoexit) {
2651 ret = av_read_frame(ic, pkt);
2653 if (ret == AVERROR_EOF || url_feof(ic->pb))
2655 if (ic->pb && ic->pb->error)
2657 SDL_LockMutex(wait_mutex);
2658 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2659 SDL_UnlockMutex(wait_mutex);
2662 /* check if packet is in play range specified by user, then queue, otherwise discard */
2663 pkt_in_play_range = duration == AV_NOPTS_VALUE ||
2664 (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
2665 av_q2d(ic->streams[pkt->stream_index]->time_base) -
2666 (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
2667 <= ((double)duration / 1000000);
2668 if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
2669 packet_queue_put(&is->audioq, pkt);
2670 } else if (pkt->stream_index == is->video_stream && pkt_in_play_range) {
2671 packet_queue_put(&is->videoq, pkt);
2672 } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
2673 packet_queue_put(&is->subtitleq, pkt);
2675 av_free_packet(pkt);
2678 /* wait until the end */
2679 while (!is->abort_request) {
2685 /* close each stream */
2686 if (is->audio_stream >= 0)
2687 stream_component_close(is, is->audio_stream);
2688 if (is->video_stream >= 0)
2689 stream_component_close(is, is->video_stream);
2690 if (is->subtitle_stream >= 0)
2691 stream_component_close(is, is->subtitle_stream);
2693 avformat_close_input(&is->ic);
2699 event.type = FF_QUIT_EVENT;
2700 event.user.data1 = is;
2701 SDL_PushEvent(&event);
2703 SDL_DestroyMutex(wait_mutex);
2707 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
2711 is = av_mallocz(sizeof(VideoState));
2714 av_strlcpy(is->filename, filename, sizeof(is->filename));
2715 is->iformat = iformat;
2719 /* start video display */
2720 is->pictq_mutex = SDL_CreateMutex();
2721 is->pictq_cond = SDL_CreateCond();
2723 is->subpq_mutex = SDL_CreateMutex();
2724 is->subpq_cond = SDL_CreateCond();
2726 packet_queue_init(&is->videoq);
2727 packet_queue_init(&is->audioq);
2728 packet_queue_init(&is->subtitleq);
2730 is->continue_read_thread = SDL_CreateCond();
2732 update_external_clock_pts(is, 0.0);
2733 is->audio_current_pts_drift = -av_gettime() / 1000000.0;
2734 is->video_current_pts_drift = is->audio_current_pts_drift;
2735 is->av_sync_type = av_sync_type;
2736 is->read_tid = SDL_CreateThread(read_thread, is);
2737 if (!is->read_tid) {
2744 static void stream_cycle_channel(VideoState *is, int codec_type)
2746 AVFormatContext *ic = is->ic;
2747 int start_index, stream_index;
2751 if (codec_type == AVMEDIA_TYPE_VIDEO) {
2752 start_index = is->last_video_stream;
2753 old_index = is->video_stream;
2754 } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
2755 start_index = is->last_audio_stream;
2756 old_index = is->audio_stream;
2758 start_index = is->last_subtitle_stream;
2759 old_index = is->subtitle_stream;
2761 stream_index = start_index;
2763 if (++stream_index >= is->ic->nb_streams)
2765 if (codec_type == AVMEDIA_TYPE_SUBTITLE)
2768 is->last_subtitle_stream = -1;
2771 if (start_index == -1)
2775 if (stream_index == start_index)
2777 st = ic->streams[stream_index];
2778 if (st->codec->codec_type == codec_type) {
2779 /* check that parameters are OK */
2780 switch (codec_type) {
2781 case AVMEDIA_TYPE_AUDIO:
2782 if (st->codec->sample_rate != 0 &&
2783 st->codec->channels != 0)
2786 case AVMEDIA_TYPE_VIDEO:
2787 case AVMEDIA_TYPE_SUBTITLE:
2795 stream_component_close(is, old_index);
2796 stream_component_open(is, stream_index);
2797 if (codec_type == AVMEDIA_TYPE_VIDEO)
2798 is->que_attachments_req = 1;
2802 static void toggle_full_screen(VideoState *is)
2804 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
2805 /* OS X needs to reallocate the SDL overlays */
2807 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
2808 is->pictq[i].reallocate = 1;
2810 is_full_screen = !is_full_screen;
2814 static void toggle_pause(VideoState *is)
2816 stream_toggle_pause(is);
2820 static void step_to_next_frame(VideoState *is)
2822 /* if the stream is paused unpause it, then step */
2824 stream_toggle_pause(is);
2828 static void toggle_audio_display(VideoState *is)
2830 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
2831 is->show_mode = (is->show_mode + 1) % SHOW_MODE_NB;
2832 fill_rectangle(screen,
2833 is->xleft, is->ytop, is->width, is->height,
2835 SDL_UpdateRect(screen, is->xleft, is->ytop, is->width, is->height);
2838 /* handle an event sent by the GUI */
2839 static void event_loop(VideoState *cur_stream)
2842 double incr, pos, frac;
2846 SDL_WaitEvent(&event);
2847 switch (event.type) {
2849 if (exit_on_keydown) {
2850 do_exit(cur_stream);
2853 switch (event.key.keysym.sym) {
2856 do_exit(cur_stream);
2859 toggle_full_screen(cur_stream);
2860 cur_stream->force_refresh = 1;
2864 toggle_pause(cur_stream);
2866 case SDLK_s: // S: Step to next frame
2867 step_to_next_frame(cur_stream);
2870 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
2873 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
2876 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
2879 toggle_audio_display(cur_stream);
2880 cur_stream->force_refresh = 1;
2900 if (seek_by_bytes) {
2901 if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
2902 pos = cur_stream->video_current_pos;
2903 } else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
2904 pos = cur_stream->audio_pkt.pos;
2906 pos = avio_tell(cur_stream->ic->pb);
2907 if (cur_stream->ic->bit_rate)
2908 incr *= cur_stream->ic->bit_rate / 8.0;
2912 stream_seek(cur_stream, pos, incr, 1);
2914 pos = get_master_clock(cur_stream);
2916 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
2923 case SDL_VIDEOEXPOSE:
2924 cur_stream->force_refresh = 1;
2926 case SDL_MOUSEBUTTONDOWN:
2927 if (exit_on_mousedown) {
2928 do_exit(cur_stream);
2931 case SDL_MOUSEMOTION:
2932 if (event.type == SDL_MOUSEBUTTONDOWN) {
2935 if (event.motion.state != SDL_PRESSED)
2939 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
2940 uint64_t size = avio_size(cur_stream->ic->pb);
2941 stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
2945 int tns, thh, tmm, tss;
2946 tns = cur_stream->ic->duration / 1000000LL;
2948 tmm = (tns % 3600) / 60;
2950 frac = x / cur_stream->width;
2953 mm = (ns % 3600) / 60;
2955 fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
2956 hh, mm, ss, thh, tmm, tss);
2957 ts = frac * cur_stream->ic->duration;
2958 if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
2959 ts += cur_stream->ic->start_time;
2960 stream_seek(cur_stream, ts, 0, 0);
2963 case SDL_VIDEORESIZE:
2964 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
2965 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
2966 screen_width = cur_stream->width = event.resize.w;
2967 screen_height = cur_stream->height = event.resize.h;
2968 cur_stream->force_refresh = 1;
2972 do_exit(cur_stream);
2974 case FF_ALLOC_EVENT:
2975 alloc_picture(event.user.data1);
2977 case FF_REFRESH_EVENT:
2978 video_refresh(event.user.data1);
2979 cur_stream->refresh = 0;
2987 static int opt_frame_size(void *optctx, const char *opt, const char *arg)
2989 av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
2990 return opt_default(NULL, "video_size", arg);
2993 static int opt_width(void *optctx, const char *opt, const char *arg)
2995 screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
2999 static int opt_height(void *optctx, const char *opt, const char *arg)
3001 screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3005 static int opt_format(void *optctx, const char *opt, const char *arg)
3007 file_iformat = av_find_input_format(arg);
3008 if (!file_iformat) {
3009 fprintf(stderr, "Unknown input format: %s\n", arg);
3010 return AVERROR(EINVAL);
3015 static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
3017 av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3018 return opt_default(NULL, "pixel_format", arg);
3021 static int opt_sync(void *optctx, const char *opt, const char *arg)
3023 if (!strcmp(arg, "audio"))
3024 av_sync_type = AV_SYNC_AUDIO_MASTER;
3025 else if (!strcmp(arg, "video"))
3026 av_sync_type = AV_SYNC_VIDEO_MASTER;
3027 else if (!strcmp(arg, "ext"))
3028 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3030 fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);
3036 static int opt_seek(void *optctx, const char *opt, const char *arg)
3038 start_time = parse_time_or_die(opt, arg, 1);
3042 static int opt_duration(void *optctx, const char *opt, const char *arg)
3044 duration = parse_time_or_die(opt, arg, 1);
3048 static int opt_show_mode(void *optctx, const char *opt, const char *arg)
3050 show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3051 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3052 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
3053 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3057 static void opt_input_file(void *optctx, const char *filename)
3059 if (input_filename) {
3060 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3061 filename, input_filename);
3064 if (!strcmp(filename, "-"))
3066 input_filename = filename;
3069 static int opt_codec(void *o, const char *opt, const char *arg)
3071 switch(opt[strlen(opt)-1]){
3072 case 'a' : audio_codec_name = arg; break;
3073 case 's' : subtitle_codec_name = arg; break;
3074 case 'v' : video_codec_name = arg; break;
3081 static const OptionDef options[] = {
3082 #include "cmdutils_common_opts.h"
3083 { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
3084 { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
3085 { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3086 { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
3087 { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
3088 { "vn", OPT_BOOL, { &video_disable }, "disable video" },
3089 { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
3090 { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
3091 { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
3092 { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
3093 { "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
3094 { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3095 { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
3096 { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
3097 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
3098 { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
3099 { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
3100 { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
3101 { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
3102 { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3103 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
3104 { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_loop_filter }, "", "" },
3105 { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_frame }, "", "" },
3106 { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_idct }, "", "" },
3107 { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { &idct }, "set idct algo", "algo" },
3108 { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options", "bit_mask" },
3109 { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3110 { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
3111 { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
3112 { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
3113 { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
3114 { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
3115 { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
3116 { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
3118 { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "video filters", "filter list" },
3120 { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
3121 { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3122 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
3123 { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
3124 { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder" },
3128 static void show_usage(void)
3130 av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3131 av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3132 av_log(NULL, AV_LOG_INFO, "\n");
3135 void show_help_default(const char *opt, const char *arg)
3137 av_log_set_callback(log_callback_help);
3139 show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
3140 show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3142 show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3143 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3144 #if !CONFIG_AVFILTER
3145 show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3147 show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
3149 printf("\nWhile playing:\n"
3151 "f toggle full screen\n"
3153 "a cycle audio channel\n"
3154 "v cycle video channel\n"
3155 "t cycle subtitle channel\n"
3156 "w show audio waves\n"
3157 "s activate frame-step mode\n"
3158 "left/right seek backward/forward 10 seconds\n"
3159 "down/up seek backward/forward 1 minute\n"
3160 "page down/page up seek backward/forward 10 minutes\n"
3161 "mouse click seek to percentage in file corresponding to fraction of width\n"
3165 static int lockmgr(void **mtx, enum AVLockOp op)
3168 case AV_LOCK_CREATE:
3169 *mtx = SDL_CreateMutex();
3173 case AV_LOCK_OBTAIN:
3174 return !!SDL_LockMutex(*mtx);
3175 case AV_LOCK_RELEASE:
3176 return !!SDL_UnlockMutex(*mtx);
3177 case AV_LOCK_DESTROY:
3178 SDL_DestroyMutex(*mtx);
3184 /* Called from the main */
3185 int main(int argc, char **argv)
3189 char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
3191 av_log_set_flags(AV_LOG_SKIP_REPEATED);
3192 parse_loglevel(argc, argv, options);
3194 /* register all codecs, demux and protocols */
3195 avcodec_register_all();
3197 avdevice_register_all();
3200 avfilter_register_all();
3203 avformat_network_init();
3207 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
3208 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
3210 show_banner(argc, argv, options);
3212 parse_options(NULL, argc, argv, options, opt_input_file);
3214 if (!input_filename) {
3216 fprintf(stderr, "An input file must be specified\n");
3217 fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3221 if (display_disable) {
3224 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3226 flags &= ~SDL_INIT_AUDIO;
3227 if (display_disable)
3228 SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
3229 #if !defined(__MINGW32__) && !defined(__APPLE__)
3230 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3232 if (SDL_Init (flags)) {
3233 fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
3234 fprintf(stderr, "(Did you set the DISPLAY variable?)\n");
3238 if (!display_disable) {
3239 #if HAVE_SDL_VIDEO_SIZE
3240 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3241 fs_screen_width = vi->current_w;
3242 fs_screen_height = vi->current_h;
3246 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3247 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3248 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3250 if (av_lockmgr_register(lockmgr)) {
3251 fprintf(stderr, "Could not initialize lock manager!\n");
3255 av_init_packet(&flush_pkt);
3256 flush_pkt.data = (char *)(intptr_t)"FLUSH";
3258 is = stream_open(input_filename, file_iformat);
3260 fprintf(stderr, "Failed to initialize VideoState!\n");