X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=ffplay.c;h=5cc4fa42582bcaf25ca18546b214f38fe3ecc521;hb=7776091b9f8af51750b27bacd5a50e35fa3930ea;hp=f116db98c63141c00547ec242dcc445f55c12655;hpb=67a8251690a17f05630eb6f45a73db0f0e806c72;p=ffmpeg diff --git a/ffplay.c b/ffplay.c index f116db98c63..5cc4fa42582 100644 --- a/ffplay.c +++ b/ffplay.c @@ -38,12 +38,13 @@ #include "libavcodec/audioconvert.h" #include "libavutil/opt.h" #include "libavcodec/avfft.h" +#include "libswresample/swresample.h" #if CONFIG_AVFILTER # include "libavfilter/avcodec.h" # include "libavfilter/avfilter.h" # include "libavfilter/avfiltergraph.h" -# include "libavfilter/vsink_buffer.h" +# include "libavfilter/buffersink.h" #endif #include @@ -70,8 +71,6 @@ const int program_birth_year = 2003; /* no AV correction is done if too big error */ #define AV_NOSYNC_THRESHOLD 10.0 -#define FRAME_SKIP_FACTOR 0.05 - /* maximum audio speed change to get correct sync */ #define SAMPLE_CORRECTION_PERCENT_MAX 10 @@ -97,12 +96,13 @@ typedef struct PacketQueue { typedef struct VideoPicture { double pts; ///audio_st->codec->channels; + channels = s->audio_tgt_channels; nb_display_channels = channels; if (!s->paused) { int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq); @@ -738,7 +755,7 @@ static void video_audio_display(VideoState *s) the last buffer computation */ if (audio_callback_time) { time_diff = av_gettime() - audio_callback_time; - delay -= (time_diff * s->audio_st->codec->sample_rate) / 1000000; + delay -= (time_diff * s->audio_tgt_freq) / 1000000; } delay += 2*data_used; @@ -887,16 +904,17 @@ static void stream_close(VideoState *is) av_free(is); } -static void do_exit(void) +static void do_exit(VideoState *is) { - if (cur_stream) { - stream_close(cur_stream); - cur_stream = NULL; + if (is) { + stream_close(is); } + av_lockmgr_register(NULL); uninit_opts(); #if CONFIG_AVFILTER avfilter_uninit(); #endif + avformat_network_deinit(); if (show_status) printf("\n"); SDL_Quit(); @@ -933,16 +951,10 @@ static int video_open(VideoState *is){ if(screen && is->width == screen->w && screen->w == w && is->height== screen->h && screen->h == h) return 0; - -#ifndef __APPLE__ screen = SDL_SetVideoMode(w, h, 0, flags); -#else - /* setting bits_per_pixel = 0 or 32 causes blank video on OS X */ - screen = SDL_SetVideoMode(w, h, 24, flags); -#endif if (!screen) { fprintf(stderr, "SDL: could not set video mode - exiting\n"); - do_exit(); + do_exit(is); } if (!window_title) window_title = input_filename; @@ -958,7 +970,7 @@ static int video_open(VideoState *is){ static void video_display(VideoState *is) { if(!screen) - video_open(cur_stream); + video_open(is); if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO) video_audio_display(is); else if (is->video_st) @@ -1057,19 +1069,9 @@ static void stream_toggle_pause(VideoState *is) is->paused = !is->paused; } -static double compute_target_time(double frame_current_pts, VideoState *is) +static double compute_target_delay(double delay, VideoState *is) { - double delay, sync_threshold, diff; - - /* compute nominal delay */ - delay = frame_current_pts - is->frame_last_pts; - if (delay <= 0 || delay >= 10.0) { - /* if incorrect delay, use previous one */ - delay = is->frame_last_delay; - } else { - is->frame_last_delay = delay; - } - is->frame_last_pts = frame_current_pts; + double sync_threshold, diff; /* update delay to follow master synchronisation source */ if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) || @@ -1089,12 +1091,31 @@ static double compute_target_time(double frame_current_pts, VideoState *is) delay = 2 * delay; } } - is->frame_timer += delay; - av_dlog(NULL, "video: delay=%0.3f pts=%0.3f A-V=%f\n", - delay, frame_current_pts, -diff); + av_dlog(NULL, "video: delay=%0.3f A-V=%f\n", + delay, -diff); + + return delay; +} + +static void pictq_next_picture(VideoState *is) { + /* update queue size and signal for next picture */ + if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE) + is->pictq_rindex = 0; - return is->frame_timer; + SDL_LockMutex(is->pictq_mutex); + is->pictq_size--; + SDL_CondSignal(is->pictq_cond); + SDL_UnlockMutex(is->pictq_mutex); +} + +static void update_video_pts(VideoState *is, double pts, int64_t pos) { + double time = av_gettime() / 1000000.0; + /* update current video pts */ + is->video_current_pts = pts; + is->video_current_pts_drift = is->video_current_pts - time; + is->video_current_pos = pos; + is->frame_last_pts = pts; } /* called to display each frame */ @@ -1102,43 +1123,60 @@ static void video_refresh(void *opaque) { VideoState *is = opaque; VideoPicture *vp; + double time; SubPicture *sp, *sp2; if (is->video_st) { retry: if (is->pictq_size == 0) { + SDL_LockMutex(is->pictq_mutex); + if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) { + update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos); + is->frame_last_dropped_pts = AV_NOPTS_VALUE; + } + SDL_UnlockMutex(is->pictq_mutex); //nothing to do, no picture to display in the que } else { - double time= av_gettime()/1000000.0; - double next_target; + double last_duration, duration, delay; /* dequeue the picture */ vp = &is->pictq[is->pictq_rindex]; - if(time < vp->target_clock) + if (vp->skip) { + pictq_next_picture(is); + goto retry; + } + + /* compute nominal last_duration */ + last_duration = vp->pts - is->frame_last_pts; + if (last_duration > 0 && last_duration < 10.0) { + /* if duration of the last frame was sane, update last_duration in video state */ + is->frame_last_duration = last_duration; + } + delay = compute_target_delay(is->frame_last_duration, is); + + time= av_gettime()/1000000.0; + if(time < is->frame_timer + delay) return; - /* update current video pts */ - is->video_current_pts = vp->pts; - is->video_current_pts_drift = is->video_current_pts - time; - is->video_current_pos = vp->pos; - if(is->pictq_size > 1){ - VideoPicture *nextvp= &is->pictq[(is->pictq_rindex+1)%VIDEO_PICTURE_QUEUE_SIZE]; - assert(nextvp->target_clock >= vp->target_clock); - next_target= nextvp->target_clock; - }else{ - next_target= vp->target_clock + vp->duration; + + if (delay > 0) + is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay)); + + SDL_LockMutex(is->pictq_mutex); + update_video_pts(is, vp->pts, vp->pos); + SDL_UnlockMutex(is->pictq_mutex); + + if(is->pictq_size > 1) { + VideoPicture *nextvp= &is->pictq[(is->pictq_rindex+1)%VIDEO_PICTURE_QUEUE_SIZE]; + duration = nextvp->pts - vp->pts; // More accurate this way, 1/time_base is often not reflecting FPS + } else { + duration = vp->duration; } - if((framedrop>0 || (framedrop && is->audio_st)) && time > next_target){ - is->skip_frames *= 1.0 + FRAME_SKIP_FACTOR; - if(is->pictq_size > 1 || time > next_target + 0.5){ - /* update queue size and signal for next picture */ - if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE) - is->pictq_rindex = 0; - - SDL_LockMutex(is->pictq_mutex); - is->pictq_size--; - SDL_CondSignal(is->pictq_cond); - SDL_UnlockMutex(is->pictq_mutex); + + if((framedrop>0 || (framedrop && is->audio_st)) && time > is->frame_timer + duration){ + if(is->pictq_size > 1){ + is->frame_drops_late++; + pictq_next_picture(is); goto retry; } } @@ -1191,14 +1229,7 @@ retry: if (!display_disable) video_display(is); - /* update queue size and signal for next picture */ - if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE) - is->pictq_rindex = 0; - - SDL_LockMutex(is->pictq_mutex); - is->pictq_size--; - SDL_CondSignal(is->pictq_cond); - SDL_UnlockMutex(is->pictq_mutex); + pictq_next_picture(is); } } else if (is->audio_st) { /* draw the next audio frame */ @@ -1230,10 +1261,10 @@ retry: av_diff = 0; if (is->audio_st && is->video_st) av_diff = get_audio_clock(is) - get_video_clock(is); - printf("%7.2f A-V:%7.3f s:%3.1f aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r", + printf("%7.2f A-V:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r", get_master_clock(is), av_diff, - FFMAX(is->skip_frames-1, 0), + is->frame_drops_early + is->frame_drops_late, aqsize / 1024, vqsize / 1024, sqsize, @@ -1280,7 +1311,7 @@ static void alloc_picture(void *opaque) fprintf(stderr, "Error: the video system does not support an image\n" "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n" "to reduce the image size.\n", vp->width, vp->height ); - do_exit(); + do_exit(is); } SDL_LockMutex(is->pictq_mutex); @@ -1317,9 +1348,6 @@ static int queue_picture(VideoState *is, AVFrame *src_frame, double pts1, int64_ /* wait until we have space to put a new picture */ SDL_LockMutex(is->pictq_mutex); - if(is->pictq_size>=VIDEO_PICTURE_QUEUE_SIZE && !is->refresh) - is->skip_frames= FFMAX(1.0 - FRAME_SKIP_FACTOR, is->skip_frames * (1.0-FRAME_SKIP_FACTOR)); - while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE && !is->videoq.abort_request) { SDL_CondWait(is->pictq_cond, is->pictq_mutex); @@ -1334,7 +1362,7 @@ static int queue_picture(VideoState *is, AVFrame *src_frame, double pts1, int64_ vp->duration = frame_delay; /* alloc or resize hardware picture buffer */ - if (!vp->bmp || + if (!vp->bmp || vp->reallocate || #if CONFIG_AVFILTER vp->width != is->out_video_filter->inputs[0]->w || vp->height != is->out_video_filter->inputs[0]->h) { @@ -1344,7 +1372,8 @@ static int queue_picture(VideoState *is, AVFrame *src_frame, double pts1, int64_ #endif SDL_Event event; - vp->allocated = 0; + vp->allocated = 0; + vp->reallocate = 0; /* the allocation must be done in the main thread to avoid locking problems */ @@ -1357,6 +1386,12 @@ static int queue_picture(VideoState *is, AVFrame *src_frame, double pts1, int64_ while (!vp->allocated && !is->videoq.abort_request) { SDL_CondWait(is->pictq_cond, is->pictq_mutex); } + /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */ + if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) { + while (!vp->allocated) { + SDL_CondWait(is->pictq_cond, is->pictq_mutex); + } + } SDL_UnlockMutex(is->pictq_mutex); if (is->videoq.abort_request) @@ -1405,13 +1440,12 @@ static int queue_picture(VideoState *is, AVFrame *src_frame, double pts1, int64_ vp->pts = pts; vp->pos = pos; + vp->skip = 0; /* now we can update the picture count */ if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE) is->pictq_windex = 0; SDL_LockMutex(is->pictq_mutex); - vp->target_clock= compute_target_time(vp->pts, is); - is->pictq_size++; SDL_UnlockMutex(is->pictq_mutex); } @@ -1431,25 +1465,26 @@ static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacke SDL_LockMutex(is->pictq_mutex); //Make sure there are no long delay timers (ideally we should just flush the que but thats harder) for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) { - is->pictq[i].target_clock= 0; + is->pictq[i].skip = 1; } while (is->pictq_size && !is->videoq.abort_request) { SDL_CondWait(is->pictq_cond, is->pictq_mutex); } is->video_current_pos = -1; - SDL_UnlockMutex(is->pictq_mutex); - is->frame_last_pts = AV_NOPTS_VALUE; - is->frame_last_delay = 0; + is->frame_last_duration = 0; is->frame_timer = (double)av_gettime() / 1000000.0; - is->skip_frames = 1; - is->skip_frames_index = 0; + is->frame_last_dropped_pts = AV_NOPTS_VALUE; + SDL_UnlockMutex(is->pictq_mutex); + return 0; } avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt); if (got_picture) { + int ret = 1; + if (decoder_reorder_pts == -1) { *pts = frame->best_effort_timestamp; } else if (decoder_reorder_pts) { @@ -1462,12 +1497,29 @@ static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacke *pts = 0; } - is->skip_frames_index += 1; - if(is->skip_frames_index >= is->skip_frames){ - is->skip_frames_index -= FFMAX(is->skip_frames, 1.0); - return 1; + if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) || is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK) && + (framedrop>0 || (framedrop && is->audio_st))) { + SDL_LockMutex(is->pictq_mutex); + if (is->frame_last_pts != AV_NOPTS_VALUE && *pts) { + double clockdiff = get_video_clock(is) - get_master_clock(is); + double dpts = av_q2d(is->video_st->time_base) * *pts; + double ptsdiff = dpts - is->frame_last_pts; + if (fabs(clockdiff) < AV_NOSYNC_THRESHOLD && + ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD && + clockdiff + ptsdiff - is->frame_last_filter_delay < 0) { + is->frame_last_dropped_pos = pkt->pos; + is->frame_last_dropped_pts = dpts; + is->frame_drops_early++; + ret = 0; + } + } + SDL_UnlockMutex(is->pictq_mutex); } + if (ret) + is->frame_last_returned_time = av_gettime() / 1000000.0; + + return ret; } return 0; } @@ -1640,12 +1692,13 @@ static int input_query_formats(AVFilterContext *ctx) static int input_config_props(AVFilterLink *link) { FilterPriv *priv = link->src->priv; - AVCodecContext *c = priv->is->video_st->codec; + AVStream *s = priv->is->video_st; - link->w = c->width; - link->h = c->height; - link->sample_aspect_ratio = priv->is->video_st->sample_aspect_ratio; - link->time_base = priv->is->video_st->time_base; + link->w = s->codec->width; + link->h = s->codec->height; + link->sample_aspect_ratio = s->sample_aspect_ratio.num ? + s->sample_aspect_ratio : s->codec->sample_aspect_ratio; + link->time_base = s->time_base; return 0; } @@ -1674,6 +1727,7 @@ static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const c char sws_flags_str[128]; int ret; enum PixelFormat pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_NONE }; + AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc(); AVFilterContext *filt_src = NULL, *filt_out = NULL; snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%d", sws_flags); graph->scale_sws_opts = av_strdup(sws_flags_str); @@ -1681,8 +1735,16 @@ static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const c if ((ret = avfilter_graph_create_filter(&filt_src, &input_filter, "src", NULL, is, graph)) < 0) return ret; - if ((ret = avfilter_graph_create_filter(&filt_out, avfilter_get_by_name("buffersink"), "out", - NULL, pix_fmts, graph)) < 0) +#if FF_API_OLD_VSINK_API + ret = avfilter_graph_create_filter(&filt_out, avfilter_get_by_name("buffersink"), "out", + NULL, pix_fmts, graph); +#else + buffersink_params->pixel_fmts = pix_fmts; + ret = avfilter_graph_create_filter(&filt_out, avfilter_get_by_name("buffersink"), "out", + NULL, buffersink_params, graph); +#endif + av_freep(&buffersink_params); + if (ret < 0) return ret; if(vfilters) { @@ -1701,7 +1763,6 @@ static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const c if ((ret = avfilter_graph_parse(graph, vfilters, &inputs, &outputs, NULL)) < 0) return ret; - av_freep(&vfilters); } else { if ((ret = avfilter_link(filt_src, 0, filt_out, 0)) < 0) return ret; @@ -1748,8 +1809,8 @@ static int video_thread(void *arg) #if CONFIG_AVFILTER if ( last_w != is->video_st->codec->width || last_h != is->video_st->codec->height) { - av_dlog(NULL, "Changing size %dx%d -> %dx%d\n", last_w, last_h, - is->video_st->codec->width, is->video_st->codec->height); + av_log(NULL, AV_LOG_INFO, "Frame changed from size:%dx%d to size:%dx%d\n", + last_w, last_h, is->video_st->codec->width, is->video_st->codec->height); avfilter_graph_free(&graph); graph = avfilter_graph_alloc(); if ((ret = configure_video_filters(graph, is, vfilters)) < 0) @@ -1758,7 +1819,7 @@ static int video_thread(void *arg) last_w = is->video_st->codec->width; last_h = is->video_st->codec->height; } - ret = av_vsink_buffer_get_video_buffer_ref(filt_out, &picref, 0); + ret = av_buffersink_get_buffer_ref(filt_out, &picref, 0); if (picref) { avfilter_fill_frame_from_video_buffer_ref(frame, picref); pts_int = picref->pts; @@ -1782,6 +1843,10 @@ static int video_thread(void *arg) if (ret < 0) goto the_end; + is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time; + if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0) + is->frame_last_filter_delay = 0; + #if CONFIG_AVFILTER if (!picref) continue; @@ -1794,9 +1859,8 @@ static int video_thread(void *arg) if (ret < 0) goto the_end; - if (step) - if (cur_stream) - stream_toggle_pause(cur_stream); + if (is->step) + stream_toggle_pause(is); } the_end: #if CONFIG_AVFILTER @@ -1902,7 +1966,7 @@ static int synchronize_audio(VideoState *is, short *samples, int n, samples_size; double ref_clock; - n = 2 * is->audio_st->codec->channels; + n = av_get_bytes_per_sample(is->audio_tgt_fmt) * is->audio_tgt_channels; samples_size = samples_size1; /* if not master, then we try to remove or add samples to correct the clock */ @@ -1924,15 +1988,15 @@ static int synchronize_audio(VideoState *is, short *samples, avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef); if (fabs(avg_diff) >= is->audio_diff_threshold) { - wanted_size = samples_size + ((int)(diff * is->audio_st->codec->sample_rate) * n); + wanted_size = samples_size + ((int)(diff * is->audio_tgt_freq) * n); nb_samples = samples_size / n; min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n; max_size = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n; if (wanted_size < min_size) wanted_size = min_size; - else if (wanted_size > max_size) - wanted_size = max_size; + else if (wanted_size > FFMIN3(max_size, sizeof(is->audio_buf1), sizeof(is->audio_buf2))) + wanted_size = FFMIN3(max_size, sizeof(is->audio_buf1), sizeof(is->audio_buf2)); /* add or remove samples to correction the synchro */ if (wanted_size < samples_size) { @@ -1954,11 +2018,9 @@ static int synchronize_audio(VideoState *is, short *samples, samples_size = wanted_size; } } -#if 0 - printf("diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n", - diff, avg_diff, samples_size - samples_size1, - is->audio_clock, is->video_clock, is->audio_diff_threshold); -#endif + av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n", + diff, avg_diff, samples_size - samples_size1, + is->audio_clock, is->video_clock, is->audio_diff_threshold); } } else { /* too big difference : may be initial PTS errors, so @@ -1977,12 +2039,18 @@ static int audio_decode_frame(VideoState *is, double *pts_ptr) AVPacket *pkt_temp = &is->audio_pkt_temp; AVPacket *pkt = &is->audio_pkt; AVCodecContext *dec= is->audio_st->codec; - int n, len1, data_size; + int len1, len2, data_size, resampled_data_size; + int64_t dec_channel_layout; double pts; + int new_packet = 0; + int flush_complete = 0; for(;;) { /* NOTE: the audio packet can contain several frames */ - while (pkt_temp->size > 0) { + while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) { + if (flush_complete) + break; + new_packet = 0; data_size = sizeof(is->audio_buf1); len1 = avcodec_decode_audio3(dec, (int16_t *)is->audio_buf1, &data_size, @@ -1995,47 +2063,62 @@ static int audio_decode_frame(VideoState *is, double *pts_ptr) pkt_temp->data += len1; pkt_temp->size -= len1; - if (data_size <= 0) + + if (data_size <= 0) { + /* stop sending empty packets if the decoder is finished */ + if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY) + flush_complete = 1; continue; + } - if (dec->sample_fmt != is->audio_src_fmt) { - if (is->reformat_ctx) - av_audio_convert_free(is->reformat_ctx); - is->reformat_ctx= av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1, - dec->sample_fmt, 1, NULL, 0); - if (!is->reformat_ctx) { - fprintf(stderr, "Cannot convert %s sample format to %s sample format\n", + dec_channel_layout = (dec->channel_layout && dec->channels == av_get_channel_layout_nb_channels(dec->channel_layout)) ? dec->channel_layout : av_get_default_channel_layout(dec->channels); + + if (dec->sample_fmt != is->audio_src_fmt || dec_channel_layout != is->audio_src_channel_layout || dec->sample_rate != is->audio_src_freq) { + if (is->swr_ctx) + swr_free(&is->swr_ctx); + is->swr_ctx = swr_alloc2(NULL, is->audio_tgt_channel_layout, is->audio_tgt_fmt, is->audio_tgt_freq, + dec_channel_layout, dec->sample_fmt, dec->sample_rate, + NULL, 0, NULL); + if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) { + fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n", + dec->sample_rate, av_get_sample_fmt_name(dec->sample_fmt), - av_get_sample_fmt_name(AV_SAMPLE_FMT_S16)); - break; + dec->channels, + is->audio_tgt_freq, + av_get_sample_fmt_name(is->audio_tgt_fmt), + is->audio_tgt_channels); + break; } - is->audio_src_fmt= dec->sample_fmt; + is->audio_src_channel_layout = dec_channel_layout; + is->audio_src_channels = dec->channels; + is->audio_src_freq = dec->sample_rate; + is->audio_src_fmt = dec->sample_fmt; } - if (is->reformat_ctx) { - const void *ibuf[6]= {is->audio_buf1}; - void *obuf[6]= {is->audio_buf2}; - int istride[6]= {av_get_bytes_per_sample(dec->sample_fmt)}; - int ostride[6]= {2}; - int len= data_size/istride[0]; - if (av_audio_convert(is->reformat_ctx, obuf, ostride, ibuf, istride, len)<0) { - printf("av_audio_convert() failed\n"); + resampled_data_size = data_size; + if (is->swr_ctx) { + const uint8_t *in[] = {is->audio_buf1}; + uint8_t *out[] = {is->audio_buf2}; + len2 = swr_convert(is->swr_ctx, out, sizeof(is->audio_buf2) / is->audio_tgt_channels / av_get_bytes_per_sample(is->audio_tgt_fmt), + in, data_size / dec->channels / av_get_bytes_per_sample(dec->sample_fmt)); + if (len2 < 0) { + fprintf(stderr, "audio_resample() failed\n"); break; } - is->audio_buf= is->audio_buf2; - /* FIXME: existing code assume that data_size equals framesize*channels*2 - remove this legacy cruft */ - data_size= len*2; - }else{ + if (len2 == sizeof(is->audio_buf2) / is->audio_tgt_channels / av_get_bytes_per_sample(is->audio_tgt_fmt)) { + fprintf(stderr, "warning: audio buffer is probably too small\n"); + swr_init(is->swr_ctx); + } + is->audio_buf = is->audio_buf2; + resampled_data_size = len2 * is->audio_tgt_channels * av_get_bytes_per_sample(is->audio_tgt_fmt); + } else { is->audio_buf= is->audio_buf1; } /* if no pts, then compute it */ pts = is->audio_clock; *pts_ptr = pts; - n = 2 * dec->channels; - is->audio_clock += (double)data_size / - (double)(n * dec->sample_rate); + is->audio_clock += (double)data_size / (dec->channels * dec->sample_rate * av_get_bytes_per_sample(dec->sample_fmt)); #ifdef DEBUG { static double last_clock; @@ -2045,7 +2128,7 @@ static int audio_decode_frame(VideoState *is, double *pts_ptr) last_clock = is->audio_clock; } #endif - return data_size; + return resampled_data_size; } /* free the current packet */ @@ -2057,12 +2140,11 @@ static int audio_decode_frame(VideoState *is, double *pts_ptr) } /* read next packet */ - if (packet_queue_get(&is->audioq, pkt, 1) < 0) + if ((new_packet = packet_queue_get(&is->audioq, pkt, 1)) < 0) return -1; - if(pkt->data == flush_pkt.data){ + + if (pkt->data == flush_pkt.data) avcodec_flush_buffers(dec); - continue; - } pkt_temp->data = pkt->data; pkt_temp->size = pkt->size; @@ -2090,7 +2172,7 @@ static void sdl_audio_callback(void *opaque, Uint8 *stream, int len) if (audio_size < 0) { /* if error, just output silence */ is->audio_buf = is->audio_buf1; - is->audio_buf_size = 1024; + is->audio_buf_size = 256 * is->audio_tgt_channels * av_get_bytes_per_sample(is->audio_tgt_fmt); memset(is->audio_buf, 0, is->audio_buf_size); } else { if (is->show_mode != SHOW_MODE_VIDEO) @@ -2109,8 +2191,7 @@ static void sdl_audio_callback(void *opaque, Uint8 *stream, int len) stream += len1; is->audio_buf_index += len1; } - bytes_per_sec = is->audio_st->codec->sample_rate * - 2 * is->audio_st->codec->channels; + bytes_per_sec = is->audio_tgt_freq * is->audio_tgt_channels * av_get_bytes_per_sample(is->audio_tgt_fmt); is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index; /* Let's assume the audio driver that is used by SDL has two periods. */ is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec; @@ -2126,6 +2207,7 @@ static int stream_component_open(VideoState *is, int stream_index) SDL_AudioSpec wanted_spec, spec; AVDictionary *opts; AVDictionaryEntry *t = NULL; + int64_t wanted_channel_layout = 0; if (stream_index < 0 || stream_index >= ic->nb_streams) return -1; @@ -2133,22 +2215,23 @@ static int stream_component_open(VideoState *is, int stream_index) opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index]); - /* prepare audio output */ - if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) { - if (avctx->channels > 0) { - avctx->request_channels = FFMIN(2, avctx->channels); - } else { - avctx->request_channels = 2; - } - } - codec = avcodec_find_decoder(avctx->codec_id); + switch(avctx->codec_type){ + case AVMEDIA_TYPE_AUDIO : if(audio_codec_name ) codec= avcodec_find_decoder_by_name( audio_codec_name); break; + case AVMEDIA_TYPE_SUBTITLE: if(subtitle_codec_name) codec= avcodec_find_decoder_by_name(subtitle_codec_name); break; + case AVMEDIA_TYPE_VIDEO : if(video_codec_name ) codec= avcodec_find_decoder_by_name( video_codec_name); break; + } if (!codec) return -1; avctx->workaround_bugs = workaround_bugs; avctx->lowres = lowres; - if(lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE; + if(avctx->lowres > codec->max_lowres){ + av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n", + codec->max_lowres); + avctx->lowres= codec->max_lowres; + } + if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE; avctx->idct_algo= idct; if(fast) avctx->flags2 |= CODEC_FLAG2_FAST; avctx->skip_frame= skip_frame; @@ -2156,11 +2239,21 @@ static int stream_component_open(VideoState *is, int stream_index) avctx->skip_loop_filter= skip_loop_filter; avctx->error_recognition= error_recognition; avctx->error_concealment= error_concealment; - avctx->thread_count= thread_count; if(codec->capabilities & CODEC_CAP_DR1) avctx->flags |= CODEC_FLAG_EMU_EDGE; + if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) { + wanted_channel_layout = (avctx->channel_layout && avctx->channels == av_get_channel_layout_nb_channels(avctx->channels)) ? avctx->channel_layout : av_get_default_channel_layout(avctx->channels); + wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX; + wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout); + wanted_spec.freq = avctx->sample_rate; + if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) { + fprintf(stderr, "Invalid sample rate or channel count!\n"); + return -1; + } + } + if (!codec || avcodec_open2(avctx, codec, &opts) < 0) return -1; @@ -2171,13 +2264,7 @@ static int stream_component_open(VideoState *is, int stream_index) /* prepare audio output */ if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) { - if(avctx->sample_rate <= 0 || avctx->channels <= 0){ - fprintf(stderr, "Invalid sample rate or channel count\n"); - return -1; - } - wanted_spec.freq = avctx->sample_rate; wanted_spec.format = AUDIO_S16SYS; - wanted_spec.channels = avctx->channels; wanted_spec.silence = 0; wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE; wanted_spec.callback = sdl_audio_callback; @@ -2187,7 +2274,21 @@ static int stream_component_open(VideoState *is, int stream_index) return -1; } is->audio_hw_buf_size = spec.size; - is->audio_src_fmt= AV_SAMPLE_FMT_S16; + if (spec.format != AUDIO_S16SYS) { + fprintf(stderr, "SDL advised audio format %d is not supported!\n", spec.format); + return -1; + } + if (spec.channels != wanted_spec.channels) { + wanted_channel_layout = av_get_default_channel_layout(spec.channels); + if (!wanted_channel_layout) { + fprintf(stderr, "SDL advised channel count %d is not supported!\n", spec.channels); + return -1; + } + } + is->audio_src_fmt = is->audio_tgt_fmt = AV_SAMPLE_FMT_S16; + is->audio_src_freq = is->audio_tgt_freq = spec.freq; + is->audio_src_channel_layout = is->audio_tgt_channel_layout = wanted_channel_layout; + is->audio_src_channels = is->audio_tgt_channels = spec.channels; } ic->streams[stream_index]->discard = AVDISCARD_DEFAULT; @@ -2203,7 +2304,7 @@ static int stream_component_open(VideoState *is, int stream_index) is->audio_diff_avg_count = 0; /* since we do not have a precise anough audio fifo fullness, we correct audio sync only if larger than this threshold */ - is->audio_diff_threshold = 2.0 * SDL_AUDIO_BUFFER_SIZE / avctx->sample_rate; + is->audio_diff_threshold = 2.0 * SDL_AUDIO_BUFFER_SIZE / wanted_spec.freq; memset(&is->audio_pkt, 0, sizeof(is->audio_pkt)); packet_queue_init(&is->audioq); @@ -2245,9 +2346,16 @@ static void stream_component_close(VideoState *is, int stream_index) SDL_CloseAudio(); packet_queue_end(&is->audioq); - if (is->reformat_ctx) - av_audio_convert_free(is->reformat_ctx); - is->reformat_ctx = NULL; + if (is->swr_ctx) + swr_free(&is->swr_ctx); + av_free_packet(&is->audio_pkt); + + if (is->rdft) { + av_rdft_end(is->rdft); + av_freep(&is->rdft_data); + is->rdft = NULL; + is->rdft_bits = 0; + } break; case AVMEDIA_TYPE_VIDEO: packet_queue_abort(&is->videoq); @@ -2348,6 +2456,8 @@ static int read_thread(void *arg) if(genpts) ic->flags |= AVFMT_FLAG_GENPTS; + av_dict_set(&codec_opts, "request_channels", "2", 0); + opts = setup_find_stream_info_opts(ic, codec_opts); orig_nb_streams = ic->nb_streams; @@ -2441,8 +2551,10 @@ static int read_thread(void *arg) else av_read_play(ic); } -#if CONFIG_RTSP_DEMUXER - if (is->paused && !strcmp(ic->iformat->name, "rtsp")) { +#if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL + if (is->paused && + (!strcmp(ic->iformat->name, "rtsp") || + (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) { /* wait 10 ms to avoid trying to get another packet */ /* XXX: horrible */ SDL_Delay(10); @@ -2494,10 +2606,18 @@ static int read_thread(void *arg) pkt->stream_index= is->video_stream; packet_queue_put(&is->videoq, pkt); } + if (is->audio_stream >= 0 && + is->audio_st->codec->codec->capabilities & CODEC_CAP_DELAY) { + av_init_packet(pkt); + pkt->data = NULL; + pkt->size = 0; + pkt->stream_index = is->audio_stream; + packet_queue_put(&is->audioq, pkt); + } SDL_Delay(10); if(is->audioq.size + is->videoq.size + is->subtitleq.size ==0){ if(loop!=1 && (!loop || --loop)){ - stream_seek(cur_stream, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0); + stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0); }else if(autoexit){ ret=AVERROR_EOF; goto fail; @@ -2642,43 +2762,44 @@ static void stream_cycle_channel(VideoState *is, int codec_type) } -static void toggle_full_screen(void) +static void toggle_full_screen(VideoState *is) { is_full_screen = !is_full_screen; - video_open(cur_stream); +#if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14) + /* OSX needs to reallocate the SDL overlays */ + for (int i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) { + is->pictq[i].reallocate = 1; + } +#endif + video_open(is); } -static void toggle_pause(void) +static void toggle_pause(VideoState *is) { - if (cur_stream) - stream_toggle_pause(cur_stream); - step = 0; + stream_toggle_pause(is); + is->step = 0; } -static void step_to_next_frame(void) +static void step_to_next_frame(VideoState *is) { - if (cur_stream) { - /* if the stream is paused unpause it, then step */ - if (cur_stream->paused) - stream_toggle_pause(cur_stream); - } - step = 1; + /* if the stream is paused unpause it, then step */ + if (is->paused) + stream_toggle_pause(is); + is->step = 1; } -static void toggle_audio_display(void) +static void toggle_audio_display(VideoState *is) { - if (cur_stream) { - int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00); - cur_stream->show_mode = (cur_stream->show_mode + 1) % SHOW_MODE_NB; - fill_rectangle(screen, - cur_stream->xleft, cur_stream->ytop, cur_stream->width, cur_stream->height, - bgcolor); - SDL_UpdateRect(screen, cur_stream->xleft, cur_stream->ytop, cur_stream->width, cur_stream->height); - } + int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00); + is->show_mode = (is->show_mode + 1) % SHOW_MODE_NB; + fill_rectangle(screen, + is->xleft, is->ytop, is->width, is->height, + bgcolor); + SDL_UpdateRect(screen, is->xleft, is->ytop, is->width, is->height); } /* handle an event sent by the GUI */ -static void event_loop(void) +static void event_loop(VideoState *cur_stream) { SDL_Event event; double incr, pos, frac; @@ -2689,38 +2810,35 @@ static void event_loop(void) switch(event.type) { case SDL_KEYDOWN: if (exit_on_keydown) { - do_exit(); + do_exit(cur_stream); break; } switch(event.key.keysym.sym) { case SDLK_ESCAPE: case SDLK_q: - do_exit(); + do_exit(cur_stream); break; case SDLK_f: - toggle_full_screen(); + toggle_full_screen(cur_stream); break; case SDLK_p: case SDLK_SPACE: - toggle_pause(); + toggle_pause(cur_stream); break; case SDLK_s: //S: Step to next frame - step_to_next_frame(); + step_to_next_frame(cur_stream); break; case SDLK_a: - if (cur_stream) - stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO); + stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO); break; case SDLK_v: - if (cur_stream) - stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO); + stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO); break; case SDLK_t: - if (cur_stream) - stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE); + stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE); break; case SDLK_w: - toggle_audio_display(); + toggle_audio_display(cur_stream); break; case SDLK_LEFT: incr = -10.0; @@ -2734,25 +2852,23 @@ static void event_loop(void) case SDLK_DOWN: incr = -60.0; do_seek: - if (cur_stream) { - if (seek_by_bytes) { - if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos>=0){ - pos= cur_stream->video_current_pos; - }else if(cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos>=0){ - pos= cur_stream->audio_pkt.pos; - }else - pos = avio_tell(cur_stream->ic->pb); - if (cur_stream->ic->bit_rate) - incr *= cur_stream->ic->bit_rate / 8.0; - else - incr *= 180000.0; - pos += incr; - stream_seek(cur_stream, pos, incr, 1); - } else { - pos = get_master_clock(cur_stream); - pos += incr; - stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0); - } + if (seek_by_bytes) { + if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos>=0){ + pos= cur_stream->video_current_pos; + }else if(cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos>=0){ + pos= cur_stream->audio_pkt.pos; + }else + pos = avio_tell(cur_stream->ic->pb); + if (cur_stream->ic->bit_rate) + incr *= cur_stream->ic->bit_rate / 8.0; + else + incr *= 180000.0; + pos += incr; + stream_seek(cur_stream, pos, incr, 1); + } else { + pos = get_master_clock(cur_stream); + pos += incr; + stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0); } break; default: @@ -2761,7 +2877,7 @@ static void event_loop(void) break; case SDL_MOUSEBUTTONDOWN: if (exit_on_mousedown) { - do_exit(); + do_exit(cur_stream); break; } case SDL_MOUSEMOTION: @@ -2772,43 +2888,39 @@ static void event_loop(void) break; x= event.motion.x; } - if (cur_stream) { - if(seek_by_bytes || cur_stream->ic->duration<=0){ - uint64_t size= avio_size(cur_stream->ic->pb); - stream_seek(cur_stream, size*x/cur_stream->width, 0, 1); - }else{ - int64_t ts; - int ns, hh, mm, ss; - int tns, thh, tmm, tss; - tns = cur_stream->ic->duration/1000000LL; - thh = tns/3600; - tmm = (tns%3600)/60; - tss = (tns%60); - frac = x/cur_stream->width; - ns = frac*tns; - hh = ns/3600; - mm = (ns%3600)/60; - ss = (ns%60); - fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100, - hh, mm, ss, thh, tmm, tss); - ts = frac*cur_stream->ic->duration; - if (cur_stream->ic->start_time != AV_NOPTS_VALUE) - ts += cur_stream->ic->start_time; - stream_seek(cur_stream, ts, 0, 0); - } + if(seek_by_bytes || cur_stream->ic->duration<=0){ + uint64_t size= avio_size(cur_stream->ic->pb); + stream_seek(cur_stream, size*x/cur_stream->width, 0, 1); + }else{ + int64_t ts; + int ns, hh, mm, ss; + int tns, thh, tmm, tss; + tns = cur_stream->ic->duration/1000000LL; + thh = tns/3600; + tmm = (tns%3600)/60; + tss = (tns%60); + frac = x/cur_stream->width; + ns = frac*tns; + hh = ns/3600; + mm = (ns%3600)/60; + ss = (ns%60); + fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100, + hh, mm, ss, thh, tmm, tss); + ts = frac*cur_stream->ic->duration; + if (cur_stream->ic->start_time != AV_NOPTS_VALUE) + ts += cur_stream->ic->start_time; + stream_seek(cur_stream, ts, 0, 0); } break; case SDL_VIDEORESIZE: - if (cur_stream) { - screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0, - SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL); - screen_width = cur_stream->width = event.resize.w; - screen_height= cur_stream->height= event.resize.h; - } + screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0, + SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL); + screen_width = cur_stream->width = event.resize.w; + screen_height= cur_stream->height= event.resize.h; break; case SDL_QUIT: case FF_QUIT_EVENT: - do_exit(); + do_exit(cur_stream); break; case FF_ALLOC_EVENT: video_open(event.user.data1); @@ -2885,15 +2997,6 @@ static int opt_duration(const char *opt, const char *arg) return 0; } -static int opt_thread_count(const char *opt, const char *arg) -{ - thread_count= parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX); -#if !HAVE_THREADS - fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n"); -#endif - return 0; -} - static int opt_show_mode(const char *opt, const char *arg) { show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO : @@ -2903,19 +3006,30 @@ static int opt_show_mode(const char *opt, const char *arg) return 0; } -static int opt_input_file(const char *opt, const char *filename) +static void opt_input_file(void *optctx, const char *filename) { if (input_filename) { fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n", filename, input_filename); - exit(1); + exit_program(1); } if (!strcmp(filename, "-")) filename = "pipe:"; input_filename = filename; +} + +static int opt_codec(void *o, const char *opt, const char *arg) +{ + switch(opt[strlen(opt)-1]){ + case 'a' : audio_codec_name = arg; break; + case 's' : subtitle_codec_name = arg; break; + case 'v' : video_codec_name = arg; break; + } return 0; } +static int dummy; + static const OptionDef options[] = { #include "cmdutils_common_opts.h" { "x", HAS_ARG, {(void*)opt_width}, "force displayed width", "width" }, @@ -2946,7 +3060,6 @@ static const OptionDef options[] = { { "er", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&error_recognition}, "set error detection threshold (0-4)", "threshold" }, { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&error_concealment}, "set error concealment options", "bit_mask" }, { "sync", HAS_ARG | OPT_EXPERT, {(void*)opt_sync}, "set audio-video sync. type (type=audio/video/ext)", "type" }, - { "threads", HAS_ARG | OPT_EXPERT, {(void*)opt_thread_count}, "thread count", "count" }, { "autoexit", OPT_BOOL | OPT_EXPERT, {(void*)&autoexit}, "exit at the end", "" }, { "exitonkeydown", OPT_BOOL | OPT_EXPERT, {(void*)&exit_on_keydown}, "exit on key down", "" }, { "exitonmousedown", OPT_BOOL | OPT_EXPERT, {(void*)&exit_on_mousedown}, "exit on mouse down", "" }, @@ -2959,7 +3072,8 @@ static const OptionDef options[] = { { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, {(void*)&rdftspeed}, "rdft speed", "msecs" }, { "showmode", HAS_ARG, {(void*)opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" }, { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" }, - { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"}, + { "i", OPT_BOOL, {(void *)&dummy}, "read specified file", "input_file"}, + { "codec", HAS_ARG | OPT_FUNC2, {(void*)opt_codec}, "force decoder", "decoder" }, { NULL, }, }; @@ -2979,15 +3093,10 @@ static int opt_help(const char *opt, const char *arg) show_help_options(options, "\nAdvanced options:\n", OPT_EXPERT, OPT_EXPERT); printf("\n"); - av_opt_show2(avcodec_opts[0], NULL, - AV_OPT_FLAG_DECODING_PARAM, 0); - printf("\n"); - av_opt_show2(avformat_opts, NULL, - AV_OPT_FLAG_DECODING_PARAM, 0); + show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM); + show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM); #if !CONFIG_AVFILTER - printf("\n"); - av_opt_show2(sws_opts, NULL, - AV_OPT_FLAG_ENCODING_PARAM, 0); + show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM); #endif printf("\nWhile playing:\n" "q, ESC quit\n" @@ -3005,12 +3114,33 @@ static int opt_help(const char *opt, const char *arg) return 0; } +static int lockmgr(void **mtx, enum AVLockOp op) +{ + switch(op) { + case AV_LOCK_CREATE: + *mtx = SDL_CreateMutex(); + if(!*mtx) + return 1; + return 0; + case AV_LOCK_OBTAIN: + return !!SDL_LockMutex(*mtx); + case AV_LOCK_RELEASE: + return !!SDL_UnlockMutex(*mtx); + case AV_LOCK_DESTROY: + SDL_DestroyMutex(*mtx); + return 0; + } + return 1; +} + /* Called from the main */ int main(int argc, char **argv) { int flags; + VideoState *is; av_log_set_flags(AV_LOG_SKIP_REPEATED); + parse_loglevel(argc, argv, options); /* register all codecs, demux and protocols */ avcodec_register_all(); @@ -3021,12 +3151,13 @@ int main(int argc, char **argv) avfilter_register_all(); #endif av_register_all(); + avformat_network_init(); init_opts(); show_banner(); - parse_options(argc, argv, options, opt_input_file); + parse_options(NULL, argc, argv, options, opt_input_file); if (!input_filename) { show_usage(); @@ -3062,12 +3193,21 @@ int main(int argc, char **argv) SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE); SDL_EventState(SDL_USEREVENT, SDL_IGNORE); + if (av_lockmgr_register(lockmgr)) { + fprintf(stderr, "Could not initialize lock manager!\n"); + do_exit(NULL); + } + av_init_packet(&flush_pkt); flush_pkt.data= "FLUSH"; - cur_stream = stream_open(input_filename, file_iformat); + is = stream_open(input_filename, file_iformat); + if (!is) { + fprintf(stderr, "Failed to initialize VideoState!\n"); + do_exit(NULL); + } - event_loop(); + event_loop(is); /* never returns */