Skip to content
Snippets Groups Projects
ffplay.c 108 KiB
Newer Older
  • Learn to ignore specific revisions
  •                 is->frame->channel_layout : av_get_default_channel_layout(is->frame->channels);
    
                wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
    
                if (is->frame->format        != is->audio_src.fmt            ||
                    dec_channel_layout       != is->audio_src.channel_layout ||
                    is->frame->sample_rate   != is->audio_src.freq           ||
                    (wanted_nb_samples       != is->frame->nb_samples && !is->swr_ctx)) {
    
                    swr_free(&is->swr_ctx);
    
                    is->swr_ctx = swr_alloc_set_opts(NULL,
    
                                                     is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
    
                                                     dec_channel_layout,           is->frame->format, is->frame->sample_rate,
    
                    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",
    
                            is->frame->sample_rate,   av_get_sample_fmt_name(is->frame->format), (int)is->frame->channels,
    
                            is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
    
                    is->audio_src.channel_layout = dec_channel_layout;
    
                    is->audio_src.channels = is->frame->channels;
                    is->audio_src.freq = is->frame->sample_rate;
                    is->audio_src.fmt = is->frame->format;
    
                    const uint8_t **in = (const uint8_t **)is->frame->extended_data;
    
                    uint8_t *out[] = {is->audio_buf2};
    
                    int out_count = sizeof(is->audio_buf2) / is->audio_tgt.channels / av_get_bytes_per_sample(is->audio_tgt.fmt);
    
                    if (wanted_nb_samples != is->frame->nb_samples) {
    
                        if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
                                                    wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
    
                            fprintf(stderr, "swr_set_compensation() failed\n");
                            break;
                        }
                    }
    
                    len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
    
                        fprintf(stderr, "swr_convert() failed\n");
    
                    if (len2 == out_count) {
    
                        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);
    
                    is->audio_buf = is->frame->data[0];
    
                    resampled_data_size = data_size;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                /* if no pts, then compute it */
                pts = is->audio_clock;
                *pts_ptr = pts;
    
                is->audio_clock += (double)data_size /
    
                    (is->frame->channels * is->frame->sample_rate * av_get_bytes_per_sample(is->frame->format));
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                {
                    static double last_clock;
                    printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
                           is->audio_clock - last_clock,
                           is->audio_clock, pts);
                    last_clock = is->audio_clock;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #endif
    
                return resampled_data_size;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* free the current packet */
            if (pkt->data)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                av_free_packet(pkt);
    
            memset(pkt_temp, 0, sizeof(*pkt_temp));
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (is->paused || is->audioq.abort_request) {
                return -1;
            }
    
            if (is->audioq.nb_packets == 0)
                SDL_CondSignal(is->continue_read_thread);
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* read next packet */
    
            if ((new_packet = packet_queue_get(&is->audioq, pkt, 1)) < 0)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                return -1;
    
            if (pkt->data == flush_pkt.data) {
    
            *pkt_temp = *pkt;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* if update the audio clock with the pts */
            if (pkt->pts != AV_NOPTS_VALUE) {
    
                is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    }
    
    /* prepare a new audio buffer */
    
    static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        VideoState *is = opaque;
        int audio_size, len1;
    
        int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        double pts;
    
        audio_callback_time = av_gettime();
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        while (len > 0) {
            if (is->audio_buf_index >= is->audio_buf_size) {
    
               audio_size = audio_decode_frame(is, &pts);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
               if (audio_size < 0) {
                    /* if error, just output silence */
    
                   is->audio_buf      = is->silence_buf;
    
                   is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
               } else {
    
                   if (is->show_mode != SHOW_MODE_VIDEO)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                       update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
                   is->audio_buf_size = audio_size;
               }
               is->audio_buf_index = 0;
            }
            len1 = is->audio_buf_size - is->audio_buf_index;
            if (len1 > len)
                len1 = len;
            memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
            len -= len1;
            stream += len1;
            is->audio_buf_index += len1;
        }
    
        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;
        is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
    
        check_external_clock_sync(is, is->audio_current_pts);
    
    static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
    
    {
        SDL_AudioSpec wanted_spec, spec;
        const char *env;
    
        const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
    
    
        env = SDL_getenv("SDL_AUDIO_CHANNELS");
    
            wanted_nb_channels = atoi(env);
    
            wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
        }
        if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
            wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
    
            wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
        }
        wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
    
        wanted_spec.freq = wanted_sample_rate;
    
        if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
            fprintf(stderr, "Invalid sample rate or channel count!\n");
            return -1;
        }
        wanted_spec.format = AUDIO_S16SYS;
        wanted_spec.silence = 0;
        wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
        wanted_spec.callback = sdl_audio_callback;
    
        wanted_spec.userdata = opaque;
    
        while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
            fprintf(stderr, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError());
            wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
            if (!wanted_spec.channels) {
                fprintf(stderr, "No more channel combinations to try, audio open failed\n");
                return -1;
            }
            wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
    
        }
        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;
            }
        }
    
    
        audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
        audio_hw_params->freq = spec.freq;
        audio_hw_params->channel_layout = wanted_channel_layout;
        audio_hw_params->channels =  spec.channels;
    
        return spec.size;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    /* open a given stream. Return 0 if OK */
    static int stream_component_open(VideoState *is, int stream_index)
    {
        AVFormatContext *ic = is->ic;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        AVCodec *codec;
    
        AVDictionary *opts;
        AVDictionaryEntry *t = NULL;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        if (stream_index < 0 || stream_index >= ic->nb_streams)
            return -1;
    
        avctx = ic->streams[stream_index]->codec;
    
        codec = avcodec_find_decoder(avctx->codec_id);
    
        opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
    
            case AVMEDIA_TYPE_AUDIO   : is->last_audio_stream    = stream_index; if(audio_codec_name   ) codec= avcodec_find_decoder_by_name(   audio_codec_name); break;
            case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; if(subtitle_codec_name) codec= avcodec_find_decoder_by_name(subtitle_codec_name); break;
            case AVMEDIA_TYPE_VIDEO   : is->last_video_stream    = stream_index; if(video_codec_name   ) codec= avcodec_find_decoder_by_name(   video_codec_name); break;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        avctx->workaround_bugs   = workaround_bugs;
        avctx->lowres            = lowres;
    
        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;
        }
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        avctx->idct_algo         = idct;
        avctx->skip_frame        = skip_frame;
        avctx->skip_idct         = skip_idct;
        avctx->skip_loop_filter  = skip_loop_filter;
        avctx->error_concealment = error_concealment;
    
        if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        if (fast)   avctx->flags2 |= CODEC_FLAG2_FAST;
    
        if(codec->capabilities & CODEC_CAP_DR1)
            avctx->flags |= CODEC_FLAG_EMU_EDGE;
    
    
        if (!av_dict_get(opts, "threads", NULL, 0))
            av_dict_set(&opts, "threads", "auto", 0);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (!codec ||
    
            avcodec_open2(avctx, codec, &opts) < 0)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            return -1;
    
        if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
            av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
            return AVERROR_OPTION_NOT_FOUND;
        }
    
        if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
    
            int audio_hw_buf_size = audio_open(is, avctx->channel_layout, avctx->channels, avctx->sample_rate, &is->audio_src);
            if (audio_hw_buf_size < 0)
    
            is->audio_hw_buf_size = audio_hw_buf_size;
            is->audio_tgt = is->audio_src;
    
        ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        switch (avctx->codec_type) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            is->audio_stream = stream_index;
            is->audio_st = ic->streams[stream_index];
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
            is->audio_buf_size  = 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            is->audio_buf_index = 0;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
            is->audio_diff_avg_coef  = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
    
            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 * is->audio_hw_buf_size / av_samples_get_buffer_size(NULL, is->audio_tgt.channels, is->audio_tgt.freq, is->audio_tgt.fmt, 1);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
    
            memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
    
            packet_queue_start(&is->audioq);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            break;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            is->video_stream = stream_index;
            is->video_st = ic->streams[stream_index];
    
    
            packet_queue_start(&is->videoq);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            is->video_tid = SDL_CreateThread(video_thread, is);
            break;
    
            is->subtitle_stream = stream_index;
            is->subtitle_st = ic->streams[stream_index];
    
            packet_queue_start(&is->subtitleq);
    
            is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
            break;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        default:
            break;
        }
        return 0;
    }
    
    static void stream_component_close(VideoState *is, int stream_index)
    {
        AVFormatContext *ic = is->ic;
    
        if (stream_index < 0 || stream_index >= ic->nb_streams)
            return;
    
        avctx = ic->streams[stream_index]->codec;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        switch (avctx->codec_type) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            packet_queue_abort(&is->audioq);
    
            SDL_CloseAudio();
    
    
            packet_queue_flush(&is->audioq);
    
            av_free_packet(&is->audio_pkt);
    
            swr_free(&is->swr_ctx);
    
            av_freep(&is->audio_buf1);
            is->audio_buf = NULL;
    
            avcodec_free_frame(&is->frame);
    
    
            if (is->rdft) {
                av_rdft_end(is->rdft);
                av_freep(&is->rdft_data);
    
                is->rdft = NULL;
                is->rdft_bits = 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            break;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            packet_queue_abort(&is->videoq);
    
            /* note: we also signal this mutex to make sure we deblock the
               video thread in all cases */
            SDL_LockMutex(is->pictq_mutex);
            SDL_CondSignal(is->pictq_cond);
            SDL_UnlockMutex(is->pictq_mutex);
    
            SDL_WaitThread(is->video_tid, NULL);
    
    
            packet_queue_flush(&is->videoq);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            break;
    
            packet_queue_abort(&is->subtitleq);
    
            /* note: we also signal this mutex to make sure we deblock the
               video thread in all cases */
            SDL_LockMutex(is->subpq_mutex);
            is->subtitle_stream_changed = 1;
    
            SDL_CondSignal(is->subpq_cond);
            SDL_UnlockMutex(is->subpq_mutex);
    
            SDL_WaitThread(is->subtitle_tid, NULL);
    
    
            packet_queue_flush(&is->subtitleq);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        default:
            break;
        }
    
    
        ic->streams[stream_index]->discard = AVDISCARD_ALL;
    
    #if CONFIG_AVFILTER
    
        free_buffer_pool(&is->buffer_pool);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        switch (avctx->codec_type) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            is->audio_st = NULL;
            is->audio_stream = -1;
            break;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            is->video_st = NULL;
            is->video_stream = -1;
            break;
    
            is->subtitle_st = NULL;
            is->subtitle_stream = -1;
            break;
    
    static int decode_interrupt_cb(void *ctx)
    
        VideoState *is = ctx;
        return is->abort_request;
    
    static int is_realtime(AVFormatContext *s)
    {
        if(   !strcmp(s->iformat->name, "rtp")
           || !strcmp(s->iformat->name, "rtsp")
           || !strcmp(s->iformat->name, "sdp")
        )
            return 1;
    
        if(s->pb && (   !strncmp(s->filename, "rtp:", 4)
                     || !strncmp(s->filename, "udp:", 4)
                    )
        )
            return 1;
        return 0;
    }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    /* this thread gets the stream from the disk or the network */
    
    static int read_thread(void *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        VideoState *is = arg;
    
        AVFormatContext *ic = NULL;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        AVPacket pkt1, *pkt = &pkt1;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        int eof = 0;
    
    Robert Krüger's avatar
    Robert Krüger committed
        int pkt_in_play_range = 0;
    
        AVDictionaryEntry *t;
    
        AVDictionary **opts;
        int orig_nb_streams;
    
        SDL_mutex *wait_mutex = SDL_CreateMutex();
    
        memset(st_index, -1, sizeof(st_index));
    
        is->last_video_stream = is->video_stream = -1;
        is->last_audio_stream = is->audio_stream = -1;
        is->last_subtitle_stream = is->subtitle_stream = -1;
    
        ic = avformat_alloc_context();
        ic->interrupt_callback.callback = decode_interrupt_cb;
    
        ic->interrupt_callback.opaque = is;
    
        err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
    
        if (err < 0) {
            print_error(is->filename, err);
            ret = -1;
            goto fail;
        }
    
        if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
            av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
            ret = AVERROR_OPTION_NOT_FOUND;
            goto fail;
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        is->ic = ic;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        if (genpts)
    
            ic->flags |= AVFMT_FLAG_GENPTS;
    
    
        opts = setup_find_stream_info_opts(ic, codec_opts);
    
        orig_nb_streams = ic->nb_streams;
    
        err = avformat_find_stream_info(ic, opts);
    
        if (err < 0) {
            fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
            ret = -1;
            goto fail;
        }
    
        for (i = 0; i < orig_nb_streams; i++)
            av_dict_free(&opts[i]);
        av_freep(&opts);
    
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        if (ic->pb)
    
            ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        if (seek_by_bytes < 0)
            seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* if seeking requested, we execute it */
        if (start_time != AV_NOPTS_VALUE) {
            int64_t timestamp;
    
            timestamp = start_time;
            /* add the stream start time */
            if (ic->start_time != AV_NOPTS_VALUE)
                timestamp += ic->start_time;
    
            ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (ret < 0) {
    
                fprintf(stderr, "%s: could not seek to position %0.3f\n",
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                        is->filename, (double)timestamp / AV_TIME_BASE);
            }
        }
    
    
        for (i = 0; i < ic->nb_streams; i++)
    
            st_index[AVMEDIA_TYPE_VIDEO] =
                av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
                                    wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
    
            st_index[AVMEDIA_TYPE_AUDIO] =
                av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
                                    wanted_stream[AVMEDIA_TYPE_AUDIO],
                                    st_index[AVMEDIA_TYPE_VIDEO],
                                    NULL, 0);
    
            st_index[AVMEDIA_TYPE_SUBTITLE] =
                av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
                                    wanted_stream[AVMEDIA_TYPE_SUBTITLE],
                                    (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
                                     st_index[AVMEDIA_TYPE_AUDIO] :
                                     st_index[AVMEDIA_TYPE_VIDEO]),
                                    NULL, 0);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (show_status) {
    
            av_dump_format(ic, 0, is->filename, 0);
    
        is->show_mode = show_mode;
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* open the streams */
    
        if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
            stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        ret = -1;
    
        if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
            ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
        }
    
        is->refresh_tid = SDL_CreateThread(refresh_thread, is);
    
        if (is->show_mode == SHOW_MODE_NONE)
            is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
    
        if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
            stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (is->video_stream < 0 && is->audio_stream < 0) {
    
            fprintf(stderr, "%s: could not open codecs\n", is->filename);
            ret = -1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            goto fail;
        }
    
    
        if (infinite_buffer < 0 && is_realtime(ic))
            infinite_buffer = 1;
    
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        for (;;) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (is->abort_request)
                break;
    
            if (is->paused != is->last_paused) {
                is->last_paused = is->paused;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                if (is->paused)
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                    is->read_pause_return = av_read_pause(ic);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                else
                    av_read_play(ic);
    
    #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);
                continue;
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (is->seek_req) {
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                int64_t seek_target = is->seek_pos;
                int64_t seek_min    = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
                int64_t seek_max    = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
    // FIXME the +-2 is due to rounding being not done in the correct direction in generation
    
    //      of the seek_pos/seek_rel variables
    
                ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                if (ret < 0) {
                    fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                } else {
    
                    if (is->audio_stream >= 0) {
                        packet_queue_flush(&is->audioq);
    
                        packet_queue_put(&is->audioq, &flush_pkt);
    
                    if (is->subtitle_stream >= 0) {
                        packet_queue_flush(&is->subtitleq);
    
                        packet_queue_put(&is->subtitleq, &flush_pkt);
    
                    if (is->video_stream >= 0) {
                        packet_queue_flush(&is->videoq);
    
                        packet_queue_put(&is->videoq, &flush_pkt);
    
                update_external_clock_pts(is, (seek_target + ic->start_time) / (double)AV_TIME_BASE);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                is->seek_req = 0;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                eof = 0;
    
            if (is->que_attachments_req) {
                avformat_queue_attached_pictures(ic);
                is->que_attachments_req = 0;
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* if the queue are full, no need to read more */
    
                  (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
    
                || (   (is->audioq   .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
                    && (is->videoq   .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request)
    
                    && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                /* wait 10 ms */
    
                SDL_LockMutex(wait_mutex);
                SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
                SDL_UnlockMutex(wait_mutex);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                continue;
            }
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
            if (eof) {
                if (is->video_stream >= 0) {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                    av_init_packet(pkt);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                    pkt->data = NULL;
                    pkt->size = 0;
                    pkt->stream_index = is->video_stream;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                    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);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
                    if (loop != 1 && (!loop || --loop)) {
    
                        stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                    } else if (autoexit) {
                        ret = AVERROR_EOF;
    
                        goto fail;
                    }
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            ret = av_read_frame(ic, pkt);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (ret < 0) {
    
                if (ret == AVERROR_EOF || url_feof(ic->pb))
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                    eof = 1;
    
                if (ic->pb && ic->pb->error)
    
                SDL_LockMutex(wait_mutex);
                SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
                SDL_UnlockMutex(wait_mutex);
    
    Robert Krüger's avatar
    Robert Krüger committed
            /* check if packet is in play range specified by user, then queue, otherwise discard */
            pkt_in_play_range = duration == AV_NOPTS_VALUE ||
                    (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
                    av_q2d(ic->streams[pkt->stream_index]->time_base) -
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                    (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
                    <= ((double)duration / 1000000);
    
    Robert Krüger's avatar
    Robert Krüger committed
            if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                packet_queue_put(&is->audioq, pkt);
    
    Robert Krüger's avatar
    Robert Krüger committed
            } else if (pkt->stream_index == is->video_stream && pkt_in_play_range) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                packet_queue_put(&is->videoq, pkt);
    
    Robert Krüger's avatar
    Robert Krüger committed
            } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
    
                packet_queue_put(&is->subtitleq, pkt);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            } else {
                av_free_packet(pkt);
            }
        }
        /* wait until the end */
        while (!is->abort_request) {
            SDL_Delay(100);
        }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     fail:
        /* close each stream */
        if (is->audio_stream >= 0)
            stream_component_close(is, is->audio_stream);
        if (is->video_stream >= 0)
            stream_component_close(is, is->video_stream);
    
        if (is->subtitle_stream >= 0)
            stream_component_close(is, is->subtitle_stream);
    
            event.type = FF_QUIT_EVENT;
            event.user.data1 = is;
            SDL_PushEvent(&event);
        }
    
        SDL_DestroyMutex(wait_mutex);
    
    static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        VideoState *is;
    
        is = av_mallocz(sizeof(VideoState));
        if (!is)
            return NULL;
    
        av_strlcpy(is->filename, filename, sizeof(is->filename));
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        is->ytop    = 0;
        is->xleft   = 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        /* start video display */
        is->pictq_mutex = SDL_CreateMutex();
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        is->pictq_cond  = SDL_CreateCond();
    
        is->subpq_mutex = SDL_CreateMutex();
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        is->subpq_cond  = SDL_CreateCond();
    
        packet_queue_init(&is->videoq);
        packet_queue_init(&is->audioq);
        packet_queue_init(&is->subtitleq);
    
    
        is->continue_read_thread = SDL_CreateCond();
    
    
        update_external_clock_pts(is, 0.0);
    
        is->audio_current_pts_drift = -av_gettime() / 1000000.0;
        is->video_current_pts_drift = is->audio_current_pts_drift;
    
        is->read_tid     = SDL_CreateThread(read_thread, is);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            av_free(is);
            return NULL;
        }
        return is;
    }
    
    
    static void stream_cycle_channel(VideoState *is, int codec_type)
    
    {
        AVFormatContext *ic = is->ic;
        int start_index, stream_index;
    
        if (codec_type == AVMEDIA_TYPE_VIDEO) {
            start_index = is->last_video_stream;
            old_index = is->video_stream;
        } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
            start_index = is->last_audio_stream;
            old_index = is->audio_stream;
        } else {
            start_index = is->last_subtitle_stream;
            old_index = is->subtitle_stream;
        }
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        for (;;) {
    
            if (++stream_index >= is->ic->nb_streams)
    
                if (codec_type == AVMEDIA_TYPE_SUBTITLE)
    
                    is->last_subtitle_stream = -1;
    
                }
                if (start_index == -1)
                    return;
                stream_index = 0;
    
            if (stream_index == start_index)
                return;
            st = ic->streams[stream_index];
    
            if (st->codec->codec_type == codec_type) {
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                switch (codec_type) {
    
                    if (st->codec->sample_rate != 0 &&
                        st->codec->channels != 0)
    
                case AVMEDIA_TYPE_VIDEO:
                case AVMEDIA_TYPE_SUBTITLE:
    
        stream_component_close(is, old_index);
    
        if (codec_type == AVMEDIA_TYPE_VIDEO)
            is->que_attachments_req = 1;
    
    static void toggle_full_screen(VideoState *is)
    
    #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
    
        /* OS X needs to reallocate the SDL overlays */
    
        int i;
        for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
    
        is_full_screen = !is_full_screen;
    
    static void toggle_pause(VideoState *is)
    
        stream_toggle_pause(is);
        is->step = 0;
    
    static void step_to_next_frame(VideoState *is)
    
        /* if the stream is paused unpause it, then step */
        if (is->paused)
            stream_toggle_pause(is);
        is->step = 1;
    
    static void toggle_audio_display(VideoState *is)
    
        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);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    /* handle an event sent by the GUI */
    
    static void event_loop(VideoState *cur_stream)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        SDL_Event event;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        for (;;) {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
            double x;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            SDL_WaitEvent(&event);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
            switch (event.type) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            case SDL_KEYDOWN:
    
                    do_exit(cur_stream);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                switch (event.key.keysym.sym) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                case SDLK_ESCAPE:
                case SDLK_q:
    
                    do_exit(cur_stream);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    break;
                case SDLK_f:
    
                    toggle_full_screen(cur_stream);
    
                    cur_stream->force_refresh = 1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    break;
                case SDLK_p:
                case SDLK_SPACE:
    
                    toggle_pause(cur_stream);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    break;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                case SDLK_s: // S: Step to next frame
    
                    step_to_next_frame(cur_stream);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                case SDLK_a:
    
                    stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
    
                    stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
    
                    stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
    
                    toggle_audio_display(cur_stream);
    
                    cur_stream->force_refresh = 1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    break;
    
                case SDLK_PAGEUP:
                    incr = 600.0;
                    goto do_seek;
                case SDLK_PAGEDOWN:
                    incr = -600.0;
                    goto do_seek;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                case SDLK_LEFT:
                    incr = -10.0;
                    goto do_seek;
                case SDLK_RIGHT:
                    incr = 10.0;
                    goto do_seek;
                case SDLK_UP:
                    incr = 60.0;
                    goto do_seek;
                case SDLK_DOWN:
                    incr = -60.0;
                do_seek:
    
                        if (seek_by_bytes) {
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                            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;
    
                        } 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);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    break;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                default:
                    break;
                }
                break;
    
            case SDL_VIDEOEXPOSE:
                cur_stream->force_refresh = 1;
                break;
    
                    do_exit(cur_stream);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
            case SDL_MOUSEMOTION:
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                if (event.type == SDL_MOUSEBUTTONDOWN) {
                    x = event.button.x;
                } else {
                    if (event.motion.state != SDL_PRESSED)
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                        break;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                    x = event.motion.x;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                    if (seek_by_bytes || cur_stream->ic->duration <= 0) {
                        uint64_t size =  avio_size(cur_stream->ic->pb);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                        stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                    } else {
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                        int64_t ts;
                        int ns, hh, mm, ss;
                        int tns, thh, tmm, tss;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                        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);
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                        fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d)       \n", frac*100,
                                hh, mm, ss, thh, tmm, tss);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                        ts = frac * cur_stream->ic->duration;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                        if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
                            ts += cur_stream->ic->start_time;
                        stream_seek(cur_stream, ts, 0, 0);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            case SDL_VIDEORESIZE:
    
                    screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                                              SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                    screen_width  = cur_stream->width  = event.resize.w;
                    screen_height = cur_stream->height = event.resize.h;
    
                    cur_stream->force_refresh = 1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                break;
            case SDL_QUIT:
    
                do_exit(cur_stream);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                break;
            case FF_ALLOC_EVENT:
                alloc_picture(event.user.data1);
                break;
            case FF_REFRESH_EVENT:
    
                video_refresh(event.user.data1);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                cur_stream->refresh = 0;
    
    static int opt_frame_size(void *optctx, const char *opt, const char *arg)
    
        av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
    
        return opt_default(NULL, "video_size", arg);
    
    static int opt_width(void *optctx, const char *opt, const char *arg)
    
        screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
        return 0;
    
    static int opt_height(void *optctx, const char *opt, const char *arg)
    
        screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
        return 0;
    
    static int opt_format(void *optctx, const char *opt, const char *arg)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        file_iformat = av_find_input_format(arg);
        if (!file_iformat) {
            fprintf(stderr, "Unknown input format: %s\n", arg);
    
            return AVERROR(EINVAL);
    
        return 0;
    
    static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
    
        av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
    
        return opt_default(NULL, "pixel_format", arg);
    
    static int opt_sync(void *optctx, const char *opt, const char *arg)
    
    {
        if (!strcmp(arg, "audio"))
            av_sync_type = AV_SYNC_AUDIO_MASTER;
        else if (!strcmp(arg, "video"))
            av_sync_type = AV_SYNC_VIDEO_MASTER;
        else if (!strcmp(arg, "ext"))
            av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
    
            fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);