One, 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"); &nbSp; 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&Nbsp;= 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], &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PFRAMEYUV->DATA[2],&NBSP;PFRAMEYUV->LINESIZE[2]); sdl_renderclear (SdlRenderer); sdl_rendercopy (Sdlrenderer, sdltexture, null, &sdlrect); &nbSp; sdl_renderpresent (Sdlrenderer); sdl_delay (+); } } 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;}
Implementing Linux player with FFmpeg and SDL2