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 "libavformat/avformat.h"
41 #include "libavdevice/avdevice.h"
42 #include "libswscale/swscale.h"
43 #include "libavutil/opt.h"
44 #include "libavcodec/avfft.h"
45 #include "libswresample/swresample.h"
48 # include "libavfilter/avcodec.h"
49 # include "libavfilter/avfilter.h"
50 # include "libavfilter/avfiltergraph.h"
51 # include "libavfilter/buffersink.h"
55 #include <SDL_thread.h>
62 const char program_name[] = "ffplay";
63 const int program_birth_year = 2003;
65 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
68 /* SDL audio buffer size, in samples. Should be small to have precise
69 A/V sync as SDL does not have hardware buffer fullness info. */
70 #define SDL_AUDIO_BUFFER_SIZE 1024
72 /* no AV sync correction is done if below the AV sync threshold */
73 #define AV_SYNC_THRESHOLD 0.01
74 /* no AV correction is done if too big error */
75 #define AV_NOSYNC_THRESHOLD 10.0
77 /* maximum audio speed change to get correct sync */
78 #define SAMPLE_CORRECTION_PERCENT_MAX 10
80 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
81 #define AUDIO_DIFF_AVG_NB 20
83 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
84 #define SAMPLE_ARRAY_SIZE (2 * 65536)
86 static int sws_flags = SWS_BICUBIC;
88 typedef struct PacketQueue {
89 AVPacketList *first_pkt, *last_pkt;
97 #define VIDEO_PICTURE_QUEUE_SIZE 2
98 #define SUBPICTURE_QUEUE_SIZE 4
100 typedef struct VideoPicture {
101 double pts; ///< presentation time stamp for this picture
102 int64_t pos; ///< byte position in file
105 int width, height; /* source height & width */
106 AVRational sample_aspect_ratio;
111 AVFilterBufferRef *picref;
115 typedef struct SubPicture {
116 double pts; /* presentation time stamp for this picture */
120 typedef struct AudioParams {
124 enum AVSampleFormat fmt;
128 AV_SYNC_AUDIO_MASTER, /* default choice */
129 AV_SYNC_VIDEO_MASTER,
130 AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
133 typedef struct VideoState {
134 SDL_Thread *read_tid;
135 SDL_Thread *video_tid;
136 SDL_Thread *refresh_tid;
137 AVInputFormat *iformat;
147 int read_pause_return;
153 double external_clock; /* external clock base */
154 int64_t external_clock_time;
157 double audio_diff_cum; /* used for AV difference average computation */
158 double audio_diff_avg_coef;
159 double audio_diff_threshold;
160 int audio_diff_avg_count;
163 int audio_hw_buf_size;
164 DECLARE_ALIGNED(16,uint8_t,audio_buf2)[AVCODEC_MAX_AUDIO_FRAME_SIZE * 4];
165 uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
168 unsigned int audio_buf_size; /* in bytes */
169 int audio_buf_index; /* in bytes */
170 int audio_write_buf_size;
171 AVPacket audio_pkt_temp;
173 struct AudioParams audio_src;
174 struct AudioParams audio_tgt;
175 struct SwrContext *swr_ctx;
176 double audio_current_pts;
177 double audio_current_pts_drift;
178 int frame_drops_early;
179 int frame_drops_late;
183 SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
185 int16_t sample_array[SAMPLE_ARRAY_SIZE];
186 int sample_array_index;
190 FFTSample *rdft_data;
193 SDL_Thread *subtitle_tid;
195 int subtitle_stream_changed;
196 AVStream *subtitle_st;
197 PacketQueue subtitleq;
198 SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
199 int subpq_size, subpq_rindex, subpq_windex;
200 SDL_mutex *subpq_mutex;
201 SDL_cond *subpq_cond;
204 double frame_last_pts;
205 double frame_last_duration;
206 double frame_last_dropped_pts;
207 double frame_last_returned_time;
208 double frame_last_filter_delay;
209 int64_t frame_last_dropped_pos;
210 double video_clock; ///< pts of last decoded frame / predicted pts of next decoded frame
214 double video_current_pts; ///< current displayed pts (different from video_clock if frame fifos are used)
215 double video_current_pts_drift; ///< video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
216 int64_t video_current_pos; ///< current displayed file pos
217 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
218 int pictq_size, pictq_rindex, pictq_windex;
219 SDL_mutex *pictq_mutex;
220 SDL_cond *pictq_cond;
222 struct SwsContext *img_convert_ctx;
226 int width, height, xleft, ytop;
230 AVFilterContext *out_video_filter; ///< the last filter in the video chain
234 int last_video_stream, last_audio_stream, last_subtitle_stream;
237 typedef struct AllocEventProps {
242 static int opt_help(const char *opt, const char *arg);
244 /* options specified by the user */
245 static AVInputFormat *file_iformat;
246 static const char *input_filename;
247 static const char *window_title;
248 static int fs_screen_width;
249 static int fs_screen_height;
250 static int screen_width = 0;
251 static int screen_height = 0;
252 static int audio_disable;
253 static int video_disable;
254 static int wanted_stream[AVMEDIA_TYPE_NB] = {
255 [AVMEDIA_TYPE_AUDIO] = -1,
256 [AVMEDIA_TYPE_VIDEO] = -1,
257 [AVMEDIA_TYPE_SUBTITLE] = -1,
259 static int seek_by_bytes = -1;
260 static int display_disable;
261 static int show_status = 1;
262 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
263 static int64_t start_time = AV_NOPTS_VALUE;
264 static int64_t duration = AV_NOPTS_VALUE;
265 static int workaround_bugs = 1;
267 static int genpts = 0;
268 static int lowres = 0;
269 static int idct = FF_IDCT_AUTO;
270 static enum AVDiscard skip_frame = AVDISCARD_DEFAULT;
271 static enum AVDiscard skip_idct = AVDISCARD_DEFAULT;
272 static enum AVDiscard skip_loop_filter = AVDISCARD_DEFAULT;
273 static int error_concealment = 3;
274 static int decoder_reorder_pts = -1;
276 static int exit_on_keydown;
277 static int exit_on_mousedown;
279 static int framedrop = -1;
280 static enum ShowMode show_mode = SHOW_MODE_NONE;
281 static const char *audio_codec_name;
282 static const char *subtitle_codec_name;
283 static const char *video_codec_name;
284 static int rdftspeed = 20;
286 static char *vfilters = NULL;
289 /* current context */
290 static int is_full_screen;
291 static int64_t audio_callback_time;
293 static AVPacket flush_pkt;
295 #define FF_ALLOC_EVENT (SDL_USEREVENT)
296 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
297 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
299 static SDL_Surface *screen;
301 void av_noreturn exit_program(int ret)
306 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
310 if (q->abort_request)
313 pkt1 = av_malloc(sizeof(AVPacketList));
322 q->last_pkt->next = pkt1;
325 q->size += pkt1->pkt.size + sizeof(*pkt1);
326 /* XXX: should duplicate packet data in DV case */
327 SDL_CondSignal(q->cond);
331 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
335 /* duplicate the packet */
336 if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
339 SDL_LockMutex(q->mutex);
340 ret = packet_queue_put_private(q, pkt);
341 SDL_UnlockMutex(q->mutex);
343 if (pkt != &flush_pkt && ret < 0)
349 /* packet queue handling */
350 static void packet_queue_init(PacketQueue *q)
352 memset(q, 0, sizeof(PacketQueue));
353 q->mutex = SDL_CreateMutex();
354 q->cond = SDL_CreateCond();
355 q->abort_request = 1;
358 static void packet_queue_flush(PacketQueue *q)
360 AVPacketList *pkt, *pkt1;
362 SDL_LockMutex(q->mutex);
363 for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
365 av_free_packet(&pkt->pkt);
372 SDL_UnlockMutex(q->mutex);
375 static void packet_queue_destroy(PacketQueue *q)
377 packet_queue_flush(q);
378 SDL_DestroyMutex(q->mutex);
379 SDL_DestroyCond(q->cond);
382 static void packet_queue_abort(PacketQueue *q)
384 SDL_LockMutex(q->mutex);
386 q->abort_request = 1;
388 SDL_CondSignal(q->cond);
390 SDL_UnlockMutex(q->mutex);
393 static void packet_queue_start(PacketQueue *q)
395 SDL_LockMutex(q->mutex);
396 q->abort_request = 0;
397 packet_queue_put_private(q, &flush_pkt);
398 SDL_UnlockMutex(q->mutex);
401 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
402 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
407 SDL_LockMutex(q->mutex);
410 if (q->abort_request) {
417 q->first_pkt = pkt1->next;
421 q->size -= pkt1->pkt.size + sizeof(*pkt1);
430 SDL_CondWait(q->cond, q->mutex);
433 SDL_UnlockMutex(q->mutex);
437 static inline void fill_rectangle(SDL_Surface *screen,
438 int x, int y, int w, int h, int color)
445 SDL_FillRect(screen, &rect, color);
448 #define ALPHA_BLEND(a, oldp, newp, s)\
449 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
451 #define RGBA_IN(r, g, b, a, s)\
453 unsigned int v = ((const uint32_t *)(s))[0];\
454 a = (v >> 24) & 0xff;\
455 r = (v >> 16) & 0xff;\
456 g = (v >> 8) & 0xff;\
460 #define YUVA_IN(y, u, v, a, s, pal)\
462 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
463 a = (val >> 24) & 0xff;\
464 y = (val >> 16) & 0xff;\
465 u = (val >> 8) & 0xff;\
469 #define YUVA_OUT(d, y, u, v, a)\
471 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
477 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
479 int wrap, wrap3, width2, skip2;
480 int y, u, v, a, u1, v1, a1, w, h;
481 uint8_t *lum, *cb, *cr;
484 int dstx, dsty, dstw, dsth;
486 dstw = av_clip(rect->w, 0, imgw);
487 dsth = av_clip(rect->h, 0, imgh);
488 dstx = av_clip(rect->x, 0, imgw - dstw);
489 dsty = av_clip(rect->y, 0, imgh - dsth);
490 lum = dst->data[0] + dsty * dst->linesize[0];
491 cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
492 cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
494 width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
496 wrap = dst->linesize[0];
497 wrap3 = rect->pict.linesize[0];
498 p = rect->pict.data[0];
499 pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
507 YUVA_IN(y, u, v, a, p, pal);
508 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
509 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
510 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
516 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
517 YUVA_IN(y, u, v, a, p, pal);
521 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
523 YUVA_IN(y, u, v, a, p + BPP, pal);
527 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
528 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
529 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
536 YUVA_IN(y, u, v, a, p, pal);
537 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
538 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
539 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
543 p += wrap3 - dstw * BPP;
544 lum += wrap - dstw - dstx;
545 cb += dst->linesize[1] - width2 - skip2;
546 cr += dst->linesize[2] - width2 - skip2;
548 for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
554 YUVA_IN(y, u, v, a, p, pal);
558 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
561 YUVA_IN(y, u, v, a, p, pal);
565 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
566 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
567 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
573 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
574 YUVA_IN(y, u, v, a, p, pal);
578 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
580 YUVA_IN(y, u, v, a, p + BPP, pal);
584 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
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);
600 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
601 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
605 p += -wrap3 + 2 * BPP;
609 YUVA_IN(y, u, v, a, p, pal);
613 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
616 YUVA_IN(y, u, v, a, p, pal);
620 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
621 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
622 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
628 p += wrap3 + (wrap3 - dstw * BPP);
629 lum += wrap + (wrap - dstw - dstx);
630 cb += dst->linesize[1] - width2 - skip2;
631 cr += dst->linesize[2] - width2 - skip2;
633 /* handle odd height */
640 YUVA_IN(y, u, v, a, p, pal);
641 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
642 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
643 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
649 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
650 YUVA_IN(y, u, v, a, p, pal);
654 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
656 YUVA_IN(y, u, v, a, p + BPP, pal);
660 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
661 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
662 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
669 YUVA_IN(y, u, v, a, p, pal);
670 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
671 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
672 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
677 static void free_subpicture(SubPicture *sp)
679 avsubtitle_free(&sp->sub);
682 static void video_image_display(VideoState *is)
688 int width, height, x, y;
692 vp = &is->pictq[is->pictq_rindex];
694 if (vp->sample_aspect_ratio.num == 0)
697 aspect_ratio = av_q2d(vp->sample_aspect_ratio);
699 if (aspect_ratio <= 0.0)
701 aspect_ratio *= (float)vp->width / (float)vp->height;
703 if (is->subtitle_st) {
704 if (is->subpq_size > 0) {
705 sp = &is->subpq[is->subpq_rindex];
707 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
708 SDL_LockYUVOverlay (vp->bmp);
710 pict.data[0] = vp->bmp->pixels[0];
711 pict.data[1] = vp->bmp->pixels[2];
712 pict.data[2] = vp->bmp->pixels[1];
714 pict.linesize[0] = vp->bmp->pitches[0];
715 pict.linesize[1] = vp->bmp->pitches[2];
716 pict.linesize[2] = vp->bmp->pitches[1];
718 for (i = 0; i < sp->sub.num_rects; i++)
719 blend_subrect(&pict, sp->sub.rects[i],
720 vp->bmp->w, vp->bmp->h);
722 SDL_UnlockYUVOverlay (vp->bmp);
728 /* XXX: we suppose the screen has a 1.0 pixel ratio */
730 width = ((int)rint(height * aspect_ratio)) & ~1;
731 if (width > is->width) {
733 height = ((int)rint(width / aspect_ratio)) & ~1;
735 x = (is->width - width) / 2;
736 y = (is->height - height) / 2;
737 is->no_background = 0;
738 rect.x = is->xleft + x;
739 rect.y = is->ytop + y;
740 rect.w = FFMAX(width, 1);
741 rect.h = FFMAX(height, 1);
742 SDL_DisplayYUVOverlay(vp->bmp, &rect);
746 static inline int compute_mod(int a, int b)
748 return a < 0 ? a%b + b : a%b;
751 static void video_audio_display(VideoState *s)
753 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
754 int ch, channels, h, h2, bgcolor, fgcolor;
756 int rdft_bits, nb_freq;
758 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
760 nb_freq = 1 << (rdft_bits - 1);
762 /* compute display index : center on currently output samples */
763 channels = s->audio_tgt.channels;
764 nb_display_channels = channels;
766 int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
768 delay = s->audio_write_buf_size;
771 /* to be more precise, we take into account the time spent since
772 the last buffer computation */
773 if (audio_callback_time) {
774 time_diff = av_gettime() - audio_callback_time;
775 delay -= (time_diff * s->audio_tgt.freq) / 1000000;
778 delay += 2 * data_used;
779 if (delay < data_used)
782 i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
783 if (s->show_mode == SHOW_MODE_WAVES) {
785 for (i = 0; i < 1000; i += channels) {
786 int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
787 int a = s->sample_array[idx];
788 int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
789 int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
790 int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
792 if (h < score && (b ^ c) < 0) {
799 s->last_i_start = i_start;
801 i_start = s->last_i_start;
804 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
805 if (s->show_mode == SHOW_MODE_WAVES) {
806 fill_rectangle(screen,
807 s->xleft, s->ytop, s->width, s->height,
810 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
812 /* total height for one channel */
813 h = s->height / nb_display_channels;
814 /* graph height / 2 */
816 for (ch = 0; ch < nb_display_channels; ch++) {
818 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
819 for (x = 0; x < s->width; x++) {
820 y = (s->sample_array[i] * h2) >> 15;
827 fill_rectangle(screen,
828 s->xleft + x, ys, 1, y,
831 if (i >= SAMPLE_ARRAY_SIZE)
832 i -= SAMPLE_ARRAY_SIZE;
836 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
838 for (ch = 1; ch < nb_display_channels; ch++) {
839 y = s->ytop + ch * h;
840 fill_rectangle(screen,
841 s->xleft, y, s->width, 1,
844 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
846 nb_display_channels= FFMIN(nb_display_channels, 2);
847 if (rdft_bits != s->rdft_bits) {
848 av_rdft_end(s->rdft);
849 av_free(s->rdft_data);
850 s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
851 s->rdft_bits = rdft_bits;
852 s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data));
856 for (ch = 0; ch < nb_display_channels; ch++) {
857 data[ch] = s->rdft_data + 2 * nb_freq * ch;
859 for (x = 0; x < 2 * nb_freq; x++) {
860 double w = (x-nb_freq) * (1.0 / nb_freq);
861 data[ch][x] = s->sample_array[i] * (1.0 - w * w);
863 if (i >= SAMPLE_ARRAY_SIZE)
864 i -= SAMPLE_ARRAY_SIZE;
866 av_rdft_calc(s->rdft, data[ch]);
868 // least efficient way to do this, we should of course directly access it but its more than fast enough
869 for (y = 0; y < s->height; y++) {
870 double w = 1 / sqrt(nb_freq);
871 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]));
872 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
873 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
876 fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
878 fill_rectangle(screen,
879 s->xpos, s->height-y, 1, 1,
883 SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
886 if (s->xpos >= s->width)
891 static void stream_close(VideoState *is)
895 /* XXX: use a special url_shutdown call to abort parse cleanly */
896 is->abort_request = 1;
897 SDL_WaitThread(is->read_tid, NULL);
898 SDL_WaitThread(is->refresh_tid, NULL);
899 packet_queue_destroy(&is->videoq);
900 packet_queue_destroy(&is->audioq);
901 packet_queue_destroy(&is->subtitleq);
903 /* free all pictures */
904 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
908 avfilter_unref_buffer(vp->picref);
913 SDL_FreeYUVOverlay(vp->bmp);
917 SDL_DestroyMutex(is->pictq_mutex);
918 SDL_DestroyCond(is->pictq_cond);
919 SDL_DestroyMutex(is->subpq_mutex);
920 SDL_DestroyCond(is->subpq_cond);
922 if (is->img_convert_ctx)
923 sws_freeContext(is->img_convert_ctx);
928 static void do_exit(VideoState *is)
933 av_lockmgr_register(NULL);
938 avformat_network_deinit();
942 av_log(NULL, AV_LOG_QUIET, "%s", "");
946 static void sigterm_handler(int sig)
951 static int video_open(VideoState *is, int force_set_video_mode)
953 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
955 VideoPicture *vp = &is->pictq[is->pictq_rindex];
957 if (is_full_screen) flags |= SDL_FULLSCREEN;
958 else flags |= SDL_RESIZABLE;
960 if (is_full_screen && fs_screen_width) {
962 h = fs_screen_height;
963 } else if (!is_full_screen && screen_width) {
966 } else if (vp->width) {
973 if (screen && is->width == screen->w && screen->w == w
974 && is->height== screen->h && screen->h == h && !force_set_video_mode)
976 screen = SDL_SetVideoMode(w, h, 0, flags);
978 fprintf(stderr, "SDL: could not set video mode - exiting\n");
982 window_title = input_filename;
983 SDL_WM_SetCaption(window_title, window_title);
985 is->width = screen->w;
986 is->height = screen->h;
991 /* display the current picture, if any */
992 static void video_display(VideoState *is)
996 if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
997 video_audio_display(is);
998 else if (is->video_st)
999 video_image_display(is);
1002 static int refresh_thread(void *opaque)
1004 VideoState *is= opaque;
1005 while (!is->abort_request) {
1007 event.type = FF_REFRESH_EVENT;
1008 event.user.data1 = opaque;
1009 if (!is->refresh && (!is->paused || is->force_refresh)) {
1011 SDL_PushEvent(&event);
1013 //FIXME ideally we should wait the correct time but SDLs event passing is so slow it would be silly
1014 usleep(is->audio_st && is->show_mode != SHOW_MODE_VIDEO ? rdftspeed*1000 : 5000);
1019 /* get the current audio clock value */
1020 static double get_audio_clock(VideoState *is)
1023 return is->audio_current_pts;
1025 return is->audio_current_pts_drift + av_gettime() / 1000000.0;
1029 /* get the current video clock value */
1030 static double get_video_clock(VideoState *is)
1033 return is->video_current_pts;
1035 return is->video_current_pts_drift + av_gettime() / 1000000.0;
1039 /* get the current external clock value */
1040 static double get_external_clock(VideoState *is)
1044 return is->external_clock + ((ti - is->external_clock_time) * 1e-6);
1047 /* get the current master clock value */
1048 static double get_master_clock(VideoState *is)
1052 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
1054 val = get_video_clock(is);
1056 val = get_audio_clock(is);
1057 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
1059 val = get_audio_clock(is);
1061 val = get_video_clock(is);
1063 val = get_external_clock(is);
1068 /* seek in the stream */
1069 static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
1071 if (!is->seek_req) {
1074 is->seek_flags &= ~AVSEEK_FLAG_BYTE;
1076 is->seek_flags |= AVSEEK_FLAG_BYTE;
1081 /* pause or resume the video */
1082 static void stream_toggle_pause(VideoState *is)
1085 is->frame_timer += av_gettime() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
1086 if (is->read_pause_return != AVERROR(ENOSYS)) {
1087 is->video_current_pts = is->video_current_pts_drift + av_gettime() / 1000000.0;
1089 is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
1091 is->paused = !is->paused;
1094 static double compute_target_delay(double delay, VideoState *is)
1096 double sync_threshold, diff;
1098 /* update delay to follow master synchronisation source */
1099 if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) ||
1100 is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
1101 /* if video is slave, we try to correct big delays by
1102 duplicating or deleting a frame */
1103 diff = get_video_clock(is) - get_master_clock(is);
1105 /* skip or repeat frame. We take into account the
1106 delay to compute the threshold. I still don't know
1107 if it is the best guess */
1108 sync_threshold = FFMAX(AV_SYNC_THRESHOLD, delay);
1109 if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
1110 if (diff <= -sync_threshold)
1112 else if (diff >= sync_threshold)
1117 av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
1123 static void pictq_next_picture(VideoState *is) {
1124 /* update queue size and signal for next picture */
1125 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
1126 is->pictq_rindex = 0;
1128 SDL_LockMutex(is->pictq_mutex);
1130 SDL_CondSignal(is->pictq_cond);
1131 SDL_UnlockMutex(is->pictq_mutex);
1134 static void update_video_pts(VideoState *is, double pts, int64_t pos) {
1135 double time = av_gettime() / 1000000.0;
1136 /* update current video pts */
1137 is->video_current_pts = pts;
1138 is->video_current_pts_drift = is->video_current_pts - time;
1139 is->video_current_pos = pos;
1140 is->frame_last_pts = pts;
1143 /* called to display each frame */
1144 static void video_refresh(void *opaque)
1146 VideoState *is = opaque;
1150 SubPicture *sp, *sp2;
1154 if (is->pictq_size == 0) {
1155 SDL_LockMutex(is->pictq_mutex);
1156 if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
1157 update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos);
1158 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1160 SDL_UnlockMutex(is->pictq_mutex);
1161 // nothing to do, no picture to display in the que
1163 double last_duration, duration, delay;
1164 /* dequeue the picture */
1165 vp = &is->pictq[is->pictq_rindex];
1168 pictq_next_picture(is);
1175 /* compute nominal last_duration */
1176 last_duration = vp->pts - is->frame_last_pts;
1177 if (last_duration > 0 && last_duration < 10.0) {
1178 /* if duration of the last frame was sane, update last_duration in video state */
1179 is->frame_last_duration = last_duration;
1181 delay = compute_target_delay(is->frame_last_duration, is);
1183 time= av_gettime()/1000000.0;
1184 if (time < is->frame_timer + delay)
1188 is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
1190 SDL_LockMutex(is->pictq_mutex);
1191 update_video_pts(is, vp->pts, vp->pos);
1192 SDL_UnlockMutex(is->pictq_mutex);
1194 if (is->pictq_size > 1) {
1195 VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
1196 duration = nextvp->pts - vp->pts;
1197 if((framedrop>0 || (framedrop && is->audio_st)) && time > is->frame_timer + duration){
1198 is->frame_drops_late++;
1199 pictq_next_picture(is);
1204 if (is->subtitle_st) {
1205 if (is->subtitle_stream_changed) {
1206 SDL_LockMutex(is->subpq_mutex);
1208 while (is->subpq_size) {
1209 free_subpicture(&is->subpq[is->subpq_rindex]);
1211 /* update queue size and signal for next picture */
1212 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1213 is->subpq_rindex = 0;
1217 is->subtitle_stream_changed = 0;
1219 SDL_CondSignal(is->subpq_cond);
1220 SDL_UnlockMutex(is->subpq_mutex);
1222 if (is->subpq_size > 0) {
1223 sp = &is->subpq[is->subpq_rindex];
1225 if (is->subpq_size > 1)
1226 sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
1230 if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1231 || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1233 free_subpicture(sp);
1235 /* update queue size and signal for next picture */
1236 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1237 is->subpq_rindex = 0;
1239 SDL_LockMutex(is->subpq_mutex);
1241 SDL_CondSignal(is->subpq_cond);
1242 SDL_UnlockMutex(is->subpq_mutex);
1249 /* display picture */
1250 if (!display_disable)
1254 pictq_next_picture(is);
1256 } else if (is->audio_st) {
1257 /* draw the next audio frame */
1259 /* if only audio stream, then display the audio bars (better
1260 than nothing, just to test the implementation */
1262 /* display picture */
1263 if (!display_disable)
1266 is->force_refresh = 0;
1268 static int64_t last_time;
1270 int aqsize, vqsize, sqsize;
1273 cur_time = av_gettime();
1274 if (!last_time || (cur_time - last_time) >= 30000) {
1279 aqsize = is->audioq.size;
1281 vqsize = is->videoq.size;
1282 if (is->subtitle_st)
1283 sqsize = is->subtitleq.size;
1285 if (is->audio_st && is->video_st)
1286 av_diff = get_audio_clock(is) - get_video_clock(is);
1287 printf("%7.2f A-V:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
1288 get_master_clock(is),
1290 is->frame_drops_early + is->frame_drops_late,
1294 is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1295 is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1297 last_time = cur_time;
1302 /* allocate a picture (needs to do that in main thread to avoid
1303 potential locking problems */
1304 static void alloc_picture(AllocEventProps *event_props)
1306 VideoState *is = event_props->is;
1307 AVFrame *frame = event_props->frame;
1310 vp = &is->pictq[is->pictq_windex];
1313 SDL_FreeYUVOverlay(vp->bmp);
1317 avfilter_unref_buffer(vp->picref);
1321 vp->width = frame->width;
1322 vp->height = frame->height;
1324 video_open(event_props->is, 0);
1326 vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1329 if (!vp->bmp || vp->bmp->pitches[0] < vp->width) {
1330 /* SDL allocates a buffer smaller than requested if the video
1331 * overlay hardware is unable to support the requested size. */
1332 fprintf(stderr, "Error: the video system does not support an image\n"
1333 "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1334 "to reduce the image size.\n", vp->width, vp->height );
1338 SDL_LockMutex(is->pictq_mutex);
1340 SDL_CondSignal(is->pictq_cond);
1341 SDL_UnlockMutex(is->pictq_mutex);
1344 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts1, int64_t pos)
1347 double frame_delay, pts = pts1;
1349 /* compute the exact PTS for the picture if it is omitted in the stream
1350 * pts1 is the dts of the pkt / pts of the frame */
1352 /* update video clock with pts, if present */
1353 is->video_clock = pts;
1355 pts = is->video_clock;
1357 /* update video clock for next frame */
1358 frame_delay = av_q2d(is->video_st->codec->time_base);
1359 /* for MPEG2, the frame can be repeated, so we update the
1360 clock accordingly */
1361 frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
1362 is->video_clock += frame_delay;
1364 #if defined(DEBUG_SYNC) && 0
1365 printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
1366 av_get_picture_type_char(src_frame->pict_type), pts, pts1);
1369 /* wait until we have space to put a new picture */
1370 SDL_LockMutex(is->pictq_mutex);
1372 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
1373 !is->videoq.abort_request) {
1374 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1376 SDL_UnlockMutex(is->pictq_mutex);
1378 if (is->videoq.abort_request)
1381 vp = &is->pictq[is->pictq_windex];
1383 /* alloc or resize hardware picture buffer */
1384 if (!vp->bmp || vp->reallocate ||
1385 vp->width != src_frame->width ||
1386 vp->height != src_frame->height) {
1388 AllocEventProps event_props;
1390 event_props.frame = src_frame;
1391 event_props.is = is;
1396 /* the allocation must be done in the main thread to avoid
1397 locking problems. We wait in this block for the event to complete,
1398 so we can pass a pointer to event_props to it. */
1399 event.type = FF_ALLOC_EVENT;
1400 event.user.data1 = &event_props;
1401 SDL_PushEvent(&event);
1403 /* wait until the picture is allocated */
1404 SDL_LockMutex(is->pictq_mutex);
1405 while (!vp->allocated && !is->videoq.abort_request) {
1406 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1408 /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1409 if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1410 while (!vp->allocated) {
1411 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1414 SDL_UnlockMutex(is->pictq_mutex);
1416 if (is->videoq.abort_request)
1420 /* if the frame is not skipped, then display it */
1422 AVPicture pict = { { 0 } };
1425 avfilter_unref_buffer(vp->picref);
1426 vp->picref = src_frame->opaque;
1429 /* get a pointer on the bitmap */
1430 SDL_LockYUVOverlay (vp->bmp);
1432 pict.data[0] = vp->bmp->pixels[0];
1433 pict.data[1] = vp->bmp->pixels[2];
1434 pict.data[2] = vp->bmp->pixels[1];
1436 pict.linesize[0] = vp->bmp->pitches[0];
1437 pict.linesize[1] = vp->bmp->pitches[2];
1438 pict.linesize[2] = vp->bmp->pitches[1];
1441 // FIXME use direct rendering
1442 av_picture_copy(&pict, (AVPicture *)src_frame,
1443 src_frame->format, vp->width, vp->height);
1444 vp->sample_aspect_ratio = vp->picref->video->sample_aspect_ratio;
1446 sws_flags = av_get_int(sws_opts, "sws_flags", NULL);
1447 is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1448 vp->width, vp->height, src_frame->format, vp->width, vp->height,
1449 PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1450 if (is->img_convert_ctx == NULL) {
1451 fprintf(stderr, "Cannot initialize the conversion context\n");
1454 sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1455 0, vp->height, pict.data, pict.linesize);
1456 vp->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, src_frame);
1458 /* update the bitmap content */
1459 SDL_UnlockYUVOverlay(vp->bmp);
1465 /* now we can update the picture count */
1466 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
1467 is->pictq_windex = 0;
1468 SDL_LockMutex(is->pictq_mutex);
1470 SDL_UnlockMutex(is->pictq_mutex);
1475 static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt)
1479 if (packet_queue_get(&is->videoq, pkt, 1) < 0)
1482 if (pkt->data == flush_pkt.data) {
1483 avcodec_flush_buffers(is->video_st->codec);
1485 SDL_LockMutex(is->pictq_mutex);
1486 // Make sure there are no long delay timers (ideally we should just flush the que but thats harder)
1487 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
1488 is->pictq[i].skip = 1;
1490 while (is->pictq_size && !is->videoq.abort_request) {
1491 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1493 is->video_current_pos = -1;
1494 is->frame_last_pts = AV_NOPTS_VALUE;
1495 is->frame_last_duration = 0;
1496 is->frame_timer = (double)av_gettime() / 1000000.0;
1497 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1498 SDL_UnlockMutex(is->pictq_mutex);
1503 avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt);
1508 if (decoder_reorder_pts == -1) {
1509 *pts = av_frame_get_best_effort_timestamp(frame);
1510 } else if (decoder_reorder_pts) {
1511 *pts = frame->pkt_pts;
1513 *pts = frame->pkt_dts;
1516 if (*pts == AV_NOPTS_VALUE) {
1520 if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) || is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK) &&
1521 (framedrop>0 || (framedrop && is->audio_st))) {
1522 SDL_LockMutex(is->pictq_mutex);
1523 if (is->frame_last_pts != AV_NOPTS_VALUE && *pts) {
1524 double clockdiff = get_video_clock(is) - get_master_clock(is);
1525 double dpts = av_q2d(is->video_st->time_base) * *pts;
1526 double ptsdiff = dpts - is->frame_last_pts;
1527 if (fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
1528 ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
1529 clockdiff + ptsdiff - is->frame_last_filter_delay < 0) {
1530 is->frame_last_dropped_pos = pkt->pos;
1531 is->frame_last_dropped_pts = dpts;
1532 is->frame_drops_early++;
1536 SDL_UnlockMutex(is->pictq_mutex);
1540 is->frame_last_returned_time = av_gettime() / 1000000.0;
1554 static int input_get_buffer(AVCodecContext *codec, AVFrame *pic)
1556 AVFilterContext *ctx = codec->opaque;
1557 AVFilterBufferRef *ref;
1558 int perms = AV_PERM_WRITE;
1559 int i, w, h, stride[AV_NUM_DATA_POINTERS];
1563 av_assert0(codec->flags & CODEC_FLAG_EMU_EDGE);
1565 if (codec->codec->capabilities & CODEC_CAP_NEG_LINESIZES)
1566 perms |= AV_PERM_NEG_LINESIZES;
1568 if (pic->buffer_hints & FF_BUFFER_HINTS_VALID) {
1569 if (pic->buffer_hints & FF_BUFFER_HINTS_READABLE) perms |= AV_PERM_READ;
1570 if (pic->buffer_hints & FF_BUFFER_HINTS_PRESERVE) perms |= AV_PERM_PRESERVE;
1571 if (pic->buffer_hints & FF_BUFFER_HINTS_REUSABLE) perms |= AV_PERM_REUSE2;
1573 if (pic->reference) perms |= AV_PERM_READ | AV_PERM_PRESERVE;
1578 if(av_image_check_size(w, h, 0, codec) || codec->pix_fmt<0)
1581 avcodec_align_dimensions2(codec, &w, &h, stride);
1582 edge = codec->flags & CODEC_FLAG_EMU_EDGE ? 0 : avcodec_get_edge_width();
1585 if (codec->pix_fmt != ctx->outputs[0]->format) {
1586 av_log(codec, AV_LOG_ERROR, "Pixel format mismatches %d %d\n", codec->pix_fmt, ctx->outputs[0]->format);
1589 if (!(ref = avfilter_get_video_buffer(ctx->outputs[0], perms, w, h)))
1592 pixel_size = av_pix_fmt_descriptors[ref->format].comp[0].step_minus1 + 1;
1593 ref->video->w = codec->width;
1594 ref->video->h = codec->height;
1595 for (i = 0; i < 4; i ++) {
1596 unsigned hshift = (i == 1 || i == 2) ? av_pix_fmt_descriptors[ref->format].log2_chroma_w : 0;
1597 unsigned vshift = (i == 1 || i == 2) ? av_pix_fmt_descriptors[ref->format].log2_chroma_h : 0;
1599 pic->base[i] = ref->data[i];
1601 ref->data[i] += ((edge * pixel_size) >> hshift) + ((edge * ref->linesize[i]) >> vshift);
1603 pic->data[i] = ref->data[i];
1604 pic->linesize[i] = ref->linesize[i];
1607 pic->type = FF_BUFFER_TYPE_USER;
1608 pic->reordered_opaque = codec->reordered_opaque;
1609 pic->width = codec->width;
1610 pic->height = codec->height;
1611 pic->format = codec->pix_fmt;
1612 pic->sample_aspect_ratio = codec->sample_aspect_ratio;
1613 if (codec->pkt) pic->pkt_pts = codec->pkt->pts;
1614 else pic->pkt_pts = AV_NOPTS_VALUE;
1618 static void input_release_buffer(AVCodecContext *codec, AVFrame *pic)
1620 memset(pic->data, 0, sizeof(pic->data));
1621 avfilter_unref_buffer(pic->opaque);
1624 static int input_reget_buffer(AVCodecContext *codec, AVFrame *pic)
1626 AVFilterBufferRef *ref = pic->opaque;
1628 if (pic->data[0] == NULL) {
1629 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
1630 return codec->get_buffer(codec, pic);
1633 if ((codec->width != ref->video->w) || (codec->height != ref->video->h) ||
1634 (codec->pix_fmt != ref->format)) {
1635 av_log(codec, AV_LOG_ERROR, "Picture properties changed.\n");
1639 pic->reordered_opaque = codec->reordered_opaque;
1640 if (codec->pkt) pic->pkt_pts = codec->pkt->pts;
1641 else pic->pkt_pts = AV_NOPTS_VALUE;
1645 static int input_init(AVFilterContext *ctx, const char *args, void *opaque)
1647 FilterPriv *priv = ctx->priv;
1648 AVCodecContext *codec;
1649 if (!opaque) return -1;
1652 codec = priv->is->video_st->codec;
1653 codec->opaque = ctx;
1654 if (codec->codec->capabilities & CODEC_CAP_DR1) {
1655 av_assert0(codec->flags & CODEC_FLAG_EMU_EDGE);
1657 codec->get_buffer = input_get_buffer;
1658 codec->release_buffer = input_release_buffer;
1659 codec->reget_buffer = input_reget_buffer;
1660 codec->thread_safe_callbacks = 1;
1663 priv->frame = avcodec_alloc_frame();
1668 static void input_uninit(AVFilterContext *ctx)
1670 FilterPriv *priv = ctx->priv;
1671 av_free(priv->frame);
1674 static int input_request_frame(AVFilterLink *link)
1676 FilterPriv *priv = link->src->priv;
1677 AVFilterBufferRef *picref;
1682 while (!(ret = get_video_frame(priv->is, priv->frame, &pts, &pkt)))
1683 av_free_packet(&pkt);
1687 if (priv->use_dr1 && priv->frame->opaque) {
1688 picref = avfilter_ref_buffer(priv->frame->opaque, ~0);
1690 picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, priv->frame->width, priv->frame->height);
1691 av_image_copy(picref->data, picref->linesize,
1692 (const uint8_t **)(void **)priv->frame->data, priv->frame->linesize,
1693 picref->format, priv->frame->width, priv->frame->height);
1695 av_free_packet(&pkt);
1697 avfilter_copy_frame_props(picref, priv->frame);
1698 picref->video->sample_aspect_ratio = av_guess_sample_aspect_ratio(priv->is->ic, priv->is->video_st, priv->frame);
1701 avfilter_start_frame(link, picref);
1702 avfilter_draw_slice(link, 0, picref->video->h, 1);
1703 avfilter_end_frame(link);
1708 static int input_query_formats(AVFilterContext *ctx)
1710 FilterPriv *priv = ctx->priv;
1711 enum PixelFormat pix_fmts[] = {
1712 priv->is->video_st->codec->pix_fmt, PIX_FMT_NONE
1715 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
1719 static int input_config_props(AVFilterLink *link)
1721 FilterPriv *priv = link->src->priv;
1722 AVStream *s = priv->is->video_st;
1724 link->w = s->codec->width;
1725 link->h = s->codec->height;
1726 link->sample_aspect_ratio = s->sample_aspect_ratio.num ?
1727 s->sample_aspect_ratio : s->codec->sample_aspect_ratio;
1728 link->time_base = s->time_base;
1733 static AVFilter input_filter =
1735 .name = "ffplay_input",
1737 .priv_size = sizeof(FilterPriv),
1740 .uninit = input_uninit,
1742 .query_formats = input_query_formats,
1744 .inputs = (AVFilterPad[]) {{ .name = NULL }},
1745 .outputs = (AVFilterPad[]) {{ .name = "default",
1746 .type = AVMEDIA_TYPE_VIDEO,
1747 .request_frame = input_request_frame,
1748 .config_props = input_config_props, },
1752 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters)
1754 static const enum PixelFormat pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
1755 char sws_flags_str[128];
1757 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
1758 AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_format;;
1759 snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%d", sws_flags);
1760 graph->scale_sws_opts = av_strdup(sws_flags_str);
1762 if ((ret = avfilter_graph_create_filter(&filt_src, &input_filter, "src",
1763 NULL, is, graph)) < 0)
1766 #if FF_API_OLD_VSINK_API
1767 ret = avfilter_graph_create_filter(&filt_out,
1768 avfilter_get_by_name("buffersink"),
1769 "out", NULL, pix_fmts, graph);
1771 buffersink_params->pixel_fmts = pix_fmts;
1772 ret = avfilter_graph_create_filter(&filt_out,
1773 avfilter_get_by_name("buffersink"),
1774 "out", NULL, buffersink_params, graph);
1776 av_freep(&buffersink_params);
1780 if ((ret = avfilter_graph_create_filter(&filt_format,
1781 avfilter_get_by_name("format"),
1782 "format", "yuv420p", NULL, graph)) < 0)
1784 if ((ret = avfilter_link(filt_format, 0, filt_out, 0)) < 0)
1789 AVFilterInOut *outputs = avfilter_inout_alloc();
1790 AVFilterInOut *inputs = avfilter_inout_alloc();
1792 outputs->name = av_strdup("in");
1793 outputs->filter_ctx = filt_src;
1794 outputs->pad_idx = 0;
1795 outputs->next = NULL;
1797 inputs->name = av_strdup("out");
1798 inputs->filter_ctx = filt_format;
1799 inputs->pad_idx = 0;
1800 inputs->next = NULL;
1802 if ((ret = avfilter_graph_parse(graph, vfilters, &inputs, &outputs, NULL)) < 0)
1805 if ((ret = avfilter_link(filt_src, 0, filt_format, 0)) < 0)
1809 if ((ret = avfilter_graph_config(graph, NULL)) < 0)
1812 is->out_video_filter = filt_out;
1817 #endif /* CONFIG_AVFILTER */
1819 static int video_thread(void *arg)
1821 VideoState *is = arg;
1822 AVFrame *frame = avcodec_alloc_frame();
1823 int64_t pts_int = AV_NOPTS_VALUE, pos = -1;
1828 AVFilterGraph *graph = avfilter_graph_alloc();
1829 AVFilterContext *filt_out = NULL;
1830 int last_w = is->video_st->codec->width;
1831 int last_h = is->video_st->codec->height;
1833 if ((ret = configure_video_filters(graph, is, vfilters)) < 0) {
1835 event.type = FF_QUIT_EVENT;
1836 event.user.data1 = is;
1837 SDL_PushEvent(&event);
1840 filt_out = is->out_video_filter;
1844 #if !CONFIG_AVFILTER
1847 AVFilterBufferRef *picref;
1848 AVRational tb = filt_out->inputs[0]->time_base;
1850 while (is->paused && !is->videoq.abort_request)
1853 if ( last_w != is->video_st->codec->width
1854 || last_h != is->video_st->codec->height) {
1855 av_log(NULL, AV_LOG_INFO, "Frame changed from size:%dx%d to size:%dx%d\n",
1856 last_w, last_h, is->video_st->codec->width, is->video_st->codec->height);
1857 avfilter_graph_free(&graph);
1858 graph = avfilter_graph_alloc();
1859 if ((ret = configure_video_filters(graph, is, vfilters)) < 0)
1861 filt_out = is->out_video_filter;
1862 last_w = is->video_st->codec->width;
1863 last_h = is->video_st->codec->height;
1865 ret = av_buffersink_get_buffer_ref(filt_out, &picref, 0);
1867 avfilter_fill_frame_from_video_buffer_ref(frame, picref);
1868 pts_int = picref->pts;
1869 tb = filt_out->inputs[0]->time_base;
1871 frame->opaque = picref;
1876 if (ret >= 0 && av_cmp_q(tb, is->video_st->time_base)) {
1877 av_unused int64_t pts1 = pts_int;
1878 pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
1879 av_dlog(NULL, "video_thread(): "
1880 "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
1881 tb.num, tb.den, pts1,
1882 is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
1885 ret = get_video_frame(is, frame, &pts_int, &pkt);
1887 av_free_packet(&pkt);
1895 is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
1896 if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
1897 is->frame_last_filter_delay = 0;
1904 pts = pts_int * av_q2d(is->video_st->time_base);
1906 ret = queue_picture(is, frame, pts, pos);
1912 stream_toggle_pause(is);
1915 avcodec_flush_buffers(is->video_st->codec);
1917 av_freep(&vfilters);
1918 avfilter_graph_free(&graph);
1924 static int subtitle_thread(void *arg)
1926 VideoState *is = arg;
1928 AVPacket pkt1, *pkt = &pkt1;
1932 int r, g, b, y, u, v, a;
1935 while (is->paused && !is->subtitleq.abort_request) {
1938 if (packet_queue_get(&is->subtitleq, pkt, 1) < 0)
1941 if (pkt->data == flush_pkt.data) {
1942 avcodec_flush_buffers(is->subtitle_st->codec);
1945 SDL_LockMutex(is->subpq_mutex);
1946 while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
1947 !is->subtitleq.abort_request) {
1948 SDL_CondWait(is->subpq_cond, is->subpq_mutex);
1950 SDL_UnlockMutex(is->subpq_mutex);
1952 if (is->subtitleq.abort_request)
1955 sp = &is->subpq[is->subpq_windex];
1957 /* NOTE: ipts is the PTS of the _first_ picture beginning in
1958 this packet, if any */
1960 if (pkt->pts != AV_NOPTS_VALUE)
1961 pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
1963 avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
1964 &got_subtitle, pkt);
1966 if (got_subtitle && sp->sub.format == 0) {
1969 for (i = 0; i < sp->sub.num_rects; i++)
1971 for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
1973 RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
1974 y = RGB_TO_Y_CCIR(r, g, b);
1975 u = RGB_TO_U_CCIR(r, g, b, 0);
1976 v = RGB_TO_V_CCIR(r, g, b, 0);
1977 YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
1981 /* now we can update the picture count */
1982 if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
1983 is->subpq_windex = 0;
1984 SDL_LockMutex(is->subpq_mutex);
1986 SDL_UnlockMutex(is->subpq_mutex);
1988 av_free_packet(pkt);
1993 /* copy samples for viewing in editor window */
1994 static void update_sample_display(VideoState *is, short *samples, int samples_size)
1998 size = samples_size / sizeof(short);
2000 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
2003 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
2005 is->sample_array_index += len;
2006 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
2007 is->sample_array_index = 0;
2012 /* return the wanted number of samples to get better sync if sync_type is video
2013 * or external master clock */
2014 static int synchronize_audio(VideoState *is, int nb_samples)
2016 int wanted_nb_samples = nb_samples;
2018 /* if not master, then we try to remove or add samples to correct the clock */
2019 if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||
2020 is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
2021 double diff, avg_diff;
2022 int min_nb_samples, max_nb_samples;
2024 diff = get_audio_clock(is) - get_master_clock(is);
2026 if (diff < AV_NOSYNC_THRESHOLD) {
2027 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
2028 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
2029 /* not enough measures to have a correct estimate */
2030 is->audio_diff_avg_count++;
2032 /* estimate the A-V difference */
2033 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
2035 if (fabs(avg_diff) >= is->audio_diff_threshold) {
2036 wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
2037 min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2038 max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2039 wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
2041 av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
2042 diff, avg_diff, wanted_nb_samples - nb_samples,
2043 is->audio_clock, is->video_clock, is->audio_diff_threshold);
2046 /* too big difference : may be initial PTS errors, so
2048 is->audio_diff_avg_count = 0;
2049 is->audio_diff_cum = 0;
2053 return wanted_nb_samples;
2056 /* decode one audio frame and returns its uncompressed size */
2057 static int audio_decode_frame(VideoState *is, double *pts_ptr)
2059 AVPacket *pkt_temp = &is->audio_pkt_temp;
2060 AVPacket *pkt = &is->audio_pkt;
2061 AVCodecContext *dec = is->audio_st->codec;
2062 int len1, len2, data_size, resampled_data_size;
2063 int64_t dec_channel_layout;
2067 int flush_complete = 0;
2068 int wanted_nb_samples;
2071 /* NOTE: the audio packet can contain several frames */
2072 while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) {
2074 if (!(is->frame = avcodec_alloc_frame()))
2075 return AVERROR(ENOMEM);
2077 avcodec_get_frame_defaults(is->frame);
2085 len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
2087 /* if error, we skip the frame */
2092 pkt_temp->data += len1;
2093 pkt_temp->size -= len1;
2096 /* stop sending empty packets if the decoder is finished */
2097 if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
2101 data_size = av_samples_get_buffer_size(NULL, dec->channels,
2102 is->frame->nb_samples,
2103 dec->sample_fmt, 1);
2105 dec_channel_layout = (dec->channel_layout && dec->channels == av_get_channel_layout_nb_channels(dec->channel_layout)) ? dec->channel_layout : av_get_default_channel_layout(dec->channels);
2106 wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
2108 if (dec->sample_fmt != is->audio_src.fmt ||
2109 dec_channel_layout != is->audio_src.channel_layout ||
2110 dec->sample_rate != is->audio_src.freq ||
2111 (wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) {
2113 swr_free(&is->swr_ctx);
2114 is->swr_ctx = swr_alloc_set_opts(NULL,
2115 is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2116 dec_channel_layout, dec->sample_fmt, dec->sample_rate,
2118 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2119 fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2121 av_get_sample_fmt_name(dec->sample_fmt),
2124 av_get_sample_fmt_name(is->audio_tgt.fmt),
2125 is->audio_tgt.channels);
2128 is->audio_src.channel_layout = dec_channel_layout;
2129 is->audio_src.channels = dec->channels;
2130 is->audio_src.freq = dec->sample_rate;
2131 is->audio_src.fmt = dec->sample_fmt;
2134 resampled_data_size = data_size;
2136 const uint8_t *in[] = { is->frame->data[0] };
2137 uint8_t *out[] = {is->audio_buf2};
2138 if (wanted_nb_samples != is->frame->nb_samples) {
2139 if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / dec->sample_rate,
2140 wanted_nb_samples * is->audio_tgt.freq / dec->sample_rate) < 0) {
2141 fprintf(stderr, "swr_set_compensation() failed\n");
2145 len2 = swr_convert(is->swr_ctx, out, sizeof(is->audio_buf2) / is->audio_tgt.channels / av_get_bytes_per_sample(is->audio_tgt.fmt),
2146 in, is->frame->nb_samples);
2148 fprintf(stderr, "audio_resample() failed\n");
2151 if (len2 == sizeof(is->audio_buf2) / is->audio_tgt.channels / av_get_bytes_per_sample(is->audio_tgt.fmt)) {
2152 fprintf(stderr, "warning: audio buffer is probably too small\n");
2153 swr_init(is->swr_ctx);
2155 is->audio_buf = is->audio_buf2;
2156 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2158 is->audio_buf = is->frame->data[0];
2161 /* if no pts, then compute it */
2162 pts = is->audio_clock;
2164 is->audio_clock += (double)data_size /
2165 (dec->channels * dec->sample_rate * av_get_bytes_per_sample(dec->sample_fmt));
2168 static double last_clock;
2169 printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
2170 is->audio_clock - last_clock,
2171 is->audio_clock, pts);
2172 last_clock = is->audio_clock;
2175 return resampled_data_size;
2178 /* free the current packet */
2180 av_free_packet(pkt);
2181 memset(pkt_temp, 0, sizeof(*pkt_temp));
2183 if (is->paused || is->audioq.abort_request) {
2187 /* read next packet */
2188 if ((new_packet = packet_queue_get(&is->audioq, pkt, 1)) < 0)
2191 if (pkt->data == flush_pkt.data) {
2192 avcodec_flush_buffers(dec);
2198 /* if update the audio clock with the pts */
2199 if (pkt->pts != AV_NOPTS_VALUE) {
2200 is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
2205 /* prepare a new audio buffer */
2206 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2208 VideoState *is = opaque;
2209 int audio_size, len1;
2211 int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
2214 audio_callback_time = av_gettime();
2217 if (is->audio_buf_index >= is->audio_buf_size) {
2218 audio_size = audio_decode_frame(is, &pts);
2219 if (audio_size < 0) {
2220 /* if error, just output silence */
2221 is->audio_buf = is->silence_buf;
2222 is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
2224 if (is->show_mode != SHOW_MODE_VIDEO)
2225 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2226 is->audio_buf_size = audio_size;
2228 is->audio_buf_index = 0;
2230 len1 = is->audio_buf_size - is->audio_buf_index;
2233 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2236 is->audio_buf_index += len1;
2238 bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2239 is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2240 /* Let's assume the audio driver that is used by SDL has two periods. */
2241 is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec;
2242 is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
2245 static int audio_open(void *opaque, int64_t channel_layout, int channels, int sample_rate, struct AudioParams *audio)
2247 SDL_AudioSpec wanted_spec, spec;
2249 int64_t wanted_channel_layout = 0;
2250 int wanted_nb_channels;
2252 env = SDL_getenv("SDL_AUDIO_CHANNELS");
2254 wanted_channel_layout = av_get_default_channel_layout(SDL_atoi(env));
2255 if (!wanted_channel_layout) {
2256 wanted_channel_layout = (channel_layout && channels == av_get_channel_layout_nb_channels(channel_layout)) ? channel_layout : av_get_default_channel_layout(channels);
2257 wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2258 wanted_nb_channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2259 /* SDL only supports 1, 2, 4 or 6 channels at the moment, so we have to make sure not to request anything else. */
2260 while (wanted_nb_channels > 0 && (wanted_nb_channels == 3 || wanted_nb_channels == 5 || wanted_nb_channels > (SDL_VERSION_ATLEAST(1, 2, 8) ? 6 : 2))) {
2261 wanted_nb_channels--;
2262 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2265 wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2266 wanted_spec.freq = sample_rate;
2267 if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2268 fprintf(stderr, "Invalid sample rate or channel count!\n");
2271 wanted_spec.format = AUDIO_S16SYS;
2272 wanted_spec.silence = 0;
2273 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
2274 wanted_spec.callback = sdl_audio_callback;
2275 wanted_spec.userdata = opaque;
2276 if (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2277 fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
2280 if (spec.format != AUDIO_S16SYS) {
2281 fprintf(stderr, "SDL advised audio format %d is not supported!\n", spec.format);
2284 if (spec.channels != wanted_spec.channels) {
2285 wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2286 if (!wanted_channel_layout) {
2287 fprintf(stderr, "SDL advised channel count %d is not supported!\n", spec.channels);
2292 audio->fmt = AV_SAMPLE_FMT_S16;
2293 audio->freq = spec.freq;
2294 audio->channel_layout = wanted_channel_layout;
2295 audio->channels = spec.channels;
2299 /* open a given stream. Return 0 if OK */
2300 static int stream_component_open(VideoState *is, int stream_index)
2302 AVFormatContext *ic = is->ic;
2303 AVCodecContext *avctx;
2306 AVDictionaryEntry *t = NULL;
2308 if (stream_index < 0 || stream_index >= ic->nb_streams)
2310 avctx = ic->streams[stream_index]->codec;
2312 codec = avcodec_find_decoder(avctx->codec_id);
2313 opts = filter_codec_opts(codec_opts, codec, ic, ic->streams[stream_index]);
2315 switch(avctx->codec_type){
2316 case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; if(audio_codec_name ) codec= avcodec_find_decoder_by_name( audio_codec_name); break;
2317 case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; if(subtitle_codec_name) codec= avcodec_find_decoder_by_name(subtitle_codec_name); break;
2318 case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; if(video_codec_name ) codec= avcodec_find_decoder_by_name( video_codec_name); break;
2323 avctx->workaround_bugs = workaround_bugs;
2324 avctx->lowres = lowres;
2325 if(avctx->lowres > codec->max_lowres){
2326 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2328 avctx->lowres= codec->max_lowres;
2330 avctx->idct_algo = idct;
2331 avctx->skip_frame = skip_frame;
2332 avctx->skip_idct = skip_idct;
2333 avctx->skip_loop_filter = skip_loop_filter;
2334 avctx->error_concealment = error_concealment;
2336 if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
2337 if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
2338 if(codec->capabilities & CODEC_CAP_DR1)
2339 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2341 if (!av_dict_get(opts, "threads", NULL, 0))
2342 av_dict_set(&opts, "threads", "auto", 0);
2344 avcodec_open2(avctx, codec, &opts) < 0)
2346 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2347 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2348 return AVERROR_OPTION_NOT_FOUND;
2351 /* prepare audio output */
2352 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2353 int audio_hw_buf_size = audio_open(is, avctx->channel_layout, avctx->channels, avctx->sample_rate, &is->audio_src);
2354 if (audio_hw_buf_size < 0)
2356 is->audio_hw_buf_size = audio_hw_buf_size;
2357 is->audio_tgt = is->audio_src;
2360 ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2361 switch (avctx->codec_type) {
2362 case AVMEDIA_TYPE_AUDIO:
2363 is->audio_stream = stream_index;
2364 is->audio_st = ic->streams[stream_index];
2365 is->audio_buf_size = 0;
2366 is->audio_buf_index = 0;
2368 /* init averaging filter */
2369 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2370 is->audio_diff_avg_count = 0;
2371 /* since we do not have a precise anough audio fifo fullness,
2372 we correct audio sync only if larger than this threshold */
2373 is->audio_diff_threshold = 2.0 * SDL_AUDIO_BUFFER_SIZE / avctx->sample_rate;
2375 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
2376 memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
2377 packet_queue_start(&is->audioq);
2380 case AVMEDIA_TYPE_VIDEO:
2381 is->video_stream = stream_index;
2382 is->video_st = ic->streams[stream_index];
2384 packet_queue_start(&is->videoq);
2385 is->video_tid = SDL_CreateThread(video_thread, is);
2387 case AVMEDIA_TYPE_SUBTITLE:
2388 is->subtitle_stream = stream_index;
2389 is->subtitle_st = ic->streams[stream_index];
2390 packet_queue_start(&is->subtitleq);
2392 is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
2400 static void stream_component_close(VideoState *is, int stream_index)
2402 AVFormatContext *ic = is->ic;
2403 AVCodecContext *avctx;
2405 if (stream_index < 0 || stream_index >= ic->nb_streams)
2407 avctx = ic->streams[stream_index]->codec;
2409 switch (avctx->codec_type) {
2410 case AVMEDIA_TYPE_AUDIO:
2411 packet_queue_abort(&is->audioq);
2415 packet_queue_flush(&is->audioq);
2416 av_free_packet(&is->audio_pkt);
2418 swr_free(&is->swr_ctx);
2419 av_freep(&is->audio_buf1);
2420 is->audio_buf = NULL;
2421 av_freep(&is->frame);
2424 av_rdft_end(is->rdft);
2425 av_freep(&is->rdft_data);
2430 case AVMEDIA_TYPE_VIDEO:
2431 packet_queue_abort(&is->videoq);
2433 /* note: we also signal this mutex to make sure we deblock the
2434 video thread in all cases */
2435 SDL_LockMutex(is->pictq_mutex);
2436 SDL_CondSignal(is->pictq_cond);
2437 SDL_UnlockMutex(is->pictq_mutex);
2439 SDL_WaitThread(is->video_tid, NULL);
2441 packet_queue_flush(&is->videoq);
2443 case AVMEDIA_TYPE_SUBTITLE:
2444 packet_queue_abort(&is->subtitleq);
2446 /* note: we also signal this mutex to make sure we deblock the
2447 video thread in all cases */
2448 SDL_LockMutex(is->subpq_mutex);
2449 is->subtitle_stream_changed = 1;
2451 SDL_CondSignal(is->subpq_cond);
2452 SDL_UnlockMutex(is->subpq_mutex);
2454 SDL_WaitThread(is->subtitle_tid, NULL);
2456 packet_queue_flush(&is->subtitleq);
2462 ic->streams[stream_index]->discard = AVDISCARD_ALL;
2463 avcodec_close(avctx);
2464 switch (avctx->codec_type) {
2465 case AVMEDIA_TYPE_AUDIO:
2466 is->audio_st = NULL;
2467 is->audio_stream = -1;
2469 case AVMEDIA_TYPE_VIDEO:
2470 is->video_st = NULL;
2471 is->video_stream = -1;
2473 case AVMEDIA_TYPE_SUBTITLE:
2474 is->subtitle_st = NULL;
2475 is->subtitle_stream = -1;
2482 static int decode_interrupt_cb(void *ctx)
2484 VideoState *is = ctx;
2485 return is->abort_request;
2488 /* this thread gets the stream from the disk or the network */
2489 static int read_thread(void *arg)
2491 VideoState *is = arg;
2492 AVFormatContext *ic = NULL;
2494 int st_index[AVMEDIA_TYPE_NB];
2495 AVPacket pkt1, *pkt = &pkt1;
2497 int pkt_in_play_range = 0;
2498 AVDictionaryEntry *t;
2499 AVDictionary **opts;
2500 int orig_nb_streams;
2502 memset(st_index, -1, sizeof(st_index));
2503 is->last_video_stream = is->video_stream = -1;
2504 is->last_audio_stream = is->audio_stream = -1;
2505 is->last_subtitle_stream = is->subtitle_stream = -1;
2507 ic = avformat_alloc_context();
2508 ic->interrupt_callback.callback = decode_interrupt_cb;
2509 ic->interrupt_callback.opaque = is;
2510 err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2512 print_error(is->filename, err);
2516 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2517 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2518 ret = AVERROR_OPTION_NOT_FOUND;
2524 ic->flags |= AVFMT_FLAG_GENPTS;
2526 opts = setup_find_stream_info_opts(ic, codec_opts);
2527 orig_nb_streams = ic->nb_streams;
2529 err = avformat_find_stream_info(ic, opts);
2531 fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
2535 for (i = 0; i < orig_nb_streams; i++)
2536 av_dict_free(&opts[i]);
2540 ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
2542 if (seek_by_bytes < 0)
2543 seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT);
2545 /* if seeking requested, we execute it */
2546 if (start_time != AV_NOPTS_VALUE) {
2549 timestamp = start_time;
2550 /* add the stream start time */
2551 if (ic->start_time != AV_NOPTS_VALUE)
2552 timestamp += ic->start_time;
2553 ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2555 fprintf(stderr, "%s: could not seek to position %0.3f\n",
2556 is->filename, (double)timestamp / AV_TIME_BASE);
2560 for (i = 0; i < ic->nb_streams; i++)
2561 ic->streams[i]->discard = AVDISCARD_ALL;
2563 st_index[AVMEDIA_TYPE_VIDEO] =
2564 av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2565 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2567 st_index[AVMEDIA_TYPE_AUDIO] =
2568 av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
2569 wanted_stream[AVMEDIA_TYPE_AUDIO],
2570 st_index[AVMEDIA_TYPE_VIDEO],
2573 st_index[AVMEDIA_TYPE_SUBTITLE] =
2574 av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
2575 wanted_stream[AVMEDIA_TYPE_SUBTITLE],
2576 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2577 st_index[AVMEDIA_TYPE_AUDIO] :
2578 st_index[AVMEDIA_TYPE_VIDEO]),
2581 av_dump_format(ic, 0, is->filename, 0);
2584 is->show_mode = show_mode;
2586 /* open the streams */
2587 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2588 stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
2592 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2593 ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
2595 is->refresh_tid = SDL_CreateThread(refresh_thread, is);
2596 if (is->show_mode == SHOW_MODE_NONE)
2597 is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
2599 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2600 stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
2603 if (is->video_stream < 0 && is->audio_stream < 0) {
2604 fprintf(stderr, "%s: could not open codecs\n", is->filename);
2610 if (is->abort_request)
2612 if (is->paused != is->last_paused) {
2613 is->last_paused = is->paused;
2615 is->read_pause_return = av_read_pause(ic);
2619 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
2621 (!strcmp(ic->iformat->name, "rtsp") ||
2622 (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
2623 /* wait 10 ms to avoid trying to get another packet */
2630 int64_t seek_target = is->seek_pos;
2631 int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
2632 int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
2633 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
2634 // of the seek_pos/seek_rel variables
2636 ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
2638 fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
2640 if (is->audio_stream >= 0) {
2641 packet_queue_flush(&is->audioq);
2642 packet_queue_put(&is->audioq, &flush_pkt);
2644 if (is->subtitle_stream >= 0) {
2645 packet_queue_flush(&is->subtitleq);
2646 packet_queue_put(&is->subtitleq, &flush_pkt);
2648 if (is->video_stream >= 0) {
2649 packet_queue_flush(&is->videoq);
2650 packet_queue_put(&is->videoq, &flush_pkt);
2657 /* if the queue are full, no need to read more */
2658 if ( is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
2659 || ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
2660 && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request)
2661 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request))) {
2667 if (is->video_stream >= 0) {
2668 av_init_packet(pkt);
2671 pkt->stream_index = is->video_stream;
2672 packet_queue_put(&is->videoq, pkt);
2674 if (is->audio_stream >= 0 &&
2675 is->audio_st->codec->codec->capabilities & CODEC_CAP_DELAY) {
2676 av_init_packet(pkt);
2679 pkt->stream_index = is->audio_stream;
2680 packet_queue_put(&is->audioq, pkt);
2683 if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
2684 if (loop != 1 && (!loop || --loop)) {
2685 stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
2686 } else if (autoexit) {
2694 ret = av_read_frame(ic, pkt);
2696 if (ret == AVERROR_EOF || url_feof(ic->pb))
2698 if (ic->pb && ic->pb->error)
2700 SDL_Delay(100); /* wait for user event */
2703 /* check if packet is in play range specified by user, then queue, otherwise discard */
2704 pkt_in_play_range = duration == AV_NOPTS_VALUE ||
2705 (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
2706 av_q2d(ic->streams[pkt->stream_index]->time_base) -
2707 (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
2708 <= ((double)duration / 1000000);
2709 if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
2710 packet_queue_put(&is->audioq, pkt);
2711 } else if (pkt->stream_index == is->video_stream && pkt_in_play_range) {
2712 packet_queue_put(&is->videoq, pkt);
2713 } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
2714 packet_queue_put(&is->subtitleq, pkt);
2716 av_free_packet(pkt);
2719 /* wait until the end */
2720 while (!is->abort_request) {
2726 /* close each stream */
2727 if (is->audio_stream >= 0)
2728 stream_component_close(is, is->audio_stream);
2729 if (is->video_stream >= 0)
2730 stream_component_close(is, is->video_stream);
2731 if (is->subtitle_stream >= 0)
2732 stream_component_close(is, is->subtitle_stream);
2734 avformat_close_input(&is->ic);
2740 event.type = FF_QUIT_EVENT;
2741 event.user.data1 = is;
2742 SDL_PushEvent(&event);
2747 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
2751 is = av_mallocz(sizeof(VideoState));
2754 av_strlcpy(is->filename, filename, sizeof(is->filename));
2755 is->iformat = iformat;
2759 /* start video display */
2760 is->pictq_mutex = SDL_CreateMutex();
2761 is->pictq_cond = SDL_CreateCond();
2763 is->subpq_mutex = SDL_CreateMutex();
2764 is->subpq_cond = SDL_CreateCond();
2766 packet_queue_init(&is->videoq);
2767 packet_queue_init(&is->audioq);
2768 packet_queue_init(&is->subtitleq);
2770 is->av_sync_type = av_sync_type;
2771 is->read_tid = SDL_CreateThread(read_thread, is);
2772 if (!is->read_tid) {
2779 static void stream_cycle_channel(VideoState *is, int codec_type)
2781 AVFormatContext *ic = is->ic;
2782 int start_index, stream_index;
2786 if (codec_type == AVMEDIA_TYPE_VIDEO) {
2787 start_index = is->last_video_stream;
2788 old_index = is->video_stream;
2789 } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
2790 start_index = is->last_audio_stream;
2791 old_index = is->audio_stream;
2793 start_index = is->last_subtitle_stream;
2794 old_index = is->subtitle_stream;
2796 stream_index = start_index;
2798 if (++stream_index >= is->ic->nb_streams)
2800 if (codec_type == AVMEDIA_TYPE_SUBTITLE)
2803 is->last_subtitle_stream = -1;
2806 if (start_index == -1)
2810 if (stream_index == start_index)
2812 st = ic->streams[stream_index];
2813 if (st->codec->codec_type == codec_type) {
2814 /* check that parameters are OK */
2815 switch (codec_type) {
2816 case AVMEDIA_TYPE_AUDIO:
2817 if (st->codec->sample_rate != 0 &&
2818 st->codec->channels != 0)
2821 case AVMEDIA_TYPE_VIDEO:
2822 case AVMEDIA_TYPE_SUBTITLE:
2830 stream_component_close(is, old_index);
2831 stream_component_open(is, stream_index);
2835 static void toggle_full_screen(VideoState *is)
2838 is_full_screen = !is_full_screen;
2839 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
2840 /* OS X needs to reallocate the SDL overlays */
2841 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
2842 is->pictq[i].reallocate = 1;
2848 static void toggle_pause(VideoState *is)
2850 stream_toggle_pause(is);
2854 static void step_to_next_frame(VideoState *is)
2856 /* if the stream is paused unpause it, then step */
2858 stream_toggle_pause(is);
2862 static void toggle_audio_display(VideoState *is)
2864 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
2865 is->show_mode = (is->show_mode + 1) % SHOW_MODE_NB;
2866 fill_rectangle(screen,
2867 is->xleft, is->ytop, is->width, is->height,
2869 SDL_UpdateRect(screen, is->xleft, is->ytop, is->width, is->height);
2872 /* handle an event sent by the GUI */
2873 static void event_loop(VideoState *cur_stream)
2876 double incr, pos, frac;
2880 SDL_WaitEvent(&event);
2881 switch (event.type) {
2883 if (exit_on_keydown) {
2884 do_exit(cur_stream);
2887 switch (event.key.keysym.sym) {
2890 do_exit(cur_stream);
2893 toggle_full_screen(cur_stream);
2894 cur_stream->force_refresh = 1;
2898 toggle_pause(cur_stream);
2900 case SDLK_s: // S: Step to next frame
2901 step_to_next_frame(cur_stream);
2904 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
2907 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
2910 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
2913 toggle_audio_display(cur_stream);
2914 cur_stream->force_refresh = 1;
2934 if (seek_by_bytes) {
2935 if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
2936 pos = cur_stream->video_current_pos;
2937 } else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
2938 pos = cur_stream->audio_pkt.pos;
2940 pos = avio_tell(cur_stream->ic->pb);
2941 if (cur_stream->ic->bit_rate)
2942 incr *= cur_stream->ic->bit_rate / 8.0;
2946 stream_seek(cur_stream, pos, incr, 1);
2948 pos = get_master_clock(cur_stream);
2950 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
2957 case SDL_VIDEOEXPOSE:
2958 cur_stream->force_refresh = 1;
2960 case SDL_MOUSEBUTTONDOWN:
2961 if (exit_on_mousedown) {
2962 do_exit(cur_stream);
2965 case SDL_MOUSEMOTION:
2966 if (event.type == SDL_MOUSEBUTTONDOWN) {
2969 if (event.motion.state != SDL_PRESSED)
2973 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
2974 uint64_t size = avio_size(cur_stream->ic->pb);
2975 stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
2979 int tns, thh, tmm, tss;
2980 tns = cur_stream->ic->duration / 1000000LL;
2982 tmm = (tns % 3600) / 60;
2984 frac = x / cur_stream->width;
2987 mm = (ns % 3600) / 60;
2989 fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
2990 hh, mm, ss, thh, tmm, tss);
2991 ts = frac * cur_stream->ic->duration;
2992 if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
2993 ts += cur_stream->ic->start_time;
2994 stream_seek(cur_stream, ts, 0, 0);
2997 case SDL_VIDEORESIZE:
2998 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
2999 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
3000 screen_width = cur_stream->width = event.resize.w;
3001 screen_height = cur_stream->height = event.resize.h;
3002 cur_stream->force_refresh = 1;
3006 do_exit(cur_stream);
3008 case FF_ALLOC_EVENT:
3009 alloc_picture(event.user.data1);
3011 case FF_REFRESH_EVENT:
3012 video_refresh(event.user.data1);
3013 cur_stream->refresh = 0;
3021 static int opt_frame_size(const char *opt, const char *arg)
3023 av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
3024 return opt_default("video_size", arg);
3027 static int opt_width(const char *opt, const char *arg)
3029 screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3033 static int opt_height(const char *opt, const char *arg)
3035 screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3039 static int opt_format(const char *opt, const char *arg)
3041 file_iformat = av_find_input_format(arg);
3042 if (!file_iformat) {
3043 fprintf(stderr, "Unknown input format: %s\n", arg);
3044 return AVERROR(EINVAL);
3049 static int opt_frame_pix_fmt(const char *opt, const char *arg)
3051 av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3052 return opt_default("pixel_format", arg);
3055 static int opt_sync(const char *opt, const char *arg)
3057 if (!strcmp(arg, "audio"))
3058 av_sync_type = AV_SYNC_AUDIO_MASTER;
3059 else if (!strcmp(arg, "video"))
3060 av_sync_type = AV_SYNC_VIDEO_MASTER;
3061 else if (!strcmp(arg, "ext"))
3062 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3064 fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);
3070 static int opt_seek(const char *opt, const char *arg)
3072 start_time = parse_time_or_die(opt, arg, 1);
3076 static int opt_duration(const char *opt, const char *arg)
3078 duration = parse_time_or_die(opt, arg, 1);
3082 static int opt_show_mode(const char *opt, const char *arg)
3084 show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3085 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3086 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
3087 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3091 static void opt_input_file(void *optctx, const char *filename)
3093 if (input_filename) {
3094 fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3095 filename, input_filename);
3098 if (!strcmp(filename, "-"))
3100 input_filename = filename;
3103 static int opt_codec(void *o, const char *opt, const char *arg)
3105 switch(opt[strlen(opt)-1]){
3106 case 'a' : audio_codec_name = arg; break;
3107 case 's' : subtitle_codec_name = arg; break;
3108 case 'v' : video_codec_name = arg; break;
3115 static const OptionDef options[] = {
3116 #include "cmdutils_common_opts.h"
3117 { "x", HAS_ARG, { (void*)opt_width }, "force displayed width", "width" },
3118 { "y", HAS_ARG, { (void*)opt_height }, "force displayed height", "height" },
3119 { "s", HAS_ARG | OPT_VIDEO, { (void*)opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3120 { "fs", OPT_BOOL, { (void*)&is_full_screen }, "force full screen" },
3121 { "an", OPT_BOOL, { (void*)&audio_disable }, "disable audio" },
3122 { "vn", OPT_BOOL, { (void*)&video_disable }, "disable video" },
3123 { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
3124 { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
3125 { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
3126 { "ss", HAS_ARG, { (void*)&opt_seek }, "seek to a given position in seconds", "pos" },
3127 { "t", HAS_ARG, { (void*)&opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
3128 { "bytes", OPT_INT | HAS_ARG, { (void*)&seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3129 { "nodisp", OPT_BOOL, { (void*)&display_disable }, "disable graphical display" },
3130 { "f", HAS_ARG, { (void*)opt_format }, "force format", "fmt" },
3131 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { (void*)opt_frame_pix_fmt }, "set pixel format", "format" },
3132 { "stats", OPT_BOOL | OPT_EXPERT, { (void*)&show_status }, "show status", "" },
3133 { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&workaround_bugs }, "workaround bugs", "" },
3134 { "fast", OPT_BOOL | OPT_EXPERT, { (void*)&fast }, "non spec compliant optimizations", "" },
3135 { "genpts", OPT_BOOL | OPT_EXPERT, { (void*)&genpts }, "generate pts", "" },
3136 { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3137 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&lowres }, "", "" },
3138 { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&skip_loop_filter }, "", "" },
3139 { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&skip_frame }, "", "" },
3140 { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&skip_idct }, "", "" },
3141 { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&idct }, "set idct algo", "algo" },
3142 { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&error_concealment }, "set error concealment options", "bit_mask" },
3143 { "sync", HAS_ARG | OPT_EXPERT, { (void*)opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3144 { "autoexit", OPT_BOOL | OPT_EXPERT, { (void*)&autoexit }, "exit at the end", "" },
3145 { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { (void*)&exit_on_keydown }, "exit on key down", "" },
3146 { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { (void*)&exit_on_mousedown }, "exit on mouse down", "" },
3147 { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&loop }, "set number of times the playback shall be looped", "loop count" },
3148 { "framedrop", OPT_BOOL | OPT_EXPERT, { (void*)&framedrop }, "drop frames when cpu is too slow", "" },
3149 { "window_title", OPT_STRING | HAS_ARG, { (void*)&window_title }, "set window title", "window title" },
3151 { "vf", OPT_STRING | HAS_ARG, { (void*)&vfilters }, "video filters", "filter list" },
3153 { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { (void*)&rdftspeed }, "rdft speed", "msecs" },
3154 { "showmode", HAS_ARG, {(void*)opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3155 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { (void*)opt_default }, "generic catch all option", "" },
3156 { "i", OPT_BOOL, {(void *)&dummy}, "read specified file", "input_file"},
3157 { "codec", HAS_ARG | OPT_FUNC2, {(void*)opt_codec}, "force decoder", "decoder" },
3161 static void show_usage(void)
3163 av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3164 av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3165 av_log(NULL, AV_LOG_INFO, "\n");
3168 static int opt_help(const char *opt, const char *arg)
3170 av_log_set_callback(log_callback_help);
3172 show_help_options(options, "Main options:\n",
3174 show_help_options(options, "\nAdvanced options:\n",
3175 OPT_EXPERT, OPT_EXPERT);
3177 show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3178 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3179 #if !CONFIG_AVFILTER
3180 show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3182 printf("\nWhile playing:\n"
3184 "f toggle full screen\n"
3186 "a cycle audio channel\n"
3187 "v cycle video channel\n"
3188 "t cycle subtitle channel\n"
3189 "w show audio waves\n"
3190 "s activate frame-step mode\n"
3191 "left/right seek backward/forward 10 seconds\n"
3192 "down/up seek backward/forward 1 minute\n"
3193 "page down/page up seek backward/forward 10 minutes\n"
3194 "mouse click seek to percentage in file corresponding to fraction of width\n"
3199 static int lockmgr(void **mtx, enum AVLockOp op)
3202 case AV_LOCK_CREATE:
3203 *mtx = SDL_CreateMutex();
3207 case AV_LOCK_OBTAIN:
3208 return !!SDL_LockMutex(*mtx);
3209 case AV_LOCK_RELEASE:
3210 return !!SDL_UnlockMutex(*mtx);
3211 case AV_LOCK_DESTROY:
3212 SDL_DestroyMutex(*mtx);
3218 /* Called from the main */
3219 int main(int argc, char **argv)
3224 av_log_set_flags(AV_LOG_SKIP_REPEATED);
3225 parse_loglevel(argc, argv, options);
3227 /* register all codecs, demux and protocols */
3228 avcodec_register_all();
3230 avdevice_register_all();
3233 avfilter_register_all();
3236 avformat_network_init();
3240 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
3241 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
3243 show_banner(argc, argv, options);
3245 parse_options(NULL, argc, argv, options, opt_input_file);
3247 if (!input_filename) {
3249 fprintf(stderr, "An input file must be specified\n");
3250 fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3254 if (display_disable) {
3257 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3259 flags &= ~SDL_INIT_AUDIO;
3260 #if !defined(__MINGW32__) && !defined(__APPLE__)
3261 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3263 if (SDL_Init (flags)) {
3264 fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
3265 fprintf(stderr, "(Did you set the DISPLAY variable?)\n");
3269 if (!display_disable) {
3270 #if HAVE_SDL_VIDEO_SIZE
3271 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3272 fs_screen_width = vi->current_w;
3273 fs_screen_height = vi->current_h;
3277 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3278 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3279 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3281 if (av_lockmgr_register(lockmgr)) {
3282 fprintf(stderr, "Could not initialize lock manager!\n");
3286 av_init_packet(&flush_pkt);
3287 flush_pkt.data = (char *)(intptr_t)"FLUSH";
3289 is = stream_open(input_filename, file_iformat);
3291 fprintf(stderr, "Failed to initialize VideoState!\n");