Skip to content
Snippets Groups Projects
ffplay.c 129 KiB
Newer Older
  • Learn to ignore specific revisions
  •             last_w = frame->width;
                last_h = frame->height;
                last_format = frame->format;
    
                last_serial = serial;
    
                frame_rate = filt_out->inputs[0]->frame_rate;
    
            ret = av_buffersrc_add_frame(filt_in, frame);
            if (ret < 0)
                goto the_end;
    
                is->frame_last_returned_time = av_gettime_relative() / 1000000.0;
    
                ret = av_buffersink_get_frame_flags(filt_out, frame, 0);
    
                    if (ret == AVERROR_EOF)
                        is->video_finished = serial;
    
                is->frame_last_filter_delay = av_gettime_relative() / 1000000.0 - is->frame_last_returned_time;
    
                if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
                    is->frame_last_filter_delay = 0;
    
                tb = filt_out->inputs[0]->time_base;
    #endif
    
                duration = (frame_rate.num && frame_rate.den ? av_q2d((AVRational){frame_rate.den, frame_rate.num}) : 0);
    
                pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(tb);
    
                ret = queue_picture(is, frame, pts, duration, av_frame_get_pkt_pos(frame), serial);
    
            if (ret < 0)
                goto the_end;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
     the_end:
    
    #if CONFIG_AVFILTER
    
        avfilter_graph_free(&graph);
    
        av_free_packet(&pkt);
    
        av_frame_free(&frame);
    
    static int subtitle_thread(void *arg)
    {
        VideoState *is = arg;
        SubPicture *sp;
        AVPacket pkt1, *pkt = &pkt1;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        for (;;) {
    
            while (is->paused && !is->subtitleq.abort_request) {
                SDL_Delay(10);
            }
    
            if (packet_queue_get(&is->subtitleq, pkt, 1, &serial) < 0)
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
            if (pkt->data == flush_pkt.data) {
    
                avcodec_flush_buffers(is->subtitle_st->codec);
                continue;
            }
    
            SDL_LockMutex(is->subpq_mutex);
            while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
                   !is->subtitleq.abort_request) {
                SDL_CondWait(is->subpq_cond, is->subpq_mutex);
            }
            SDL_UnlockMutex(is->subpq_mutex);
    
            sp = &is->subpq[is->subpq_windex];
    
           /* NOTE: ipts is the PTS of the _first_ picture beginning in
               this packet, if any */
            pts = 0;
            if (pkt->pts != AV_NOPTS_VALUE)
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
    
            avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
                                     &got_subtitle, pkt);
    
            if (got_subtitle && sp->sub.format == 0) {
    
                if (sp->sub.pts != AV_NOPTS_VALUE)
                    pts = sp->sub.pts / (double)AV_TIME_BASE;
    
                for (i = 0; i < sp->sub.num_rects; i++)
                {
    
                    for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
    
                        RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
    
                        y = RGB_TO_Y_CCIR(r, g, b);
                        u = RGB_TO_U_CCIR(r, g, b, 0);
                        v = RGB_TO_V_CCIR(r, g, b, 0);
    
                        YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
    
                    }
                }
    
                /* now we can update the picture count */
                if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
                    is->subpq_windex = 0;
                SDL_LockMutex(is->subpq_mutex);
                is->subpq_size++;
                SDL_UnlockMutex(is->subpq_mutex);
    
            } else if (got_subtitle) {
                avsubtitle_free(&sp->sub);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    /* copy samples for viewing in editor window */
    static void update_sample_display(VideoState *is, short *samples, int samples_size)
    {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        size = samples_size / sizeof(short);
        while (size > 0) {
            len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
            if (len > size)
                len = size;
            memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
            samples += len;
            is->sample_array_index += len;
            if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
                is->sample_array_index = 0;
            size -= len;
        }
    }
    
    
    /* return the wanted number of samples to get better sync if sync_type is video
     * or external master clock */
    static int synchronize_audio(VideoState *is, int nb_samples)
    
        int wanted_nb_samples = nb_samples;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        /* if not master, then we try to remove or add samples to correct the clock */
    
        if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
    
            int min_nb_samples, max_nb_samples;
    
            diff = get_clock(&is->audclk) - get_master_clock(is);
    
            if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
    
                is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
                if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
                    /* not enough measures to have a correct estimate */
                    is->audio_diff_avg_count++;
                } else {
                    /* estimate the A-V difference */
                    avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
    
                    if (fabs(avg_diff) >= is->audio_diff_threshold) {
    
                        wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
    
                        min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
                        max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
                        wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
    
                    av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n",
    
                            diff, avg_diff, wanted_nb_samples - nb_samples,
    
                            is->audio_clock, is->audio_diff_threshold);
    
            } else {
                /* too big difference : may be initial PTS errors, so
                   reset A-V filter */
                is->audio_diff_avg_count = 0;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                is->audio_diff_cum       = 0;
    
    /**
     * Decode one audio frame and return its uncompressed size.
     *
     * The processed audio frame is decoded, converted if required, and
     * stored in is->audio_buf, with size in bytes given by the return
     * value.
     */
    
    static int audio_decode_frame(VideoState *is)
    
        AVPacket *pkt_temp = &is->audio_pkt_temp;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        AVPacket *pkt = &is->audio_pkt;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        AVCodecContext *dec = is->audio_st->codec;
    
        int len1, data_size, resampled_data_size;
    
        int64_t dec_channel_layout;
        int got_frame;
    
    Marton Balint's avatar
    Marton Balint committed
        AVRational tb;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        for (;;) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* NOTE: the audio packet can contain several frames */
    
            while (pkt_temp->stream_index != -1 || is->audio_buf_frames_pending) {
    
                if (!is->frame) {
    
                    if (!(is->frame = av_frame_alloc()))
    
                        return AVERROR(ENOMEM);
    
                } else {
                    av_frame_unref(is->frame);
                }
    
                if (is->audioq.serial != is->audio_pkt_temp_serial)
                    break;
    
    
                if (!is->audio_buf_frames_pending) {
    
    Marton Balint's avatar
    Marton Balint committed
                    len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
                    if (len1 < 0) {
                        /* if error, we skip the frame */
                        pkt_temp->size = 0;
                        break;
                    }
    
                    pkt_temp->dts =
                    pkt_temp->pts = AV_NOPTS_VALUE;
    
    Marton Balint's avatar
    Marton Balint committed
                    pkt_temp->data += len1;
                    pkt_temp->size -= len1;
    
                    if (pkt_temp->data && pkt_temp->size <= 0 || !pkt_temp->data && !got_frame)
                        pkt_temp->stream_index = -1;
    
                    if (!pkt_temp->data && !got_frame)
                        is->audio_finished = is->audio_pkt_temp_serial;
    
                    if (!got_frame)
    
    Marton Balint's avatar
    Marton Balint committed
                        continue;
    
    Marton Balint's avatar
    Marton Balint committed
                    tb = (AVRational){1, is->frame->sample_rate};
                    if (is->frame->pts != AV_NOPTS_VALUE)
                        is->frame->pts = av_rescale_q(is->frame->pts, dec->time_base, tb);
    
                    else if (is->frame->pkt_pts != AV_NOPTS_VALUE)
                        is->frame->pts = av_rescale_q(is->frame->pkt_pts, is->audio_st->time_base, tb);
    
                    else if (is->audio_frame_next_pts != AV_NOPTS_VALUE)
    
    #if CONFIG_AVFILTER
    
                        is->frame->pts = av_rescale_q(is->audio_frame_next_pts, (AVRational){1, is->audio_filter_src.freq}, tb);
    
    #else
                        is->frame->pts = av_rescale_q(is->audio_frame_next_pts, (AVRational){1, is->audio_src.freq}, tb);
    #endif
    
    
                    if (is->frame->pts != AV_NOPTS_VALUE)
                        is->audio_frame_next_pts = is->frame->pts + is->frame->nb_samples;
    
    Marton Balint's avatar
    Marton Balint committed
    
    #if CONFIG_AVFILTER
                    dec_channel_layout = get_valid_channel_layout(is->frame->channel_layout, av_frame_get_channels(is->frame));
    
                    reconfigure =
                        cmp_audio_fmts(is->audio_filter_src.fmt, is->audio_filter_src.channels,
                                       is->frame->format, av_frame_get_channels(is->frame))    ||
                        is->audio_filter_src.channel_layout != dec_channel_layout ||
                        is->audio_filter_src.freq           != is->frame->sample_rate ||
                        is->audio_pkt_temp_serial           != is->audio_last_serial;
    
                    if (reconfigure) {
                        char buf1[1024], buf2[1024];
                        av_get_channel_layout_string(buf1, sizeof(buf1), -1, is->audio_filter_src.channel_layout);
                        av_get_channel_layout_string(buf2, sizeof(buf2), -1, dec_channel_layout);
                        av_log(NULL, AV_LOG_DEBUG,
                               "Audio frame changed from rate:%d ch:%d fmt:%s layout:%s serial:%d to rate:%d ch:%d fmt:%s layout:%s serial:%d\n",
                               is->audio_filter_src.freq, is->audio_filter_src.channels, av_get_sample_fmt_name(is->audio_filter_src.fmt), buf1, is->audio_last_serial,
                               is->frame->sample_rate, av_frame_get_channels(is->frame), av_get_sample_fmt_name(is->frame->format), buf2, is->audio_pkt_temp_serial);
    
                        is->audio_filter_src.fmt            = is->frame->format;
                        is->audio_filter_src.channels       = av_frame_get_channels(is->frame);
                        is->audio_filter_src.channel_layout = dec_channel_layout;
                        is->audio_filter_src.freq           = is->frame->sample_rate;
                        is->audio_last_serial               = is->audio_pkt_temp_serial;
    
                        if ((ret = configure_audio_filters(is, afilters, 1)) < 0)
                            return ret;
                    }
    
                    if ((ret = av_buffersrc_add_frame(is->in_audio_filter, is->frame)) < 0)
                        return ret;
    
    #endif
                }
    #if CONFIG_AVFILTER
                if ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, is->frame, 0)) < 0) {
                    if (ret == AVERROR(EAGAIN)) {
                        is->audio_buf_frames_pending = 0;
                        continue;
                    }
    
                    if (ret == AVERROR_EOF)
                        is->audio_finished = is->audio_pkt_temp_serial;
    
                is->audio_buf_frames_pending = 1;
                tb = is->out_audio_filter->inputs[0]->time_base;
    
    Marton Balint's avatar
    Marton Balint committed
    #endif
    
                data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(is->frame),
    
                                                       is->frame->nb_samples,
    
                    (is->frame->channel_layout && av_frame_get_channels(is->frame) == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
                    is->frame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(is->frame));
    
                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) {
    
                        av_log(NULL, AV_LOG_ERROR,
                               "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), av_frame_get_channels(is->frame),
    
                                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       = av_frame_get_channels(is->frame);
    
                    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_buf1;
                    int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate + 256;
                    int out_size  = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
    
                        av_log(NULL, AV_LOG_ERROR, "av_samples_get_buffer_size() failed\n");
    
                    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) {
    
                            av_log(NULL, AV_LOG_ERROR, "swr_set_compensation() failed\n");
    
                    av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
                    if (!is->audio_buf1)
                        return AVERROR(ENOMEM);
    
                    len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
    
                        av_log(NULL, AV_LOG_ERROR, "swr_convert() failed\n");
    
                    if (len2 == out_count) {
    
                        av_log(NULL, AV_LOG_WARNING, "audio buffer is probably too small\n");
    
                    is->audio_buf = is->audio_buf1;
    
                    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;
    
                /* update the audio clock with the pts */
    
    Marton Balint's avatar
    Marton Balint committed
                    is->audio_clock = is->frame->pts * av_q2d(tb) + (double) is->frame->nb_samples / is->frame->sample_rate;
    
                else
                    is->audio_clock = NAN;
                is->audio_clock_serial = is->audio_pkt_temp_serial;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                {
                    static double last_clock;
    
                    printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                           is->audio_clock - last_clock,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    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));
    
            pkt_temp->stream_index = -1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                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 ((packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                return -1;
    
            if (pkt->data == flush_pkt.data) {
    
                is->audio_frame_next_pts = AV_NOPTS_VALUE;
    
                if ((is->ic->iformat->flags & (AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK)) && !is->ic->iformat->read_seek)
                    is->audio_frame_next_pts = is->audio_st->start_time;
    
            *pkt_temp = *pkt;
    
    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;
    
    
        audio_callback_time = av_gettime_relative();
    
    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);
    
    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) / is->audio_tgt.frame_size * is->audio_tgt.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;
        }
    
        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. */
    
            set_clock_at(&is->audclk, is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / is->audio_tgt.bytes_per_sec, is->audio_clock_serial, audio_callback_time / 1000000.0);
    
    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;
    
        static const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
    
        static const int next_sample_rates[] = {0, 44100, 48000, 96000, 192000};
        int next_sample_rate_idx = FF_ARRAY_ELEMS(next_sample_rates) - 1;
    
    
        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_nb_channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
        wanted_spec.channels = wanted_nb_channels;
    
        wanted_spec.freq = wanted_sample_rate;
    
        if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
    
            av_log(NULL, AV_LOG_ERROR, "Invalid sample rate or channel count!\n");
    
        while (next_sample_rate_idx && next_sample_rates[next_sample_rate_idx] >= wanted_spec.freq)
            next_sample_rate_idx--;
    
        wanted_spec.format = AUDIO_S16SYS;
        wanted_spec.silence = 0;
    
        wanted_spec.samples = FFMAX(SDL_AUDIO_MIN_BUFFER_SIZE, 2 << av_log2(wanted_spec.freq / SDL_AUDIO_MAX_CALLBACKS_PER_SEC));
    
        wanted_spec.callback = sdl_audio_callback;
    
        wanted_spec.userdata = opaque;
    
        while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
    
            av_log(NULL, AV_LOG_WARNING, "SDL_OpenAudio (%d channels, %d Hz): %s\n",
                   wanted_spec.channels, wanted_spec.freq, SDL_GetError());
    
            wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
            if (!wanted_spec.channels) {
    
                wanted_spec.freq = next_sample_rates[next_sample_rate_idx--];
                wanted_spec.channels = wanted_nb_channels;
                if (!wanted_spec.freq) {
                    av_log(NULL, AV_LOG_ERROR,
                           "No more 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) {
    
            av_log(NULL, AV_LOG_ERROR,
                   "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) {
    
                av_log(NULL, AV_LOG_ERROR,
                       "SDL advised channel count %d is not supported!\n", spec.channels);
    
        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;
    
        audio_hw_params->frame_size = av_samples_get_buffer_size(NULL, audio_hw_params->channels, 1, audio_hw_params->fmt, 1);
        audio_hw_params->bytes_per_sec = av_samples_get_buffer_size(NULL, audio_hw_params->channels, audio_hw_params->freq, audio_hw_params->fmt, 1);
        if (audio_hw_params->bytes_per_sec <= 0 || audio_hw_params->frame_size <= 0) {
            av_log(NULL, AV_LOG_ERROR, "av_samples_get_buffer_size failed\n");
            return -1;
        }
    
        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;
    
        const char *forced_codec_name = NULL;
    
        AVDictionary *opts;
        AVDictionaryEntry *t = NULL;
    
    Marton Balint's avatar
    Marton Balint committed
        int sample_rate, nb_channels;
        int64_t channel_layout;
    
    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);
    
            case AVMEDIA_TYPE_AUDIO   : is->last_audio_stream    = stream_index; forced_codec_name =    audio_codec_name; break;
            case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
            case AVMEDIA_TYPE_VIDEO   : is->last_video_stream    = stream_index; forced_codec_name =    video_codec_name; break;
    
        if (forced_codec_name)
            codec = avcodec_find_decoder_by_name(forced_codec_name);
        if (!codec) {
    
            if (forced_codec_name) av_log(NULL, AV_LOG_WARNING,
                                          "No codec could be found with name '%s'\n", forced_codec_name);
            else                   av_log(NULL, AV_LOG_WARNING,
                                          "No codec could be found with id %d\n", avctx->codec_id);
    
        avctx->codec_id = codec->id;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        avctx->workaround_bugs   = workaround_bugs;
    
        if(stream_lowres > av_codec_get_max_lowres(codec)){
    
            av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
    
            stream_lowres = av_codec_get_max_lowres(codec);
    
        av_codec_set_lowres(avctx, stream_lowres);
    
        if(stream_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;
    
    
        opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
    
        if (!av_dict_get(opts, "threads", NULL, 0))
            av_dict_set(&opts, "threads", "auto", 0);
    
            av_dict_set_int(&opts, "lowres", stream_lowres, 0);
    
        if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
    
            av_dict_set(&opts, "refcounted_frames", "1", 0);
    
        if (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;
        }
    
        ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        switch (avctx->codec_type) {
    
    Marton Balint's avatar
    Marton Balint committed
    #if CONFIG_AVFILTER
            {
                AVFilterLink *link;
    
                is->audio_filter_src.freq           = avctx->sample_rate;
                is->audio_filter_src.channels       = avctx->channels;
                is->audio_filter_src.channel_layout = get_valid_channel_layout(avctx->channel_layout, avctx->channels);
                is->audio_filter_src.fmt            = avctx->sample_fmt;
                if ((ret = configure_audio_filters(is, afilters, 0)) < 0)
                    return ret;
                link = is->out_audio_filter->inputs[0];
                sample_rate    = link->sample_rate;
                nb_channels    = link->channels;
                channel_layout = link->channel_layout;
            }
    #else
            sample_rate    = avctx->sample_rate;
            nb_channels    = avctx->channels;
            channel_layout = avctx->channel_layout;
    #endif
    
    
            /* prepare audio output */
    
    Marton Balint's avatar
    Marton Balint committed
            if ((ret = audio_open(is, channel_layout, nb_channels, sample_rate, &is->audio_tgt)) < 0)
    
                return ret;
            is->audio_hw_buf_size = ret;
            is->audio_src = is->audio_tgt;
    
    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 = (double)(is->audio_hw_buf_size) / is->audio_tgt.bytes_per_sec;
    
    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));
    
            is->audio_pkt_temp.stream_index = -1;
    
    
            is->audio_stream = stream_index;
            is->audio_st = ic->streams[stream_index];
    
    
            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);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            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_buf1_size = 0;
    
            is->audio_buf = NULL;
    
            av_frame_free(&is->frame);
    
    
            if (is->rdft) {
                av_rdft_end(is->rdft);
                av_freep(&is->rdft_data);
    
                is->rdft = NULL;
                is->rdft_bits = 0;
    
    Marton Balint's avatar
    Marton Balint committed
    #if CONFIG_AVFILTER
            avfilter_graph_free(&is->agraph);
    #endif
    
    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);
            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;
    
    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) {
    
            av_log(NULL, AV_LOG_WARNING,
                   "%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 avio_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) && strcmp("ogg", ic->iformat->name);
    
        is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
    
    
        if (!window_title && (t = av_dict_get(ic->metadata, "title", NULL, 0)))
            window_title = av_asprintf("%s - %s", t->value, input_filename);
    
    
    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) {
    
                av_log(NULL, AV_LOG_WARNING, "%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);
    
        if (!video_disable && !subtitle_disable)
    
            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;
    
        if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
            AVStream *st = ic->streams[st_index[AVMEDIA_TYPE_VIDEO]];
            AVCodecContext *avctx = st->codec;
    
            AVRational sar = av_guess_sample_aspect_ratio(ic, st, NULL);
            if (avctx->width)
                set_default_window_size(avctx->width, avctx->height, sar);
    
    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
        }
    
        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) {
    
            av_log(NULL, AV_LOG_FATAL, "Failed to open file '%s' or configure filtergraph\n",
                   is->filename);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            goto fail;
        }
    
    
    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) {
    
                    av_log(NULL, AV_LOG_ERROR,
                           "%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);
    
                    if (is->seek_flags & AVSEEK_FLAG_BYTE) {
    
                       set_clock(&is->extclk, NAN, 0);
    
                       set_clock(&is->extclk, seek_target / (double)AV_TIME_BASE, 0);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                }
                is->seek_req = 0;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                eof = 0;
    
                if (is->paused)
                    step_to_next_frame(is);
    
            if (is->queue_attachments_req) {
    
                if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
                    AVPacket copy;
                    if ((ret = av_copy_packet(&copy, &is->video_st->attached_pic)) < 0)
                        goto fail;
                    packet_queue_put(&is->videoq, &copy);
    
                    packet_queue_put_nullpacket(&is->videoq, is->video_stream);
    
                is->queue_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->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))
    
                    && (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;
            }
    
            if (!is->paused &&
                (!is->audio_st || is->audio_finished == is->audioq.serial) &&
    
                (!is->video_st || (is->video_finished == is->videoq.serial && pictq_nb_remaining(is) == 0))) {
    
                if (loop != 1 && (!loop || --loop)) {
                    stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
                } else if (autoexit) {
                    ret = AVERROR_EOF;
                    goto fail;