Skip to content
Snippets Groups Projects
ffplay.c 129 KiB
Newer Older
  • Learn to ignore specific revisions
  • Aneesh Dogra's avatar
    Aneesh Dogra committed
                    for (x = 0; x < 2 * nb_freq; x++) {
                        double w = (x-nb_freq) * (1.0 / nb_freq);
                        data[ch][x] = s->sample_array[i] * (1.0 - w * w);
    
                        i += channels;
                        if (i >= SAMPLE_ARRAY_SIZE)
                            i -= SAMPLE_ARRAY_SIZE;
                    }
    
                    av_rdft_calc(s->rdft, data[ch]);
    
    Diego Biurrun's avatar
    Diego Biurrun committed
                /* Least efficient way to do this, we should of course
                 * directly access it but it is more than fast enough. */
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                for (y = 0; y < s->height; y++) {
                    double w = 1 / sqrt(nb_freq);
                    int a = sqrt(w * sqrt(data[0][2 * y + 0] * data[0][2 * y + 0] + data[0][2 * y + 1] * data[0][2 * y + 1]));
                    int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
                           + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
                    a = FFMIN(a, 255);
                    b = FFMIN(b, 255);
                    fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
    
    
                    fill_rectangle(screen,
                                s->xpos, s->height-y, 1, 1,
    
                }
            }
            SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
            if (s->xpos >= s->width)
    
                s->xpos= s->xleft;
        }
    
    static void stream_close(VideoState *is)
    {
        int i;
        /* XXX: use a special url_shutdown call to abort parse cleanly */
        is->abort_request = 1;
        SDL_WaitThread(is->read_tid, NULL);
    
        packet_queue_destroy(&is->videoq);
        packet_queue_destroy(&is->audioq);
        packet_queue_destroy(&is->subtitleq);
    
    
        /* free all pictures */
    
        for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
            free_picture(&is->pictq[i]);
    
        for (i = 0; i < SUBPICTURE_QUEUE_SIZE; i++)
            free_subpicture(&is->subpq[i]);
    
        SDL_DestroyMutex(is->pictq_mutex);
        SDL_DestroyCond(is->pictq_cond);
        SDL_DestroyMutex(is->subpq_mutex);
        SDL_DestroyCond(is->subpq_cond);
    
        SDL_DestroyCond(is->continue_read_thread);
    
    #if !CONFIG_AVFILTER
    
        sws_freeContext(is->img_convert_ctx);
    
    static void do_exit(VideoState *is)
    
        if (is) {
            stream_close(is);
    
        uninit_opts();
    #if CONFIG_AVFILTER
    
        avformat_network_deinit();
    
        if (show_status)
            printf("\n");
        SDL_Quit();
    
        av_log(NULL, AV_LOG_QUIET, "%s", "");
    
    static void sigterm_handler(int sig)
    {
        exit(123);
    }
    
    
    static void set_default_window_size(int width, int height, AVRational sar)
    
        calculate_display_rect(&rect, 0, 0, INT_MAX, height, width, height, sar);
    
        default_width  = rect.w;
        default_height = rect.h;
    }
    
    
    static int video_open(VideoState *is, int force_set_video_mode, VideoPicture *vp)
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
    {
        int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        if (is_full_screen) flags |= SDL_FULLSCREEN;
        else                flags |= SDL_RESIZABLE;
    
            set_default_window_size(vp->width, vp->height, vp->sar);
    
        if (is_full_screen && fs_screen_width) {
            w = fs_screen_width;
            h = fs_screen_height;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        } else if (!is_full_screen && screen_width) {
    
            w = default_width;
            h = default_height;
    
        w = FFMIN(16383, w);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        if (screen && is->width == screen->w && screen->w == w
    
           && is->height== screen->h && screen->h == h && !force_set_video_mode)
    
        screen = SDL_SetVideoMode(w, h, 0, flags);
        if (!screen) {
    
            av_log(NULL, AV_LOG_FATAL, "SDL: could not set video mode - exiting\n");
    
        if (!window_title)
            window_title = input_filename;
        SDL_WM_SetCaption(window_title, window_title);
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        is->width  = screen->w;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    /* display the current picture, if any */
    static void video_display(VideoState *is)
    {
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        if (!screen)
    
            video_open(is, 0, NULL);
    
        if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            video_audio_display(is);
        else if (is->video_st)
            video_image_display(is);
    }
    
    
    static double get_clock(Clock *c)
    
        if (*c->queue_serial != c->serial)
    
        if (c->paused) {
            return c->pts;
    
            double time = av_gettime_relative() / 1000000.0;
    
            return c->pts_drift + time - (time - c->last_updated) * (1.0 - c->speed);
    
    static void set_clock_at(Clock *c, double pts, int serial, double time)
    
        c->pts = pts;
        c->last_updated = time;
        c->pts_drift = c->pts - time;
        c->serial = serial;
    
    static void set_clock(Clock *c, double pts, int serial)
    
        double time = av_gettime_relative() / 1000000.0;
    
        set_clock_at(c, pts, serial, time);
    }
    
    static void set_clock_speed(Clock *c, double speed)
    {
        set_clock(c, get_clock(c), c->serial);
        c->speed = speed;
    }
    
    static void init_clock(Clock *c, int *queue_serial)
    {
        c->speed = 1.0;
        c->paused = 0;
        c->queue_serial = queue_serial;
        set_clock(c, NAN, -1);
    }
    
    static void sync_clock_to_slave(Clock *c, Clock *slave)
    {
        double clock = get_clock(c);
        double slave_clock = get_clock(slave);
        if (!isnan(slave_clock) && (isnan(clock) || fabs(clock - slave_clock) > AV_NOSYNC_THRESHOLD))
            set_clock(c, slave_clock, slave->serial);
    
    static int get_master_sync_type(VideoState *is) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
            if (is->video_st)
    
                return AV_SYNC_VIDEO_MASTER;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            else
    
                return AV_SYNC_AUDIO_MASTER;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
            if (is->audio_st)
    
                return AV_SYNC_AUDIO_MASTER;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            else
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        } else {
    
            return AV_SYNC_EXTERNAL_CLOCK;
        }
    }
    
    /* get the current master clock value */
    static double get_master_clock(VideoState *is)
    {
        double val;
    
        switch (get_master_sync_type(is)) {
            case AV_SYNC_VIDEO_MASTER:
    
                val = get_clock(&is->vidclk);
    
                break;
            case AV_SYNC_AUDIO_MASTER:
    
                val = get_clock(&is->audclk);
    
                val = get_clock(&is->extclk);
    
    static void check_external_clock_speed(VideoState *is) {
       if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
           is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
    
           set_clock_speed(&is->extclk, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->extclk.speed - EXTERNAL_CLOCK_SPEED_STEP));
    
       } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
                  (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
    
           set_clock_speed(&is->extclk, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->extclk.speed + EXTERNAL_CLOCK_SPEED_STEP));
    
           double speed = is->extclk.speed;
    
               set_clock_speed(&is->extclk, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    /* seek in the stream */
    
    static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
    
        if (!is->seek_req) {
            is->seek_pos = pos;
    
            is->seek_rel = rel;
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
            is->seek_flags &= ~AVSEEK_FLAG_BYTE;
    
            if (seek_by_bytes)
                is->seek_flags |= AVSEEK_FLAG_BYTE;
    
            SDL_CondSignal(is->continue_read_thread);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    /* pause or resume the video */
    
    static void stream_toggle_pause(VideoState *is)
    
            is->frame_timer += av_gettime_relative() / 1000000.0 + is->vidclk.pts_drift - is->vidclk.pts;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
            if (is->read_pause_return != AVERROR(ENOSYS)) {
    
                is->vidclk.paused = 0;
    
            set_clock(&is->vidclk, get_clock(&is->vidclk), is->vidclk.serial);
    
        set_clock(&is->extclk, get_clock(&is->extclk), is->extclk.serial);
        is->paused = is->audclk.paused = is->vidclk.paused = is->extclk.paused = !is->paused;
    
    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 double compute_target_delay(double delay, VideoState *is)
    
    
        /* update delay to follow master synchronisation source */
    
        if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
    
            /* if video is slave, we try to correct big delays by
               duplicating or deleting a frame */
    
            diff = get_clock(&is->vidclk) - get_master_clock(is);
    
    
            /* skip or repeat frame. We take into account the
               delay to compute the threshold. I still don't know
               if it is the best guess */
    
            sync_threshold = FFMAX(AV_SYNC_THRESHOLD_MIN, FFMIN(AV_SYNC_THRESHOLD_MAX, delay));
            if (!isnan(diff) && fabs(diff) < is->max_frame_duration) {
    
                    delay = FFMAX(0, delay + diff);
                else if (diff >= sync_threshold && delay > AV_SYNC_FRAMEDUP_THRESHOLD)
                    delay = delay + diff;
    
                else if (diff >= sync_threshold)
                    delay = 2 * delay;
            }
        }
    
        av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
                delay, -diff);
    
        return delay;
    }
    
    
    static double vp_duration(VideoState *is, VideoPicture *vp, VideoPicture *nextvp) {
        if (vp->serial == nextvp->serial) {
            double duration = nextvp->pts - vp->pts;
            if (isnan(duration) || duration <= 0 || duration > is->max_frame_duration)
                return vp->duration;
            else
                return duration;
        } else {
            return 0.0;
        }
    }
    
    
    /* return the number of undisplayed pictures in the queue */
    static int pictq_nb_remaining(VideoState *is) {
        return is->pictq_size - is->pictq_rindex_shown;
    }
    
    /* jump back to the previous picture if available by resetting rindex_shown */
    static int pictq_prev_picture(VideoState *is) {
        int ret = is->pictq_rindex_shown;
        is->pictq_rindex_shown = 0;
        return ret;
    }
    
    
    static void pictq_next_picture(VideoState *is) {
    
        if (!is->pictq_rindex_shown) {
            is->pictq_rindex_shown = 1;
            return;
        }
    
        /* update queue size and signal for next picture */
        if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
            is->pictq_rindex = 0;
    
        SDL_LockMutex(is->pictq_mutex);
        is->pictq_size--;
        SDL_CondSignal(is->pictq_cond);
        SDL_UnlockMutex(is->pictq_mutex);
    
    static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
    
        /* update current video pts */
    
        set_clock(&is->vidclk, pts, serial);
        sync_clock_to_slave(&is->extclk, &is->vidclk);
    
        is->video_current_pos = pos;
    }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    /* called to display each frame */
    
    static void video_refresh(void *opaque, double *remaining_time)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        VideoState *is = opaque;
    
        double time;
    
        if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
            check_external_clock_speed(is);
    
    
        if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
    
            time = av_gettime_relative() / 1000000.0;
    
            if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
                video_display(is);
                is->last_vis_time = time;
            }
            *remaining_time = FFMIN(*remaining_time, is->last_vis_time + rdftspeed - time);
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (is->video_st) {
    
                redisplay = pictq_prev_picture(is);
    
    retry:
    
            if (pictq_nb_remaining(is) == 0) {
    
                // nothing to do, no picture to display in the queue
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            } else {
    
                double last_duration, duration, delay;
    
                lastvp = &is->pictq[is->pictq_rindex];
                vp = &is->pictq[(is->pictq_rindex + is->pictq_rindex_shown) % VIDEO_PICTURE_QUEUE_SIZE];
    
                if (vp->serial != is->videoq.serial) {
    
                if (lastvp->serial != vp->serial && !redisplay)
    
                    is->frame_timer = av_gettime_relative() / 1000000.0;
    
                /* compute nominal last_duration */
    
                last_duration = vp_duration(is, lastvp, vp);
    
                    delay = compute_target_delay(last_duration, is);
    
                time= av_gettime_relative()/1000000.0;
    
                if (time < is->frame_timer + delay && !redisplay) {
    
                    *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
    
                    return;
    
                is->frame_timer += delay;
                if (delay > 0 && time - is->frame_timer > AV_SYNC_THRESHOLD_MAX)
                    is->frame_timer = time;
    
                SDL_LockMutex(is->pictq_mutex);
    
                if (!redisplay && !isnan(vp->pts))
    
                    update_video_pts(is, vp->pts, vp->pos, vp->serial);
    
                SDL_UnlockMutex(is->pictq_mutex);
    
                if (pictq_nb_remaining(is) > 1) {
                    VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + is->pictq_rindex_shown + 1) % VIDEO_PICTURE_QUEUE_SIZE];
    
                    duration = vp_duration(is, vp, nextvp);
    
                    if(!is->step && (redisplay || framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
                        if (!redisplay)
                            is->frame_drops_late++;
    
                        goto retry;
                    }
                }
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
                if (is->subtitle_st) {
    
                            sp = &is->subpq[is->subpq_rindex];
    
                            if (is->subpq_size > 1)
                                sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
                            else
                                sp2 = NULL;
    
    
                            if (sp->serial != is->subtitleq.serial
                                    || (is->vidclk.pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
    
                                    || (sp2 && is->vidclk.pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
    
                            {
                                free_subpicture(sp);
    
                                /* update queue size and signal for next picture */
                                if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
                                    is->subpq_rindex = 0;
    
                                SDL_LockMutex(is->subpq_mutex);
                                is->subpq_size--;
                                SDL_CondSignal(is->subpq_cond);
                                SDL_UnlockMutex(is->subpq_mutex);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                /* display picture */
    
                if (!display_disable && is->show_mode == SHOW_MODE_VIDEO)
    
                    video_display(is);
    
                pictq_next_picture(is);
    
    
                if (is->step && !is->paused)
                    stream_toggle_pause(is);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (show_status) {
            static int64_t last_time;
            int64_t cur_time;
    
            cur_time = av_gettime_relative();
    
            if (!last_time || (cur_time - last_time) >= 30000) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                aqsize = 0;
                vqsize = 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                if (is->audio_st)
                    aqsize = is->audioq.size;
                if (is->video_st)
                    vqsize = is->videoq.size;
    
                if (is->subtitle_st)
                    sqsize = is->subtitleq.size;
    
                av_diff = 0;
                if (is->audio_st && is->video_st)
    
                    av_diff = get_clock(&is->audclk) - get_clock(&is->vidclk);
    
                else if (is->video_st)
                    av_diff = get_master_clock(is) - get_clock(&is->vidclk);
                else if (is->audio_st)
                    av_diff = get_master_clock(is) - get_clock(&is->audclk);
    
                av_log(NULL, AV_LOG_INFO,
                       "%7.2f %s:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64"   \r",
    
                       get_master_clock(is),
    
                       (is->audio_st && is->video_st) ? "A-V" : (is->video_st ? "M-V" : (is->audio_st ? "M-A" : "   ")),
    
                       is->frame_drops_early + is->frame_drops_late,
    
                       aqsize / 1024,
                       vqsize / 1024,
                       sqsize,
                       is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
                       is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                fflush(stdout);
                last_time = cur_time;
            }
        }
    }
    
    /* allocate a picture (needs to do that in main thread to avoid
       potential locking problems */
    
    static void alloc_picture(VideoState *is)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        VideoPicture *vp;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        vp = &is->pictq[is->pictq_windex];
    
    
        free_picture(vp);
    
        video_open(is, 0, vp);
    
        vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
    
                                       SDL_YV12_OVERLAY,
    
        bufferdiff = vp->bmp ? FFMAX(vp->bmp->pixels[0], vp->bmp->pixels[1]) - FFMIN(vp->bmp->pixels[0], vp->bmp->pixels[1]) : 0;
    
        if (!vp->bmp || vp->bmp->pitches[0] < vp->width || bufferdiff < (int64_t)vp->height * vp->bmp->pitches[0]) {
    
            /* SDL allocates a buffer smaller than requested if the video
             * overlay hardware is unable to support the requested size. */
    
            av_log(NULL, AV_LOG_FATAL,
                   "Error: the video system does not support an image\n"
    
    Michael Niedermayer's avatar
    Michael Niedermayer committed
                            "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
    
                            "to reduce the image size.\n", vp->width, vp->height );
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        SDL_LockMutex(is->pictq_mutex);
        vp->allocated = 1;
        SDL_CondSignal(is->pictq_cond);
        SDL_UnlockMutex(is->pictq_mutex);
    }
    
    
    static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
        int i, width, height;
        Uint8 *p, *maxp;
        for (i = 0; i < 3; i++) {
            width  = bmp->w;
            height = bmp->h;
            if (i > 0) {
                width  >>= 1;
                height >>= 1;
            }
            if (bmp->pitches[i] > width) {
                maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
                for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
                    *(p+1) = *p;
            }
        }
    }
    
    
    static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, double duration, int64_t pos, int serial)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        VideoPicture *vp;
    
        printf("frame_type=%c pts=%0.3f\n",
               av_get_picture_type_char(src_frame->pict_type), pts);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* wait until we have space to put a new picture */
        SDL_LockMutex(is->pictq_mutex);
    
        while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
               !is->videoq.abort_request) {
            SDL_CondWait(is->pictq_cond, is->pictq_mutex);
        }
        SDL_UnlockMutex(is->pictq_mutex);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (is->videoq.abort_request)
            return -1;
    
        vp = &is->pictq[is->pictq_windex];
    
    
        vp->sar = src_frame->sample_aspect_ratio;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* alloc or resize hardware picture buffer */
    
        if (!vp->bmp || vp->reallocate || !vp->allocated ||
    
            vp->width  != src_frame->width ||
            vp->height != src_frame->height) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            SDL_Event event;
    
    
            vp->width = src_frame->width;
            vp->height = src_frame->height;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
            /* the allocation must be done in the main thread to avoid
    
               locking problems. */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            event.type = FF_ALLOC_EVENT;
    
            event.user.data1 = is;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            SDL_PushEvent(&event);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* wait until the picture is allocated */
            SDL_LockMutex(is->pictq_mutex);
            while (!vp->allocated && !is->videoq.abort_request) {
                SDL_CondWait(is->pictq_cond, is->pictq_mutex);
            }
    
            /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
            if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
    
                while (!vp->allocated && !is->abort_request) {
    
                    SDL_CondWait(is->pictq_cond, is->pictq_mutex);
                }
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            SDL_UnlockMutex(is->pictq_mutex);
    
            if (is->videoq.abort_request)
                return -1;
        }
    
    
        /* if the frame is not skipped, then display it */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (vp->bmp) {
    
            AVPicture pict = { { 0 } };
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* get a pointer on the bitmap */
            SDL_LockYUVOverlay (vp->bmp);
    
            pict.data[0] = vp->bmp->pixels[0];
            pict.data[1] = vp->bmp->pixels[2];
            pict.data[2] = vp->bmp->pixels[1];
    
            pict.linesize[0] = vp->bmp->pitches[0];
            pict.linesize[1] = vp->bmp->pitches[2];
            pict.linesize[2] = vp->bmp->pitches[1];
    
    
    #if CONFIG_AVFILTER
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
            // FIXME use direct rendering
    
            av_picture_copy(&pict, (AVPicture *)src_frame,
    
                            src_frame->format, vp->width, vp->height);
    
            av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
    
            is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
    
                vp->width, vp->height, src_frame->format, vp->width, vp->height,
    
                AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
    
                av_log(NULL, AV_LOG_FATAL, "Cannot initialize the conversion context\n");
    
            sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
    
                      0, vp->height, pict.data, pict.linesize);
    #endif
    
            /* workaround SDL PITCH_WORKAROUND */
            duplicate_right_border_pixels(vp->bmp);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* update the bitmap content */
            SDL_UnlockYUVOverlay(vp->bmp);
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
            /* now we can update the picture count */
            if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
                is->pictq_windex = 0;
            SDL_LockMutex(is->pictq_mutex);
            is->pictq_size++;
            SDL_UnlockMutex(is->pictq_mutex);
        }
    
    static int get_video_frame(VideoState *is, AVFrame *frame, AVPacket *pkt, int *serial)
    
        if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
    
        if (pkt->data == flush_pkt.data) {
            avcodec_flush_buffers(is->video_st->codec);
            return 0;
        }
    
        if(avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt) < 0)
    
        if (!got_picture && !pkt->data)
            is->video_finished = *serial;
    
    
        if (got_picture) {
    
            int ret = 1;
    
            if (decoder_reorder_pts == -1) {
    
                frame->pts = av_frame_get_best_effort_timestamp(frame);
    
            } else if (decoder_reorder_pts) {
    
                frame->pts = frame->pkt_pts;
    
                frame->pts = frame->pkt_dts;
    
            if (frame->pts != AV_NOPTS_VALUE)
                dpts = av_q2d(is->video_st->time_base) * frame->pts;
    
            frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
    
    
            if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
    
                if (frame->pts != AV_NOPTS_VALUE) {
                    double diff = dpts - get_master_clock(is);
                    if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD &&
                        diff - is->frame_last_filter_delay < 0 &&
                        *serial == is->vidclk.serial &&
    
                        is->frame_drops_early++;
    
                        av_frame_unref(frame);
    
            return ret;
    
        return 0;
    }
    
    #if CONFIG_AVFILTER
    
    static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
                                     AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
    {
    
        AVFilterInOut *outputs = NULL, *inputs = NULL;
    
        if (filtergraph) {
            outputs = avfilter_inout_alloc();
            inputs  = avfilter_inout_alloc();
            if (!outputs || !inputs) {
                ret = AVERROR(ENOMEM);
                goto fail;
            }
    
            outputs->name       = av_strdup("in");
            outputs->filter_ctx = source_ctx;
            outputs->pad_idx    = 0;
            outputs->next       = NULL;
    
            inputs->name        = av_strdup("out");
            inputs->filter_ctx  = sink_ctx;
            inputs->pad_idx     = 0;
            inputs->next        = NULL;
    
    
            if ((ret = avfilter_graph_parse_ptr(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
    
                goto fail;
        } else {
            if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
                goto fail;
        }
    
    
        /* Reorder the filters to ensure that inputs of the custom filters are merged first */
        for (i = 0; i < graph->nb_filters - nb_filters; i++)
            FFSWAP(AVFilterContext*, graph->filters[i], graph->filters[i + nb_filters]);
    
    
        ret = avfilter_graph_config(graph, NULL);
    
    fail:
        avfilter_inout_free(&outputs);
        avfilter_inout_free(&inputs);
        return ret;
    }
    
    
    static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame)
    
        static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
    
    Stefano Sabatini's avatar
    Stefano Sabatini committed
        char sws_flags_str[128];
    
        char buffersrc_args[256];
    
        AVFilterContext *filt_src = NULL, *filt_out = NULL, *last_filter = NULL;
    
        AVCodecContext *codec = is->video_st->codec;
    
        AVRational fr = av_guess_frame_rate(is->ic, is->video_st, NULL);
    
        av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
    
        snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
    
    Stefano Sabatini's avatar
    Stefano Sabatini committed
        graph->scale_sws_opts = av_strdup(sws_flags_str);
    
        snprintf(buffersrc_args, sizeof(buffersrc_args),
                 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
    
                 frame->width, frame->height, frame->format,
    
                 is->video_st->time_base.num, is->video_st->time_base.den,
    
                 codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
    
        if (fr.num && fr.den)
            av_strlcatf(buffersrc_args, sizeof(buffersrc_args), ":frame_rate=%d/%d", fr.num, fr.den);
    
    
        if ((ret = avfilter_graph_create_filter(&filt_src,
                                                avfilter_get_by_name("buffer"),
    
                                                "ffplay_buffer", buffersrc_args, NULL,
    
        ret = avfilter_graph_create_filter(&filt_out,
    
                                           avfilter_get_by_name("buffersink"),
    
                                           "ffplay_buffersink", NULL, NULL, graph);
    
        if ((ret = av_opt_set_int_list(filt_out, "pix_fmts", pix_fmts,  AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
            goto fail;
    
    
        last_filter = filt_out;
    
    /* Note: this macro adds a filter before the lastly added filter, so the
     * processing order of the filters is in reverse */
    #define INSERT_FILT(name, arg) do {                                         \
        AVFilterContext *filt_ctx;                                              \
                                                                                \
        ret = avfilter_graph_create_filter(&filt_ctx,                           \
                                           avfilter_get_by_name(name),          \
                                           "ffplay_" name, arg, NULL, graph);   \
        if (ret < 0)                                                            \
            goto fail;                                                          \
                                                                                \
        ret = avfilter_link(filt_ctx, 0, last_filter, 0);                       \
        if (ret < 0)                                                            \
            goto fail;                                                          \
                                                                                \
        last_filter = filt_ctx;                                                 \
    } while (0)
    
    
        /* SDL YUV code is not handling odd width/height for some driver
         * combinations, therefore we crop the picture to an even width/height. */
    
        INSERT_FILT("crop", "floor(in_w/2)*2:floor(in_h/2)*2");
    
        if (autorotate) {
            AVDictionaryEntry *rotate_tag = av_dict_get(is->video_st->metadata, "rotate", NULL, 0);
            if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0")) {
                if (!strcmp(rotate_tag->value, "90")) {
                    INSERT_FILT("transpose", "clock");
                } else if (!strcmp(rotate_tag->value, "180")) {
                    INSERT_FILT("hflip", NULL);
                    INSERT_FILT("vflip", NULL);
                } else if (!strcmp(rotate_tag->value, "270")) {
                    INSERT_FILT("transpose", "cclock");
                } else {
                    char rotate_buf[64];
                    snprintf(rotate_buf, sizeof(rotate_buf), "%s*PI/180", rotate_tag->value);
                    INSERT_FILT("rotate", rotate_buf);
                }
            }
        }
    
        if ((ret = configure_filtergraph(graph, vfilters, filt_src, last_filter)) < 0)
    
        is->in_video_filter  = filt_src;
    
        is->out_video_filter = filt_out;
    
    Marton Balint's avatar
    Marton Balint committed
    static int configure_audio_filters(VideoState *is, const char *afilters, int force_output_format)
    {
    
        static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
    
    Marton Balint's avatar
    Marton Balint committed
        int sample_rates[2] = { 0, -1 };
        int64_t channel_layouts[2] = { 0, -1 };
        int channels[2] = { 0, -1 };
        AVFilterContext *filt_asrc = NULL, *filt_asink = NULL;
    
        char aresample_swr_opts[512] = "";
        AVDictionaryEntry *e = NULL;
    
    Marton Balint's avatar
    Marton Balint committed
        char asrc_args[256];
        int ret;
    
        avfilter_graph_free(&is->agraph);
        if (!(is->agraph = avfilter_graph_alloc()))
            return AVERROR(ENOMEM);
    
    
        while ((e = av_dict_get(swr_opts, "", e, AV_DICT_IGNORE_SUFFIX)))
            av_strlcatf(aresample_swr_opts, sizeof(aresample_swr_opts), "%s=%s:", e->key, e->value);
        if (strlen(aresample_swr_opts))
            aresample_swr_opts[strlen(aresample_swr_opts)-1] = '\0';
        av_opt_set(is->agraph, "aresample_swr_opts", aresample_swr_opts, 0);
    
    
    Marton Balint's avatar
    Marton Balint committed
        ret = snprintf(asrc_args, sizeof(asrc_args),
    
                       "sample_rate=%d:sample_fmt=%s:channels=%d:time_base=%d/%d",
    
    Marton Balint's avatar
    Marton Balint committed
                       is->audio_filter_src.freq, av_get_sample_fmt_name(is->audio_filter_src.fmt),
    
                       is->audio_filter_src.channels,
                       1, is->audio_filter_src.freq);
    
    Marton Balint's avatar
    Marton Balint committed
        if (is->audio_filter_src.channel_layout)
            snprintf(asrc_args + ret, sizeof(asrc_args) - ret,
                     ":channel_layout=0x%"PRIx64,  is->audio_filter_src.channel_layout);
    
        ret = avfilter_graph_create_filter(&filt_asrc,
                                           avfilter_get_by_name("abuffer"), "ffplay_abuffer",
                                           asrc_args, NULL, is->agraph);
        if (ret < 0)
            goto end;
    
    
    
        ret = avfilter_graph_create_filter(&filt_asink,
                                           avfilter_get_by_name("abuffersink"), "ffplay_abuffersink",
                                           NULL, NULL, is->agraph);
        if (ret < 0)
            goto end;
    
        if ((ret = av_opt_set_int_list(filt_asink, "sample_fmts", sample_fmts,  AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
            goto end;
        if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
    
    Marton Balint's avatar
    Marton Balint committed
            goto end;
    
        if (force_output_format) {
            channel_layouts[0] = is->audio_tgt.channel_layout;
    
            channels       [0] = is->audio_tgt.channels;
            sample_rates   [0] = is->audio_tgt.freq;
            if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 0, AV_OPT_SEARCH_CHILDREN)) < 0)
                goto end;
            if ((ret = av_opt_set_int_list(filt_asink, "channel_layouts", channel_layouts,  -1, AV_OPT_SEARCH_CHILDREN)) < 0)
                goto end;
            if ((ret = av_opt_set_int_list(filt_asink, "channel_counts" , channels       ,  -1, AV_OPT_SEARCH_CHILDREN)) < 0)
                goto end;
            if ((ret = av_opt_set_int_list(filt_asink, "sample_rates"   , sample_rates   ,  -1, AV_OPT_SEARCH_CHILDREN)) < 0)
                goto end;
    
    Marton Balint's avatar
    Marton Balint committed
        }
    
    
        if ((ret = configure_filtergraph(is->agraph, afilters, filt_asrc, filt_asink)) < 0)
            goto end;
    
        is->in_audio_filter  = filt_asrc;
        is->out_audio_filter = filt_asink;
    
    end:
        if (ret < 0)
            avfilter_graph_free(&is->agraph);
        return ret;
    }
    
    #endif  /* CONFIG_AVFILTER */
    
    static int video_thread(void *arg)
    {
    
        AVPacket pkt = { 0 };
    
        AVFrame *frame = av_frame_alloc();
    
        AVRational tb = is->video_st->time_base;
    
        AVRational frame_rate = av_guess_frame_rate(is->ic, is->video_st, NULL);
    
    
    #if CONFIG_AVFILTER
        AVFilterGraph *graph = avfilter_graph_alloc();
    
        AVFilterContext *filt_out = NULL, *filt_in = NULL;
    
        enum AVPixelFormat last_format = -2;
    
        int last_serial = -1;
    
    Aneesh Dogra's avatar
    Aneesh Dogra committed
        for (;;) {
    
            while (is->paused && !is->videoq.abort_request)
                SDL_Delay(10);
    
            ret = get_video_frame(is, frame, &pkt, &serial);
    
            if (ret < 0)
                goto the_end;
            if (!ret)
                continue;
    
    
    #if CONFIG_AVFILTER
    
            if (   last_w != frame->width
                || last_h != frame->height
    
                || last_format != frame->format
    
                || last_serial != serial
                || last_vfilter_idx != is->vfilter_idx) {
    
                av_log(NULL, AV_LOG_DEBUG,
    
                       "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
                       last_w, last_h,
                       (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
                       frame->width, frame->height,
                       (const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), serial);
    
                avfilter_graph_free(&graph);
                graph = avfilter_graph_alloc();
    
                if ((ret = configure_video_filters(graph, is, vfilters_list ? vfilters_list[is->vfilter_idx] : NULL, frame)) < 0) {
    
                    SDL_Event event;
                    event.type = FF_QUIT_EVENT;
                    event.user.data1 = is;
                    SDL_PushEvent(&event);