⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ffplay.c

📁 ffmpeg源码分析
💻 C
📖 第 1 页 / 共 5 页
字号:
        fprintf(stderr, "Year: %d\n", s->year);    if (s->genre[0] != '\0')        fprintf(stderr, "Genre: %s\n", s->genre);}/* since we have only one decoding thread, we can use a global   variable instead of a thread local variable */static VideoState *global_video_state;static int decode_interrupt_cb(void){    return (global_video_state && global_video_state->abort_request);}/* this thread gets the stream from the disk or the network */static int decode_thread(void *arg){    VideoState *is = arg;    AVFormatContext *ic;    int err, i, ret, video_index, audio_index, use_play;    AVPacket pkt1, *pkt = &pkt1;    AVFormatParameters params, *ap = &params;    video_index = -1;    audio_index = -1;    is->video_stream = -1;    is->audio_stream = -1;    is->subtitle_stream = -1;    global_video_state = is;    url_set_interrupt_cb(decode_interrupt_cb);    memset(ap, 0, sizeof(*ap));    ap->image_format = image_format;    ap->initial_pause = 1; /* we force a pause when starting an RTSP                              stream */    err = av_open_input_file(&ic, is->filename, is->iformat, 0, ap);    if (err < 0) {        print_error(is->filename, err);        ret = -1;        goto fail;    }    is->ic = ic;#ifdef CONFIG_NETWORK    use_play = (ic->iformat == &rtsp_demux);#else    use_play = 0;#endif    if(genpts)        ic->flags |= AVFMT_FLAG_GENPTS;    if (!use_play) {        err = av_find_stream_info(ic);        if (err < 0) {            fprintf(stderr, "%s: could not find codec parameters\n", is->filename);            ret = -1;            goto fail;        }        ic->pb.eof_reached= 0; //FIXME hack, ffplay maybe shouldnt use url_feof() to test for the end    }    /* 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 = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);        if (ret < 0) {            fprintf(stderr, "%s: could not seek to position %0.3f\n",                    is->filename, (double)timestamp / AV_TIME_BASE);        }    }    /* now we can begin to play (RTSP stream only) */    av_read_play(ic);    if (use_play) {        err = av_find_stream_info(ic);        if (err < 0) {            fprintf(stderr, "%s: could not find codec parameters\n", is->filename);            ret = -1;            goto fail;        }    }    for(i = 0; i < ic->nb_streams; i++) {        AVCodecContext *enc = ic->streams[i]->codec;        switch(enc->codec_type) {        case CODEC_TYPE_AUDIO:            if (audio_index < 0 && !audio_disable)                audio_index = i;            break;        case CODEC_TYPE_VIDEO:            if (video_index < 0 && !video_disable)                video_index = i;            break;        default:            break;        }    }    if (show_status) {        dump_format(ic, 0, is->filename, 0);        dump_stream_info(ic);    }    /* open the streams */    if (audio_index >= 0) {        stream_component_open(is, audio_index);    }    if (video_index >= 0) {        stream_component_open(is, video_index);    } else {        if (!display_disable)            is->show_audio = 1;    }    if (is->video_stream < 0 && is->audio_stream < 0) {        fprintf(stderr, "%s: could not open codecs\n", is->filename);        ret = -1;        goto fail;    }    for(;;) {        if (is->abort_request)            break;#ifdef CONFIG_NETWORK        if (is->paused != is->last_paused) {            is->last_paused = is->paused;            if (is->paused)                av_read_pause(ic);            else                av_read_play(ic);        }        if (is->paused && ic->iformat == &rtsp_demux) {            /* wait 10 ms to avoid trying to get another packet */            /* XXX: horrible */            SDL_Delay(10);            continue;        }#endif        if (is->seek_req) {            /* XXX: must lock decoder threads */            SDL_LockMutex(is->video_decoder_mutex);            SDL_LockMutex(is->audio_decoder_mutex);            SDL_LockMutex(is->subtitle_decoder_mutex);            ret = av_seek_frame(is->ic, -1, is->seek_pos, is->seek_flags);            if (ret < 0) {                fprintf(stderr, "%s: error while seeking\n", is->ic->filename);            }else{                if (is->audio_stream >= 0) {                    packet_queue_flush(&is->audioq);                }                if (is->subtitle_stream >= 0) {                    packet_queue_flush(&is->subtitleq);                }                if (is->video_stream >= 0) {                    packet_queue_flush(&is->videoq);                    avcodec_flush_buffers(ic->streams[video_index]->codec);                }            }            SDL_UnlockMutex(is->subtitle_decoder_mutex);            SDL_UnlockMutex(is->audio_decoder_mutex);            SDL_UnlockMutex(is->video_decoder_mutex);            is->seek_req = 0;        }        /* if the queue are full, no need to read more */        if (is->audioq.size > MAX_AUDIOQ_SIZE ||            is->videoq.size > MAX_VIDEOQ_SIZE ||            is->subtitleq.size > MAX_SUBTITLEQ_SIZE ||            url_feof(&ic->pb)) {            /* wait 10 ms */            SDL_Delay(10);            continue;        }        ret = av_read_frame(ic, pkt);        if (ret < 0) {            if (url_ferror(&ic->pb) == 0) {                SDL_Delay(100); /* wait for user event */                continue;            } else                break;        }        if (pkt->stream_index == is->audio_stream) {            packet_queue_put(&is->audioq, pkt);        } else if (pkt->stream_index == is->video_stream) {            packet_queue_put(&is->videoq, pkt);        } else if (pkt->stream_index == is->subtitle_stream) {            packet_queue_put(&is->subtitleq, pkt);        } else {            av_free_packet(pkt);        }    }    /* wait until the end */    while (!is->abort_request) {        SDL_Delay(100);    }    ret = 0; fail:    /* disable interrupting */    global_video_state = NULL;    /* 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);    if (is->ic) {        av_close_input_file(is->ic);        is->ic = NULL; /* safety */    }    url_set_interrupt_cb(NULL);    if (ret != 0) {        SDL_Event event;        event.type = FF_QUIT_EVENT;        event.user.data1 = is;        SDL_PushEvent(&event);    }    return 0;}static VideoState *stream_open(const char *filename, AVInputFormat *iformat){    VideoState *is;    is = av_mallocz(sizeof(VideoState));    if (!is)        return NULL;    pstrcpy(is->filename, sizeof(is->filename), filename);    is->iformat = iformat;    if (screen) {        is->width = screen->w;        is->height = screen->h;    }    is->ytop = 0;    is->xleft = 0;    /* start video display */    is->pictq_mutex = SDL_CreateMutex();    is->pictq_cond = SDL_CreateCond();    is->subpq_mutex = SDL_CreateMutex();    is->subpq_cond = SDL_CreateCond();    is->subtitle_decoder_mutex = SDL_CreateMutex();    is->audio_decoder_mutex = SDL_CreateMutex();    is->video_decoder_mutex = SDL_CreateMutex();    /* add the refresh timer to draw the picture */    schedule_refresh(is, 40);    is->av_sync_type = av_sync_type;    is->parse_tid = SDL_CreateThread(decode_thread, is);    if (!is->parse_tid) {        av_free(is);        return NULL;    }    return is;}static void stream_close(VideoState *is){    VideoPicture *vp;    int i;    /* XXX: use a special url_shutdown call to abort parse cleanly */    is->abort_request = 1;    SDL_WaitThread(is->parse_tid, NULL);    /* free all pictures */    for(i=0;i<VIDEO_PICTURE_QUEUE_SIZE; i++) {        vp = &is->pictq[i];        if (vp->bmp) {            SDL_FreeYUVOverlay(vp->bmp);            vp->bmp = NULL;        }    }    SDL_DestroyMutex(is->pictq_mutex);    SDL_DestroyCond(is->pictq_cond);    SDL_DestroyMutex(is->subpq_mutex);    SDL_DestroyCond(is->subpq_cond);    SDL_DestroyMutex(is->subtitle_decoder_mutex);    SDL_DestroyMutex(is->audio_decoder_mutex);    SDL_DestroyMutex(is->video_decoder_mutex);}void stream_cycle_channel(VideoState *is, int codec_type){    AVFormatContext *ic = is->ic;    int start_index, stream_index;    AVStream *st;    if (codec_type == CODEC_TYPE_VIDEO)        start_index = is->video_stream;    else if (codec_type == CODEC_TYPE_AUDIO)        start_index = is->audio_stream;    else        start_index = is->subtitle_stream;    if (start_index < (codec_type == CODEC_TYPE_SUBTITLE ? -1 : 0))        return;    stream_index = start_index;    for(;;) {        if (++stream_index >= is->ic->nb_streams)        {            if (codec_type == CODEC_TYPE_SUBTITLE)            {                stream_index = -1;                goto the_end;            } else                stream_index = 0;        }        if (stream_index == start_index)            return;        st = ic->streams[stream_index];        if (st->codec->codec_type == codec_type) {            /* check that parameters are OK */            switch(codec_type) {            case CODEC_TYPE_AUDIO:                if (st->codec->sample_rate != 0 &&                    st->codec->channels != 0)                    goto the_end;                break;            case CODEC_TYPE_VIDEO:            case CODEC_TYPE_SUBTITLE:                goto the_end;            default:                break;            }        }    } the_end:    stream_component_close(is, start_index);    stream_component_open(is, stream_index);}void toggle_full_screen(void){    int w, h, flags;    is_full_screen = !is_full_screen;    if (!fs_screen_width) {        /* use default SDL method */        SDL_WM_ToggleFullScreen(screen);    } else {        /* use the recorded resolution */        flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;        if (is_full_screen) {            w = fs_screen_width;            h = fs_screen_height;            flags |= SDL_FULLSCREEN;        } else {            w = screen_width;            h = screen_height;            flags |= SDL_RESIZABLE;        }        screen = SDL_SetVideoMode(w, h, 0, flags);        cur_stream->width = w;        cur_stream->height = h;    }}void toggle_pause(void){    if (cur_stream)        stream_pause(cur_stream);    step = 0;}void step_to_next_frame(void){    if (cur_stream) {        if (cur_stream->paused)            cur_stream->paused=0;        cur_stream->video_current_pts = get_video_clock(cur_stream);    }    step = 1;}void do_exit(void){    if (cur_stream) {        stream_close(cur_stream);        cur_stream = NULL;    }    if (show_status)        printf("\n");    SDL_Quit();    exit(0);}void toggle_audio_display(void){    if (cur_stream) {        cur_stream->show_audio = !cur_stream->show_audio;    }}/* handle an event sent by the GUI */void event_loop(void){    SDL_Event event;    double incr, pos, frac;    for(;;) {        SDL_WaitEvent(&event);        switch(event.type) {        case SDL_KEYDOWN:            switch(event.key.keysym.sym) {            case SDLK_ESCAPE:            case SDLK_q:                do_exit();                break;            case SDLK_f:                toggle_full_screen();                break;            case SDLK_p:            case SDLK_SPACE:                toggle_pause();                break;            case SDLK_s: //S: Step to next frame                step_to_next_frame();                break;            case SDLK_a:                if (cur_stream)                    stream_cycle_channel(cur_stream, CODEC_TYPE_AUDIO);                break;            case SDLK_v:                if (cur_stream)                    stream_cycle_channel(cur_stream, CODE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -