X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=ffplay.c;h=c998b5d44678769f4874477aa9d44183909028e4;hb=67341f6a7742275017e6eee18fd25c1ec7770f79;hp=27dda94e20b643b720e00da0ce12077edc20d9bf;hpb=094a4968188b0129d674c1369bad45a86cd0d46d;p=ffmpeg diff --git a/ffplay.c b/ffplay.c index 27dda94e20b..c998b5d4467 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); @@ -737,7 +749,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; @@ -932,13 +944,7 @@ 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(is); @@ -1056,19 +1062,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) || @@ -1088,12 +1084,22 @@ 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); } /* called to display each frame */ @@ -1110,34 +1116,45 @@ retry: //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); + + if(time < is->frame_timer + delay) return; + + is->frame_last_pts = vp->pts; + if (delay > 0) + is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay)); + /* 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(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){ + pictq_next_picture(is); goto retry; } } @@ -1190,14 +1207,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 */ @@ -1229,10 +1239,9 @@ 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 aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r", get_master_clock(is), av_diff, - FFMAX(is->skip_frames-1, 0), aqsize / 1024, vqsize / 1024, sqsize, @@ -1316,9 +1325,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); @@ -1333,7 +1339,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) { @@ -1343,7 +1349,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 */ @@ -1410,13 +1417,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); } @@ -1436,7 +1442,7 @@ 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); @@ -1445,10 +1451,8 @@ static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacke 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; return 0; } @@ -1467,11 +1471,7 @@ 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; - } + return 1; } return 0; @@ -1680,6 +1680,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); @@ -1687,8 +1688,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) { @@ -1763,7 +1772,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; @@ -1906,7 +1915,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 */ @@ -1928,15 +1937,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) { @@ -1958,11 +1967,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 @@ -1981,12 +1988,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, @@ -1999,47 +2012,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; @@ -2049,7 +2077,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 */ @@ -2061,12 +2089,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; @@ -2094,7 +2121,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) @@ -2113,8 +2140,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; @@ -2130,6 +2156,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; @@ -2137,16 +2164,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; @@ -2160,11 +2183,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; @@ -2175,13 +2208,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; @@ -2191,7 +2218,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; @@ -2207,7 +2248,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); @@ -2249,9 +2290,14 @@ 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); + } break; case AVMEDIA_TYPE_VIDEO: packet_queue_abort(&is->videoq); @@ -2352,6 +2398,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; @@ -2498,6 +2546,14 @@ 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)){ @@ -2649,6 +2705,12 @@ static void stream_cycle_channel(VideoState *is, int codec_type) static void toggle_full_screen(VideoState *is) { is_full_screen = !is_full_screen; +#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); } @@ -2875,15 +2937,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 : @@ -2893,19 +2946,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" }, @@ -2936,7 +3000,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", "" }, @@ -2949,7 +3012,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, }, }; @@ -2969,15 +3033,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" @@ -3021,6 +3080,7 @@ int main(int argc, char **argv) VideoState *is; av_log_set_flags(AV_LOG_SKIP_REPEATED); + parse_loglevel(argc, argv, options); /* register all codecs, demux and protocols */ avcodec_register_all(); @@ -3036,7 +3096,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();