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/buffersink.h"
52 # include "libavfilter/buffersrc.h"
56 #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 minimum AV sync threshold */
73 #define AV_SYNC_THRESHOLD_MIN 0.01
74 /* AV sync correction is done if above the maximum AV sync threshold */
75 #define AV_SYNC_THRESHOLD_MAX 0.1
76 /* If a frame duration is longer than this, it will not be duplicated to compensate AV sync */
77 #define AV_SYNC_FRAMEDUP_THRESHOLD 0.1
78 /* no AV correction is done if too big error */
79 #define AV_NOSYNC_THRESHOLD 10.0
81 /* maximum audio speed change to get correct sync */
82 #define SAMPLE_CORRECTION_PERCENT_MAX 10
84 /* external clock speed adjustment constants for realtime sources based on buffer fullness */
85 #define EXTERNAL_CLOCK_SPEED_MIN 0.900
86 #define EXTERNAL_CLOCK_SPEED_MAX 1.010
87 #define EXTERNAL_CLOCK_SPEED_STEP 0.001
89 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
90 #define AUDIO_DIFF_AVG_NB 20
92 /* polls for possible required screen refresh at least this often, should be less than 1/fps */
93 #define REFRESH_RATE 0.01
95 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
96 /* TODO: We assume that a decoded and resampled frame fits into this buffer */
97 #define SAMPLE_ARRAY_SIZE (8 * 65536)
99 #define CURSOR_HIDE_DELAY 1000000
101 static int64_t sws_flags = SWS_BICUBIC;
103 typedef struct MyAVPacketList {
105 struct MyAVPacketList *next;
109 typedef struct PacketQueue {
110 MyAVPacketList *first_pkt, *last_pkt;
119 #define VIDEO_PICTURE_QUEUE_SIZE 3
120 #define SUBPICTURE_QUEUE_SIZE 4
122 typedef struct VideoPicture {
123 double pts; // presentation timestamp for this picture
124 int64_t pos; // byte position in file
126 int width, height; /* source height & width */
134 typedef struct SubPicture {
135 double pts; /* presentation time stamp for this picture */
140 typedef struct AudioParams {
143 int64_t channel_layout;
144 enum AVSampleFormat fmt;
147 typedef struct Clock {
148 double pts; /* clock base */
149 double pts_drift; /* clock base minus time at which we updated the clock */
152 int serial; /* clock is based on a packet with this serial */
154 int *queue_serial; /* pointer to the current packet queue serial, used for obsolete clock detection */
158 AV_SYNC_AUDIO_MASTER, /* default choice */
159 AV_SYNC_VIDEO_MASTER,
160 AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
163 typedef struct VideoState {
164 SDL_Thread *read_tid;
165 SDL_Thread *video_tid;
166 AVInputFormat *iformat;
172 int queue_attachments_req;
177 int read_pause_return;
190 int audio_clock_serial;
191 double audio_diff_cum; /* used for AV difference average computation */
192 double audio_diff_avg_coef;
193 double audio_diff_threshold;
194 int audio_diff_avg_count;
197 int audio_hw_buf_size;
198 uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
201 unsigned int audio_buf_size; /* in bytes */
202 unsigned int audio_buf1_size;
203 int audio_buf_index; /* in bytes */
204 int audio_write_buf_size;
205 int audio_buf_frames_pending;
206 AVPacket audio_pkt_temp;
208 int audio_pkt_temp_serial;
209 int audio_last_serial;
210 struct AudioParams audio_src;
212 struct AudioParams audio_filter_src;
214 struct AudioParams audio_tgt;
215 struct SwrContext *swr_ctx;
216 int frame_drops_early;
217 int frame_drops_late;
219 int64_t audio_frame_next_pts;
222 SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
224 int16_t sample_array[SAMPLE_ARRAY_SIZE];
225 int sample_array_index;
229 FFTSample *rdft_data;
231 double last_vis_time;
233 SDL_Thread *subtitle_tid;
235 AVStream *subtitle_st;
236 PacketQueue subtitleq;
237 SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
238 int subpq_size, subpq_rindex, subpq_windex;
239 SDL_mutex *subpq_mutex;
240 SDL_cond *subpq_cond;
243 double frame_last_pts;
244 double frame_last_duration;
245 double frame_last_dropped_pts;
246 double frame_last_returned_time;
247 double frame_last_filter_delay;
248 int64_t frame_last_dropped_pos;
249 int frame_last_dropped_serial;
253 int64_t video_current_pos; // current displayed file pos
254 double max_frame_duration; // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity
255 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
256 int pictq_size, pictq_rindex, pictq_windex;
257 SDL_mutex *pictq_mutex;
258 SDL_cond *pictq_cond;
260 struct SwsContext *img_convert_ctx;
262 SDL_Rect last_display_rect;
265 int width, height, xleft, ytop;
269 AVFilterContext *in_video_filter; // the first filter in the video chain
270 AVFilterContext *out_video_filter; // the last filter in the video chain
271 AVFilterContext *in_audio_filter; // the first filter in the audio chain
272 AVFilterContext *out_audio_filter; // the last filter in the audio chain
273 AVFilterGraph *agraph; // audio filter graph
276 int last_video_stream, last_audio_stream, last_subtitle_stream;
278 SDL_cond *continue_read_thread;
281 /* options specified by the user */
282 static AVInputFormat *file_iformat;
283 static const char *input_filename;
284 static const char *window_title;
285 static int fs_screen_width;
286 static int fs_screen_height;
287 static int default_width = 640;
288 static int default_height = 480;
289 static int screen_width = 0;
290 static int screen_height = 0;
291 static int audio_disable;
292 static int video_disable;
293 static int subtitle_disable;
294 static int wanted_stream[AVMEDIA_TYPE_NB] = {
295 [AVMEDIA_TYPE_AUDIO] = -1,
296 [AVMEDIA_TYPE_VIDEO] = -1,
297 [AVMEDIA_TYPE_SUBTITLE] = -1,
299 static int seek_by_bytes = -1;
300 static int display_disable;
301 static int show_status = 1;
302 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
303 static int64_t start_time = AV_NOPTS_VALUE;
304 static int64_t duration = AV_NOPTS_VALUE;
305 static int workaround_bugs = 1;
307 static int genpts = 0;
308 static int lowres = 0;
309 static int error_concealment = 3;
310 static int decoder_reorder_pts = -1;
312 static int exit_on_keydown;
313 static int exit_on_mousedown;
315 static int framedrop = -1;
316 static int infinite_buffer = -1;
317 static enum ShowMode show_mode = SHOW_MODE_NONE;
318 static const char *audio_codec_name;
319 static const char *subtitle_codec_name;
320 static const char *video_codec_name;
321 double rdftspeed = 0.02;
322 static int64_t cursor_last_shown;
323 static int cursor_hidden = 0;
325 static char *vfilters = NULL;
326 static char *afilters = NULL;
329 /* current context */
330 static int is_full_screen;
331 static int64_t audio_callback_time;
333 static AVPacket flush_pkt;
335 #define FF_ALLOC_EVENT (SDL_USEREVENT)
336 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
338 static SDL_Surface *screen;
341 int cmp_audio_fmts(enum AVSampleFormat fmt1, int64_t channel_count1,
342 enum AVSampleFormat fmt2, int64_t channel_count2)
344 /* If channel count == 1, planar and non-planar formats are the same */
345 if (channel_count1 == 1 && channel_count2 == 1)
346 return av_get_packed_sample_fmt(fmt1) != av_get_packed_sample_fmt(fmt2);
348 return channel_count1 != channel_count2 || fmt1 != fmt2;
352 int64_t get_valid_channel_layout(int64_t channel_layout, int channels)
354 if (channel_layout && av_get_channel_layout_nb_channels(channel_layout) == channels)
355 return channel_layout;
360 static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
362 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
364 MyAVPacketList *pkt1;
366 if (q->abort_request)
369 pkt1 = av_malloc(sizeof(MyAVPacketList));
374 if (pkt == &flush_pkt)
376 pkt1->serial = q->serial;
381 q->last_pkt->next = pkt1;
384 q->size += pkt1->pkt.size + sizeof(*pkt1);
385 /* XXX: should duplicate packet data in DV case */
386 SDL_CondSignal(q->cond);
390 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
394 /* duplicate the packet */
395 if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
398 SDL_LockMutex(q->mutex);
399 ret = packet_queue_put_private(q, pkt);
400 SDL_UnlockMutex(q->mutex);
402 if (pkt != &flush_pkt && ret < 0)
408 /* packet queue handling */
409 static void packet_queue_init(PacketQueue *q)
411 memset(q, 0, sizeof(PacketQueue));
412 q->mutex = SDL_CreateMutex();
413 q->cond = SDL_CreateCond();
414 q->abort_request = 1;
417 static void packet_queue_flush(PacketQueue *q)
419 MyAVPacketList *pkt, *pkt1;
421 SDL_LockMutex(q->mutex);
422 for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
424 av_free_packet(&pkt->pkt);
431 SDL_UnlockMutex(q->mutex);
434 static void packet_queue_destroy(PacketQueue *q)
436 packet_queue_flush(q);
437 SDL_DestroyMutex(q->mutex);
438 SDL_DestroyCond(q->cond);
441 static void packet_queue_abort(PacketQueue *q)
443 SDL_LockMutex(q->mutex);
445 q->abort_request = 1;
447 SDL_CondSignal(q->cond);
449 SDL_UnlockMutex(q->mutex);
452 static void packet_queue_start(PacketQueue *q)
454 SDL_LockMutex(q->mutex);
455 q->abort_request = 0;
456 packet_queue_put_private(q, &flush_pkt);
457 SDL_UnlockMutex(q->mutex);
460 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
461 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
463 MyAVPacketList *pkt1;
466 SDL_LockMutex(q->mutex);
469 if (q->abort_request) {
476 q->first_pkt = pkt1->next;
480 q->size -= pkt1->pkt.size + sizeof(*pkt1);
483 *serial = pkt1->serial;
491 SDL_CondWait(q->cond, q->mutex);
494 SDL_UnlockMutex(q->mutex);
498 static inline void fill_rectangle(SDL_Surface *screen,
499 int x, int y, int w, int h, int color, int update)
506 SDL_FillRect(screen, &rect, color);
507 if (update && w > 0 && h > 0)
508 SDL_UpdateRect(screen, x, y, w, h);
511 /* draw only the border of a rectangle */
512 static void fill_border(int xleft, int ytop, int width, int height, int x, int y, int w, int h, int color, int update)
516 /* fill the background */
520 w2 = width - (x + w);
526 h2 = height - (y + h);
529 fill_rectangle(screen,
533 fill_rectangle(screen,
534 xleft + width - w2, ytop,
537 fill_rectangle(screen,
541 fill_rectangle(screen,
542 xleft + w1, ytop + height - h2,
547 #define ALPHA_BLEND(a, oldp, newp, s)\
548 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
550 #define RGBA_IN(r, g, b, a, s)\
552 unsigned int v = ((const uint32_t *)(s))[0];\
553 a = (v >> 24) & 0xff;\
554 r = (v >> 16) & 0xff;\
555 g = (v >> 8) & 0xff;\
559 #define YUVA_IN(y, u, v, a, s, pal)\
561 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
562 a = (val >> 24) & 0xff;\
563 y = (val >> 16) & 0xff;\
564 u = (val >> 8) & 0xff;\
568 #define YUVA_OUT(d, y, u, v, a)\
570 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
576 static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
578 int wrap, wrap3, width2, skip2;
579 int y, u, v, a, u1, v1, a1, w, h;
580 uint8_t *lum, *cb, *cr;
583 int dstx, dsty, dstw, dsth;
585 dstw = av_clip(rect->w, 0, imgw);
586 dsth = av_clip(rect->h, 0, imgh);
587 dstx = av_clip(rect->x, 0, imgw - dstw);
588 dsty = av_clip(rect->y, 0, imgh - dsth);
589 lum = dst->data[0] + dsty * dst->linesize[0];
590 cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
591 cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
593 width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
595 wrap = dst->linesize[0];
596 wrap3 = rect->pict.linesize[0];
597 p = rect->pict.data[0];
598 pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
606 YUVA_IN(y, u, v, a, p, pal);
607 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
608 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
609 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
615 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
616 YUVA_IN(y, u, v, a, p, pal);
620 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
622 YUVA_IN(y, u, v, a, p + BPP, pal);
626 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
627 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
628 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
635 YUVA_IN(y, u, v, a, p, pal);
636 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
637 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
638 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
642 p += wrap3 - dstw * BPP;
643 lum += wrap - dstw - dstx;
644 cb += dst->linesize[1] - width2 - skip2;
645 cr += dst->linesize[2] - width2 - skip2;
647 for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
653 YUVA_IN(y, u, v, a, p, pal);
657 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
660 YUVA_IN(y, u, v, a, p, pal);
664 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
665 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
666 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
672 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
673 YUVA_IN(y, u, v, a, p, pal);
677 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
679 YUVA_IN(y, u, v, a, p + BPP, pal);
683 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
687 YUVA_IN(y, u, v, a, p, pal);
691 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
693 YUVA_IN(y, u, v, a, p + BPP, pal);
697 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
699 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
700 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
704 p += -wrap3 + 2 * BPP;
708 YUVA_IN(y, u, v, a, p, pal);
712 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
715 YUVA_IN(y, u, v, a, p, pal);
719 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
720 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
721 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
727 p += wrap3 + (wrap3 - dstw * BPP);
728 lum += wrap + (wrap - dstw - dstx);
729 cb += dst->linesize[1] - width2 - skip2;
730 cr += dst->linesize[2] - width2 - skip2;
732 /* handle odd height */
739 YUVA_IN(y, u, v, a, p, pal);
740 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
741 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
742 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
748 for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
749 YUVA_IN(y, u, v, a, p, pal);
753 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
755 YUVA_IN(y, u, v, a, p + BPP, pal);
759 lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
760 cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
761 cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
768 YUVA_IN(y, u, v, a, p, pal);
769 lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
770 cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
771 cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
776 static void free_subpicture(SubPicture *sp)
778 avsubtitle_free(&sp->sub);
781 static void calculate_display_rect(SDL_Rect *rect, int scr_xleft, int scr_ytop, int scr_width, int scr_height, VideoPicture *vp)
784 int width, height, x, y;
786 if (vp->sar.num == 0)
789 aspect_ratio = av_q2d(vp->sar);
791 if (aspect_ratio <= 0.0)
793 aspect_ratio *= (float)vp->width / (float)vp->height;
795 /* XXX: we suppose the screen has a 1.0 pixel ratio */
797 width = ((int)rint(height * aspect_ratio)) & ~1;
798 if (width > scr_width) {
800 height = ((int)rint(width / aspect_ratio)) & ~1;
802 x = (scr_width - width) / 2;
803 y = (scr_height - height) / 2;
804 rect->x = scr_xleft + x;
805 rect->y = scr_ytop + y;
806 rect->w = FFMAX(width, 1);
807 rect->h = FFMAX(height, 1);
810 static void video_image_display(VideoState *is)
818 vp = &is->pictq[is->pictq_rindex];
820 if (is->subtitle_st) {
821 if (is->subpq_size > 0) {
822 sp = &is->subpq[is->subpq_rindex];
824 if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
825 SDL_LockYUVOverlay (vp->bmp);
827 pict.data[0] = vp->bmp->pixels[0];
828 pict.data[1] = vp->bmp->pixels[2];
829 pict.data[2] = vp->bmp->pixels[1];
831 pict.linesize[0] = vp->bmp->pitches[0];
832 pict.linesize[1] = vp->bmp->pitches[2];
833 pict.linesize[2] = vp->bmp->pitches[1];
835 for (i = 0; i < sp->sub.num_rects; i++)
836 blend_subrect(&pict, sp->sub.rects[i],
837 vp->bmp->w, vp->bmp->h);
839 SDL_UnlockYUVOverlay (vp->bmp);
844 calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp);
846 SDL_DisplayYUVOverlay(vp->bmp, &rect);
848 if (rect.x != is->last_display_rect.x || rect.y != is->last_display_rect.y || rect.w != is->last_display_rect.w || rect.h != is->last_display_rect.h || is->force_refresh) {
849 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
850 fill_border(is->xleft, is->ytop, is->width, is->height, rect.x, rect.y, rect.w, rect.h, bgcolor, 1);
851 is->last_display_rect = rect;
856 static inline int compute_mod(int a, int b)
858 return a < 0 ? a%b + b : a%b;
861 static void video_audio_display(VideoState *s)
863 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
864 int ch, channels, h, h2, bgcolor, fgcolor;
866 int rdft_bits, nb_freq;
868 for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
870 nb_freq = 1 << (rdft_bits - 1);
872 /* compute display index : center on currently output samples */
873 channels = s->audio_tgt.channels;
874 nb_display_channels = channels;
876 int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
878 delay = s->audio_write_buf_size;
881 /* to be more precise, we take into account the time spent since
882 the last buffer computation */
883 if (audio_callback_time) {
884 time_diff = av_gettime() - audio_callback_time;
885 delay -= (time_diff * s->audio_tgt.freq) / 1000000;
888 delay += 2 * data_used;
889 if (delay < data_used)
892 i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
893 if (s->show_mode == SHOW_MODE_WAVES) {
895 for (i = 0; i < 1000; i += channels) {
896 int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
897 int a = s->sample_array[idx];
898 int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
899 int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
900 int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
902 if (h < score && (b ^ c) < 0) {
909 s->last_i_start = i_start;
911 i_start = s->last_i_start;
914 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
915 if (s->show_mode == SHOW_MODE_WAVES) {
916 fill_rectangle(screen,
917 s->xleft, s->ytop, s->width, s->height,
920 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
922 /* total height for one channel */
923 h = s->height / nb_display_channels;
924 /* graph height / 2 */
926 for (ch = 0; ch < nb_display_channels; ch++) {
928 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
929 for (x = 0; x < s->width; x++) {
930 y = (s->sample_array[i] * h2) >> 15;
937 fill_rectangle(screen,
938 s->xleft + x, ys, 1, y,
941 if (i >= SAMPLE_ARRAY_SIZE)
942 i -= SAMPLE_ARRAY_SIZE;
946 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
948 for (ch = 1; ch < nb_display_channels; ch++) {
949 y = s->ytop + ch * h;
950 fill_rectangle(screen,
951 s->xleft, y, s->width, 1,
954 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
956 nb_display_channels= FFMIN(nb_display_channels, 2);
957 if (rdft_bits != s->rdft_bits) {
958 av_rdft_end(s->rdft);
959 av_free(s->rdft_data);
960 s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
961 s->rdft_bits = rdft_bits;
962 s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data));
966 for (ch = 0; ch < nb_display_channels; ch++) {
967 data[ch] = s->rdft_data + 2 * nb_freq * ch;
969 for (x = 0; x < 2 * nb_freq; x++) {
970 double w = (x-nb_freq) * (1.0 / nb_freq);
971 data[ch][x] = s->sample_array[i] * (1.0 - w * w);
973 if (i >= SAMPLE_ARRAY_SIZE)
974 i -= SAMPLE_ARRAY_SIZE;
976 av_rdft_calc(s->rdft, data[ch]);
978 /* Least efficient way to do this, we should of course
979 * directly access it but it is more than fast enough. */
980 for (y = 0; y < s->height; y++) {
981 double w = 1 / sqrt(nb_freq);
982 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]));
983 int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
984 + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
987 fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
989 fill_rectangle(screen,
990 s->xpos, s->height-y, 1, 1,
994 SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
997 if (s->xpos >= s->width)
1002 static void stream_close(VideoState *is)
1006 /* XXX: use a special url_shutdown call to abort parse cleanly */
1007 is->abort_request = 1;
1008 SDL_WaitThread(is->read_tid, NULL);
1009 packet_queue_destroy(&is->videoq);
1010 packet_queue_destroy(&is->audioq);
1011 packet_queue_destroy(&is->subtitleq);
1013 /* free all pictures */
1014 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
1017 SDL_FreeYUVOverlay(vp->bmp);
1021 SDL_DestroyMutex(is->pictq_mutex);
1022 SDL_DestroyCond(is->pictq_cond);
1023 SDL_DestroyMutex(is->subpq_mutex);
1024 SDL_DestroyCond(is->subpq_cond);
1025 SDL_DestroyCond(is->continue_read_thread);
1026 #if !CONFIG_AVFILTER
1027 sws_freeContext(is->img_convert_ctx);
1032 static void do_exit(VideoState *is)
1037 av_lockmgr_register(NULL);
1040 av_freep(&vfilters);
1042 avformat_network_deinit();
1046 av_log(NULL, AV_LOG_QUIET, "%s", "");
1050 static void sigterm_handler(int sig)
1055 static int video_open(VideoState *is, int force_set_video_mode, VideoPicture *vp)
1057 int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
1061 if (is_full_screen) flags |= SDL_FULLSCREEN;
1062 else flags |= SDL_RESIZABLE;
1064 if (vp && vp->width) {
1065 calculate_display_rect(&rect, 0, 0, INT_MAX, vp->height, vp);
1066 default_width = rect.w;
1067 default_height = rect.h;
1070 if (is_full_screen && fs_screen_width) {
1071 w = fs_screen_width;
1072 h = fs_screen_height;
1073 } else if (!is_full_screen && screen_width) {
1080 w = FFMIN(16383, w);
1081 if (screen && is->width == screen->w && screen->w == w
1082 && is->height== screen->h && screen->h == h && !force_set_video_mode)
1084 screen = SDL_SetVideoMode(w, h, 0, flags);
1086 av_log(NULL, AV_LOG_FATAL, "SDL: could not set video mode - exiting\n");
1090 window_title = input_filename;
1091 SDL_WM_SetCaption(window_title, window_title);
1093 is->width = screen->w;
1094 is->height = screen->h;
1099 /* display the current picture, if any */
1100 static void video_display(VideoState *is)
1103 video_open(is, 0, NULL);
1104 if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
1105 video_audio_display(is);
1106 else if (is->video_st)
1107 video_image_display(is);
1110 static double get_clock(Clock *c)
1112 if (*c->queue_serial != c->serial)
1117 double time = av_gettime() / 1000000.0;
1118 return c->pts_drift + time - (time - c->last_updated) * (1.0 - c->speed);
1122 static void set_clock_at(Clock *c, double pts, int serial, double time)
1125 c->last_updated = time;
1126 c->pts_drift = c->pts - time;
1130 static void set_clock(Clock *c, double pts, int serial)
1132 double time = av_gettime() / 1000000.0;
1133 set_clock_at(c, pts, serial, time);
1136 static void set_clock_speed(Clock *c, double speed)
1138 set_clock(c, get_clock(c), c->serial);
1142 static void init_clock(Clock *c, int *queue_serial)
1146 c->queue_serial = queue_serial;
1147 set_clock(c, NAN, -1);
1150 static void sync_clock_to_slave(Clock *c, Clock *slave)
1152 double clock = get_clock(c);
1153 double slave_clock = get_clock(slave);
1154 if (!isnan(slave_clock) && (isnan(clock) || fabs(clock - slave_clock) > AV_NOSYNC_THRESHOLD))
1155 set_clock(c, slave_clock, slave->serial);
1158 static int get_master_sync_type(VideoState *is) {
1159 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
1161 return AV_SYNC_VIDEO_MASTER;
1163 return AV_SYNC_AUDIO_MASTER;
1164 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
1166 return AV_SYNC_AUDIO_MASTER;
1168 return AV_SYNC_EXTERNAL_CLOCK;
1170 return AV_SYNC_EXTERNAL_CLOCK;
1174 /* get the current master clock value */
1175 static double get_master_clock(VideoState *is)
1179 switch (get_master_sync_type(is)) {
1180 case AV_SYNC_VIDEO_MASTER:
1181 val = get_clock(&is->vidclk);
1183 case AV_SYNC_AUDIO_MASTER:
1184 val = get_clock(&is->audclk);
1187 val = get_clock(&is->extclk);
1193 static void check_external_clock_speed(VideoState *is) {
1194 if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
1195 is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
1196 set_clock_speed(&is->extclk, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->extclk.speed - EXTERNAL_CLOCK_SPEED_STEP));
1197 } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
1198 (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
1199 set_clock_speed(&is->extclk, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->extclk.speed + EXTERNAL_CLOCK_SPEED_STEP));
1201 double speed = is->extclk.speed;
1203 set_clock_speed(&is->extclk, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
1207 /* seek in the stream */
1208 static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
1210 if (!is->seek_req) {
1213 is->seek_flags &= ~AVSEEK_FLAG_BYTE;
1215 is->seek_flags |= AVSEEK_FLAG_BYTE;
1217 SDL_CondSignal(is->continue_read_thread);
1221 /* pause or resume the video */
1222 static void stream_toggle_pause(VideoState *is)
1225 is->frame_timer += av_gettime() / 1000000.0 + is->vidclk.pts_drift - is->vidclk.pts;
1226 if (is->read_pause_return != AVERROR(ENOSYS)) {
1227 is->vidclk.paused = 0;
1229 set_clock(&is->vidclk, get_clock(&is->vidclk), is->vidclk.serial);
1231 set_clock(&is->extclk, get_clock(&is->extclk), is->extclk.serial);
1232 is->paused = is->audclk.paused = is->vidclk.paused = is->extclk.paused = !is->paused;
1235 static void toggle_pause(VideoState *is)
1237 stream_toggle_pause(is);
1241 static void step_to_next_frame(VideoState *is)
1243 /* if the stream is paused unpause it, then step */
1245 stream_toggle_pause(is);
1249 static double compute_target_delay(double delay, VideoState *is)
1251 double sync_threshold, diff;
1253 /* update delay to follow master synchronisation source */
1254 if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
1255 /* if video is slave, we try to correct big delays by
1256 duplicating or deleting a frame */
1257 diff = get_clock(&is->vidclk) - get_master_clock(is);
1259 /* skip or repeat frame. We take into account the
1260 delay to compute the threshold. I still don't know
1261 if it is the best guess */
1262 sync_threshold = FFMAX(AV_SYNC_THRESHOLD_MIN, FFMIN(AV_SYNC_THRESHOLD_MAX, delay));
1263 if (!isnan(diff) && fabs(diff) < is->max_frame_duration) {
1264 if (diff <= -sync_threshold)
1265 delay = FFMAX(0, delay + diff);
1266 else if (diff >= sync_threshold && delay > AV_SYNC_FRAMEDUP_THRESHOLD)
1267 delay = delay + diff;
1268 else if (diff >= sync_threshold)
1273 av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
1279 static void pictq_next_picture(VideoState *is) {
1280 /* update queue size and signal for next picture */
1281 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
1282 is->pictq_rindex = 0;
1284 SDL_LockMutex(is->pictq_mutex);
1286 SDL_CondSignal(is->pictq_cond);
1287 SDL_UnlockMutex(is->pictq_mutex);
1290 static int pictq_prev_picture(VideoState *is) {
1291 VideoPicture *prevvp;
1293 /* update queue size and signal for the previous picture */
1294 prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE];
1295 if (prevvp->allocated && prevvp->serial == is->videoq.serial) {
1296 SDL_LockMutex(is->pictq_mutex);
1297 if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE) {
1298 if (--is->pictq_rindex == -1)
1299 is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1;
1303 SDL_CondSignal(is->pictq_cond);
1304 SDL_UnlockMutex(is->pictq_mutex);
1309 static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
1310 /* update current video pts */
1311 set_clock(&is->vidclk, pts, serial);
1312 sync_clock_to_slave(&is->extclk, &is->vidclk);
1313 is->video_current_pos = pos;
1314 is->frame_last_pts = pts;
1317 /* called to display each frame */
1318 static void video_refresh(void *opaque, double *remaining_time)
1320 VideoState *is = opaque;
1324 SubPicture *sp, *sp2;
1326 if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
1327 check_external_clock_speed(is);
1329 if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
1330 time = av_gettime() / 1000000.0;
1331 if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
1333 is->last_vis_time = time;
1335 *remaining_time = FFMIN(*remaining_time, is->last_vis_time + rdftspeed - time);
1340 if (is->force_refresh)
1341 redisplay = pictq_prev_picture(is);
1343 if (is->pictq_size == 0) {
1344 SDL_LockMutex(is->pictq_mutex);
1345 if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
1346 update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, is->frame_last_dropped_serial);
1347 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1349 SDL_UnlockMutex(is->pictq_mutex);
1350 // nothing to do, no picture to display in the queue
1352 double last_duration, duration, delay;
1353 /* dequeue the picture */
1354 vp = &is->pictq[is->pictq_rindex];
1356 if (vp->serial != is->videoq.serial) {
1357 pictq_next_picture(is);
1365 /* compute nominal last_duration */
1366 last_duration = vp->pts - is->frame_last_pts;
1367 if (!isnan(last_duration) && last_duration > 0 && last_duration < is->max_frame_duration) {
1368 /* if duration of the last frame was sane, update last_duration in video state */
1369 is->frame_last_duration = last_duration;
1374 delay = compute_target_delay(is->frame_last_duration, is);
1376 time= av_gettime()/1000000.0;
1377 if (time < is->frame_timer + delay && !redisplay) {
1378 *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
1382 is->frame_timer += delay;
1383 if (delay > 0 && time - is->frame_timer > AV_SYNC_THRESHOLD_MAX)
1384 is->frame_timer = time;
1386 SDL_LockMutex(is->pictq_mutex);
1387 if (!redisplay && !isnan(vp->pts))
1388 update_video_pts(is, vp->pts, vp->pos, vp->serial);
1389 SDL_UnlockMutex(is->pictq_mutex);
1391 if (is->pictq_size > 1) {
1392 VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
1393 duration = nextvp->pts - vp->pts;
1394 if(!is->step && (redisplay || framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
1396 is->frame_drops_late++;
1397 pictq_next_picture(is);
1403 if (is->subtitle_st) {
1404 while (is->subpq_size > 0) {
1405 sp = &is->subpq[is->subpq_rindex];
1407 if (is->subpq_size > 1)
1408 sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
1412 if (sp->serial != is->subtitleq.serial
1413 || (is->vidclk.pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
1414 || (sp2 && is->vidclk.pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
1416 free_subpicture(sp);
1418 /* update queue size and signal for next picture */
1419 if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
1420 is->subpq_rindex = 0;
1422 SDL_LockMutex(is->subpq_mutex);
1424 SDL_CondSignal(is->subpq_cond);
1425 SDL_UnlockMutex(is->subpq_mutex);
1433 /* display picture */
1434 if (!display_disable && is->show_mode == SHOW_MODE_VIDEO)
1437 pictq_next_picture(is);
1439 if (is->step && !is->paused)
1440 stream_toggle_pause(is);
1443 is->force_refresh = 0;
1445 static int64_t last_time;
1447 int aqsize, vqsize, sqsize;
1450 cur_time = av_gettime();
1451 if (!last_time || (cur_time - last_time) >= 30000) {
1456 aqsize = is->audioq.size;
1458 vqsize = is->videoq.size;
1459 if (is->subtitle_st)
1460 sqsize = is->subtitleq.size;
1462 if (is->audio_st && is->video_st)
1463 av_diff = get_clock(&is->audclk) - get_clock(&is->vidclk);
1464 else if (is->video_st)
1465 av_diff = get_master_clock(is) - get_clock(&is->vidclk);
1466 else if (is->audio_st)
1467 av_diff = get_master_clock(is) - get_clock(&is->audclk);
1468 av_log(NULL, AV_LOG_INFO,
1469 "%7.2f %s:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
1470 get_master_clock(is),
1471 (is->audio_st && is->video_st) ? "A-V" : (is->video_st ? "M-V" : (is->audio_st ? "M-A" : " ")),
1473 is->frame_drops_early + is->frame_drops_late,
1477 is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
1478 is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
1480 last_time = cur_time;
1485 /* allocate a picture (needs to do that in main thread to avoid
1486 potential locking problems */
1487 static void alloc_picture(VideoState *is)
1492 vp = &is->pictq[is->pictq_windex];
1495 SDL_FreeYUVOverlay(vp->bmp);
1497 video_open(is, 0, vp);
1499 vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
1502 bufferdiff = vp->bmp ? FFMAX(vp->bmp->pixels[0], vp->bmp->pixels[1]) - FFMIN(vp->bmp->pixels[0], vp->bmp->pixels[1]) : 0;
1503 if (!vp->bmp || vp->bmp->pitches[0] < vp->width || bufferdiff < (int64_t)vp->height * vp->bmp->pitches[0]) {
1504 /* SDL allocates a buffer smaller than requested if the video
1505 * overlay hardware is unable to support the requested size. */
1506 av_log(NULL, AV_LOG_FATAL,
1507 "Error: the video system does not support an image\n"
1508 "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
1509 "to reduce the image size.\n", vp->width, vp->height );
1513 SDL_LockMutex(is->pictq_mutex);
1515 SDL_CondSignal(is->pictq_cond);
1516 SDL_UnlockMutex(is->pictq_mutex);
1519 static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
1520 int i, width, height;
1522 for (i = 0; i < 3; i++) {
1529 if (bmp->pitches[i] > width) {
1530 maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
1531 for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
1537 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t pos, int serial)
1541 #if defined(DEBUG_SYNC) && 0
1542 printf("frame_type=%c pts=%0.3f\n",
1543 av_get_picture_type_char(src_frame->pict_type), pts);
1546 /* wait until we have space to put a new picture */
1547 SDL_LockMutex(is->pictq_mutex);
1549 /* keep the last already displayed picture in the queue */
1550 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 1 &&
1551 !is->videoq.abort_request) {
1552 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1554 SDL_UnlockMutex(is->pictq_mutex);
1556 if (is->videoq.abort_request)
1559 vp = &is->pictq[is->pictq_windex];
1561 vp->sar = src_frame->sample_aspect_ratio;
1563 /* alloc or resize hardware picture buffer */
1564 if (!vp->bmp || vp->reallocate || !vp->allocated ||
1565 vp->width != src_frame->width ||
1566 vp->height != src_frame->height) {
1571 vp->width = src_frame->width;
1572 vp->height = src_frame->height;
1574 /* the allocation must be done in the main thread to avoid
1575 locking problems. */
1576 event.type = FF_ALLOC_EVENT;
1577 event.user.data1 = is;
1578 SDL_PushEvent(&event);
1580 /* wait until the picture is allocated */
1581 SDL_LockMutex(is->pictq_mutex);
1582 while (!vp->allocated && !is->videoq.abort_request) {
1583 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1585 /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
1586 if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
1587 while (!vp->allocated) {
1588 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1591 SDL_UnlockMutex(is->pictq_mutex);
1593 if (is->videoq.abort_request)
1597 /* if the frame is not skipped, then display it */
1599 AVPicture pict = { { 0 } };
1601 /* get a pointer on the bitmap */
1602 SDL_LockYUVOverlay (vp->bmp);
1604 pict.data[0] = vp->bmp->pixels[0];
1605 pict.data[1] = vp->bmp->pixels[2];
1606 pict.data[2] = vp->bmp->pixels[1];
1608 pict.linesize[0] = vp->bmp->pitches[0];
1609 pict.linesize[1] = vp->bmp->pitches[2];
1610 pict.linesize[2] = vp->bmp->pitches[1];
1613 // FIXME use direct rendering
1614 av_picture_copy(&pict, (AVPicture *)src_frame,
1615 src_frame->format, vp->width, vp->height);
1617 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1618 is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1619 vp->width, vp->height, src_frame->format, vp->width, vp->height,
1620 AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
1621 if (is->img_convert_ctx == NULL) {
1622 av_log(NULL, AV_LOG_FATAL, "Cannot initialize the conversion context\n");
1625 sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
1626 0, vp->height, pict.data, pict.linesize);
1628 /* workaround SDL PITCH_WORKAROUND */
1629 duplicate_right_border_pixels(vp->bmp);
1630 /* update the bitmap content */
1631 SDL_UnlockYUVOverlay(vp->bmp);
1635 vp->serial = serial;
1637 /* now we can update the picture count */
1638 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
1639 is->pictq_windex = 0;
1640 SDL_LockMutex(is->pictq_mutex);
1642 SDL_UnlockMutex(is->pictq_mutex);
1647 static int get_video_frame(VideoState *is, AVFrame *frame, AVPacket *pkt, int *serial)
1651 if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
1654 if (pkt->data == flush_pkt.data) {
1655 avcodec_flush_buffers(is->video_st->codec);
1657 SDL_LockMutex(is->pictq_mutex);
1658 // Make sure there are no long delay timers (ideally we should just flush the queue but that's harder)
1659 while (is->pictq_size && !is->videoq.abort_request) {
1660 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
1662 is->video_current_pos = -1;
1663 is->frame_last_pts = AV_NOPTS_VALUE;
1664 is->frame_last_duration = 0;
1665 is->frame_timer = (double)av_gettime() / 1000000.0;
1666 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
1667 SDL_UnlockMutex(is->pictq_mutex);
1671 if(avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt) < 0)
1678 if (decoder_reorder_pts == -1) {
1679 frame->pts = av_frame_get_best_effort_timestamp(frame);
1680 } else if (decoder_reorder_pts) {
1681 frame->pts = frame->pkt_pts;
1683 frame->pts = frame->pkt_dts;
1686 if (frame->pts != AV_NOPTS_VALUE)
1687 dpts = av_q2d(is->video_st->time_base) * frame->pts;
1689 frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
1691 if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
1692 SDL_LockMutex(is->pictq_mutex);
1693 if (is->frame_last_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE) {
1694 double clockdiff = get_clock(&is->vidclk) - get_master_clock(is);
1695 double ptsdiff = dpts - is->frame_last_pts;
1696 if (!isnan(clockdiff) && fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
1697 !isnan(ptsdiff) && ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
1698 clockdiff + ptsdiff - is->frame_last_filter_delay < 0 &&
1699 is->videoq.nb_packets) {
1700 is->frame_last_dropped_pos = pkt->pos;
1701 is->frame_last_dropped_pts = dpts;
1702 is->frame_last_dropped_serial = *serial;
1703 is->frame_drops_early++;
1704 av_frame_unref(frame);
1708 SDL_UnlockMutex(is->pictq_mutex);
1717 static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
1718 AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
1721 AVFilterInOut *outputs = NULL, *inputs = NULL;
1724 outputs = avfilter_inout_alloc();
1725 inputs = avfilter_inout_alloc();
1726 if (!outputs || !inputs) {
1727 ret = AVERROR(ENOMEM);
1731 outputs->name = av_strdup("in");
1732 outputs->filter_ctx = source_ctx;
1733 outputs->pad_idx = 0;
1734 outputs->next = NULL;
1736 inputs->name = av_strdup("out");
1737 inputs->filter_ctx = sink_ctx;
1738 inputs->pad_idx = 0;
1739 inputs->next = NULL;
1741 if ((ret = avfilter_graph_parse_ptr(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
1744 if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
1748 ret = avfilter_graph_config(graph, NULL);
1750 avfilter_inout_free(&outputs);
1751 avfilter_inout_free(&inputs);
1755 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame)
1757 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
1758 char sws_flags_str[128];
1759 char buffersrc_args[256];
1761 AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_crop;
1762 AVCodecContext *codec = is->video_st->codec;
1763 AVRational fr = av_guess_frame_rate(is->ic, is->video_st, NULL);
1765 av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1766 snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
1767 graph->scale_sws_opts = av_strdup(sws_flags_str);
1769 snprintf(buffersrc_args, sizeof(buffersrc_args),
1770 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
1771 frame->width, frame->height, frame->format,
1772 is->video_st->time_base.num, is->video_st->time_base.den,
1773 codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
1774 if (fr.num && fr.den)
1775 av_strlcatf(buffersrc_args, sizeof(buffersrc_args), ":frame_rate=%d/%d", fr.num, fr.den);
1777 if ((ret = avfilter_graph_create_filter(&filt_src,
1778 avfilter_get_by_name("buffer"),
1779 "ffplay_buffer", buffersrc_args, NULL,
1783 ret = avfilter_graph_create_filter(&filt_out,
1784 avfilter_get_by_name("buffersink"),
1785 "ffplay_buffersink", NULL, NULL, graph);
1789 if ((ret = av_opt_set_int_list(filt_out, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
1792 /* SDL YUV code is not handling odd width/height for some driver
1793 * combinations, therefore we crop the picture to an even width/height. */
1794 if ((ret = avfilter_graph_create_filter(&filt_crop,
1795 avfilter_get_by_name("crop"),
1796 "ffplay_crop", "floor(in_w/2)*2:floor(in_h/2)*2", NULL, graph)) < 0)
1798 if ((ret = avfilter_link(filt_crop, 0, filt_out, 0)) < 0)
1801 if ((ret = configure_filtergraph(graph, vfilters, filt_src, filt_crop)) < 0)
1804 is->in_video_filter = filt_src;
1805 is->out_video_filter = filt_out;
1811 static int configure_audio_filters(VideoState *is, const char *afilters, int force_output_format)
1813 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
1814 int sample_rates[2] = { 0, -1 };
1815 int64_t channel_layouts[2] = { 0, -1 };
1816 int channels[2] = { 0, -1 };
1817 AVFilterContext *filt_asrc = NULL, *filt_asink = NULL;
1818 char asrc_args[256];
1821 avfilter_graph_free(&is->agraph);
1822 if (!(is->agraph = avfilter_graph_alloc()))
1823 return AVERROR(ENOMEM);
1825 ret = snprintf(asrc_args, sizeof(asrc_args),
1826 "sample_rate=%d:sample_fmt=%s:channels=%d:time_base=%d/%d",
1827 is->audio_filter_src.freq, av_get_sample_fmt_name(is->audio_filter_src.fmt),
1828 is->audio_filter_src.channels,
1829 1, is->audio_filter_src.freq);
1830 if (is->audio_filter_src.channel_layout)
1831 snprintf(asrc_args + ret, sizeof(asrc_args) - ret,
1832 ":channel_layout=0x%"PRIx64, is->audio_filter_src.channel_layout);
1834 ret = avfilter_graph_create_filter(&filt_asrc,
1835 avfilter_get_by_name("abuffer"), "ffplay_abuffer",
1836 asrc_args, NULL, is->agraph);
1841 ret = avfilter_graph_create_filter(&filt_asink,
1842 avfilter_get_by_name("abuffersink"), "ffplay_abuffersink",
1843 NULL, NULL, is->agraph);
1847 if ((ret = av_opt_set_int_list(filt_asink, "sample_fmts", sample_fmts, AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
1849 if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
1852 if (force_output_format) {
1853 channel_layouts[0] = is->audio_tgt.channel_layout;
1854 channels [0] = is->audio_tgt.channels;
1855 sample_rates [0] = is->audio_tgt.freq;
1856 if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 0, AV_OPT_SEARCH_CHILDREN)) < 0)
1858 if ((ret = av_opt_set_int_list(filt_asink, "channel_layouts", channel_layouts, -1, AV_OPT_SEARCH_CHILDREN)) < 0)
1860 if ((ret = av_opt_set_int_list(filt_asink, "channel_counts" , channels , -1, AV_OPT_SEARCH_CHILDREN)) < 0)
1862 if ((ret = av_opt_set_int_list(filt_asink, "sample_rates" , sample_rates , -1, AV_OPT_SEARCH_CHILDREN)) < 0)
1867 if ((ret = configure_filtergraph(is->agraph, afilters, filt_asrc, filt_asink)) < 0)
1870 is->in_audio_filter = filt_asrc;
1871 is->out_audio_filter = filt_asink;
1875 avfilter_graph_free(&is->agraph);
1878 #endif /* CONFIG_AVFILTER */
1880 static int video_thread(void *arg)
1882 AVPacket pkt = { 0 };
1883 VideoState *is = arg;
1884 AVFrame *frame = av_frame_alloc();
1890 AVFilterGraph *graph = avfilter_graph_alloc();
1891 AVFilterContext *filt_out = NULL, *filt_in = NULL;
1894 enum AVPixelFormat last_format = -2;
1895 int last_serial = -1;
1899 while (is->paused && !is->videoq.abort_request)
1902 avcodec_get_frame_defaults(frame);
1903 av_free_packet(&pkt);
1905 ret = get_video_frame(is, frame, &pkt, &serial);
1912 if ( last_w != frame->width
1913 || last_h != frame->height
1914 || last_format != frame->format
1915 || last_serial != serial) {
1916 av_log(NULL, AV_LOG_DEBUG,
1917 "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
1919 (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
1920 frame->width, frame->height,
1921 (const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), serial);
1922 avfilter_graph_free(&graph);
1923 graph = avfilter_graph_alloc();
1924 if ((ret = configure_video_filters(graph, is, vfilters, frame)) < 0) {
1926 event.type = FF_QUIT_EVENT;
1927 event.user.data1 = is;
1928 SDL_PushEvent(&event);
1929 av_free_packet(&pkt);
1932 filt_in = is->in_video_filter;
1933 filt_out = is->out_video_filter;
1934 last_w = frame->width;
1935 last_h = frame->height;
1936 last_format = frame->format;
1937 last_serial = serial;
1940 ret = av_buffersrc_add_frame(filt_in, frame);
1943 av_frame_unref(frame);
1944 avcodec_get_frame_defaults(frame);
1945 av_free_packet(&pkt);
1948 is->frame_last_returned_time = av_gettime() / 1000000.0;
1950 ret = av_buffersink_get_frame_flags(filt_out, frame, 0);
1956 is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
1957 if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
1958 is->frame_last_filter_delay = 0;
1960 pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(filt_out->inputs[0]->time_base);
1961 ret = queue_picture(is, frame, pts, av_frame_get_pkt_pos(frame), serial);
1962 av_frame_unref(frame);
1965 pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(is->video_st->time_base);
1966 ret = queue_picture(is, frame, pts, pkt.pos, serial);
1967 av_frame_unref(frame);
1974 avcodec_flush_buffers(is->video_st->codec);
1976 avfilter_graph_free(&graph);
1978 av_free_packet(&pkt);
1979 av_frame_free(&frame);
1983 static int subtitle_thread(void *arg)
1985 VideoState *is = arg;
1987 AVPacket pkt1, *pkt = &pkt1;
1992 int r, g, b, y, u, v, a;
1995 while (is->paused && !is->subtitleq.abort_request) {
1998 if (packet_queue_get(&is->subtitleq, pkt, 1, &serial) < 0)
2001 if (pkt->data == flush_pkt.data) {
2002 avcodec_flush_buffers(is->subtitle_st->codec);
2005 SDL_LockMutex(is->subpq_mutex);
2006 while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
2007 !is->subtitleq.abort_request) {
2008 SDL_CondWait(is->subpq_cond, is->subpq_mutex);
2010 SDL_UnlockMutex(is->subpq_mutex);
2012 if (is->subtitleq.abort_request)
2015 sp = &is->subpq[is->subpq_windex];
2017 /* NOTE: ipts is the PTS of the _first_ picture beginning in
2018 this packet, if any */
2020 if (pkt->pts != AV_NOPTS_VALUE)
2021 pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
2023 avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
2024 &got_subtitle, pkt);
2025 if (got_subtitle && sp->sub.format == 0) {
2026 if (sp->sub.pts != AV_NOPTS_VALUE)
2027 pts = sp->sub.pts / (double)AV_TIME_BASE;
2029 sp->serial = serial;
2031 for (i = 0; i < sp->sub.num_rects; i++)
2033 for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
2035 RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
2036 y = RGB_TO_Y_CCIR(r, g, b);
2037 u = RGB_TO_U_CCIR(r, g, b, 0);
2038 v = RGB_TO_V_CCIR(r, g, b, 0);
2039 YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
2043 /* now we can update the picture count */
2044 if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
2045 is->subpq_windex = 0;
2046 SDL_LockMutex(is->subpq_mutex);
2048 SDL_UnlockMutex(is->subpq_mutex);
2050 av_free_packet(pkt);
2055 /* copy samples for viewing in editor window */
2056 static void update_sample_display(VideoState *is, short *samples, int samples_size)
2060 size = samples_size / sizeof(short);
2062 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
2065 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
2067 is->sample_array_index += len;
2068 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
2069 is->sample_array_index = 0;
2074 /* return the wanted number of samples to get better sync if sync_type is video
2075 * or external master clock */
2076 static int synchronize_audio(VideoState *is, int nb_samples)
2078 int wanted_nb_samples = nb_samples;
2080 /* if not master, then we try to remove or add samples to correct the clock */
2081 if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
2082 double diff, avg_diff;
2083 int min_nb_samples, max_nb_samples;
2085 diff = get_clock(&is->audclk) - get_master_clock(is);
2087 if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
2088 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
2089 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
2090 /* not enough measures to have a correct estimate */
2091 is->audio_diff_avg_count++;
2093 /* estimate the A-V difference */
2094 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
2096 if (fabs(avg_diff) >= is->audio_diff_threshold) {
2097 wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
2098 min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2099 max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
2100 wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
2102 av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n",
2103 diff, avg_diff, wanted_nb_samples - nb_samples,
2104 is->audio_clock, is->audio_diff_threshold);
2107 /* too big difference : may be initial PTS errors, so
2109 is->audio_diff_avg_count = 0;
2110 is->audio_diff_cum = 0;
2114 return wanted_nb_samples;
2118 * Decode one audio frame and return its uncompressed size.
2120 * The processed audio frame is decoded, converted if required, and
2121 * stored in is->audio_buf, with size in bytes given by the return
2124 static int audio_decode_frame(VideoState *is)
2126 AVPacket *pkt_temp = &is->audio_pkt_temp;
2127 AVPacket *pkt = &is->audio_pkt;
2128 AVCodecContext *dec = is->audio_st->codec;
2129 int len1, data_size, resampled_data_size;
2130 int64_t dec_channel_layout;
2132 av_unused double audio_clock0;
2133 int wanted_nb_samples;
2139 /* NOTE: the audio packet can contain several frames */
2140 while (pkt_temp->stream_index != -1 || is->audio_buf_frames_pending) {
2142 if (!(is->frame = avcodec_alloc_frame()))
2143 return AVERROR(ENOMEM);
2145 av_frame_unref(is->frame);
2146 avcodec_get_frame_defaults(is->frame);
2149 if (is->audioq.serial != is->audio_pkt_temp_serial)
2155 if (!is->audio_buf_frames_pending) {
2156 len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
2158 /* if error, we skip the frame */
2164 pkt_temp->pts = AV_NOPTS_VALUE;
2165 pkt_temp->data += len1;
2166 pkt_temp->size -= len1;
2167 if (pkt_temp->data && pkt_temp->size <= 0 || !pkt_temp->data && !got_frame)
2168 pkt_temp->stream_index = -1;
2173 tb = (AVRational){1, is->frame->sample_rate};
2174 if (is->frame->pts != AV_NOPTS_VALUE)
2175 is->frame->pts = av_rescale_q(is->frame->pts, dec->time_base, tb);
2176 else if (is->frame->pkt_pts != AV_NOPTS_VALUE)
2177 is->frame->pts = av_rescale_q(is->frame->pkt_pts, is->audio_st->time_base, tb);
2178 else if (is->audio_frame_next_pts != AV_NOPTS_VALUE)
2180 is->frame->pts = av_rescale_q(is->audio_frame_next_pts, (AVRational){1, is->audio_filter_src.freq}, tb);
2182 is->frame->pts = av_rescale_q(is->audio_frame_next_pts, (AVRational){1, is->audio_src.freq}, tb);
2185 if (is->frame->pts != AV_NOPTS_VALUE)
2186 is->audio_frame_next_pts = is->frame->pts + is->frame->nb_samples;
2189 dec_channel_layout = get_valid_channel_layout(is->frame->channel_layout, av_frame_get_channels(is->frame));
2192 cmp_audio_fmts(is->audio_filter_src.fmt, is->audio_filter_src.channels,
2193 is->frame->format, av_frame_get_channels(is->frame)) ||
2194 is->audio_filter_src.channel_layout != dec_channel_layout ||
2195 is->audio_filter_src.freq != is->frame->sample_rate ||
2196 is->audio_pkt_temp_serial != is->audio_last_serial;
2199 char buf1[1024], buf2[1024];
2200 av_get_channel_layout_string(buf1, sizeof(buf1), -1, is->audio_filter_src.channel_layout);
2201 av_get_channel_layout_string(buf2, sizeof(buf2), -1, dec_channel_layout);
2202 av_log(NULL, AV_LOG_DEBUG,
2203 "Audio frame changed from rate:%d ch:%d fmt:%s layout:%s serial:%d to rate:%d ch:%d fmt:%s layout:%s serial:%d\n",
2204 is->audio_filter_src.freq, is->audio_filter_src.channels, av_get_sample_fmt_name(is->audio_filter_src.fmt), buf1, is->audio_last_serial,
2205 is->frame->sample_rate, av_frame_get_channels(is->frame), av_get_sample_fmt_name(is->frame->format), buf2, is->audio_pkt_temp_serial);
2207 is->audio_filter_src.fmt = is->frame->format;
2208 is->audio_filter_src.channels = av_frame_get_channels(is->frame);
2209 is->audio_filter_src.channel_layout = dec_channel_layout;
2210 is->audio_filter_src.freq = is->frame->sample_rate;
2211 is->audio_last_serial = is->audio_pkt_temp_serial;
2213 if ((ret = configure_audio_filters(is, afilters, 1)) < 0)
2217 if ((ret = av_buffersrc_add_frame(is->in_audio_filter, is->frame)) < 0)
2219 av_frame_unref(is->frame);
2223 if ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, is->frame, 0)) < 0) {
2224 if (ret == AVERROR(EAGAIN)) {
2225 is->audio_buf_frames_pending = 0;
2230 is->audio_buf_frames_pending = 1;
2231 tb = is->out_audio_filter->inputs[0]->time_base;
2234 data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(is->frame),
2235 is->frame->nb_samples,
2236 is->frame->format, 1);
2238 dec_channel_layout =
2239 (is->frame->channel_layout && av_frame_get_channels(is->frame) == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
2240 is->frame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(is->frame));
2241 wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
2243 if (is->frame->format != is->audio_src.fmt ||
2244 dec_channel_layout != is->audio_src.channel_layout ||
2245 is->frame->sample_rate != is->audio_src.freq ||
2246 (wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) {
2247 swr_free(&is->swr_ctx);
2248 is->swr_ctx = swr_alloc_set_opts(NULL,
2249 is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
2250 dec_channel_layout, is->frame->format, is->frame->sample_rate,
2252 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
2253 av_log(NULL, AV_LOG_ERROR,
2254 "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
2255 is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), av_frame_get_channels(is->frame),
2256 is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
2259 is->audio_src.channel_layout = dec_channel_layout;
2260 is->audio_src.channels = av_frame_get_channels(is->frame);
2261 is->audio_src.freq = is->frame->sample_rate;
2262 is->audio_src.fmt = is->frame->format;
2266 const uint8_t **in = (const uint8_t **)is->frame->extended_data;
2267 uint8_t **out = &is->audio_buf1;
2268 int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate + 256;
2269 int out_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
2272 av_log(NULL, AV_LOG_ERROR, "av_samples_get_buffer_size() failed\n");
2275 if (wanted_nb_samples != is->frame->nb_samples) {
2276 if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
2277 wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
2278 av_log(NULL, AV_LOG_ERROR, "swr_set_compensation() failed\n");
2282 av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
2283 if (!is->audio_buf1)
2284 return AVERROR(ENOMEM);
2285 len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
2287 av_log(NULL, AV_LOG_ERROR, "swr_convert() failed\n");
2290 if (len2 == out_count) {
2291 av_log(NULL, AV_LOG_WARNING, "audio buffer is probably too small\n");
2292 swr_init(is->swr_ctx);
2294 is->audio_buf = is->audio_buf1;
2295 resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2297 is->audio_buf = is->frame->data[0];
2298 resampled_data_size = data_size;
2301 audio_clock0 = is->audio_clock;
2302 /* update the audio clock with the pts */
2303 if (is->frame->pts != AV_NOPTS_VALUE)
2304 is->audio_clock = is->frame->pts * av_q2d(tb) + (double) is->frame->nb_samples / is->frame->sample_rate;
2306 is->audio_clock = NAN;
2307 is->audio_clock_serial = is->audio_pkt_temp_serial;
2310 static double last_clock;
2311 printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
2312 is->audio_clock - last_clock,
2313 is->audio_clock, audio_clock0);
2314 last_clock = is->audio_clock;
2317 return resampled_data_size;
2320 /* free the current packet */
2322 av_free_packet(pkt);
2323 memset(pkt_temp, 0, sizeof(*pkt_temp));
2324 pkt_temp->stream_index = -1;
2326 if (is->audioq.abort_request) {
2330 if (is->audioq.nb_packets == 0)
2331 SDL_CondSignal(is->continue_read_thread);
2333 /* read next packet */
2334 if ((packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
2337 if (pkt->data == flush_pkt.data) {
2338 avcodec_flush_buffers(dec);
2339 is->audio_buf_frames_pending = 0;
2340 is->audio_frame_next_pts = AV_NOPTS_VALUE;
2341 if ((is->ic->iformat->flags & (AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK)) && !is->ic->iformat->read_seek)
2342 is->audio_frame_next_pts = is->audio_st->start_time;
2349 /* prepare a new audio buffer */
2350 static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
2352 VideoState *is = opaque;
2353 int audio_size, len1;
2355 int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
2357 audio_callback_time = av_gettime();
2360 if (is->audio_buf_index >= is->audio_buf_size) {
2361 audio_size = audio_decode_frame(is);
2362 if (audio_size < 0) {
2363 /* if error, just output silence */
2364 is->audio_buf = is->silence_buf;
2365 is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
2367 if (is->show_mode != SHOW_MODE_VIDEO)
2368 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
2369 is->audio_buf_size = audio_size;
2371 is->audio_buf_index = 0;
2373 len1 = is->audio_buf_size - is->audio_buf_index;
2376 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
2379 is->audio_buf_index += len1;
2381 bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
2382 is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
2383 /* Let's assume the audio driver that is used by SDL has two periods. */
2384 if (!isnan(is->audio_clock)) {
2385 set_clock_at(&is->audclk, is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec, is->audio_clock_serial, audio_callback_time / 1000000.0);
2386 sync_clock_to_slave(&is->extclk, &is->audclk);
2390 static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
2392 SDL_AudioSpec wanted_spec, spec;
2394 const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
2396 env = SDL_getenv("SDL_AUDIO_CHANNELS");
2398 wanted_nb_channels = atoi(env);
2399 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2401 if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
2402 wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
2403 wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
2405 wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
2406 wanted_spec.freq = wanted_sample_rate;
2407 if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
2408 av_log(NULL, AV_LOG_ERROR, "Invalid sample rate or channel count!\n");
2411 wanted_spec.format = AUDIO_S16SYS;
2412 wanted_spec.silence = 0;
2413 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
2414 wanted_spec.callback = sdl_audio_callback;
2415 wanted_spec.userdata = opaque;
2416 while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
2417 av_log(NULL, AV_LOG_WARNING, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError());
2418 wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
2419 if (!wanted_spec.channels) {
2420 av_log(NULL, AV_LOG_ERROR,
2421 "No more channel combinations to try, audio open failed\n");
2424 wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
2426 if (spec.format != AUDIO_S16SYS) {
2427 av_log(NULL, AV_LOG_ERROR,
2428 "SDL advised audio format %d is not supported!\n", spec.format);
2431 if (spec.channels != wanted_spec.channels) {
2432 wanted_channel_layout = av_get_default_channel_layout(spec.channels);
2433 if (!wanted_channel_layout) {
2434 av_log(NULL, AV_LOG_ERROR,
2435 "SDL advised channel count %d is not supported!\n", spec.channels);
2440 audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
2441 audio_hw_params->freq = spec.freq;
2442 audio_hw_params->channel_layout = wanted_channel_layout;
2443 audio_hw_params->channels = spec.channels;
2447 /* open a given stream. Return 0 if OK */
2448 static int stream_component_open(VideoState *is, int stream_index)
2450 AVFormatContext *ic = is->ic;
2451 AVCodecContext *avctx;
2453 const char *forced_codec_name = NULL;
2455 AVDictionaryEntry *t = NULL;
2456 int sample_rate, nb_channels;
2457 int64_t channel_layout;
2460 if (stream_index < 0 || stream_index >= ic->nb_streams)
2462 avctx = ic->streams[stream_index]->codec;
2464 codec = avcodec_find_decoder(avctx->codec_id);
2466 switch(avctx->codec_type){
2467 case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; forced_codec_name = audio_codec_name; break;
2468 case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
2469 case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; forced_codec_name = video_codec_name; break;
2471 if (forced_codec_name)
2472 codec = avcodec_find_decoder_by_name(forced_codec_name);
2474 if (forced_codec_name) av_log(NULL, AV_LOG_WARNING,
2475 "No codec could be found with name '%s'\n", forced_codec_name);
2476 else av_log(NULL, AV_LOG_WARNING,
2477 "No codec could be found with id %d\n", avctx->codec_id);
2481 avctx->codec_id = codec->id;
2482 avctx->workaround_bugs = workaround_bugs;
2483 avctx->lowres = lowres;
2484 if(avctx->lowres > codec->max_lowres){
2485 av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
2487 avctx->lowres= codec->max_lowres;
2489 avctx->error_concealment = error_concealment;
2491 if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
2492 if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
2493 if(codec->capabilities & CODEC_CAP_DR1)
2494 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2496 opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
2497 if (!av_dict_get(opts, "threads", NULL, 0))
2498 av_dict_set(&opts, "threads", "auto", 0);
2500 av_dict_set(&opts, "lowres", av_asprintf("%d", avctx->lowres), AV_DICT_DONT_STRDUP_VAL);
2501 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
2502 av_dict_set(&opts, "refcounted_frames", "1", 0);
2503 if (avcodec_open2(avctx, codec, &opts) < 0)
2505 if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2506 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2507 return AVERROR_OPTION_NOT_FOUND;
2510 ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2511 switch (avctx->codec_type) {
2512 case AVMEDIA_TYPE_AUDIO:
2517 is->audio_filter_src.freq = avctx->sample_rate;
2518 is->audio_filter_src.channels = avctx->channels;
2519 is->audio_filter_src.channel_layout = get_valid_channel_layout(avctx->channel_layout, avctx->channels);
2520 is->audio_filter_src.fmt = avctx->sample_fmt;
2521 if ((ret = configure_audio_filters(is, afilters, 0)) < 0)
2523 link = is->out_audio_filter->inputs[0];
2524 sample_rate = link->sample_rate;
2525 nb_channels = link->channels;
2526 channel_layout = link->channel_layout;
2529 sample_rate = avctx->sample_rate;
2530 nb_channels = avctx->channels;
2531 channel_layout = avctx->channel_layout;
2534 /* prepare audio output */
2535 if ((ret = audio_open(is, channel_layout, nb_channels, sample_rate, &is->audio_tgt)) < 0)
2537 is->audio_hw_buf_size = ret;
2538 is->audio_src = is->audio_tgt;
2539 is->audio_buf_size = 0;
2540 is->audio_buf_index = 0;
2542 /* init averaging filter */
2543 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
2544 is->audio_diff_avg_count = 0;
2545 /* since we do not have a precise anough audio fifo fullness,
2546 we correct audio sync only if larger than this threshold */
2547 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);
2549 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
2550 memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
2551 is->audio_pkt_temp.stream_index = -1;
2553 is->audio_stream = stream_index;
2554 is->audio_st = ic->streams[stream_index];
2556 packet_queue_start(&is->audioq);
2559 case AVMEDIA_TYPE_VIDEO:
2560 is->video_stream = stream_index;
2561 is->video_st = ic->streams[stream_index];
2563 packet_queue_start(&is->videoq);
2564 is->video_tid = SDL_CreateThread(video_thread, is);
2565 is->queue_attachments_req = 1;
2567 case AVMEDIA_TYPE_SUBTITLE:
2568 is->subtitle_stream = stream_index;
2569 is->subtitle_st = ic->streams[stream_index];
2570 packet_queue_start(&is->subtitleq);
2572 is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
2580 static void stream_component_close(VideoState *is, int stream_index)
2582 AVFormatContext *ic = is->ic;
2583 AVCodecContext *avctx;
2585 if (stream_index < 0 || stream_index >= ic->nb_streams)
2587 avctx = ic->streams[stream_index]->codec;
2589 switch (avctx->codec_type) {
2590 case AVMEDIA_TYPE_AUDIO:
2591 packet_queue_abort(&is->audioq);
2595 packet_queue_flush(&is->audioq);
2596 av_free_packet(&is->audio_pkt);
2597 swr_free(&is->swr_ctx);
2598 av_freep(&is->audio_buf1);
2599 is->audio_buf1_size = 0;
2600 is->audio_buf = NULL;
2601 av_frame_free(&is->frame);
2604 av_rdft_end(is->rdft);
2605 av_freep(&is->rdft_data);
2610 avfilter_graph_free(&is->agraph);
2613 case AVMEDIA_TYPE_VIDEO:
2614 packet_queue_abort(&is->videoq);
2616 /* note: we also signal this mutex to make sure we deblock the
2617 video thread in all cases */
2618 SDL_LockMutex(is->pictq_mutex);
2619 SDL_CondSignal(is->pictq_cond);
2620 SDL_UnlockMutex(is->pictq_mutex);
2622 SDL_WaitThread(is->video_tid, NULL);
2624 packet_queue_flush(&is->videoq);
2626 case AVMEDIA_TYPE_SUBTITLE:
2627 packet_queue_abort(&is->subtitleq);
2629 /* note: we also signal this mutex to make sure we deblock the
2630 video thread in all cases */
2631 SDL_LockMutex(is->subpq_mutex);
2632 SDL_CondSignal(is->subpq_cond);
2633 SDL_UnlockMutex(is->subpq_mutex);
2635 SDL_WaitThread(is->subtitle_tid, NULL);
2637 packet_queue_flush(&is->subtitleq);
2643 ic->streams[stream_index]->discard = AVDISCARD_ALL;
2644 avcodec_close(avctx);
2645 switch (avctx->codec_type) {
2646 case AVMEDIA_TYPE_AUDIO:
2647 is->audio_st = NULL;
2648 is->audio_stream = -1;
2650 case AVMEDIA_TYPE_VIDEO:
2651 is->video_st = NULL;
2652 is->video_stream = -1;
2654 case AVMEDIA_TYPE_SUBTITLE:
2655 is->subtitle_st = NULL;
2656 is->subtitle_stream = -1;
2663 static int decode_interrupt_cb(void *ctx)
2665 VideoState *is = ctx;
2666 return is->abort_request;
2669 static int is_realtime(AVFormatContext *s)
2671 if( !strcmp(s->iformat->name, "rtp")
2672 || !strcmp(s->iformat->name, "rtsp")
2673 || !strcmp(s->iformat->name, "sdp")
2677 if(s->pb && ( !strncmp(s->filename, "rtp:", 4)
2678 || !strncmp(s->filename, "udp:", 4)
2685 /* this thread gets the stream from the disk or the network */
2686 static int read_thread(void *arg)
2688 VideoState *is = arg;
2689 AVFormatContext *ic = NULL;
2691 int st_index[AVMEDIA_TYPE_NB];
2692 AVPacket pkt1, *pkt = &pkt1;
2694 int64_t stream_start_time;
2695 int pkt_in_play_range = 0;
2696 AVDictionaryEntry *t;
2697 AVDictionary **opts;
2698 int orig_nb_streams;
2699 SDL_mutex *wait_mutex = SDL_CreateMutex();
2701 memset(st_index, -1, sizeof(st_index));
2702 is->last_video_stream = is->video_stream = -1;
2703 is->last_audio_stream = is->audio_stream = -1;
2704 is->last_subtitle_stream = is->subtitle_stream = -1;
2706 ic = avformat_alloc_context();
2707 ic->interrupt_callback.callback = decode_interrupt_cb;
2708 ic->interrupt_callback.opaque = is;
2709 err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
2711 print_error(is->filename, err);
2715 if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
2716 av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
2717 ret = AVERROR_OPTION_NOT_FOUND;
2723 ic->flags |= AVFMT_FLAG_GENPTS;
2725 opts = setup_find_stream_info_opts(ic, codec_opts);
2726 orig_nb_streams = ic->nb_streams;
2728 err = avformat_find_stream_info(ic, opts);
2730 av_log(NULL, AV_LOG_WARNING,
2731 "%s: could not find codec parameters\n", is->filename);
2735 for (i = 0; i < orig_nb_streams; i++)
2736 av_dict_free(&opts[i]);
2740 ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
2742 if (seek_by_bytes < 0)
2743 seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT) && strcmp("ogg", ic->iformat->name);
2745 is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
2747 if (!window_title && (t = av_dict_get(ic->metadata, "title", NULL, 0)))
2748 window_title = av_asprintf("%s - %s", t->value, input_filename);
2750 /* if seeking requested, we execute it */
2751 if (start_time != AV_NOPTS_VALUE) {
2754 timestamp = start_time;
2755 /* add the stream start time */
2756 if (ic->start_time != AV_NOPTS_VALUE)
2757 timestamp += ic->start_time;
2758 ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
2760 av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
2761 is->filename, (double)timestamp / AV_TIME_BASE);
2765 is->realtime = is_realtime(ic);
2767 for (i = 0; i < ic->nb_streams; i++)
2768 ic->streams[i]->discard = AVDISCARD_ALL;
2770 st_index[AVMEDIA_TYPE_VIDEO] =
2771 av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2772 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
2774 st_index[AVMEDIA_TYPE_AUDIO] =
2775 av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
2776 wanted_stream[AVMEDIA_TYPE_AUDIO],
2777 st_index[AVMEDIA_TYPE_VIDEO],
2779 if (!video_disable && !subtitle_disable)
2780 st_index[AVMEDIA_TYPE_SUBTITLE] =
2781 av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
2782 wanted_stream[AVMEDIA_TYPE_SUBTITLE],
2783 (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
2784 st_index[AVMEDIA_TYPE_AUDIO] :
2785 st_index[AVMEDIA_TYPE_VIDEO]),
2788 av_dump_format(ic, 0, is->filename, 0);
2791 is->show_mode = show_mode;
2793 /* open the streams */
2794 if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
2795 stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
2799 if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
2800 ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
2802 if (is->show_mode == SHOW_MODE_NONE)
2803 is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
2805 if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
2806 stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
2809 if (is->video_stream < 0 && is->audio_stream < 0) {
2810 av_log(NULL, AV_LOG_FATAL, "Failed to open file '%s' or configure filtergraph\n",
2816 if (infinite_buffer < 0 && is->realtime)
2817 infinite_buffer = 1;
2820 if (is->abort_request)
2822 if (is->paused != is->last_paused) {
2823 is->last_paused = is->paused;
2825 is->read_pause_return = av_read_pause(ic);
2829 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
2831 (!strcmp(ic->iformat->name, "rtsp") ||
2832 (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
2833 /* wait 10 ms to avoid trying to get another packet */
2840 int64_t seek_target = is->seek_pos;
2841 int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
2842 int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
2843 // FIXME the +-2 is due to rounding being not done in the correct direction in generation
2844 // of the seek_pos/seek_rel variables
2846 ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
2848 av_log(NULL, AV_LOG_ERROR,
2849 "%s: error while seeking\n", is->ic->filename);
2851 if (is->audio_stream >= 0) {
2852 packet_queue_flush(&is->audioq);
2853 packet_queue_put(&is->audioq, &flush_pkt);
2855 if (is->subtitle_stream >= 0) {
2856 packet_queue_flush(&is->subtitleq);
2857 packet_queue_put(&is->subtitleq, &flush_pkt);
2859 if (is->video_stream >= 0) {
2860 packet_queue_flush(&is->videoq);
2861 packet_queue_put(&is->videoq, &flush_pkt);
2863 if (is->seek_flags & AVSEEK_FLAG_BYTE) {
2864 set_clock(&is->extclk, NAN, 0);
2866 set_clock(&is->extclk, seek_target / (double)AV_TIME_BASE, 0);
2870 is->queue_attachments_req = 1;
2873 step_to_next_frame(is);
2875 if (is->queue_attachments_req) {
2876 if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
2878 if ((ret = av_copy_packet(©, &is->video_st->attached_pic)) < 0)
2880 packet_queue_put(&is->videoq, ©);
2882 is->queue_attachments_req = 0;
2885 /* if the queue are full, no need to read more */
2886 if (infinite_buffer<1 &&
2887 (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
2888 || ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
2889 && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request
2890 || (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2891 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
2893 SDL_LockMutex(wait_mutex);
2894 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2895 SDL_UnlockMutex(wait_mutex);
2899 if (is->video_stream >= 0) {
2900 av_init_packet(pkt);
2903 pkt->stream_index = is->video_stream;
2904 packet_queue_put(&is->videoq, pkt);
2906 if (is->audio_stream >= 0) {
2907 av_init_packet(pkt);
2910 pkt->stream_index = is->audio_stream;
2911 packet_queue_put(&is->audioq, pkt);
2914 if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
2915 if (loop != 1 && (!loop || --loop)) {
2916 stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
2917 } else if (autoexit) {
2925 ret = av_read_frame(ic, pkt);
2927 if (ret == AVERROR_EOF || url_feof(ic->pb))
2929 if (ic->pb && ic->pb->error)
2931 SDL_LockMutex(wait_mutex);
2932 SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
2933 SDL_UnlockMutex(wait_mutex);
2936 /* check if packet is in play range specified by user, then queue, otherwise discard */
2937 stream_start_time = ic->streams[pkt->stream_index]->start_time;
2938 pkt_in_play_range = duration == AV_NOPTS_VALUE ||
2939 (pkt->pts - (stream_start_time != AV_NOPTS_VALUE ? stream_start_time : 0)) *
2940 av_q2d(ic->streams[pkt->stream_index]->time_base) -
2941 (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
2942 <= ((double)duration / 1000000);
2943 if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
2944 packet_queue_put(&is->audioq, pkt);
2945 } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
2946 && !(is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
2947 packet_queue_put(&is->videoq, pkt);
2948 } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
2949 packet_queue_put(&is->subtitleq, pkt);
2951 av_free_packet(pkt);
2954 /* wait until the end */
2955 while (!is->abort_request) {
2961 /* close each stream */
2962 if (is->audio_stream >= 0)
2963 stream_component_close(is, is->audio_stream);
2964 if (is->video_stream >= 0)
2965 stream_component_close(is, is->video_stream);
2966 if (is->subtitle_stream >= 0)
2967 stream_component_close(is, is->subtitle_stream);
2969 avformat_close_input(&is->ic);
2975 event.type = FF_QUIT_EVENT;
2976 event.user.data1 = is;
2977 SDL_PushEvent(&event);
2979 SDL_DestroyMutex(wait_mutex);
2983 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
2987 is = av_mallocz(sizeof(VideoState));
2990 av_strlcpy(is->filename, filename, sizeof(is->filename));
2991 is->iformat = iformat;
2995 /* start video display */
2996 is->pictq_mutex = SDL_CreateMutex();
2997 is->pictq_cond = SDL_CreateCond();
2999 is->subpq_mutex = SDL_CreateMutex();
3000 is->subpq_cond = SDL_CreateCond();
3002 packet_queue_init(&is->videoq);
3003 packet_queue_init(&is->audioq);
3004 packet_queue_init(&is->subtitleq);
3006 is->continue_read_thread = SDL_CreateCond();
3008 init_clock(&is->vidclk, &is->videoq.serial);
3009 init_clock(&is->audclk, &is->audioq.serial);
3010 init_clock(&is->extclk, &is->extclk.serial);
3011 is->audio_clock_serial = -1;
3012 is->audio_last_serial = -1;
3013 is->av_sync_type = av_sync_type;
3014 is->read_tid = SDL_CreateThread(read_thread, is);
3015 if (!is->read_tid) {
3022 static void stream_cycle_channel(VideoState *is, int codec_type)
3024 AVFormatContext *ic = is->ic;
3025 int start_index, stream_index;
3029 if (codec_type == AVMEDIA_TYPE_VIDEO) {
3030 start_index = is->last_video_stream;
3031 old_index = is->video_stream;
3032 } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
3033 start_index = is->last_audio_stream;
3034 old_index = is->audio_stream;
3036 start_index = is->last_subtitle_stream;
3037 old_index = is->subtitle_stream;
3039 stream_index = start_index;
3041 if (++stream_index >= is->ic->nb_streams)
3043 if (codec_type == AVMEDIA_TYPE_SUBTITLE)
3046 is->last_subtitle_stream = -1;
3049 if (start_index == -1)
3053 if (stream_index == start_index)
3055 st = ic->streams[stream_index];
3056 if (st->codec->codec_type == codec_type) {
3057 /* check that parameters are OK */
3058 switch (codec_type) {
3059 case AVMEDIA_TYPE_AUDIO:
3060 if (st->codec->sample_rate != 0 &&
3061 st->codec->channels != 0)
3064 case AVMEDIA_TYPE_VIDEO:
3065 case AVMEDIA_TYPE_SUBTITLE:
3073 stream_component_close(is, old_index);
3074 stream_component_open(is, stream_index);
3078 static void toggle_full_screen(VideoState *is)
3080 #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
3081 /* OS X needs to reallocate the SDL overlays */
3083 for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
3084 is->pictq[i].reallocate = 1;
3086 is_full_screen = !is_full_screen;
3087 video_open(is, 1, NULL);
3090 static void toggle_audio_display(VideoState *is)
3092 int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
3093 int next = is->show_mode;
3095 next = (next + 1) % SHOW_MODE_NB;
3096 } while (next != is->show_mode && (next == SHOW_MODE_VIDEO && !is->video_st || next != SHOW_MODE_VIDEO && !is->audio_st));
3097 if (is->show_mode != next) {
3098 fill_rectangle(screen,
3099 is->xleft, is->ytop, is->width, is->height,
3101 is->force_refresh = 1;
3102 is->show_mode = next;
3106 static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) {
3107 double remaining_time = 0.0;
3109 while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
3110 if (!cursor_hidden && av_gettime() - cursor_last_shown > CURSOR_HIDE_DELAY) {
3114 if (remaining_time > 0.0)
3115 av_usleep((int64_t)(remaining_time * 1000000.0));
3116 remaining_time = REFRESH_RATE;
3117 if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh))
3118 video_refresh(is, &remaining_time);
3123 /* handle an event sent by the GUI */
3124 static void event_loop(VideoState *cur_stream)
3127 double incr, pos, frac;
3131 refresh_loop_wait_event(cur_stream, &event);
3132 switch (event.type) {
3134 if (exit_on_keydown) {
3135 do_exit(cur_stream);
3138 switch (event.key.keysym.sym) {
3141 do_exit(cur_stream);
3144 toggle_full_screen(cur_stream);
3145 cur_stream->force_refresh = 1;
3149 toggle_pause(cur_stream);
3151 case SDLK_s: // S: Step to next frame
3152 step_to_next_frame(cur_stream);
3155 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
3158 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
3161 stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
3164 toggle_audio_display(cur_stream);
3184 if (seek_by_bytes) {
3185 if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
3186 pos = cur_stream->video_current_pos;
3187 } else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
3188 pos = cur_stream->audio_pkt.pos;
3190 pos = avio_tell(cur_stream->ic->pb);
3191 if (cur_stream->ic->bit_rate)
3192 incr *= cur_stream->ic->bit_rate / 8.0;
3196 stream_seek(cur_stream, pos, incr, 1);
3198 pos = get_master_clock(cur_stream);
3200 pos = (double)cur_stream->seek_pos / AV_TIME_BASE;
3202 if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
3203 pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
3204 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
3211 case SDL_VIDEOEXPOSE:
3212 cur_stream->force_refresh = 1;
3214 case SDL_MOUSEBUTTONDOWN:
3215 if (exit_on_mousedown) {
3216 do_exit(cur_stream);
3219 case SDL_MOUSEMOTION:
3220 if (cursor_hidden) {
3224 cursor_last_shown = av_gettime();
3225 if (event.type == SDL_MOUSEBUTTONDOWN) {
3228 if (event.motion.state != SDL_PRESSED)
3232 if (seek_by_bytes || cur_stream->ic->duration <= 0) {
3233 uint64_t size = avio_size(cur_stream->ic->pb);
3234 stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
3238 int tns, thh, tmm, tss;
3239 tns = cur_stream->ic->duration / 1000000LL;
3241 tmm = (tns % 3600) / 60;
3243 frac = x / cur_stream->width;
3246 mm = (ns % 3600) / 60;
3248 av_log(NULL, AV_LOG_INFO,
3249 "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
3250 hh, mm, ss, thh, tmm, tss);
3251 ts = frac * cur_stream->ic->duration;
3252 if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
3253 ts += cur_stream->ic->start_time;
3254 stream_seek(cur_stream, ts, 0, 0);
3257 case SDL_VIDEORESIZE:
3258 screen = SDL_SetVideoMode(FFMIN(16383, event.resize.w), event.resize.h, 0,
3259 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
3261 av_log(NULL, AV_LOG_FATAL, "Failed to set video mode\n");
3262 do_exit(cur_stream);
3264 screen_width = cur_stream->width = screen->w;
3265 screen_height = cur_stream->height = screen->h;
3266 cur_stream->force_refresh = 1;
3270 do_exit(cur_stream);
3272 case FF_ALLOC_EVENT:
3273 alloc_picture(event.user.data1);
3281 static int opt_frame_size(void *optctx, const char *opt, const char *arg)
3283 av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
3284 return opt_default(NULL, "video_size", arg);
3287 static int opt_width(void *optctx, const char *opt, const char *arg)
3289 screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3293 static int opt_height(void *optctx, const char *opt, const char *arg)
3295 screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
3299 static int opt_format(void *optctx, const char *opt, const char *arg)
3301 file_iformat = av_find_input_format(arg);
3302 if (!file_iformat) {
3303 av_log(NULL, AV_LOG_FATAL, "Unknown input format: %s\n", arg);
3304 return AVERROR(EINVAL);
3309 static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
3311 av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
3312 return opt_default(NULL, "pixel_format", arg);
3315 static int opt_sync(void *optctx, const char *opt, const char *arg)
3317 if (!strcmp(arg, "audio"))
3318 av_sync_type = AV_SYNC_AUDIO_MASTER;
3319 else if (!strcmp(arg, "video"))
3320 av_sync_type = AV_SYNC_VIDEO_MASTER;
3321 else if (!strcmp(arg, "ext"))
3322 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
3324 av_log(NULL, AV_LOG_ERROR, "Unknown value for %s: %s\n", opt, arg);
3330 static int opt_seek(void *optctx, const char *opt, const char *arg)
3332 start_time = parse_time_or_die(opt, arg, 1);
3336 static int opt_duration(void *optctx, const char *opt, const char *arg)
3338 duration = parse_time_or_die(opt, arg, 1);
3342 static int opt_show_mode(void *optctx, const char *opt, const char *arg)
3344 show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
3345 !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
3346 !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
3347 parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
3351 static void opt_input_file(void *optctx, const char *filename)
3353 if (input_filename) {
3354 av_log(NULL, AV_LOG_FATAL,
3355 "Argument '%s' provided as input filename, but '%s' was already specified.\n",
3356 filename, input_filename);
3359 if (!strcmp(filename, "-"))
3361 input_filename = filename;
3364 static int opt_codec(void *optctx, const char *opt, const char *arg)
3366 const char *spec = strchr(opt, ':');
3368 av_log(NULL, AV_LOG_ERROR,
3369 "No media specifier was specified in '%s' in option '%s'\n",
3371 return AVERROR(EINVAL);
3375 case 'a' : audio_codec_name = arg; break;
3376 case 's' : subtitle_codec_name = arg; break;
3377 case 'v' : video_codec_name = arg; break;
3379 av_log(NULL, AV_LOG_ERROR,
3380 "Invalid media specifier '%s' in option '%s'\n", spec, opt);
3381 return AVERROR(EINVAL);
3388 static const OptionDef options[] = {
3389 #include "cmdutils_common_opts.h"
3390 { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
3391 { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
3392 { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
3393 { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
3394 { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
3395 { "vn", OPT_BOOL, { &video_disable }, "disable video" },
3396 { "sn", OPT_BOOL, { &subtitle_disable }, "disable subtitling" },
3397 { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
3398 { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
3399 { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
3400 { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
3401 { "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
3402 { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
3403 { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
3404 { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
3405 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
3406 { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
3407 { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
3408 { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
3409 { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
3410 { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
3411 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
3412 { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options", "bit_mask" },
3413 { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
3414 { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
3415 { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
3416 { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
3417 { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
3418 { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
3419 { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
3420 { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
3422 { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "set video filters", "filter_graph" },
3423 { "af", OPT_STRING | HAS_ARG, { &afilters }, "set audio filters", "filter_graph" },
3425 { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
3426 { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
3427 { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
3428 { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
3429 { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
3430 { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &audio_codec_name }, "force audio decoder", "decoder_name" },
3431 { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
3432 { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &video_codec_name }, "force video decoder", "decoder_name" },
3436 static void show_usage(void)
3438 av_log(NULL, AV_LOG_INFO, "Simple media player\n");
3439 av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
3440 av_log(NULL, AV_LOG_INFO, "\n");
3443 void show_help_default(const char *opt, const char *arg)
3445 av_log_set_callback(log_callback_help);
3447 show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
3448 show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3450 show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3451 show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3452 #if !CONFIG_AVFILTER
3453 show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
3455 show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
3457 printf("\nWhile playing:\n"
3459 "f toggle full screen\n"
3461 "a cycle audio channel\n"
3462 "v cycle video channel\n"
3463 "t cycle subtitle channel\n"
3464 "w show audio waves\n"
3465 "s activate frame-step mode\n"
3466 "left/right seek backward/forward 10 seconds\n"
3467 "down/up seek backward/forward 1 minute\n"
3468 "page down/page up seek backward/forward 10 minutes\n"
3469 "mouse click seek to percentage in file corresponding to fraction of width\n"
3473 static int lockmgr(void **mtx, enum AVLockOp op)
3476 case AV_LOCK_CREATE:
3477 *mtx = SDL_CreateMutex();
3481 case AV_LOCK_OBTAIN:
3482 return !!SDL_LockMutex(*mtx);
3483 case AV_LOCK_RELEASE:
3484 return !!SDL_UnlockMutex(*mtx);
3485 case AV_LOCK_DESTROY:
3486 SDL_DestroyMutex(*mtx);
3492 /* Called from the main */
3493 int main(int argc, char **argv)
3497 char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
3499 av_log_set_flags(AV_LOG_SKIP_REPEATED);
3500 parse_loglevel(argc, argv, options);
3502 /* register all codecs, demux and protocols */
3503 avcodec_register_all();
3505 avdevice_register_all();
3508 avfilter_register_all();
3511 avformat_network_init();
3515 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
3516 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
3518 show_banner(argc, argv, options);
3520 parse_options(NULL, argc, argv, options, opt_input_file);
3522 if (!input_filename) {
3524 av_log(NULL, AV_LOG_FATAL, "An input file must be specified\n");
3525 av_log(NULL, AV_LOG_FATAL,
3526 "Use -h to get full help or, even better, run 'man %s'\n", program_name);
3530 if (display_disable) {
3533 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
3535 flags &= ~SDL_INIT_AUDIO;
3536 if (display_disable)
3537 SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
3538 #if !defined(__MINGW32__) && !defined(__APPLE__)
3539 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
3541 if (SDL_Init (flags)) {
3542 av_log(NULL, AV_LOG_FATAL, "Could not initialize SDL - %s\n", SDL_GetError());
3543 av_log(NULL, AV_LOG_FATAL, "(Did you set the DISPLAY variable?)\n");
3547 if (!display_disable) {
3548 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
3549 fs_screen_width = vi->current_w;
3550 fs_screen_height = vi->current_h;
3553 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
3554 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
3555 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
3557 if (av_lockmgr_register(lockmgr)) {
3558 av_log(NULL, AV_LOG_FATAL, "Could not initialize lock manager!\n");
3562 av_init_packet(&flush_pkt);
3563 flush_pkt.data = (uint8_t *)&flush_pkt;
3565 is = stream_open(input_filename, file_iformat);
3567 av_log(NULL, AV_LOG_FATAL, "Failed to initialize VideoState!\n");