simplified version handling
[libav.git] / ffplay.c
1 /*
2 * FFplay : Simple Media Player based on the ffmpeg libraries
3 * Copyright (c) 2003 Fabrice Bellard
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #define HAVE_AV_CONFIG_H
20 #include "avformat.h"
21
22 #include "cmdutils.h"
23
24 #include <SDL.h>
25 #include <SDL_thread.h>
26
27 #ifdef CONFIG_WIN32
28 #undef main /* We don't want SDL to override our main() */
29 #endif
30
31 #if defined(__linux__)
32 #define HAVE_X11
33 #endif
34
35 #ifdef HAVE_X11
36 #include <X11/Xlib.h>
37 #endif
38
39 //#define DEBUG_SYNC
40
41 #define MAX_VIDEOQ_SIZE (5 * 256 * 1024)
42 #define MAX_AUDIOQ_SIZE (5 * 16 * 1024)
43
44 /* SDL audio buffer size, in samples. Should be small to have precise
45 A/V sync as SDL does not have hardware buffer fullness info. */
46 #define SDL_AUDIO_BUFFER_SIZE 1024
47
48 /* no AV sync correction is done if below the AV sync threshold */
49 #define AV_SYNC_THRESHOLD 0.08
50 /* no AV correction is done if too big error */
51 #define AV_NOSYNC_THRESHOLD 10.0
52
53 /* maximum audio speed change to get correct sync */
54 #define SAMPLE_CORRECTION_PERCENT_MAX 10
55
56 /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
57 #define AUDIO_DIFF_AVG_NB 20
58
59 /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
60 #define SAMPLE_ARRAY_SIZE (2*65536)
61
62 typedef struct PacketQueue {
63 AVPacketList *first_pkt, *last_pkt;
64 int nb_packets;
65 int size;
66 int abort_request;
67 SDL_mutex *mutex;
68 SDL_cond *cond;
69 } PacketQueue;
70
71 #define VIDEO_PICTURE_QUEUE_SIZE 1
72
73 typedef struct VideoPicture {
74 double pts; /* presentation time stamp for this picture */
75 SDL_Overlay *bmp;
76 int width, height; /* source height & width */
77 int allocated;
78 } VideoPicture;
79
80 enum {
81 AV_SYNC_AUDIO_MASTER, /* default choice */
82 AV_SYNC_VIDEO_MASTER,
83 AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
84 };
85
86 typedef struct VideoState {
87 SDL_Thread *parse_tid;
88 SDL_Thread *video_tid;
89 AVInputFormat *iformat;
90 int no_background;
91 int abort_request;
92 int paused;
93 int last_paused;
94 AVFormatContext *ic;
95 int dtg_active_format;
96
97 int audio_stream;
98
99 int av_sync_type;
100 double external_clock; /* external clock base */
101 int64_t external_clock_time;
102
103 double audio_clock;
104 double audio_diff_cum; /* used for AV difference average computation */
105 double audio_diff_avg_coef;
106 double audio_diff_threshold;
107 int audio_diff_avg_count;
108 AVStream *audio_st;
109 PacketQueue audioq;
110 int audio_hw_buf_size;
111 /* samples output by the codec. we reserve more space for avsync
112 compensation */
113 uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
114 int audio_buf_size; /* in bytes */
115 int audio_buf_index; /* in bytes */
116 AVPacket audio_pkt;
117 uint8_t *audio_pkt_data;
118 int audio_pkt_size;
119 int64_t audio_pkt_ipts;
120
121 int show_audio; /* if true, display audio samples */
122 int16_t sample_array[SAMPLE_ARRAY_SIZE];
123 int sample_array_index;
124 int last_i_start;
125
126 double frame_timer;
127 double frame_last_pts;
128 double frame_last_delay;
129 double video_clock;
130 int video_stream;
131 AVStream *video_st;
132 PacketQueue videoq;
133 int64_t ipts;
134 int picture_start; /* true if picture starts */
135 double video_last_P_pts; /* pts of the last P picture (needed if B
136 frames are present) */
137 double video_current_pts; /* current displayed pts (different from
138 video_clock if frame fifos are used) */
139 int64_t video_current_pts_time; /* time at which we updated
140 video_current_pts - used to
141 have running video pts */
142 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
143 int pictq_size, pictq_rindex, pictq_windex;
144 SDL_mutex *pictq_mutex;
145 SDL_cond *pictq_cond;
146
147 // QETimer *video_timer;
148 char filename[1024];
149 int width, height, xleft, ytop;
150 } VideoState;
151
152 void show_help(void);
153 static int audio_write_get_buf_size(VideoState *is);
154
155 /* options specified by the user */
156 static AVInputFormat *file_iformat;
157 static const char *input_filename;
158 static int fs_screen_width;
159 static int fs_screen_height;
160 static int screen_width = 640;
161 static int screen_height = 480;
162 static int audio_disable;
163 static int video_disable;
164 static int display_disable;
165 static int show_status;
166 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
167
168 /* current context */
169 static int is_full_screen;
170 static VideoState *cur_stream;
171 static int64_t audio_callback_time;
172
173 #define FF_ALLOC_EVENT (SDL_USEREVENT)
174 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
175 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
176
177 SDL_Surface *screen;
178
179 /* packet queue handling */
180 static void packet_queue_init(PacketQueue *q)
181 {
182 memset(q, 0, sizeof(PacketQueue));
183 q->mutex = SDL_CreateMutex();
184 q->cond = SDL_CreateCond();
185 }
186
187 static void packet_queue_end(PacketQueue *q)
188 {
189 AVPacketList *pkt, *pkt1;
190
191 for(pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
192 pkt1 = pkt->next;
193 av_free_packet(&pkt->pkt);
194 }
195 SDL_DestroyMutex(q->mutex);
196 SDL_DestroyCond(q->cond);
197 }
198
199 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
200 {
201 AVPacketList *pkt1;
202
203 pkt1 = av_malloc(sizeof(AVPacketList));
204 if (!pkt1)
205 return -1;
206 pkt1->pkt = *pkt;
207 pkt1->next = NULL;
208
209 SDL_LockMutex(q->mutex);
210
211 if (!q->last_pkt)
212
213 q->first_pkt = pkt1;
214 else
215 q->last_pkt->next = pkt1;
216 q->last_pkt = pkt1;
217 q->nb_packets++;
218 q->size += pkt1->pkt.size;
219 /* XXX: should duplicate packet data in DV case */
220 SDL_CondSignal(q->cond);
221
222 SDL_UnlockMutex(q->mutex);
223 return 0;
224 }
225
226 static void packet_queue_abort(PacketQueue *q)
227 {
228 SDL_LockMutex(q->mutex);
229
230 q->abort_request = 1;
231
232 SDL_CondSignal(q->cond);
233
234 SDL_UnlockMutex(q->mutex);
235 }
236
237 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
238 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
239 {
240 AVPacketList *pkt1;
241 int ret;
242
243 SDL_LockMutex(q->mutex);
244
245 for(;;) {
246 if (q->abort_request) {
247 ret = -1;
248 break;
249 }
250
251 pkt1 = q->first_pkt;
252 if (pkt1) {
253 q->first_pkt = pkt1->next;
254 if (!q->first_pkt)
255 q->last_pkt = NULL;
256 q->nb_packets--;
257 q->size -= pkt1->pkt.size;
258 *pkt = pkt1->pkt;
259 av_free(pkt1);
260 ret = 1;
261 break;
262 } else if (!block) {
263 ret = 0;
264 break;
265 } else {
266 SDL_CondWait(q->cond, q->mutex);
267 }
268 }
269 SDL_UnlockMutex(q->mutex);
270 return ret;
271 }
272
273 static inline void fill_rectangle(SDL_Surface *screen,
274 int x, int y, int w, int h, int color)
275 {
276 SDL_Rect rect;
277 rect.x = x;
278 rect.y = y;
279 rect.w = w;
280 rect.h = h;
281 SDL_FillRect(screen, &rect, color);
282 }
283
284 #if 0
285 /* draw only the border of a rectangle */
286 void fill_border(VideoState *s, int x, int y, int w, int h, int color)
287 {
288 int w1, w2, h1, h2;
289
290 /* fill the background */
291 w1 = x;
292 if (w1 < 0)
293 w1 = 0;
294 w2 = s->width - (x + w);
295 if (w2 < 0)
296 w2 = 0;
297 h1 = y;
298 if (h1 < 0)
299 h1 = 0;
300 h2 = s->height - (y + h);
301 if (h2 < 0)
302 h2 = 0;
303 fill_rectangle(screen,
304 s->xleft, s->ytop,
305 w1, s->height,
306 color);
307 fill_rectangle(screen,
308 s->xleft + s->width - w2, s->ytop,
309 w2, s->height,
310 color);
311 fill_rectangle(screen,
312 s->xleft + w1, s->ytop,
313 s->width - w1 - w2, h1,
314 color);
315 fill_rectangle(screen,
316 s->xleft + w1, s->ytop + s->height - h2,
317 s->width - w1 - w2, h2,
318 color);
319 }
320 #endif
321
322 static void video_image_display(VideoState *is)
323 {
324 VideoPicture *vp;
325 float aspect_ratio;
326 int width, height, x, y;
327 SDL_Rect rect;
328
329 vp = &is->pictq[is->pictq_rindex];
330 if (vp->bmp) {
331 /* XXX: use variable in the frame */
332 aspect_ratio = is->video_st->codec.aspect_ratio;
333 if (aspect_ratio <= 0.0)
334 aspect_ratio = (float)is->video_st->codec.width /
335 (float)is->video_st->codec.height;
336 /* if an active format is indicated, then it overrides the
337 mpeg format */
338 #if 0
339 if (is->video_st->codec.dtg_active_format != is->dtg_active_format) {
340 is->dtg_active_format = is->video_st->codec.dtg_active_format;
341 printf("dtg_active_format=%d\n", is->dtg_active_format);
342 }
343 #endif
344 #if 0
345 switch(is->video_st->codec.dtg_active_format) {
346 case FF_DTG_AFD_SAME:
347 default:
348 /* nothing to do */
349 break;
350 case FF_DTG_AFD_4_3:
351 aspect_ratio = 4.0 / 3.0;
352 break;
353 case FF_DTG_AFD_16_9:
354 aspect_ratio = 16.0 / 9.0;
355 break;
356 case FF_DTG_AFD_14_9:
357 aspect_ratio = 14.0 / 9.0;
358 break;
359 case FF_DTG_AFD_4_3_SP_14_9:
360 aspect_ratio = 14.0 / 9.0;
361 break;
362 case FF_DTG_AFD_16_9_SP_14_9:
363 aspect_ratio = 14.0 / 9.0;
364 break;
365 case FF_DTG_AFD_SP_4_3:
366 aspect_ratio = 4.0 / 3.0;
367 break;
368 }
369 #endif
370
371 /* XXX: we suppose the screen has a 1.0 pixel ratio */
372 height = is->height;
373 width = ((int)rint(height * aspect_ratio)) & -3;
374 if (width > is->width) {
375 width = is->width;
376 height = ((int)rint(width / aspect_ratio)) & -3;
377 }
378 x = (is->width - width) / 2;
379 y = (is->height - height) / 2;
380 if (!is->no_background) {
381 /* fill the background */
382 // fill_border(is, x, y, width, height, QERGB(0x00, 0x00, 0x00));
383 } else {
384 is->no_background = 0;
385 }
386 rect.x = is->xleft + x;
387 rect.y = is->xleft + y;
388 rect.w = width;
389 rect.h = height;
390 SDL_DisplayYUVOverlay(vp->bmp, &rect);
391 } else {
392 #if 0
393 fill_rectangle(screen,
394 is->xleft, is->ytop, is->width, is->height,
395 QERGB(0x00, 0x00, 0x00));
396 #endif
397 }
398 }
399
400 static inline int compute_mod(int a, int b)
401 {
402 a = a % b;
403 if (a >= 0)
404 return a;
405 else
406 return a + b;
407 }
408
409 static void video_audio_display(VideoState *s)
410 {
411 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
412 int ch, channels, h, h2, bgcolor, fgcolor;
413 int16_t time_diff;
414
415 /* compute display index : center on currently output samples */
416 channels = s->audio_st->codec.channels;
417 nb_display_channels = channels;
418 if (!s->paused) {
419 n = 2 * channels;
420 delay = audio_write_get_buf_size(s);
421 delay /= n;
422
423 /* to be more precise, we take into account the time spent since
424 the last buffer computation */
425 if (audio_callback_time) {
426 time_diff = av_gettime() - audio_callback_time;
427 delay += (time_diff * s->audio_st->codec.sample_rate) / 1000000;
428 }
429
430 delay -= s->width / 2;
431 if (delay < s->width)
432 delay = s->width;
433 i_start = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
434 s->last_i_start = i_start;
435 } else {
436 i_start = s->last_i_start;
437 }
438
439 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
440 fill_rectangle(screen,
441 s->xleft, s->ytop, s->width, s->height,
442 bgcolor);
443
444 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
445
446 /* total height for one channel */
447 h = s->height / nb_display_channels;
448 /* graph height / 2 */
449 h2 = (h * 9) / 20;
450 for(ch = 0;ch < nb_display_channels; ch++) {
451 i = i_start + ch;
452 y1 = s->ytop + ch * h + (h / 2); /* position of center line */
453 for(x = 0; x < s->width; x++) {
454 y = (s->sample_array[i] * h2) >> 15;
455 if (y < 0) {
456 y = -y;
457 ys = y1 - y;
458 } else {
459 ys = y1;
460 }
461 fill_rectangle(screen,
462 s->xleft + x, ys, 1, y,
463 fgcolor);
464 i += channels;
465 if (i >= SAMPLE_ARRAY_SIZE)
466 i -= SAMPLE_ARRAY_SIZE;
467 }
468 }
469
470 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
471
472 for(ch = 1;ch < nb_display_channels; ch++) {
473 y = s->ytop + ch * h;
474 fill_rectangle(screen,
475 s->xleft, y, s->width, 1,
476 fgcolor);
477 }
478 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
479 }
480
481 /* display the current picture, if any */
482 static void video_display(VideoState *is)
483 {
484 if (is->audio_st && is->show_audio)
485 video_audio_display(is);
486 else if (is->video_st)
487 video_image_display(is);
488 }
489
490 static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque)
491 {
492 SDL_Event event;
493 event.type = FF_REFRESH_EVENT;
494 event.user.data1 = opaque;
495 SDL_PushEvent(&event);
496 return 0; /* 0 means stop timer */
497 }
498
499 /* schedule a video refresh in 'delay' ms */
500 static void schedule_refresh(VideoState *is, int delay)
501 {
502 SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
503 }
504
505 /* get the current audio clock value */
506 static double get_audio_clock(VideoState *is)
507 {
508 double pts;
509 int hw_buf_size, bytes_per_sec;
510 pts = is->audio_clock;
511 hw_buf_size = audio_write_get_buf_size(is);
512 bytes_per_sec = 0;
513 if (is->audio_st) {
514 bytes_per_sec = is->audio_st->codec.sample_rate *
515 2 * is->audio_st->codec.channels;
516 }
517 if (bytes_per_sec)
518 pts -= (double)hw_buf_size / bytes_per_sec;
519 return pts;
520 }
521
522 /* get the current video clock value */
523 static double get_video_clock(VideoState *is)
524 {
525 double delta;
526 delta = (av_gettime() - is->video_current_pts_time) / 1000000.0;
527 return is->video_current_pts + delta;
528 }
529
530 /* get the current external clock value */
531 static double get_external_clock(VideoState *is)
532 {
533 int64_t ti;
534 ti = av_gettime();
535 return is->external_clock + ((ti - is->external_clock_time) * 1e-6);
536 }
537
538 /* get the current master clock value */
539 static double get_master_clock(VideoState *is)
540 {
541 double val;
542
543 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st)
544 val = get_video_clock(is);
545 else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st)
546 val = get_audio_clock(is);
547 else
548 val = get_external_clock(is);
549 return val;
550 }
551
552 /* called to display each frame */
553 static void video_refresh_timer(void *opaque)
554 {
555 VideoState *is = opaque;
556 VideoPicture *vp;
557 double actual_delay, delay, sync_threshold, ref_clock, diff;
558
559
560 if (is->video_st) {
561 if (is->pictq_size == 0) {
562 /* if no picture, need to wait */
563 schedule_refresh(is, 40);
564 } else {
565 /* dequeue the picture */
566 vp = &is->pictq[is->pictq_rindex];
567
568 /* update current video pts */
569 is->video_current_pts = vp->pts;
570 is->video_current_pts_time = av_gettime();
571
572 /* compute nominal delay */
573 delay = vp->pts - is->frame_last_pts;
574 if (delay <= 0 || delay >= 1.0) {
575 /* if incorrect delay, use previous one */
576 delay = is->frame_last_delay;
577 }
578 is->frame_last_delay = delay;
579 is->frame_last_pts = vp->pts;
580
581 /* update delay to follow master synchronisation source */
582 if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) ||
583 is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
584 /* if video is slave, we try to correct big delays by
585 duplicating or deleting a frame */
586 ref_clock = get_master_clock(is);
587 diff = vp->pts - ref_clock;
588
589 /* skip or repeat frame. We take into account the
590 delay to compute the threshold. I still don't know
591 if it is the best guess */
592 sync_threshold = AV_SYNC_THRESHOLD;
593 if (delay > sync_threshold)
594 sync_threshold = delay;
595 if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
596 if (diff <= -sync_threshold)
597 delay = 0;
598 else if (diff >= sync_threshold)
599 delay = 2 * delay;
600 }
601 }
602
603 is->frame_timer += delay;
604 /* compute the REAL delay (we need to do that to avoid
605 long term errors */
606 actual_delay = is->frame_timer - (av_gettime() / 1000000.0);
607 if (actual_delay < 0.010) {
608 /* XXX: should skip picture */
609 actual_delay = 0.010;
610 }
611 /* launch timer for next picture */
612 schedule_refresh(is, (int)(actual_delay * 1000 + 0.5));
613
614 #if defined(DEBUG_SYNC)
615 printf("video: delay=%0.3f actual_delay=%0.3f pts=%0.3f A-V=%f\n",
616 delay, actual_delay, vp->pts, -diff);
617 #endif
618
619 /* display picture */
620 video_display(is);
621
622 /* update queue size and signal for next picture */
623 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
624 is->pictq_rindex = 0;
625
626 SDL_LockMutex(is->pictq_mutex);
627 is->pictq_size--;
628 SDL_CondSignal(is->pictq_cond);
629 SDL_UnlockMutex(is->pictq_mutex);
630 }
631 } else if (is->audio_st) {
632 /* draw the next audio frame */
633
634 schedule_refresh(is, 40);
635
636 /* if only audio stream, then display the audio bars (better
637 than nothing, just to test the implementation */
638
639 /* display picture */
640 video_display(is);
641 } else {
642 schedule_refresh(is, 100);
643 }
644 if (show_status) {
645 static int64_t last_time;
646 int64_t cur_time;
647 int aqsize, vqsize;
648 double av_diff;
649
650 cur_time = av_gettime();
651 if (!last_time || (cur_time - last_time) >= 500 * 1000) {
652 aqsize = 0;
653 vqsize = 0;
654 if (is->audio_st)
655 aqsize = is->audioq.size;
656 if (is->video_st)
657 vqsize = is->videoq.size;
658 av_diff = 0;
659 if (is->audio_st && is->video_st)
660 av_diff = get_audio_clock(is) - get_video_clock(is);
661 printf("%7.2f A-V:%7.3f aq=%5dKB vq=%5dKB \r",
662 get_master_clock(is), av_diff, aqsize / 1024, vqsize / 1024);
663 fflush(stdout);
664 last_time = cur_time;
665 }
666 }
667 }
668
669 /* allocate a picture (needs to do that in main thread to avoid
670 potential locking problems */
671 static void alloc_picture(void *opaque)
672 {
673 VideoState *is = opaque;
674 VideoPicture *vp;
675 int is_yuv;
676
677 vp = &is->pictq[is->pictq_windex];
678
679 if (vp->bmp)
680 SDL_FreeYUVOverlay(vp->bmp);
681
682 /* XXX: use generic function */
683 switch(is->video_st->codec.pix_fmt) {
684 case PIX_FMT_YUV420P:
685 case PIX_FMT_YUV422P:
686 case PIX_FMT_YUV444P:
687 case PIX_FMT_YUV422:
688 case PIX_FMT_YUV410P:
689 case PIX_FMT_YUV411P:
690 is_yuv = 1;
691 break;
692 default:
693 is_yuv = 0;
694 break;
695 }
696
697 if (is_yuv) {
698 vp->bmp = SDL_CreateYUVOverlay(is->video_st->codec.width,
699 is->video_st->codec.height,
700 SDL_YV12_OVERLAY,
701 screen);
702 } else {
703 #if 0
704 vp->bmp = bmp_alloc(screen,
705 is->video_st->codec.width,
706 is->video_st->codec.height,
707 screen->bitmap_format,
708 0);
709 #endif
710 vp->bmp = NULL;
711 }
712 vp->width = is->video_st->codec.width;
713 vp->height = is->video_st->codec.height;
714
715 SDL_LockMutex(is->pictq_mutex);
716 vp->allocated = 1;
717 SDL_CondSignal(is->pictq_cond);
718 SDL_UnlockMutex(is->pictq_mutex);
719 }
720
721 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts)
722 {
723 VideoPicture *vp;
724 int dst_pix_fmt;
725 AVPicture pict;
726
727 /* wait until we have space to put a new picture */
728 SDL_LockMutex(is->pictq_mutex);
729 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
730 !is->videoq.abort_request) {
731 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
732 }
733 SDL_UnlockMutex(is->pictq_mutex);
734
735 if (is->videoq.abort_request)
736 return -1;
737
738 vp = &is->pictq[is->pictq_windex];
739
740 /* alloc or resize hardware picture buffer */
741 if (!vp->bmp ||
742 vp->width != is->video_st->codec.width ||
743 vp->height != is->video_st->codec.height) {
744 SDL_Event event;
745
746 vp->allocated = 0;
747
748 /* the allocation must be done in the main thread to avoid
749 locking problems */
750 event.type = FF_ALLOC_EVENT;
751 event.user.data1 = is;
752 SDL_PushEvent(&event);
753
754 /* wait until the picture is allocated */
755 SDL_LockMutex(is->pictq_mutex);
756 while (!vp->allocated && !is->videoq.abort_request) {
757 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
758 }
759 SDL_UnlockMutex(is->pictq_mutex);
760
761 if (is->videoq.abort_request)
762 return -1;
763 }
764
765 /* if the frame is not skipped, then display it */
766 if (vp->bmp) {
767 /* get a pointer on the bitmap */
768 SDL_LockYUVOverlay (vp->bmp);
769
770 dst_pix_fmt = PIX_FMT_YUV420P;
771 pict.data[0] = vp->bmp->pixels[0];
772 pict.data[1] = vp->bmp->pixels[2];
773 pict.data[2] = vp->bmp->pixels[1];
774
775 pict.linesize[0] = vp->bmp->pitches[0];
776 pict.linesize[1] = vp->bmp->pitches[2];
777 pict.linesize[2] = vp->bmp->pitches[1];
778
779 img_convert(&pict, dst_pix_fmt,
780 (AVPicture *)src_frame, is->video_st->codec.pix_fmt,
781 is->video_st->codec.width, is->video_st->codec.height);
782 /* update the bitmap content */
783 SDL_UnlockYUVOverlay(vp->bmp);
784
785 vp->pts = pts;
786
787 /* now we can update the picture count */
788 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
789 is->pictq_windex = 0;
790 SDL_LockMutex(is->pictq_mutex);
791 is->pictq_size++;
792 SDL_UnlockMutex(is->pictq_mutex);
793 }
794 return 0;
795 }
796
797 /* compute the exact PTS for the picture if it is omitted in the stream */
798 static int output_picture2(VideoState *is, AVFrame *src_frame, double pts1)
799 {
800 double frame_delay, pts;
801
802 pts = pts1;
803
804 /* if B frames are present, and if the current picture is a I
805 or P frame, we use the last pts */
806 if (is->video_st->codec.has_b_frames &&
807 src_frame->pict_type != FF_B_TYPE) {
808 /* use last pts */
809 pts = is->video_last_P_pts;
810 /* get the pts for the next I or P frame if present */
811 is->video_last_P_pts = pts1;
812 }
813
814 if (pts != 0) {
815 /* update video clock with pts, if present */
816 is->video_clock = pts;
817 } else {
818 frame_delay = (double)is->video_st->codec.frame_rate_base /
819 (double)is->video_st->codec.frame_rate;
820 is->video_clock += frame_delay;
821 /* for MPEG2, the frame can be repeated, so we update the
822 clock accordingly */
823 if (src_frame->repeat_pict) {
824 is->video_clock += src_frame->repeat_pict * (frame_delay * 0.5);
825 }
826 }
827
828 #if defined(DEBUG_SYNC) && 0
829 {
830 int ftype;
831 if (src_frame->pict_type == FF_B_TYPE)
832 ftype = 'B';
833 else if (src_frame->pict_type == FF_I_TYPE)
834 ftype = 'I';
835 else
836 ftype = 'P';
837 printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
838 ftype, is->video_clock, pts1);
839 }
840 #endif
841 return queue_picture(is, src_frame, is->video_clock);
842 }
843
844 static int video_thread(void *arg)
845 {
846 VideoState *is = arg;
847 AVPacket pkt1, *pkt = &pkt1;
848 unsigned char *ptr;
849 int len, len1, got_picture;
850 AVFrame frame;
851 int64_t ipts;
852 double pts;
853
854 for(;;) {
855 while (is->paused && !is->videoq.abort_request) {
856 SDL_Delay(10);
857 }
858 if (packet_queue_get(&is->videoq, pkt, 1) < 0)
859 break;
860 /* NOTE: ipts is the PTS of the _first_ picture beginning in
861 this packet, if any */
862 ipts = pkt->pts;
863 ptr = pkt->data;
864 if (is->video_st->codec.codec_id == CODEC_ID_RAWVIDEO) {
865 avpicture_fill((AVPicture *)&frame, ptr,
866 is->video_st->codec.pix_fmt,
867 is->video_st->codec.width,
868 is->video_st->codec.height);
869 pts = 0;
870 if (ipts != AV_NOPTS_VALUE)
871 pts = (double)ipts * is->ic->pts_num / is->ic->pts_den;
872 frame.pict_type = FF_I_TYPE;
873 if (output_picture2(is, &frame, pts) < 0)
874 goto the_end;
875 } else {
876 len = pkt->size;
877 while (len > 0) {
878 if (is->picture_start) {
879 is->ipts = ipts;
880 is->picture_start = 0;
881 ipts = AV_NOPTS_VALUE;
882 }
883 len1 = avcodec_decode_video(&is->video_st->codec,
884 &frame, &got_picture, ptr, len);
885 if (len1 < 0)
886 break;
887 if (got_picture) {
888 pts = 0;
889 if (is->ipts != AV_NOPTS_VALUE)
890 pts = (double)is->ipts * is->ic->pts_num / is->ic->pts_den;
891 if (output_picture2(is, &frame, pts) < 0)
892 goto the_end;
893 is->picture_start = 1;
894 }
895 ptr += len1;
896 len -= len1;
897 }
898 }
899 av_free_packet(pkt);
900 }
901 the_end:
902 return 0;
903 }
904
905 /* copy samples for viewing in editor window */
906 static void update_sample_display(VideoState *is, short *samples, int samples_size)
907 {
908 int size, len, channels;
909
910 channels = is->audio_st->codec.channels;
911
912 size = samples_size / sizeof(short);
913 while (size > 0) {
914 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
915 if (len > size)
916 len = size;
917 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
918 samples += len;
919 is->sample_array_index += len;
920 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
921 is->sample_array_index = 0;
922 size -= len;
923 }
924 }
925
926 /* return the new audio buffer size (samples can be added or deleted
927 to get better sync if video or external master clock) */
928 static int synchronize_audio(VideoState *is, short *samples,
929 int samples_size1, double pts)
930 {
931 int n, samples_size;
932 double ref_clock;
933
934 n = 2 * is->audio_st->codec.channels;
935 samples_size = samples_size1;
936
937 /* if not master, then we try to remove or add samples to correct the clock */
938 if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||
939 is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
940 double diff, avg_diff;
941 int wanted_size, min_size, max_size, nb_samples;
942
943 ref_clock = get_master_clock(is);
944 diff = get_audio_clock(is) - ref_clock;
945
946 if (diff < AV_NOSYNC_THRESHOLD) {
947 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
948 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
949 /* not enough measures to have a correct estimate */
950 is->audio_diff_avg_count++;
951 } else {
952 /* estimate the A-V difference */
953 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
954
955 if (fabs(avg_diff) >= is->audio_diff_threshold) {
956 wanted_size = samples_size + ((int)(diff * is->audio_st->codec.sample_rate) * n);
957 nb_samples = samples_size / n;
958
959 min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
960 max_size = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
961 if (wanted_size < min_size)
962 wanted_size = min_size;
963 else if (wanted_size > max_size)
964 wanted_size = max_size;
965
966 /* add or remove samples to correction the synchro */
967 if (wanted_size < samples_size) {
968 /* remove samples */
969 samples_size = wanted_size;
970 } else if (wanted_size > samples_size) {
971 uint8_t *samples_end, *q;
972 int nb;
973
974 /* add samples */
975 nb = (samples_size - wanted_size);
976 samples_end = (uint8_t *)samples + samples_size - n;
977 q = samples_end + n;
978 while (nb > 0) {
979 memcpy(q, samples_end, n);
980 q += n;
981 nb -= n;
982 }
983 samples_size = wanted_size;
984 }
985 }
986 #if 0
987 printf("diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
988 diff, avg_diff, samples_size - samples_size1,
989 is->audio_clock, is->video_clock, is->audio_diff_threshold);
990 #endif
991 }
992 } else {
993 /* too big difference : may be initial PTS errors, so
994 reset A-V filter */
995 is->audio_diff_avg_count = 0;
996 is->audio_diff_cum = 0;
997 }
998 }
999
1000 return samples_size;
1001 }
1002
1003 /* decode one audio frame and returns its uncompressed size */
1004 static int audio_decode_frame(VideoState *is, uint8_t *audio_buf, double *pts_ptr)
1005 {
1006 AVPacket *pkt = &is->audio_pkt;
1007 int len1, data_size;
1008 double pts;
1009
1010 for(;;) {
1011 if (is->paused || is->audioq.abort_request) {
1012 return -1;
1013 }
1014 while (is->audio_pkt_size > 0) {
1015 len1 = avcodec_decode_audio(&is->audio_st->codec,
1016 (int16_t *)audio_buf, &data_size,
1017 is->audio_pkt_data, is->audio_pkt_size);
1018 if (len1 < 0)
1019 break;
1020 is->audio_pkt_data += len1;
1021 is->audio_pkt_size -= len1;
1022 if (data_size > 0) {
1023 pts = 0;
1024 if (is->audio_pkt_ipts != AV_NOPTS_VALUE)
1025 pts = (double)is->audio_pkt_ipts * is->ic->pts_num / is->ic->pts_den;
1026 /* if no pts, then compute it */
1027 if (pts != 0) {
1028 is->audio_clock = pts;
1029 } else {
1030 int n;
1031 n = 2 * is->audio_st->codec.channels;
1032 is->audio_clock += (double)data_size / (double)(n * is->audio_st->codec.sample_rate);
1033 }
1034 #if defined(DEBUG_SYNC)
1035 {
1036 static double last_clock;
1037 printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
1038 is->audio_clock - last_clock,
1039 is->audio_clock, pts);
1040 last_clock = is->audio_clock;
1041 }
1042 #endif
1043 *pts_ptr = is->audio_clock;
1044 is->audio_pkt_ipts = AV_NOPTS_VALUE;
1045 /* we got samples : we can exit now */
1046 return data_size;
1047 }
1048 }
1049
1050 /* free previous packet if any */
1051 if (pkt->destruct)
1052 av_free_packet(pkt);
1053
1054 /* read next packet */
1055 if (packet_queue_get(&is->audioq, pkt, 1) < 0)
1056 return -1;
1057 is->audio_pkt_data = pkt->data;
1058 is->audio_pkt_size = pkt->size;
1059 is->audio_pkt_ipts = pkt->pts;
1060 }
1061 }
1062
1063 /* get the current audio output buffer size, in samples. With SDL, we
1064 cannot have a precise information */
1065 static int audio_write_get_buf_size(VideoState *is)
1066 {
1067 return is->audio_hw_buf_size - is->audio_buf_index;
1068 }
1069
1070
1071 /* prepare a new audio buffer */
1072 void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
1073 {
1074 VideoState *is = opaque;
1075 int audio_size, len1;
1076 double pts;
1077
1078 audio_callback_time = av_gettime();
1079
1080 while (len > 0) {
1081 if (is->audio_buf_index >= is->audio_buf_size) {
1082 audio_size = audio_decode_frame(is, is->audio_buf, &pts);
1083 if (audio_size < 0) {
1084 /* if error, just output silence */
1085 is->audio_buf_size = 1024;
1086 memset(is->audio_buf, 0, is->audio_buf_size);
1087 } else {
1088 if (is->show_audio)
1089 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
1090 audio_size = synchronize_audio(is, (int16_t *)is->audio_buf, audio_size,
1091 pts);
1092 is->audio_buf_size = audio_size;
1093 }
1094 is->audio_buf_index = 0;
1095 }
1096 len1 = is->audio_buf_size - is->audio_buf_index;
1097 if (len1 > len)
1098 len1 = len;
1099 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
1100 len -= len1;
1101 stream += len1;
1102 is->audio_buf_index += len1;
1103 }
1104 }
1105
1106
1107 /* open a given stream. Return 0 if OK */
1108 static int stream_component_open(VideoState *is, int stream_index)
1109 {
1110 AVFormatContext *ic = is->ic;
1111 AVCodecContext *enc;
1112 AVCodec *codec;
1113 SDL_AudioSpec wanted_spec, spec;
1114
1115 if (stream_index < 0 || stream_index >= ic->nb_streams)
1116 return -1;
1117 enc = &ic->streams[stream_index]->codec;
1118
1119 /* prepare audio output */
1120 if (enc->codec_type == CODEC_TYPE_AUDIO) {
1121 wanted_spec.freq = enc->sample_rate;
1122 wanted_spec.format = AUDIO_S16SYS;
1123 /* hack for AC3. XXX: suppress that */
1124 if (enc->channels > 2)
1125 enc->channels = 2;
1126 wanted_spec.channels = enc->channels;
1127 wanted_spec.silence = 0;
1128 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
1129 wanted_spec.callback = sdl_audio_callback;
1130 wanted_spec.userdata = is;
1131 if (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
1132 fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
1133 return -1;
1134 }
1135 is->audio_hw_buf_size = spec.size;
1136 }
1137
1138 codec = avcodec_find_decoder(enc->codec_id);
1139 if (!codec ||
1140 avcodec_open(enc, codec) < 0)
1141 return -1;
1142 switch(enc->codec_type) {
1143 case CODEC_TYPE_AUDIO:
1144 is->audio_stream = stream_index;
1145 is->audio_st = ic->streams[stream_index];
1146 is->audio_buf_size = 0;
1147 is->audio_buf_index = 0;
1148 is->audio_pkt_size = 0;
1149
1150 /* init averaging filter */
1151 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
1152 is->audio_diff_avg_count = 0;
1153 /* since we do not have a precise anough audio fifo fullness,
1154 we correct audio sync only if larger than this threshold */
1155 is->audio_diff_threshold = 2.0 * SDL_AUDIO_BUFFER_SIZE / enc->sample_rate;
1156
1157 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
1158 packet_queue_init(&is->audioq);
1159 SDL_PauseAudio(0);
1160 break;
1161 case CODEC_TYPE_VIDEO:
1162 is->video_stream = stream_index;
1163 is->video_st = ic->streams[stream_index];
1164
1165 is->frame_last_delay = 40e-3;
1166 is->frame_timer = (double)av_gettime() / 1000000.0;
1167 is->picture_start = 1;
1168 is->video_current_pts_time = av_gettime();
1169
1170 packet_queue_init(&is->videoq);
1171 is->video_tid = SDL_CreateThread(video_thread, is);
1172 break;
1173 default:
1174 break;
1175 }
1176 return 0;
1177 }
1178
1179 static void stream_component_close(VideoState *is, int stream_index)
1180 {
1181 AVFormatContext *ic = is->ic;
1182 AVCodecContext *enc;
1183
1184 enc = &ic->streams[stream_index]->codec;
1185
1186 switch(enc->codec_type) {
1187 case CODEC_TYPE_AUDIO:
1188 packet_queue_abort(&is->audioq);
1189
1190 SDL_CloseAudio();
1191
1192 packet_queue_end(&is->audioq);
1193 break;
1194 case CODEC_TYPE_VIDEO:
1195 packet_queue_abort(&is->videoq);
1196
1197 /* note: we also signal this mutex to make sure we deblock the
1198 video thread in all cases */
1199 SDL_LockMutex(is->pictq_mutex);
1200 SDL_CondSignal(is->pictq_cond);
1201 SDL_UnlockMutex(is->pictq_mutex);
1202
1203 SDL_WaitThread(is->video_tid, NULL);
1204
1205 packet_queue_end(&is->videoq);
1206 break;
1207 default:
1208 break;
1209 }
1210
1211 avcodec_close(enc);
1212 switch(enc->codec_type) {
1213 case CODEC_TYPE_AUDIO:
1214 is->audio_st = NULL;
1215 is->audio_stream = -1;
1216 break;
1217 case CODEC_TYPE_VIDEO:
1218 is->video_st = NULL;
1219 is->video_stream = -1;
1220 break;
1221 default:
1222 break;
1223 }
1224 }
1225
1226 void dump_stream_info(AVFormatContext *s)
1227 {
1228 if (s->track != 0)
1229 fprintf(stderr, "Track: %d\n", s->track);
1230 if (s->title[0] != '\0')
1231 fprintf(stderr, "Title: %s\n", s->title);
1232 if (s->author[0] != '\0')
1233 fprintf(stderr, "Author: %s\n", s->author);
1234 if (s->album[0] != '\0')
1235 fprintf(stderr, "Album: %s\n", s->album);
1236 if (s->year != 0)
1237 fprintf(stderr, "Year: %d\n", s->year);
1238 if (s->genre[0] != '\0')
1239 fprintf(stderr, "Genre: %s\n", s->genre);
1240 }
1241
1242 /* since we have only one decoding thread, we can use a global
1243 variable instead of a thread local variable */
1244 static VideoState *global_video_state;
1245
1246 static int decode_interrupt_cb(void)
1247 {
1248 return (global_video_state && global_video_state->abort_request);
1249 }
1250
1251 /* this thread gets the stream from the disk or the network */
1252 static int decode_thread(void *arg)
1253 {
1254 VideoState *is = arg;
1255 AVFormatContext *ic;
1256 int err, i, ret, video_index, audio_index;
1257 AVPacket pkt1, *pkt = &pkt1;
1258
1259 video_index = -1;
1260 audio_index = -1;
1261 is->video_stream = -1;
1262 is->audio_stream = -1;
1263
1264 global_video_state = is;
1265 url_set_interrupt_cb(decode_interrupt_cb);
1266
1267 err = av_open_input_file(&ic, is->filename, is->iformat, 0, NULL);
1268 if (err < 0) {
1269 print_error(is->filename, err);
1270 ret = -1;
1271 goto fail;
1272 }
1273 is->ic = ic;
1274 err = av_find_stream_info(ic);
1275 if (err < 0) {
1276 fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
1277 ret = -1;
1278 goto fail;
1279 }
1280
1281 for(i = 0; i < ic->nb_streams; i++) {
1282 AVCodecContext *enc = &ic->streams[i]->codec;
1283 switch(enc->codec_type) {
1284 case CODEC_TYPE_AUDIO:
1285 if (audio_index < 0 && !audio_disable)
1286 audio_index = i;
1287 break;
1288 case CODEC_TYPE_VIDEO:
1289 if (video_index < 0 && !video_disable)
1290 video_index = i;
1291 break;
1292 default:
1293 break;
1294 }
1295 }
1296 if (show_status) {
1297 dump_format(ic, 0, is->filename, 0);
1298 dump_stream_info(ic);
1299 }
1300
1301 /* open the streams */
1302 if (audio_index >= 0) {
1303 stream_component_open(is, audio_index);
1304 }
1305
1306 if (video_index >= 0) {
1307 stream_component_open(is, video_index);
1308 } else {
1309 if (!display_disable)
1310 is->show_audio = 1;
1311 }
1312
1313 if (is->video_stream < 0 && is->audio_stream < 0) {
1314 fprintf(stderr, "%s: could not open codecs\n", is->filename);
1315 ret = -1;
1316 goto fail;
1317 }
1318
1319 for(;;) {
1320 if (is->abort_request)
1321 break;
1322 #ifdef CONFIG_NETWORK
1323 if (is->paused != is->last_paused) {
1324 is->last_paused = is->paused;
1325 if (ic->iformat == &rtsp_demux) {
1326 if (is->paused)
1327 rtsp_pause(ic);
1328 else
1329 rtsp_resume(ic);
1330 }
1331 }
1332 if (is->paused && ic->iformat == &rtsp_demux) {
1333 /* wait 10 ms to avoid trying to get another packet */
1334 /* XXX: horrible */
1335 SDL_Delay(10);
1336 continue;
1337 }
1338 #endif
1339
1340 /* if the queue are full, no need to read more */
1341 if (is->audioq.size > MAX_AUDIOQ_SIZE ||
1342 is->videoq.size > MAX_VIDEOQ_SIZE) {
1343 /* wait 10 ms */
1344 SDL_Delay(10);
1345 continue;
1346 }
1347 ret = av_read_packet(ic, pkt);
1348 if (ret < 0) {
1349 break;
1350 }
1351 if (pkt->stream_index == is->audio_stream) {
1352 packet_queue_put(&is->audioq, pkt);
1353 } else if (pkt->stream_index == is->video_stream) {
1354 packet_queue_put(&is->videoq, pkt);
1355 } else {
1356 av_free_packet(pkt);
1357 }
1358 }
1359 /* wait until the end */
1360 while (!is->abort_request) {
1361 SDL_Delay(100);
1362 }
1363
1364 ret = 0;
1365 fail:
1366 /* disable interrupting */
1367 global_video_state = NULL;
1368
1369 /* close each stream */
1370 if (is->audio_stream >= 0)
1371 stream_component_close(is, is->audio_stream);
1372 if (is->video_stream >= 0)
1373 stream_component_close(is, is->video_stream);
1374 if (is->ic) {
1375 av_close_input_file(is->ic);
1376 is->ic = NULL; /* safety */
1377 }
1378 url_set_interrupt_cb(NULL);
1379
1380 if (ret != 0) {
1381 SDL_Event event;
1382
1383 event.type = FF_QUIT_EVENT;
1384 event.user.data1 = is;
1385 SDL_PushEvent(&event);
1386 }
1387 return 0;
1388 }
1389
1390 /* pause or resume the video */
1391 static void stream_pause(VideoState *is)
1392 {
1393 is->paused = !is->paused;
1394 }
1395
1396 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
1397 {
1398 VideoState *is;
1399
1400 is = av_mallocz(sizeof(VideoState));
1401 if (!is)
1402 return NULL;
1403 pstrcpy(is->filename, sizeof(is->filename), filename);
1404 is->iformat = iformat;
1405 if (screen) {
1406 is->width = screen->w;
1407 is->height = screen->h;
1408 }
1409 is->ytop = 0;
1410 is->xleft = 0;
1411
1412 /* start video display */
1413 is->pictq_mutex = SDL_CreateMutex();
1414 is->pictq_cond = SDL_CreateCond();
1415
1416 /* add the refresh timer to draw the picture */
1417 schedule_refresh(is, 40);
1418
1419 is->av_sync_type = av_sync_type;
1420 is->parse_tid = SDL_CreateThread(decode_thread, is);
1421 if (!is->parse_tid) {
1422 av_free(is);
1423 return NULL;
1424 }
1425 return is;
1426 }
1427
1428 static void stream_close(VideoState *is)
1429 {
1430 VideoPicture *vp;
1431 int i;
1432 /* XXX: use a special url_shutdown call to abort parse cleanly */
1433 is->abort_request = 1;
1434 SDL_WaitThread(is->parse_tid, NULL);
1435
1436 /* free all pictures */
1437 for(i=0;i<VIDEO_PICTURE_QUEUE_SIZE; i++) {
1438 vp = &is->pictq[i];
1439 if (vp->bmp) {
1440 SDL_FreeYUVOverlay(vp->bmp);
1441 vp->bmp = NULL;
1442 }
1443 }
1444 SDL_DestroyMutex(is->pictq_mutex);
1445 SDL_DestroyCond(is->pictq_cond);
1446 }
1447
1448 void stream_cycle_channel(VideoState *is, int codec_type)
1449 {
1450 AVFormatContext *ic = is->ic;
1451 int start_index, stream_index;
1452 AVStream *st;
1453
1454 if (codec_type == CODEC_TYPE_VIDEO)
1455 start_index = is->video_stream;
1456 else
1457 start_index = is->audio_stream;
1458 if (start_index < 0)
1459 return;
1460 stream_index = start_index;
1461 for(;;) {
1462 if (++stream_index >= is->ic->nb_streams)
1463 stream_index = 0;
1464 if (stream_index == start_index)
1465 return;
1466 st = ic->streams[stream_index];
1467 if (st->codec.codec_type == codec_type) {
1468 /* check that parameters are OK */
1469 switch(codec_type) {
1470 case CODEC_TYPE_AUDIO:
1471 if (st->codec.sample_rate != 0 &&
1472 st->codec.channels != 0)
1473 goto the_end;
1474 break;
1475 case CODEC_TYPE_VIDEO:
1476 goto the_end;
1477 default:
1478 break;
1479 }
1480 }
1481 }
1482 the_end:
1483 stream_component_close(is, start_index);
1484 stream_component_open(is, stream_index);
1485 }
1486
1487
1488 void toggle_full_screen(void)
1489 {
1490 int w, h, flags;
1491 is_full_screen = !is_full_screen;
1492 if (!fs_screen_width) {
1493 /* use default SDL method */
1494 SDL_WM_ToggleFullScreen(screen);
1495 } else {
1496 /* use the recorded resolution */
1497 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
1498 if (is_full_screen) {
1499 w = fs_screen_width;
1500 h = fs_screen_height;
1501 flags |= SDL_FULLSCREEN;
1502 } else {
1503 w = screen_width;
1504 h = screen_height;
1505 flags |= SDL_RESIZABLE;
1506 }
1507 screen = SDL_SetVideoMode(w, h, 0, flags);
1508 cur_stream->width = w;
1509 cur_stream->height = h;
1510 }
1511 }
1512
1513 void toggle_pause(void)
1514 {
1515 if (cur_stream)
1516 stream_pause(cur_stream);
1517 }
1518
1519 void do_exit(void)
1520 {
1521 if (cur_stream) {
1522 stream_close(cur_stream);
1523 cur_stream = NULL;
1524 }
1525 if (show_status)
1526 printf("\n");
1527 SDL_Quit();
1528 exit(0);
1529 }
1530
1531 void toggle_audio_display(void)
1532 {
1533 if (cur_stream) {
1534 cur_stream->show_audio = !cur_stream->show_audio;
1535 }
1536 }
1537
1538 /* handle an event sent by the GUI */
1539 void event_loop(void)
1540 {
1541 SDL_Event event;
1542
1543 for(;;) {
1544 SDL_WaitEvent(&event);
1545 switch(event.type) {
1546 case SDL_KEYDOWN:
1547 switch(event.key.keysym.sym) {
1548 case SDLK_ESCAPE:
1549 case SDLK_q:
1550 do_exit();
1551 break;
1552 case SDLK_f:
1553 toggle_full_screen();
1554 break;
1555 case SDLK_p:
1556 case SDLK_SPACE:
1557 toggle_pause();
1558 break;
1559 case SDLK_a:
1560 if (cur_stream)
1561 stream_cycle_channel(cur_stream, CODEC_TYPE_AUDIO);
1562 break;
1563 case SDLK_v:
1564 if (cur_stream)
1565 stream_cycle_channel(cur_stream, CODEC_TYPE_VIDEO);
1566 break;
1567 case SDLK_w:
1568 toggle_audio_display();
1569 break;
1570 default:
1571 break;
1572 }
1573 break;
1574 case SDL_VIDEORESIZE:
1575 if (cur_stream) {
1576 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
1577 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
1578 cur_stream->width = event.resize.w;
1579 cur_stream->height = event.resize.h;
1580 }
1581 break;
1582 case SDL_QUIT:
1583 case FF_QUIT_EVENT:
1584 do_exit();
1585 break;
1586 case FF_ALLOC_EVENT:
1587 alloc_picture(event.user.data1);
1588 break;
1589 case FF_REFRESH_EVENT:
1590 video_refresh_timer(event.user.data1);
1591 break;
1592 default:
1593 break;
1594 }
1595 }
1596 }
1597
1598 void opt_width(const char *arg)
1599 {
1600 screen_width = atoi(arg);
1601 }
1602
1603 void opt_height(const char *arg)
1604 {
1605 screen_height = atoi(arg);
1606 }
1607
1608 static void opt_format(const char *arg)
1609 {
1610 file_iformat = av_find_input_format(arg);
1611 if (!file_iformat) {
1612 fprintf(stderr, "Unknown input format: %s\n", arg);
1613 exit(1);
1614 }
1615 }
1616 #ifdef CONFIG_NETWORK
1617 void opt_rtp_tcp(void)
1618 {
1619 /* only tcp protocol */
1620 rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP);
1621 }
1622 #endif
1623
1624 void opt_sync(const char *arg)
1625 {
1626 if (!strcmp(arg, "audio"))
1627 av_sync_type = AV_SYNC_AUDIO_MASTER;
1628 else if (!strcmp(arg, "video"))
1629 av_sync_type = AV_SYNC_VIDEO_MASTER;
1630 else if (!strcmp(arg, "ext"))
1631 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
1632 else
1633 show_help();
1634 }
1635
1636 const OptionDef options[] = {
1637 { "h", 0, {(void*)show_help}, "show help" },
1638 { "x", HAS_ARG, {(void*)opt_width}, "force displayed width", "width" },
1639 { "y", HAS_ARG, {(void*)opt_height}, "force displayed height", "height" },
1640 #if 0
1641 /* disabled as SDL/X11 does not support it correctly on application launch */
1642 { "fs", OPT_BOOL, {(void*)&is_full_screen}, "force full screen" },
1643 #endif
1644 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
1645 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
1646 { "nodisp", OPT_BOOL, {(void*)&display_disable}, "disable graphical display" },
1647 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
1648 { "stats", OPT_BOOL | OPT_EXPERT, {(void*)&show_status}, "show status", "" },
1649 #ifdef CONFIG_NETWORK
1650 { "rtp_tcp", OPT_EXPERT, {(void*)&opt_rtp_tcp}, "force RTP/TCP protocol usage", "" },
1651 #endif
1652 { "sync", HAS_ARG | OPT_EXPERT, {(void*)&opt_sync}, "set audio-video sync. type (type=audio/video/ext)", "type" },
1653 { NULL, },
1654 };
1655
1656 void show_help(void)
1657 {
1658 printf("ffplay version " FFMPEG_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
1659 "usage: ffplay [options] input_file\n"
1660 "Simple media player\n");
1661 printf("\n");
1662 show_help_options(options, "Main options:\n",
1663 OPT_EXPERT, 0);
1664 show_help_options(options, "\nAdvanced options:\n",
1665 OPT_EXPERT, OPT_EXPERT);
1666 printf("\nWhile playing:\n"
1667 "q, ESC quit\n"
1668 "f toggle full screen\n"
1669 "p, SPC pause\n"
1670 "a cycle audio channel\n"
1671 "v cycle video channel\n"
1672 "w show audio waves\n"
1673 );
1674 exit(1);
1675 }
1676
1677 void parse_arg_file(const char *filename)
1678 {
1679 if (!strcmp(filename, "-"))
1680 filename = "pipe:";
1681 input_filename = filename;
1682 }
1683
1684 /* Called from the main */
1685 int main(int argc, char **argv)
1686 {
1687 int flags, w, h;
1688
1689 /* register all codecs, demux and protocols */
1690 av_register_all();
1691
1692 parse_options(argc, argv, options);
1693
1694 if (!input_filename)
1695 show_help();
1696
1697 if (display_disable) {
1698 video_disable = 1;
1699 }
1700 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
1701 #ifndef CONFIG_WIN32
1702 flags |= SDL_INIT_EVENTTHREAD; /* Not supported on win32 */
1703 #endif
1704 if (SDL_Init (flags)) {
1705 fprintf(stderr, "Could not initialize SDL - exiting\n");
1706 exit(1);
1707 }
1708
1709 if (!display_disable) {
1710 #ifdef HAVE_X11
1711 /* save the screen resolution... SDL should allow full screen
1712 by resizing the window */
1713 {
1714 Display *dpy;
1715 dpy = XOpenDisplay(NULL);
1716 if (dpy) {
1717 fs_screen_width = DisplayWidth(dpy, DefaultScreen(dpy));
1718 fs_screen_height = DisplayHeight(dpy, DefaultScreen(dpy));
1719 XCloseDisplay(dpy);
1720 }
1721 }
1722 #endif
1723 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
1724 if (is_full_screen && fs_screen_width) {
1725 w = fs_screen_width;
1726 h = fs_screen_height;
1727 flags |= SDL_FULLSCREEN;
1728 } else {
1729 w = screen_width;
1730 h = screen_height;
1731 flags |= SDL_RESIZABLE;
1732 }
1733 screen = SDL_SetVideoMode(w, h, 0, flags);
1734 if (!screen) {
1735 fprintf(stderr, "SDL: could not set video mode - exiting\n");
1736 exit(1);
1737 }
1738 SDL_WM_SetCaption("FFplay", "FFplay");
1739 }
1740
1741 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
1742 SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
1743 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
1744 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
1745
1746 cur_stream = stream_open(input_filename, file_iformat);
1747
1748 event_loop();
1749
1750 /* never returns */
1751
1752 return 0;
1753 }