首页 > 系统服务 > 详细

用ffmpeg和SDL2实现linux player

时间:2018-02-19 11:14:06      阅读:309      评论:0      收藏:0      [点我收藏+]
一、version 1

#include <stdio.h>
#include <libavcodec/avcodec.h>  
#include <libavformat/avformat.h>  
#include <libswscale/swscale.h> 
#include <libavutil/imgutils.h> 
#include <SDL2/SDL.h>

int main(int argc, char *argv[])
{
    char filePath[] = "/home/lvzg/Videos/a.ts";
    int videoIndex = -1, i;
    int ret;
    int gotPicture;

    struct SwsContext *swsConvertCtx;
    AVFormatContext *pFormatCtx;
    AVCodecContext  *pCodecCtx;
    AVCodec         *pCodec;
    AVFrame         *pFrame,*pFrameYUV;
    AVPacket        *packet;
    unsigned char   *outBuffer;
    unsigned int     outBufferSize;
 
    SDL_Window      *sdlScreen;
    SDL_Renderer    *sdlRenderer;
    SDL_Texture     *sdlTexture;
    SDL_Rect         sdlRect;

    av_register_all();
 avformat_network_init();  
    pFormatCtx = avformat_alloc_context();

    if(avformat_open_input(&pFormatCtx, filePath, NULL, NULL) != 0){  
        printf("Couldn't open input stream.\n");
        return -1;  
    }

    if(avformat_find_stream_info(pFormatCtx,NULL) < 0){  
        printf("Couldn't find stream information.\n");  
        return -1;  
    }

    for(i = 0; i < pFormatCtx->nb_streams; i++){
        if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){
            videoIndex = i;
            break;
        }
    }
    if(videoIndex == -1){
        printf("Didn't find a video stream.\n");  
        return -1; 
    }

    pCodecCtx = pFormatCtx->streams[videoIndex]->codec;
    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    if(pCodec == NULL){
        printf("Codec not found.\n");  
        return -1;
    }

    if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0){  
        printf("Could not open codec.\n");  
        return -1;  
    }

    pFrame = av_frame_alloc();
    pFrameYUV = av_frame_alloc();

    outBufferSize = avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
    outBuffer = (unsigned char *)av_malloc(outBufferSize);
    avpicture_fill((AVPicture *)pFrameYUV, outBuffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
 packet = (AVPacket*)av_malloc(sizeof(AVPacket));

 swsConvertCtx = sws_getContext(
        pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
        pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P,
        SWS_BICUBIC, NULL, NULL, NULL);
    if(swsConvertCtx == NULL){
        goto AV_CLEAN;
    }
    
    // SDL init
    if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)){  
        printf( "Could not initialize SDL - %s\n", SDL_GetError());
        return -1;
    }

    sdlScreen = SDL_CreateWindow("Linux Media Player",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  pCodecCtx->width, pCodecCtx->height,  
        SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
    if(!sdlScreen) {  
        printf("SDL_CreateWindow failed.\n");  
        return -1;
    }

    sdlRenderer = SDL_CreateRenderer(sdlScreen, -1, 0);
    sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_YV12,
        SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height);

 sdlRect.x = 0;
    sdlRect.y = 0;
    sdlRect.w = pCodecCtx->width;
    sdlRect.h = pCodecCtx->height;

    while(av_read_frame(pFormatCtx, packet) >= 0){
        if(packet->stream_index == videoIndex){
            ret = avcodec_decode_video2(pCodecCtx, pFrame, &gotPicture, packet);
            if(ret < 0){
                printf("decode error\n");
                return -1;
            }
            if(gotPicture){
                sws_scale(swsConvertCtx, 
                    (const unsigned char * const *)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
                    (const unsigned char * const *)pFrameYUV->data, pFrameYUV->linesize);

                SDL_UpdateYUVTexture(sdlTexture, &sdlRect,
                    pFrameYUV->data[0], pFrameYUV->linesize[0],
                    pFrameYUV->data[1], pFrameYUV->linesize[1],
                    pFrameYUV->data[2], pFrameYUV->linesize[2]);
                SDL_RenderClear(sdlRenderer);
                SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect);
                SDL_RenderPresent(sdlRenderer);
                SDL_Delay(40);
            }
        }
        av_free_packet(packet);
    }
    
    sws_freeContext(swsConvertCtx);
    SDL_DestroyTexture(sdlTexture);
 SDL_Quit();
    
AV_CLEAN: 
    av_frame_free(&pFrameYUV);  
 av_frame_free(&pFrame);
    avcodec_close(pCodecCtx);
    avformat_close_input(&pFormatCtx);
    
    return 0;
}


用ffmpeg和SDL2实现linux player

原文:http://blog.51cto.com/lvzg2005/2071865

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!