00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #define HAVE_AV_CONFIG_H
00020 #include "avformat.h"
00021
00022 #include "cmdutils.h"
00023
00024 #include <SDL.h>
00025 #include <SDL_thread.h>
00026
00027 #ifdef CONFIG_WIN32
00028 #undef main
00029 #endif
00030
00031 #ifdef CONFIG_OS2
00032 #define INCL_DOS
00033 #include <os2.h>
00034 #include <stdio.h>
00035
00036 void MorphToPM()
00037 {
00038 PPIB pib;
00039 PTIB tib;
00040
00041 DosGetInfoBlocks(&tib, &pib);
00042
00043
00044 if (pib->pib_ultype==2) pib->pib_ultype = 3;
00045 }
00046 #endif
00047
00048 #if defined(__linux__)
00049 #define HAVE_X11
00050 #endif
00051
00052 #ifdef HAVE_X11
00053 #include <X11/Xlib.h>
00054 #endif
00055
00056
00057
00058 #define MAX_VIDEOQ_SIZE (5 * 256 * 1024)
00059 #define MAX_AUDIOQ_SIZE (5 * 16 * 1024)
00060
00061
00062
00063 #define SDL_AUDIO_BUFFER_SIZE 1024
00064
00065
00066 #define AV_SYNC_THRESHOLD 0.01
00067
00068 #define AV_NOSYNC_THRESHOLD 10.0
00069
00070
00071 #define SAMPLE_CORRECTION_PERCENT_MAX 10
00072
00073
00074 #define AUDIO_DIFF_AVG_NB 20
00075
00076
00077 #define SAMPLE_ARRAY_SIZE (2*65536)
00078
00079 typedef struct PacketQueue {
00080 AVPacketList *first_pkt, *last_pkt;
00081 int nb_packets;
00082 int size;
00083 int abort_request;
00084 SDL_mutex *mutex;
00085 SDL_cond *cond;
00086 } PacketQueue;
00087
00088 #define VIDEO_PICTURE_QUEUE_SIZE 1
00089
00090 typedef struct VideoPicture {
00091 double pts;
00092 SDL_Overlay *bmp;
00093 int width, height;
00094 int allocated;
00095 } VideoPicture;
00096
00097 enum {
00098 AV_SYNC_AUDIO_MASTER,
00099 AV_SYNC_VIDEO_MASTER,
00100 AV_SYNC_EXTERNAL_CLOCK,
00101 };
00102
00103 typedef struct VideoState {
00104 SDL_Thread *parse_tid;
00105 SDL_Thread *video_tid;
00106 AVInputFormat *iformat;
00107 int no_background;
00108 int abort_request;
00109 int paused;
00110 int last_paused;
00111 int seek_req;
00112 int seek_flags;
00113 int64_t seek_pos;
00114 AVFormatContext *ic;
00115 int dtg_active_format;
00116
00117 int audio_stream;
00118
00119 int av_sync_type;
00120 double external_clock;
00121 int64_t external_clock_time;
00122
00123 double audio_clock;
00124 double audio_diff_cum;
00125 double audio_diff_avg_coef;
00126 double audio_diff_threshold;
00127 int audio_diff_avg_count;
00128 AVStream *audio_st;
00129 PacketQueue audioq;
00130 int audio_hw_buf_size;
00131
00132
00133 uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
00134 unsigned int audio_buf_size;
00135 int audio_buf_index;
00136 AVPacket audio_pkt;
00137 uint8_t *audio_pkt_data;
00138 int audio_pkt_size;
00139
00140 int show_audio;
00141 int16_t sample_array[SAMPLE_ARRAY_SIZE];
00142 int sample_array_index;
00143 int last_i_start;
00144
00145 double frame_timer;
00146 double frame_last_pts;
00147 double frame_last_delay;
00148 double video_clock;
00149 int video_stream;
00150 AVStream *video_st;
00151 PacketQueue videoq;
00152 double video_last_P_pts;
00153
00154 double video_current_pts;
00155
00156 int64_t video_current_pts_time;
00157
00158
00159 VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
00160 int pictq_size, pictq_rindex, pictq_windex;
00161 SDL_mutex *pictq_mutex;
00162 SDL_cond *pictq_cond;
00163
00164 SDL_mutex *video_decoder_mutex;
00165 SDL_mutex *audio_decoder_mutex;
00166
00167
00168 char filename[1024];
00169 int width, height, xleft, ytop;
00170 } VideoState;
00171
00172 void show_help(void);
00173 static int audio_write_get_buf_size(VideoState *is);
00174
00175
00176 static AVInputFormat *file_iformat;
00177 static AVImageFormat *image_format;
00178 static const char *input_filename;
00179 static int fs_screen_width;
00180 static int fs_screen_height;
00181 static int screen_width = 640;
00182 static int screen_height = 480;
00183 static int audio_disable;
00184 static int video_disable;
00185 static int display_disable;
00186 static int show_status;
00187 static int av_sync_type = AV_SYNC_AUDIO_MASTER;
00188 static int64_t start_time = AV_NOPTS_VALUE;
00189 static int debug = 0;
00190 static int debug_mv = 0;
00191 static int step = 0;
00192 static int thread_count = 1;
00193 static int workaround_bugs = 1;
00194 static int fast = 0;
00195 static int lowres = 0;
00196 static int idct = FF_IDCT_AUTO;
00197 static enum AVDiscard skip_frame= AVDISCARD_DEFAULT;
00198 static enum AVDiscard skip_idct= AVDISCARD_DEFAULT;
00199 static enum AVDiscard skip_loop_filter= AVDISCARD_DEFAULT;
00200 static int error_resilience = FF_ER_CAREFUL;
00201 static int error_concealment = 3;
00202
00203
00204 static int is_full_screen;
00205 static VideoState *cur_stream;
00206 static int64_t audio_callback_time;
00207
00208 #define FF_ALLOC_EVENT (SDL_USEREVENT)
00209 #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
00210 #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
00211
00212 SDL_Surface *screen;
00213
00214
00215 static void packet_queue_init(PacketQueue *q)
00216 {
00217 memset(q, 0, sizeof(PacketQueue));
00218 q->mutex = SDL_CreateMutex();
00219 q->cond = SDL_CreateCond();
00220 }
00221
00222 static void packet_queue_flush(PacketQueue *q)
00223 {
00224 AVPacketList *pkt, *pkt1;
00225
00226 SDL_LockMutex(q->mutex);
00227 for(pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
00228 pkt1 = pkt->next;
00229 av_free_packet(&pkt->pkt);
00230 av_freep(&pkt);
00231 }
00232 q->last_pkt = NULL;
00233 q->first_pkt = NULL;
00234 q->nb_packets = 0;
00235 q->size = 0;
00236 SDL_UnlockMutex(q->mutex);
00237 }
00238
00239 static void packet_queue_end(PacketQueue *q)
00240 {
00241 packet_queue_flush(q);
00242 SDL_DestroyMutex(q->mutex);
00243 SDL_DestroyCond(q->cond);
00244 }
00245
00246 static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
00247 {
00248 AVPacketList *pkt1;
00249
00250
00251 if (av_dup_packet(pkt) < 0)
00252 return -1;
00253
00254 pkt1 = av_malloc(sizeof(AVPacketList));
00255 if (!pkt1)
00256 return -1;
00257 pkt1->pkt = *pkt;
00258 pkt1->next = NULL;
00259
00260
00261 SDL_LockMutex(q->mutex);
00262
00263 if (!q->last_pkt)
00264
00265 q->first_pkt = pkt1;
00266 else
00267 q->last_pkt->next = pkt1;
00268 q->last_pkt = pkt1;
00269 q->nb_packets++;
00270 q->size += pkt1->pkt.size;
00271
00272 SDL_CondSignal(q->cond);
00273
00274 SDL_UnlockMutex(q->mutex);
00275 return 0;
00276 }
00277
00278 static void packet_queue_abort(PacketQueue *q)
00279 {
00280 SDL_LockMutex(q->mutex);
00281
00282 q->abort_request = 1;
00283
00284 SDL_CondSignal(q->cond);
00285
00286 SDL_UnlockMutex(q->mutex);
00287 }
00288
00289
00290 static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
00291 {
00292 AVPacketList *pkt1;
00293 int ret;
00294
00295 SDL_LockMutex(q->mutex);
00296
00297 for(;;) {
00298 if (q->abort_request) {
00299 ret = -1;
00300 break;
00301 }
00302
00303 pkt1 = q->first_pkt;
00304 if (pkt1) {
00305 q->first_pkt = pkt1->next;
00306 if (!q->first_pkt)
00307 q->last_pkt = NULL;
00308 q->nb_packets--;
00309 q->size -= pkt1->pkt.size;
00310 *pkt = pkt1->pkt;
00311 av_free(pkt1);
00312 ret = 1;
00313 break;
00314 } else if (!block) {
00315 ret = 0;
00316 break;
00317 } else {
00318 SDL_CondWait(q->cond, q->mutex);
00319 }
00320 }
00321 SDL_UnlockMutex(q->mutex);
00322 return ret;
00323 }
00324
00325 static inline void fill_rectangle(SDL_Surface *screen,
00326 int x, int y, int w, int h, int color)
00327 {
00328 SDL_Rect rect;
00329 rect.x = x;
00330 rect.y = y;
00331 rect.w = w;
00332 rect.h = h;
00333 SDL_FillRect(screen, &rect, color);
00334 }
00335
00336 #if 0
00337
00338 void fill_border(VideoState *s, int x, int y, int w, int h, int color)
00339 {
00340 int w1, w2, h1, h2;
00341
00342
00343 w1 = x;
00344 if (w1 < 0)
00345 w1 = 0;
00346 w2 = s->width - (x + w);
00347 if (w2 < 0)
00348 w2 = 0;
00349 h1 = y;
00350 if (h1 < 0)
00351 h1 = 0;
00352 h2 = s->height - (y + h);
00353 if (h2 < 0)
00354 h2 = 0;
00355 fill_rectangle(screen,
00356 s->xleft, s->ytop,
00357 w1, s->height,
00358 color);
00359 fill_rectangle(screen,
00360 s->xleft + s->width - w2, s->ytop,
00361 w2, s->height,
00362 color);
00363 fill_rectangle(screen,
00364 s->xleft + w1, s->ytop,
00365 s->width - w1 - w2, h1,
00366 color);
00367 fill_rectangle(screen,
00368 s->xleft + w1, s->ytop + s->height - h2,
00369 s->width - w1 - w2, h2,
00370 color);
00371 }
00372 #endif
00373
00374 static void video_image_display(VideoState *is)
00375 {
00376 VideoPicture *vp;
00377 float aspect_ratio;
00378 int width, height, x, y;
00379 SDL_Rect rect;
00380
00381 vp = &is->pictq[is->pictq_rindex];
00382 if (vp->bmp) {
00383
00384 if (is->video_st->codec->sample_aspect_ratio.num == 0)
00385 aspect_ratio = 0;
00386 else
00387 aspect_ratio = av_q2d(is->video_st->codec->sample_aspect_ratio)
00388 * is->video_st->codec->width / is->video_st->codec->height;;
00389 if (aspect_ratio <= 0.0)
00390 aspect_ratio = (float)is->video_st->codec->width /
00391 (float)is->video_st->codec->height;
00392
00393
00394 #if 0
00395 if (is->video_st->codec->dtg_active_format != is->dtg_active_format) {
00396 is->dtg_active_format = is->video_st->codec->dtg_active_format;
00397 printf("dtg_active_format=%d\n", is->dtg_active_format);
00398 }
00399 #endif
00400 #if 0
00401 switch(is->video_st->codec->dtg_active_format) {
00402 case FF_DTG_AFD_SAME:
00403 default:
00404
00405 break;
00406 case FF_DTG_AFD_4_3:
00407 aspect_ratio = 4.0 / 3.0;
00408 break;
00409 case FF_DTG_AFD_16_9:
00410 aspect_ratio = 16.0 / 9.0;
00411 break;
00412 case FF_DTG_AFD_14_9:
00413 aspect_ratio = 14.0 / 9.0;
00414 break;
00415 case FF_DTG_AFD_4_3_SP_14_9:
00416 aspect_ratio = 14.0 / 9.0;
00417 break;
00418 case FF_DTG_AFD_16_9_SP_14_9:
00419 aspect_ratio = 14.0 / 9.0;
00420 break;
00421 case FF_DTG_AFD_SP_4_3:
00422 aspect_ratio = 4.0 / 3.0;
00423 break;
00424 }
00425 #endif
00426
00427
00428 height = is->height;
00429 width = ((int)rint(height * aspect_ratio)) & -3;
00430 if (width > is->width) {
00431 width = is->width;
00432 height = ((int)rint(width / aspect_ratio)) & -3;
00433 }
00434 x = (is->width - width) / 2;
00435 y = (is->height - height) / 2;
00436 if (!is->no_background) {
00437
00438
00439 } else {
00440 is->no_background = 0;
00441 }
00442 rect.x = is->xleft + x;
00443 rect.y = is->xleft + y;
00444 rect.w = width;
00445 rect.h = height;
00446 SDL_DisplayYUVOverlay(vp->bmp, &rect);
00447 } else {
00448 #if 0
00449 fill_rectangle(screen,
00450 is->xleft, is->ytop, is->width, is->height,
00451 QERGB(0x00, 0x00, 0x00));
00452 #endif
00453 }
00454 }
00455
00456 static inline int compute_mod(int a, int b)
00457 {
00458 a = a % b;
00459 if (a >= 0)
00460 return a;
00461 else
00462 return a + b;
00463 }
00464
00465 static void video_audio_display(VideoState *s)
00466 {
00467 int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
00468 int ch, channels, h, h2, bgcolor, fgcolor;
00469 int16_t time_diff;
00470
00471
00472 channels = s->audio_st->codec->channels;
00473 nb_display_channels = channels;
00474 if (!s->paused) {
00475 n = 2 * channels;
00476 delay = audio_write_get_buf_size(s);
00477 delay /= n;
00478
00479
00480
00481 if (audio_callback_time) {
00482 time_diff = av_gettime() - audio_callback_time;
00483 delay += (time_diff * s->audio_st->codec->sample_rate) / 1000000;
00484 }
00485
00486 delay -= s->width / 2;
00487 if (delay < s->width)
00488 delay = s->width;
00489 i_start = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
00490 s->last_i_start = i_start;
00491 } else {
00492 i_start = s->last_i_start;
00493 }
00494
00495 bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
00496 fill_rectangle(screen,
00497 s->xleft, s->ytop, s->width, s->height,
00498 bgcolor);
00499
00500 fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
00501
00502
00503 h = s->height / nb_display_channels;
00504
00505 h2 = (h * 9) / 20;
00506 for(ch = 0;ch < nb_display_channels; ch++) {
00507 i = i_start + ch;
00508 y1 = s->ytop + ch * h + (h / 2);
00509 for(x = 0; x < s->width; x++) {
00510 y = (s->sample_array[i] * h2) >> 15;
00511 if (y < 0) {
00512 y = -y;
00513 ys = y1 - y;
00514 } else {
00515 ys = y1;
00516 }
00517 fill_rectangle(screen,
00518 s->xleft + x, ys, 1, y,
00519 fgcolor);
00520 i += channels;
00521 if (i >= SAMPLE_ARRAY_SIZE)
00522 i -= SAMPLE_ARRAY_SIZE;
00523 }
00524 }
00525
00526 fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
00527
00528 for(ch = 1;ch < nb_display_channels; ch++) {
00529 y = s->ytop + ch * h;
00530 fill_rectangle(screen,
00531 s->xleft, y, s->width, 1,
00532 fgcolor);
00533 }
00534 SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
00535 }
00536
00537
00538 static void video_display(VideoState *is)
00539 {
00540 if (is->audio_st && is->show_audio)
00541 video_audio_display(is);
00542 else if (is->video_st)
00543 video_image_display(is);
00544 }
00545
00546 static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque)
00547 {
00548 SDL_Event event;
00549 event.type = FF_REFRESH_EVENT;
00550 event.user.data1 = opaque;
00551 SDL_PushEvent(&event);
00552 return 0;
00553 }
00554
00555
00556 static void schedule_refresh(VideoState *is, int delay)
00557 {
00558 SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
00559 }
00560
00561
00562 static double get_audio_clock(VideoState *is)
00563 {
00564 double pts;
00565 int hw_buf_size, bytes_per_sec;
00566 pts = is->audio_clock;
00567 hw_buf_size = audio_write_get_buf_size(is);
00568 bytes_per_sec = 0;
00569 if (is->audio_st) {
00570 bytes_per_sec = is->audio_st->codec->sample_rate *
00571 2 * is->audio_st->codec->channels;
00572 }
00573 if (bytes_per_sec)
00574 pts -= (double)hw_buf_size / bytes_per_sec;
00575 return pts;
00576 }
00577
00578
00579 static double get_video_clock(VideoState *is)
00580 {
00581 double delta;
00582 if (is->paused) {
00583 delta = 0;
00584 } else {
00585 delta = (av_gettime() - is->video_current_pts_time) / 1000000.0;
00586 }
00587 return is->video_current_pts + delta;
00588 }
00589
00590
00591 static double get_external_clock(VideoState *is)
00592 {
00593 int64_t ti;
00594 ti = av_gettime();
00595 return is->external_clock + ((ti - is->external_clock_time) * 1e-6);
00596 }
00597
00598
00599 static double get_master_clock(VideoState *is)
00600 {
00601 double val;
00602
00603 if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
00604 if (is->video_st)
00605 val = get_video_clock(is);
00606 else
00607 val = get_audio_clock(is);
00608 } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
00609 if (is->audio_st)
00610 val = get_audio_clock(is);
00611 else
00612 val = get_video_clock(is);
00613 } else {
00614 val = get_external_clock(is);
00615 }
00616 return val;
00617 }
00618
00619
00620 static void stream_seek(VideoState *is, int64_t pos, int rel)
00621 {
00622 if (!is->seek_req) {
00623 is->seek_pos = pos;
00624 is->seek_flags = rel < 0 ? AVSEEK_FLAG_BACKWARD : 0;
00625 is->seek_req = 1;
00626 }
00627 }
00628
00629
00630 static void stream_pause(VideoState *is)
00631 {
00632 is->paused = !is->paused;
00633 if (is->paused) {
00634 is->video_current_pts = get_video_clock(is);
00635 }
00636 }
00637
00638
00639 static void video_refresh_timer(void *opaque)
00640 {
00641 VideoState *is = opaque;
00642 VideoPicture *vp;
00643 double actual_delay, delay, sync_threshold, ref_clock, diff;
00644
00645
00646 if (is->video_st) {
00647 if (is->pictq_size == 0) {
00648
00649 schedule_refresh(is, 1);
00650 } else {
00651
00652 vp = &is->pictq[is->pictq_rindex];
00653
00654
00655 is->video_current_pts = vp->pts;
00656 is->video_current_pts_time = av_gettime();
00657
00658
00659 delay = vp->pts - is->frame_last_pts;
00660 if (delay <= 0 || delay >= 1.0) {
00661
00662 delay = is->frame_last_delay;
00663 }
00664 is->frame_last_delay = delay;
00665 is->frame_last_pts = vp->pts;
00666
00667
00668 if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) ||
00669 is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
00670
00671
00672 ref_clock = get_master_clock(is);
00673 diff = vp->pts - ref_clock;
00674
00675
00676
00677
00678 sync_threshold = AV_SYNC_THRESHOLD;
00679 if (delay > sync_threshold)
00680 sync_threshold = delay;
00681 if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
00682 if (diff <= -sync_threshold)
00683 delay = 0;
00684 else if (diff >= sync_threshold)
00685 delay = 2 * delay;
00686 }
00687 }
00688
00689 is->frame_timer += delay;
00690
00691
00692 actual_delay = is->frame_timer - (av_gettime() / 1000000.0);
00693 if (actual_delay < 0.010) {
00694
00695 actual_delay = 0.010;
00696 }
00697
00698 schedule_refresh(is, (int)(actual_delay * 1000 + 0.5));
00699
00700 #if defined(DEBUG_SYNC)
00701 printf("video: delay=%0.3f actual_delay=%0.3f pts=%0.3f A-V=%f\n",
00702 delay, actual_delay, vp->pts, -diff);
00703 #endif
00704
00705
00706 video_display(is);
00707
00708
00709 if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
00710 is->pictq_rindex = 0;
00711
00712 SDL_LockMutex(is->pictq_mutex);
00713 is->pictq_size--;
00714 SDL_CondSignal(is->pictq_cond);
00715 SDL_UnlockMutex(is->pictq_mutex);
00716 }
00717 } else if (is->audio_st) {
00718
00719
00720 schedule_refresh(is, 40);
00721
00722
00723
00724
00725
00726 video_display(is);
00727 } else {
00728 schedule_refresh(is, 100);
00729 }
00730 if (show_status) {
00731 static int64_t last_time;
00732 int64_t cur_time;
00733 int aqsize, vqsize;
00734 double av_diff;
00735
00736 cur_time = av_gettime();
00737 if (!last_time || (cur_time - last_time) >= 500 * 1000) {
00738 aqsize = 0;
00739 vqsize = 0;
00740 if (is->audio_st)
00741 aqsize = is->audioq.size;
00742 if (is->video_st)
00743 vqsize = is->videoq.size;
00744 av_diff = 0;
00745 if (is->audio_st && is->video_st)
00746 av_diff = get_audio_clock(is) - get_video_clock(is);
00747 printf("%7.2f A-V:%7.3f aq=%5dKB vq=%5dKB \r",
00748 get_master_clock(is), av_diff, aqsize / 1024, vqsize / 1024);
00749 fflush(stdout);
00750 last_time = cur_time;
00751 }
00752 }
00753 }
00754
00755
00756
00757 static void alloc_picture(void *opaque)
00758 {
00759 VideoState *is = opaque;
00760 VideoPicture *vp;
00761
00762 vp = &is->pictq[is->pictq_windex];
00763
00764 if (vp->bmp)
00765 SDL_FreeYUVOverlay(vp->bmp);
00766
00767 #if 0
00768
00769
00770 switch(is->video_st->codec->pix_fmt) {
00771 case PIX_FMT_YUV420P:
00772 case PIX_FMT_YUV422P:
00773 case PIX_FMT_YUV444P:
00774 case PIX_FMT_YUV422:
00775 case PIX_FMT_YUV410P:
00776 case PIX_FMT_YUV411P:
00777 is_yuv = 1;
00778 break;
00779 default:
00780 is_yuv = 0;
00781 break;
00782 }
00783 #endif
00784 vp->bmp = SDL_CreateYUVOverlay(is->video_st->codec->width,
00785 is->video_st->codec->height,
00786 SDL_YV12_OVERLAY,
00787 screen);
00788 vp->width = is->video_st->codec->width;
00789 vp->height = is->video_st->codec->height;
00790
00791 SDL_LockMutex(is->pictq_mutex);
00792 vp->allocated = 1;
00793 SDL_CondSignal(is->pictq_cond);
00794 SDL_UnlockMutex(is->pictq_mutex);
00795 }
00796
00797 static int queue_picture(VideoState *is, AVFrame *src_frame, double pts)
00798 {
00799 VideoPicture *vp;
00800 int dst_pix_fmt;
00801 AVPicture pict;
00802
00803
00804 SDL_LockMutex(is->pictq_mutex);
00805 while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
00806 !is->videoq.abort_request) {
00807 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
00808 }
00809 SDL_UnlockMutex(is->pictq_mutex);
00810
00811 if (is->videoq.abort_request)
00812 return -1;
00813
00814 vp = &is->pictq[is->pictq_windex];
00815
00816
00817 if (!vp->bmp ||
00818 vp->width != is->video_st->codec->width ||
00819 vp->height != is->video_st->codec->height) {
00820 SDL_Event event;
00821
00822 vp->allocated = 0;
00823
00824
00825
00826 event.type = FF_ALLOC_EVENT;
00827 event.user.data1 = is;
00828 SDL_PushEvent(&event);
00829
00830
00831 SDL_LockMutex(is->pictq_mutex);
00832 while (!vp->allocated && !is->videoq.abort_request) {
00833 SDL_CondWait(is->pictq_cond, is->pictq_mutex);
00834 }
00835 SDL_UnlockMutex(is->pictq_mutex);
00836
00837 if (is->videoq.abort_request)
00838 return -1;
00839 }
00840
00841
00842 if (vp->bmp) {
00843
00844 SDL_LockYUVOverlay (vp->bmp);
00845
00846 dst_pix_fmt = PIX_FMT_YUV420P;
00847 pict.data[0] = vp->bmp->pixels[0];
00848 pict.data[1] = vp->bmp->pixels[2];
00849 pict.data[2] = vp->bmp->pixels[1];
00850
00851 pict.linesize[0] = vp->bmp->pitches[0];
00852 pict.linesize[1] = vp->bmp->pitches[2];
00853 pict.linesize[2] = vp->bmp->pitches[1];
00854 img_convert(&pict, dst_pix_fmt,
00855 (AVPicture *)src_frame, is->video_st->codec->pix_fmt,
00856 is->video_st->codec->width, is->video_st->codec->height);
00857
00858 SDL_UnlockYUVOverlay(vp->bmp);
00859
00860 vp->pts = pts;
00861
00862
00863 if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
00864 is->pictq_windex = 0;
00865 SDL_LockMutex(is->pictq_mutex);
00866 is->pictq_size++;
00867 SDL_UnlockMutex(is->pictq_mutex);
00868 }
00869 return 0;
00870 }
00871
00872
00873 static int output_picture2(VideoState *is, AVFrame *src_frame, double pts1)
00874 {
00875 double frame_delay, pts;
00876
00877 pts = pts1;
00878
00879 if (pts != 0) {
00880
00881 is->video_clock = pts;
00882 } else {
00883 pts = is->video_clock;
00884 }
00885
00886 frame_delay = av_q2d(is->video_st->codec->time_base);
00887
00888
00889 if (src_frame->repeat_pict) {
00890 frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
00891 }
00892 is->video_clock += frame_delay;
00893
00894 #if defined(DEBUG_SYNC) && 0
00895 {
00896 int ftype;
00897 if (src_frame->pict_type == FF_B_TYPE)
00898 ftype = 'B';
00899 else if (src_frame->pict_type == FF_I_TYPE)
00900 ftype = 'I';
00901 else
00902 ftype = 'P';
00903 printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
00904 ftype, pts, pts1);
00905 }
00906 #endif
00907 return queue_picture(is, src_frame, pts);
00908 }
00909
00910 static int video_thread(void *arg)
00911 {
00912 VideoState *is = arg;
00913 AVPacket pkt1, *pkt = &pkt1;
00914 int len1, got_picture;
00915 AVFrame *frame= avcodec_alloc_frame();
00916 double pts;
00917
00918 for(;;) {
00919 while (is->paused && !is->videoq.abort_request) {
00920 SDL_Delay(10);
00921 }
00922 if (packet_queue_get(&is->videoq, pkt, 1) < 0)
00923 break;
00924
00925
00926 pts = 0;
00927 if (pkt->dts != AV_NOPTS_VALUE)
00928 pts = av_q2d(is->video_st->time_base)*pkt->dts;
00929
00930 SDL_LockMutex(is->video_decoder_mutex);
00931 len1 = avcodec_decode_video(is->video_st->codec,
00932 frame, &got_picture,
00933 pkt->data, pkt->size);
00934 SDL_UnlockMutex(is->video_decoder_mutex);
00935
00936
00937 if (got_picture) {
00938 if (output_picture2(is, frame, pts) < 0)
00939 goto the_end;
00940 }
00941 av_free_packet(pkt);
00942 if (step)
00943 if (cur_stream)
00944 stream_pause(cur_stream);
00945 }
00946 the_end:
00947 av_free(frame);
00948 return 0;
00949 }
00950
00951
00952 static void update_sample_display(VideoState *is, short *samples, int samples_size)
00953 {
00954 int size, len, channels;
00955
00956 channels = is->audio_st->codec->channels;
00957
00958 size = samples_size / sizeof(short);
00959 while (size > 0) {
00960 len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
00961 if (len > size)
00962 len = size;
00963 memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
00964 samples += len;
00965 is->sample_array_index += len;
00966 if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
00967 is->sample_array_index = 0;
00968 size -= len;
00969 }
00970 }
00971
00972
00973
00974 static int synchronize_audio(VideoState *is, short *samples,
00975 int samples_size1, double pts)
00976 {
00977 int n, samples_size;
00978 double ref_clock;
00979
00980 n = 2 * is->audio_st->codec->channels;
00981 samples_size = samples_size1;
00982
00983
00984 if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||
00985 is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
00986 double diff, avg_diff;
00987 int wanted_size, min_size, max_size, nb_samples;
00988
00989 ref_clock = get_master_clock(is);
00990 diff = get_audio_clock(is) - ref_clock;
00991
00992 if (diff < AV_NOSYNC_THRESHOLD) {
00993 is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
00994 if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
00995
00996 is->audio_diff_avg_count++;
00997 } else {
00998
00999 avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
01000
01001 if (fabs(avg_diff) >= is->audio_diff_threshold) {
01002 wanted_size = samples_size + ((int)(diff * is->audio_st->codec->sample_rate) * n);
01003 nb_samples = samples_size / n;
01004
01005 min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
01006 max_size = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
01007 if (wanted_size < min_size)
01008 wanted_size = min_size;
01009 else if (wanted_size > max_size)
01010 wanted_size = max_size;
01011
01012
01013 if (wanted_size < samples_size) {
01014
01015 samples_size = wanted_size;
01016 } else if (wanted_size > samples_size) {
01017 uint8_t *samples_end, *q;
01018 int nb;
01019
01020
01021 nb = (samples_size - wanted_size);
01022 samples_end = (uint8_t *)samples + samples_size - n;
01023 q = samples_end + n;
01024 while (nb > 0) {
01025 memcpy(q, samples_end, n);
01026 q += n;
01027 nb -= n;
01028 }
01029 samples_size = wanted_size;
01030 }
01031 }
01032 #if 0
01033 printf("diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
01034 diff, avg_diff, samples_size - samples_size1,
01035 is->audio_clock, is->video_clock, is->audio_diff_threshold);
01036 #endif
01037 }
01038 } else {
01039
01040
01041 is->audio_diff_avg_count = 0;
01042 is->audio_diff_cum = 0;
01043 }
01044 }
01045
01046 return samples_size;
01047 }
01048
01049
01050 static int audio_decode_frame(VideoState *is, uint8_t *audio_buf, double *pts_ptr)
01051 {
01052 AVPacket *pkt = &is->audio_pkt;
01053 int n, len1, data_size;
01054 double pts;
01055
01056 for(;;) {
01057
01058 while (is->audio_pkt_size > 0) {
01059 SDL_LockMutex(is->audio_decoder_mutex);
01060 len1 = avcodec_decode_audio(is->audio_st->codec,
01061 (int16_t *)audio_buf, &data_size,
01062 is->audio_pkt_data, is->audio_pkt_size);
01063 SDL_UnlockMutex(is->audio_decoder_mutex);
01064 if (len1 < 0) {
01065
01066 is->audio_pkt_size = 0;
01067 break;
01068 }
01069
01070 is->audio_pkt_data += len1;
01071 is->audio_pkt_size -= len1;
01072 if (data_size <= 0)
01073 continue;
01074
01075 pts = is->audio_clock;
01076 *pts_ptr = pts;
01077 n = 2 * is->audio_st->codec->channels;
01078 is->audio_clock += (double)data_size /
01079 (double)(n * is->audio_st->codec->sample_rate);
01080 #if defined(DEBUG_SYNC)
01081 {
01082 static double last_clock;
01083 printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
01084 is->audio_clock - last_clock,
01085 is->audio_clock, pts);
01086 last_clock = is->audio_clock;
01087 }
01088 #endif
01089 return data_size;
01090 }
01091
01092
01093 if (pkt->data)
01094 av_free_packet(pkt);
01095
01096 if (is->paused || is->audioq.abort_request) {
01097 return -1;
01098 }
01099
01100
01101 if (packet_queue_get(&is->audioq, pkt, 1) < 0)
01102 return -1;
01103 is->audio_pkt_data = pkt->data;
01104 is->audio_pkt_size = pkt->size;
01105
01106
01107 if (pkt->pts != AV_NOPTS_VALUE) {
01108 is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
01109 }
01110 }
01111 }
01112
01113
01114
01115 static int audio_write_get_buf_size(VideoState *is)
01116 {
01117 return is->audio_hw_buf_size - is->audio_buf_index;
01118 }
01119
01120
01121
01122 void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
01123 {
01124 VideoState *is = opaque;
01125 int audio_size, len1;
01126 double pts;
01127
01128 audio_callback_time = av_gettime();
01129
01130 while (len > 0) {
01131 if (is->audio_buf_index >= is->audio_buf_size) {
01132 audio_size = audio_decode_frame(is, is->audio_buf, &pts);
01133 if (audio_size < 0) {
01134
01135 is->audio_buf_size = 1024;
01136 memset(is->audio_buf, 0, is->audio_buf_size);
01137 } else {
01138 if (is->show_audio)
01139 update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
01140 audio_size = synchronize_audio(is, (int16_t *)is->audio_buf, audio_size,
01141 pts);
01142 is->audio_buf_size = audio_size;
01143 }
01144 is->audio_buf_index = 0;
01145 }
01146 len1 = is->audio_buf_size - is->audio_buf_index;
01147 if (len1 > len)
01148 len1 = len;
01149 memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
01150 len -= len1;
01151 stream += len1;
01152 is->audio_buf_index += len1;
01153 }
01154 }
01155
01156
01157
01158 static int stream_component_open(VideoState *is, int stream_index)
01159 {
01160 AVFormatContext *ic = is->ic;
01161 AVCodecContext *enc;
01162 AVCodec *codec;
01163 SDL_AudioSpec wanted_spec, spec;
01164
01165 if (stream_index < 0 || stream_index >= ic->nb_streams)
01166 return -1;
01167 enc = ic->streams[stream_index]->codec;
01168
01169
01170 if (enc->codec_type == CODEC_TYPE_AUDIO) {
01171 wanted_spec.freq = enc->sample_rate;
01172 wanted_spec.format = AUDIO_S16SYS;
01173
01174 if (enc->channels > 2)
01175 enc->channels = 2;
01176 wanted_spec.channels = enc->channels;
01177 wanted_spec.silence = 0;
01178 wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
01179 wanted_spec.callback = sdl_audio_callback;
01180 wanted_spec.userdata = is;
01181 if (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
01182 fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
01183 return -1;
01184 }
01185 is->audio_hw_buf_size = spec.size;
01186 }
01187
01188 codec = avcodec_find_decoder(enc->codec_id);
01189 enc->debug_mv = debug_mv;
01190 enc->debug = debug;
01191 if(debug)
01192 av_log_set_level(AV_LOG_DEBUG);
01193 enc->workaround_bugs = workaround_bugs;
01194 enc->lowres = lowres;
01195 if(lowres) enc->flags |= CODEC_FLAG_EMU_EDGE;
01196 enc->idct_algo= idct;
01197 if(fast) enc->flags2 |= CODEC_FLAG2_FAST;
01198 enc->skip_frame= skip_frame;
01199 enc->skip_idct= skip_idct;
01200 enc->skip_loop_filter= skip_loop_filter;
01201 enc->error_resilience= error_resilience;
01202 enc->error_concealment= error_concealment;
01203 if (!codec ||
01204 avcodec_open(enc, codec) < 0)
01205 return -1;
01206 #if defined(HAVE_THREADS)
01207 if(thread_count>1)
01208 avcodec_thread_init(enc, thread_count);
01209 #endif
01210 enc->thread_count= thread_count;
01211 switch(enc->codec_type) {
01212 case CODEC_TYPE_AUDIO:
01213 is->audio_stream = stream_index;
01214 is->audio_st = ic->streams[stream_index];
01215 is->audio_buf_size = 0;
01216 is->audio_buf_index = 0;
01217
01218
01219 is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
01220 is->audio_diff_avg_count = 0;
01221
01222
01223 is->audio_diff_threshold = 2.0 * SDL_AUDIO_BUFFER_SIZE / enc->sample_rate;
01224
01225 memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
01226 packet_queue_init(&is->audioq);
01227 SDL_PauseAudio(0);
01228 break;
01229 case CODEC_TYPE_VIDEO:
01230 is->video_stream = stream_index;
01231 is->video_st = ic->streams[stream_index];
01232
01233 is->frame_last_delay = 40e-3;
01234 is->frame_timer = (double)av_gettime() / 1000000.0;
01235 is->video_current_pts_time = av_gettime();
01236
01237 packet_queue_init(&is->videoq);
01238 is->video_tid = SDL_CreateThread(video_thread, is);
01239 break;
01240 default:
01241 break;
01242 }
01243 return 0;
01244 }
01245
01246 static void stream_component_close(VideoState *is, int stream_index)
01247 {
01248 AVFormatContext *ic = is->ic;
01249 AVCodecContext *enc;
01250
01251 enc = ic->streams[stream_index]->codec;
01252
01253 switch(enc->codec_type) {
01254 case CODEC_TYPE_AUDIO:
01255 packet_queue_abort(&is->audioq);
01256
01257 SDL_CloseAudio();
01258
01259 packet_queue_end(&is->audioq);
01260 break;
01261 case CODEC_TYPE_VIDEO:
01262 packet_queue_abort(&is->videoq);
01263
01264
01265
01266 SDL_LockMutex(is->pictq_mutex);
01267 SDL_CondSignal(is->pictq_cond);
01268 SDL_UnlockMutex(is->pictq_mutex);
01269
01270 SDL_WaitThread(is->video_tid, NULL);
01271
01272 packet_queue_end(&is->videoq);
01273 break;
01274 default:
01275 break;
01276 }
01277
01278 avcodec_close(enc);
01279 switch(enc->codec_type) {
01280 case CODEC_TYPE_AUDIO:
01281 is->audio_st = NULL;
01282 is->audio_stream = -1;
01283 break;
01284 case CODEC_TYPE_VIDEO:
01285 is->video_st = NULL;
01286 is->video_stream = -1;
01287 break;
01288 default:
01289 break;
01290 }
01291 }
01292
01293 void dump_stream_info(AVFormatContext *s)
01294 {
01295 if (s->track != 0)
01296 fprintf(stderr, "Track: %d\n", s->track);
01297 if (s->title[0] != '\0')
01298 fprintf(stderr, "Title: %s\n", s->title);
01299 if (s->author[0] != '\0')
01300 fprintf(stderr, "Author: %s\n", s->author);
01301 if (s->album[0] != '\0')
01302 fprintf(stderr, "Album: %s\n", s->album);
01303 if (s->year != 0)
01304 fprintf(stderr, "Year: %d\n", s->year);
01305 if (s->genre[0] != '\0')
01306 fprintf(stderr, "Genre: %s\n", s->genre);
01307 }
01308
01309
01310
01311 static VideoState *global_video_state;
01312
01313 static int decode_interrupt_cb(void)
01314 {
01315 return (global_video_state && global_video_state->abort_request);
01316 }
01317
01318
01319 static int decode_thread(void *arg)
01320 {
01321 VideoState *is = arg;
01322 AVFormatContext *ic;
01323 int err, i, ret, video_index, audio_index, use_play;
01324 AVPacket pkt1, *pkt = &pkt1;
01325 AVFormatParameters params, *ap = ¶ms;
01326
01327 video_index = -1;
01328 audio_index = -1;
01329 is->video_stream = -1;
01330 is->audio_stream = -1;
01331
01332 global_video_state = is;
01333 url_set_interrupt_cb(decode_interrupt_cb);
01334
01335 memset(ap, 0, sizeof(*ap));
01336 ap->image_format = image_format;
01337 ap->initial_pause = 1;
01338
01339
01340 err = av_open_input_file(&ic, is->filename, is->iformat, 0, ap);
01341 if (err < 0) {
01342 print_error(is->filename, err);
01343 ret = -1;
01344 goto fail;
01345 }
01346 is->ic = ic;
01347 #ifdef CONFIG_NETWORK
01348 use_play = (ic->iformat == &rtsp_demux);
01349 #else
01350 use_play = 0;
01351 #endif
01352 if (!use_play) {
01353 err = av_find_stream_info(ic);
01354 if (err < 0) {
01355 fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
01356 ret = -1;
01357 goto fail;
01358 }
01359 ic->pb.eof_reached= 0;
01360 }
01361
01362
01363 if (start_time != AV_NOPTS_VALUE) {
01364 int64_t timestamp;
01365
01366 timestamp = start_time;
01367
01368 if (ic->start_time != AV_NOPTS_VALUE)
01369 timestamp += ic->start_time;
01370 ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);
01371 if (ret < 0) {
01372 fprintf(stderr, "%s: could not seek to position %0.3f\n",
01373 is->filename, (double)timestamp / AV_TIME_BASE);
01374 }
01375 }
01376
01377
01378 av_read_play(ic);
01379
01380 if (use_play) {
01381 err = av_find_stream_info(ic);
01382 if (err < 0) {
01383 fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
01384 ret = -1;
01385 goto fail;
01386 }
01387 }
01388
01389 for(i = 0; i < ic->nb_streams; i++) {
01390 AVCodecContext *enc = ic->streams[i]->codec;
01391 switch(enc->codec_type) {
01392 case CODEC_TYPE_AUDIO:
01393 if (audio_index < 0 && !audio_disable)
01394 audio_index = i;
01395 break;
01396 case CODEC_TYPE_VIDEO:
01397 if (video_index < 0 && !video_disable)
01398 video_index = i;
01399 break;
01400 default:
01401 break;
01402 }
01403 }
01404 if (show_status) {
01405 dump_format(ic, 0, is->filename, 0);
01406 dump_stream_info(ic);
01407 }
01408
01409
01410 if (audio_index >= 0) {
01411 stream_component_open(is, audio_index);
01412 }
01413
01414 if (video_index >= 0) {
01415 stream_component_open(is, video_index);
01416 } else {
01417 if (!display_disable)
01418 is->show_audio = 1;
01419 }
01420
01421 if (is->video_stream < 0 && is->audio_stream < 0) {
01422 fprintf(stderr, "%s: could not open codecs\n", is->filename);
01423 ret = -1;
01424 goto fail;
01425 }
01426
01427 for(;;) {
01428 if (is->abort_request)
01429 break;
01430 #ifdef CONFIG_NETWORK
01431 if (is->paused != is->last_paused) {
01432 is->last_paused = is->paused;
01433 if (is->paused)
01434 av_read_pause(ic);
01435 else
01436 av_read_play(ic);
01437 }
01438 if (is->paused && ic->iformat == &rtsp_demux) {
01439
01440
01441 SDL_Delay(10);
01442 continue;
01443 }
01444 #endif
01445 if (is->seek_req) {
01446
01447 SDL_LockMutex(is->video_decoder_mutex);
01448 SDL_LockMutex(is->audio_decoder_mutex);
01449 ret = av_seek_frame(is->ic, -1, is->seek_pos, is->seek_flags);
01450 if (ret < 0) {
01451 fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
01452 }else{
01453 if (is->audio_stream >= 0) {
01454 packet_queue_flush(&is->audioq);
01455 }
01456 if (is->video_stream >= 0) {
01457 packet_queue_flush(&is->videoq);
01458 avcodec_flush_buffers(ic->streams[video_index]->codec);
01459 }
01460 }
01461 SDL_UnlockMutex(is->audio_decoder_mutex);
01462 SDL_UnlockMutex(is->video_decoder_mutex);
01463 is->seek_req = 0;
01464 }
01465
01466
01467 if (is->audioq.size > MAX_AUDIOQ_SIZE ||
01468 is->videoq.size > MAX_VIDEOQ_SIZE ||
01469 url_feof(&ic->pb)) {
01470
01471 SDL_Delay(10);
01472 continue;
01473 }
01474 ret = av_read_frame(ic, pkt);
01475 if (ret < 0) {
01476 if (url_ferror(&ic->pb) == 0) {
01477 SDL_Delay(100);
01478 continue;
01479 } else
01480 break;
01481 }
01482 if (pkt->stream_index == is->audio_stream) {
01483 packet_queue_put(&is->audioq, pkt);
01484 } else if (pkt->stream_index == is->video_stream) {
01485 packet_queue_put(&is->videoq, pkt);
01486 } else {
01487 av_free_packet(pkt);
01488 }
01489 }
01490
01491 while (!is->abort_request) {
01492 SDL_Delay(100);
01493 }
01494
01495 ret = 0;
01496 fail:
01497
01498 global_video_state = NULL;
01499
01500
01501 if (is->audio_stream >= 0)
01502 stream_component_close(is, is->audio_stream);
01503 if (is->video_stream >= 0)
01504 stream_component_close(is, is->video_stream);
01505 if (is->ic) {
01506 av_close_input_file(is->ic);
01507 is->ic = NULL;
01508 }
01509 url_set_interrupt_cb(NULL);
01510
01511 if (ret != 0) {
01512 SDL_Event event;
01513
01514 event.type = FF_QUIT_EVENT;
01515 event.user.data1 = is;
01516 SDL_PushEvent(&event);
01517 }
01518 return 0;
01519 }
01520
01521 static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
01522 {
01523 VideoState *is;
01524
01525 is = av_mallocz(sizeof(VideoState));
01526 if (!is)
01527 return NULL;
01528 pstrcpy(is->filename, sizeof(is->filename), filename);
01529 is->iformat = iformat;
01530 if (screen) {
01531 is->width = screen->w;
01532 is->height = screen->h;
01533 }
01534 is->ytop = 0;
01535 is->xleft = 0;
01536
01537
01538 is->pictq_mutex = SDL_CreateMutex();
01539 is->pictq_cond = SDL_CreateCond();
01540
01541 is->audio_decoder_mutex = SDL_CreateMutex();
01542 is->video_decoder_mutex = SDL_CreateMutex();
01543
01544
01545 schedule_refresh(is, 40);
01546
01547 is->av_sync_type = av_sync_type;
01548 is->parse_tid = SDL_CreateThread(decode_thread, is);
01549 if (!is->parse_tid) {
01550 av_free(is);
01551 return NULL;
01552 }
01553 return is;
01554 }
01555
01556 static void stream_close(VideoState *is)
01557 {
01558 VideoPicture *vp;
01559 int i;
01560
01561 is->abort_request = 1;
01562 SDL_WaitThread(is->parse_tid, NULL);
01563
01564
01565 for(i=0;i<VIDEO_PICTURE_QUEUE_SIZE; i++) {
01566 vp = &is->pictq[i];
01567 if (vp->bmp) {
01568 SDL_FreeYUVOverlay(vp->bmp);
01569 vp->bmp = NULL;
01570 }
01571 }
01572 SDL_DestroyMutex(is->pictq_mutex);
01573 SDL_DestroyCond(is->pictq_cond);
01574 SDL_DestroyMutex(is->audio_decoder_mutex);
01575 SDL_DestroyMutex(is->video_decoder_mutex);
01576 }
01577
01578 void stream_cycle_channel(VideoState *is, int codec_type)
01579 {
01580 AVFormatContext *ic = is->ic;
01581 int start_index, stream_index;
01582 AVStream *st;
01583
01584 if (codec_type == CODEC_TYPE_VIDEO)
01585 start_index = is->video_stream;
01586 else
01587 start_index = is->audio_stream;
01588 if (start_index < 0)
01589 return;
01590 stream_index = start_index;
01591 for(;;) {
01592 if (++stream_index >= is->ic->nb_streams)
01593 stream_index = 0;
01594 if (stream_index == start_index)
01595 return;
01596 st = ic->streams[stream_index];
01597 if (st->codec->codec_type == codec_type) {
01598
01599 switch(codec_type) {
01600 case CODEC_TYPE_AUDIO:
01601 if (st->codec->sample_rate != 0 &&
01602 st->codec->channels != 0)
01603 goto the_end;
01604 break;
01605 case CODEC_TYPE_VIDEO:
01606 goto the_end;
01607 default:
01608 break;
01609 }
01610 }
01611 }
01612 the_end:
01613 stream_component_close(is, start_index);
01614 stream_component_open(is, stream_index);
01615 }
01616
01617
01618 void toggle_full_screen(void)
01619 {
01620 int w, h, flags;
01621 is_full_screen = !is_full_screen;
01622 if (!fs_screen_width) {
01623
01624 SDL_WM_ToggleFullScreen(screen);
01625 } else {
01626
01627 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
01628 if (is_full_screen) {
01629 w = fs_screen_width;
01630 h = fs_screen_height;
01631 flags |= SDL_FULLSCREEN;
01632 } else {
01633 w = screen_width;
01634 h = screen_height;
01635 flags |= SDL_RESIZABLE;
01636 }
01637 screen = SDL_SetVideoMode(w, h, 0, flags);
01638 cur_stream->width = w;
01639 cur_stream->height = h;
01640 }
01641 }
01642
01643 void toggle_pause(void)
01644 {
01645 if (cur_stream)
01646 stream_pause(cur_stream);
01647 step = 0;
01648 }
01649
01650 void step_to_next_frame(void)
01651 {
01652 if (cur_stream) {
01653 if (cur_stream->paused)
01654 cur_stream->paused=0;
01655 cur_stream->video_current_pts = get_video_clock(cur_stream);
01656 }
01657 step = 1;
01658 }
01659
01660 void do_exit(void)
01661 {
01662 if (cur_stream) {
01663 stream_close(cur_stream);
01664 cur_stream = NULL;
01665 }
01666 if (show_status)
01667 printf("\n");
01668 SDL_Quit();
01669 exit(0);
01670 }
01671
01672 void toggle_audio_display(void)
01673 {
01674 if (cur_stream) {
01675 cur_stream->show_audio = !cur_stream->show_audio;
01676 }
01677 }
01678
01679
01680 void event_loop(void)
01681 {
01682 SDL_Event event;
01683 double incr, pos, frac;
01684
01685 for(;;) {
01686 SDL_WaitEvent(&event);
01687 switch(event.type) {
01688 case SDL_KEYDOWN:
01689 switch(event.key.keysym.sym) {
01690 case SDLK_ESCAPE:
01691 case SDLK_q:
01692 do_exit();
01693 break;
01694 case SDLK_f:
01695 toggle_full_screen();
01696 break;
01697 case SDLK_p:
01698 case SDLK_SPACE:
01699 toggle_pause();
01700 break;
01701 case SDLK_s:
01702 step_to_next_frame();
01703 break;
01704 case SDLK_a:
01705 if (cur_stream)
01706 stream_cycle_channel(cur_stream, CODEC_TYPE_AUDIO);
01707 break;
01708 case SDLK_v:
01709 if (cur_stream)
01710 stream_cycle_channel(cur_stream, CODEC_TYPE_VIDEO);
01711 break;
01712 case SDLK_w:
01713 toggle_audio_display();
01714 break;
01715 case SDLK_LEFT:
01716 incr = -10.0;
01717 goto do_seek;
01718 case SDLK_RIGHT:
01719 incr = 10.0;
01720 goto do_seek;
01721 case SDLK_UP:
01722 incr = 60.0;
01723 goto do_seek;
01724 case SDLK_DOWN:
01725 incr = -60.0;
01726 do_seek:
01727 if (cur_stream) {
01728 pos = get_master_clock(cur_stream);
01729 pos += incr;
01730 stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), incr);
01731 }
01732 break;
01733 default:
01734 break;
01735 }
01736 break;
01737 case SDL_MOUSEBUTTONDOWN:
01738 if (cur_stream) {
01739 int ns, hh, mm, ss;
01740 int tns, thh, tmm, tss;
01741 tns = cur_stream->ic->duration/1000000LL;
01742 thh = tns/3600;
01743 tmm = (tns%3600)/60;
01744 tss = (tns%60);
01745 frac = (double)event.button.x/(double)cur_stream->width;
01746 ns = frac*tns;
01747 hh = ns/3600;
01748 mm = (ns%3600)/60;
01749 ss = (ns%60);
01750 fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
01751 hh, mm, ss, thh, tmm, tss);
01752 stream_seek(cur_stream, (int64_t)(cur_stream->ic->start_time+frac*cur_stream->ic->duration), 0);
01753 }
01754 break;
01755 case SDL_VIDEORESIZE:
01756 if (cur_stream) {
01757 screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
01758 SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
01759 cur_stream->width = event.resize.w;
01760 cur_stream->height = event.resize.h;
01761 }
01762 break;
01763 case SDL_QUIT:
01764 case FF_QUIT_EVENT:
01765 do_exit();
01766 break;
01767 case FF_ALLOC_EVENT:
01768 alloc_picture(event.user.data1);
01769 break;
01770 case FF_REFRESH_EVENT:
01771 video_refresh_timer(event.user.data1);
01772 break;
01773 default:
01774 break;
01775 }
01776 }
01777 }
01778
01779 void opt_width(const char *arg)
01780 {
01781 screen_width = atoi(arg);
01782 }
01783
01784 void opt_height(const char *arg)
01785 {
01786 screen_height = atoi(arg);
01787 }
01788
01789 static void opt_format(const char *arg)
01790 {
01791 file_iformat = av_find_input_format(arg);
01792 if (!file_iformat) {
01793 fprintf(stderr, "Unknown input format: %s\n", arg);
01794 exit(1);
01795 }
01796 }
01797
01798 static void opt_image_format(const char *arg)
01799 {
01800 AVImageFormat *f;
01801
01802 for(f = first_image_format; f != NULL; f = f->next) {
01803 if (!strcmp(arg, f->name))
01804 break;
01805 }
01806 if (!f) {
01807 fprintf(stderr, "Unknown image format: '%s'\n", arg);
01808 exit(1);
01809 }
01810 image_format = f;
01811 }
01812
01813 #ifdef CONFIG_NETWORK
01814 void opt_rtp_tcp(void)
01815 {
01816
01817 rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP);
01818 }
01819 #endif
01820
01821 void opt_sync(const char *arg)
01822 {
01823 if (!strcmp(arg, "audio"))
01824 av_sync_type = AV_SYNC_AUDIO_MASTER;
01825 else if (!strcmp(arg, "video"))
01826 av_sync_type = AV_SYNC_VIDEO_MASTER;
01827 else if (!strcmp(arg, "ext"))
01828 av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
01829 else
01830 show_help();
01831 }
01832
01833 void opt_seek(const char *arg)
01834 {
01835 start_time = parse_date(arg, 1);
01836 }
01837
01838 static void opt_debug(const char *arg)
01839 {
01840 debug = atoi(arg);
01841 }
01842
01843 static void opt_vismv(const char *arg)
01844 {
01845 debug_mv = atoi(arg);
01846 }
01847
01848 static void opt_thread_count(const char *arg)
01849 {
01850 thread_count= atoi(arg);
01851 #if !defined(HAVE_THREADS)
01852 fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
01853 #endif
01854 }
01855
01856 const OptionDef options[] = {
01857 { "h", 0, {(void*)show_help}, "show help" },
01858 { "x", HAS_ARG, {(void*)opt_width}, "force displayed width", "width" },
01859 { "y", HAS_ARG, {(void*)opt_height}, "force displayed height", "height" },
01860 #if 0
01861
01862 { "fs", OPT_BOOL, {(void*)&is_full_screen}, "force full screen" },
01863 #endif
01864 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
01865 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
01866 { "ss", HAS_ARG, {(void*)&opt_seek}, "seek to a given position in seconds", "pos" },
01867 { "nodisp", OPT_BOOL, {(void*)&display_disable}, "disable graphical display" },
01868 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
01869 { "img", HAS_ARG, {(void*)opt_image_format}, "force image format", "img_fmt" },
01870 { "stats", OPT_BOOL | OPT_EXPERT, {(void*)&show_status}, "show status", "" },
01871 { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info", "" },
01872 { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&workaround_bugs}, "workaround bugs", "" },
01873 { "vismv", HAS_ARG | OPT_EXPERT, {(void*)opt_vismv}, "visualize motion vectors", "" },
01874 { "fast", OPT_BOOL | OPT_EXPERT, {(void*)&fast}, "non spec compliant optimizations", "" },
01875 { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&lowres}, "", "" },
01876 { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&skip_loop_filter}, "", "" },
01877 { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&skip_frame}, "", "" },
01878 { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&skip_idct}, "", "" },
01879 { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&idct}, "set idct algo", "algo" },
01880 { "er", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&error_resilience}, "set error detection threshold (0-4)", "threshold" },
01881 { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&error_concealment}, "set error concealment options", "bit_mask" },
01882 #ifdef CONFIG_NETWORK
01883 { "rtp_tcp", OPT_EXPERT, {(void*)&opt_rtp_tcp}, "force RTP/TCP protocol usage", "" },
01884 #endif
01885 { "sync", HAS_ARG | OPT_EXPERT, {(void*)opt_sync}, "set audio-video sync. type (type=audio/video/ext)", "type" },
01886 { "threads", HAS_ARG | OPT_EXPERT, {(void*)opt_thread_count}, "thread count", "count" },
01887 { NULL, },
01888 };
01889
01890 void show_help(void)
01891 {
01892 printf("ffplay version " FFMPEG_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
01893 "usage: ffplay [options] input_file\n"
01894 "Simple media player\n");
01895 printf("\n");
01896 show_help_options(options, "Main options:\n",
01897 OPT_EXPERT, 0);
01898 show_help_options(options, "\nAdvanced options:\n",
01899 OPT_EXPERT, OPT_EXPERT);
01900 printf("\nWhile playing:\n"
01901 "q, ESC quit\n"
01902 "f toggle full screen\n"
01903 "p, SPC pause\n"
01904 "a cycle audio channel\n"
01905 "v cycle video channel\n"
01906 "w show audio waves\n"
01907 "left/right seek backward/forward 10 seconds\n"
01908 "down/up seek backward/forward 1 minute\n"
01909 "mouse click seek to percentage in file corresponding to fraction of width\n"
01910 );
01911 exit(1);
01912 }
01913
01914 void parse_arg_file(const char *filename)
01915 {
01916 if (!strcmp(filename, "-"))
01917 filename = "pipe:";
01918 input_filename = filename;
01919 }
01920
01921
01922 int main(int argc, char **argv)
01923 {
01924 int flags, w, h;
01925
01926
01927 av_register_all();
01928
01929 #ifdef CONFIG_OS2
01930 MorphToPM();
01931
01932
01933 setbuf( stdout, NULL );
01934 setbuf( stderr, NULL );
01935 #endif
01936
01937 parse_options(argc, argv, options);
01938
01939 if (!input_filename)
01940 show_help();
01941
01942 if (display_disable) {
01943 video_disable = 1;
01944 }
01945 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
01946 #ifndef CONFIG_WIN32
01947 flags |= SDL_INIT_EVENTTHREAD;
01948 #endif
01949 if (SDL_Init (flags)) {
01950 fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
01951 exit(1);
01952 }
01953
01954 if (!display_disable) {
01955 #ifdef HAVE_X11
01956
01957
01958 {
01959 Display *dpy;
01960 dpy = XOpenDisplay(NULL);
01961 if (dpy) {
01962 fs_screen_width = DisplayWidth(dpy, DefaultScreen(dpy));
01963 fs_screen_height = DisplayHeight(dpy, DefaultScreen(dpy));
01964 XCloseDisplay(dpy);
01965 }
01966 }
01967 #endif
01968 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
01969 if (is_full_screen && fs_screen_width) {
01970 w = fs_screen_width;
01971 h = fs_screen_height;
01972 flags |= SDL_FULLSCREEN;
01973 } else {
01974 w = screen_width;
01975 h = screen_height;
01976 flags |= SDL_RESIZABLE;
01977 }
01978 screen = SDL_SetVideoMode(w, h, 0, flags);
01979 if (!screen) {
01980 fprintf(stderr, "SDL: could not set video mode - exiting\n");
01981 exit(1);
01982 }
01983 SDL_WM_SetCaption("FFplay", "FFplay");
01984 }
01985
01986 SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
01987 SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
01988 SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
01989 SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
01990
01991 cur_stream = stream_open(input_filename, file_iformat);
01992
01993 event_loop();
01994
01995
01996
01997 return 0;
01998 }