X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=ffplay.c;h=f56c2d70ab85860b1c861c4778560ffcfd9cb389;hb=c5db8b4d09762f5228eaf3c3a0017657ed27d866;hp=c31bcfb607535a68a752c0cc466a26dcec55a1fb;hpb=edae3dbf1d9d7d76af961914c07998eb7bbb482b;p=ffmpeg diff --git a/ffplay.c b/ffplay.c index c31bcfb6075..f56c2d70ab8 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 @@ -152,9 +153,9 @@ typedef struct VideoState { PacketQueue audioq; int audio_hw_buf_size; /* samples output by the codec. we reserve more space for avsync - compensation */ - DECLARE_ALIGNED(16,uint8_t,audio_buf1)[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2]; - DECLARE_ALIGNED(16,uint8_t,audio_buf2)[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2]; + compensation, resampling and format conversion */ + DECLARE_ALIGNED(16,uint8_t,audio_buf1)[AVCODEC_MAX_AUDIO_FRAME_SIZE * 4]; + DECLARE_ALIGNED(16,uint8_t,audio_buf2)[AVCODEC_MAX_AUDIO_FRAME_SIZE * 4]; uint8_t *audio_buf; unsigned int audio_buf_size; /* in bytes */ int audio_buf_index; /* in bytes */ @@ -162,7 +163,14 @@ typedef struct VideoState { AVPacket audio_pkt_temp; AVPacket audio_pkt; enum AVSampleFormat audio_src_fmt; - AVAudioConvert *reformat_ctx; + enum AVSampleFormat audio_tgt_fmt; + int audio_src_channels; + int audio_tgt_channels; + int64_t audio_src_channel_layout; + int64_t audio_tgt_channel_layout; + int audio_src_freq; + int audio_tgt_freq; + struct SwrContext *swr_ctx; double audio_current_pts; double audio_current_pts_drift; @@ -207,6 +215,7 @@ typedef struct VideoState { char filename[1024]; int width, height, xleft, ytop; + int step; #if CONFIG_AVFILTER AVFilterContext *out_video_filter; ///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 +752,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,12 +901,12 @@ 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(); @@ -942,7 +956,7 @@ static int video_open(VideoState *is){ #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 +972,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) @@ -1130,7 +1144,7 @@ retry: } 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){ + if(is->pictq_size > 1){ /* update queue size and signal for next picture */ if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE) is->pictq_rindex = 0; @@ -1280,7 +1294,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); @@ -1357,6 +1371,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) @@ -1675,6 +1695,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); @@ -1682,8 +1703,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) { @@ -1758,7 +1787,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; @@ -1794,9 +1823,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 +1930,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 +1952,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 +1982,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 +2003,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 +2027,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, + 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 +2092,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 +2104,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 +2136,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 +2155,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 +2171,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,16 +2179,12 @@ 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; @@ -2156,11 +2198,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 +2223,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 +2233,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 +2263,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 +2305,8 @@ 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); break; case AVMEDIA_TYPE_VIDEO: packet_queue_abort(&is->videoq); @@ -2348,6 +2407,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; @@ -2494,10 +2555,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 +2711,38 @@ 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); + 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 +2753,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 +2795,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 +2820,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 +2831,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 +2940,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 +2949,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 +3003,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 +3015,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, }, }; @@ -2972,6 +3029,7 @@ static void show_usage(void) static int opt_help(const char *opt, const char *arg) { + const AVClass *class; av_log_set_callback(log_callback_help); show_usage(); show_help_options(options, "Main options:\n", @@ -2979,14 +3037,17 @@ 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, + class = avcodec_get_class(); + av_opt_show2(&class, NULL, AV_OPT_FLAG_DECODING_PARAM, 0); printf("\n"); - av_opt_show2(avformat_opts, NULL, + class = avformat_get_class(); + av_opt_show2(&class, NULL, AV_OPT_FLAG_DECODING_PARAM, 0); #if !CONFIG_AVFILTER printf("\n"); - av_opt_show2(sws_opts, NULL, + class = sws_get_class(); + av_opt_show2(&class, NULL, AV_OPT_FLAG_ENCODING_PARAM, 0); #endif printf("\nWhile playing:\n" @@ -3005,12 +3066,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(); @@ -3026,7 +3108,7 @@ int main(int argc, char **argv) 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 +3144,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 */