"FFmpeg learning" uses SDL2.0 to display ffmpeg decoded data

Source: Internet
Author: User
Tags min


According to the code in the study found that the author used SDL1.2 is outdated, the SDL official website under the SDL2.0 version,

found that the Sdl_setvideomode function and the sdl_overlay structure within the tutorial02 have been deprecated,

After a Google and SDL demo study, the following code can be compiled

TUTORIAL02.C//A pedagogical video player that would stream through every video frame as fast as it can.
This tutorial is written by Stephen Dranger (dranger@gmail.com). Code based on Ffplay, Copyright (c) 2003 Fabrice Bellard,//And a tutorial by Martin Bohme (Boehme@inb.uni-luebeckr emovethis.de)//Tested on Gentoo, CVS version 5/01/07 compiled with GCC 4.1.1///use of the Makefile to build all example

S.////Run using//TUTORIAL02 Myvideofile.mpg////To play the video stream on your screen.

#include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> #include <SDL2/SDL.h> #include <SDL2/SDL_thread.h> #ifdef __mingw32__ #undef Main/* Prevents SDL from Overri Ding Main () */#endif #include <stdio.h> int randomint (int min, int max) {return min + rand ()% (Max-min +
    } int main (int argc, char *argv[]) {Avformatcontext *pformatctx = NULL; int I, videostream;
    Avcodeccontext *pcodecctx = NULL;
    Avcodec *pcodec = NULL;
    Avframe *pframe = NULL;
    Avpacket packet;
    int framefinished;
    float Aspect_ratio;
    Avdictionary *optionsdict = NULL;
    struct Swscontext *sws_ctx = NULL;
    Sdl_createtexture ();
    Sdl_texture *bmp = NULL;
    Sdl_window *screen = NULL;
    Sdl_rect Rect;
    Sdl_event Event;
        if (ARGC < 2) {fprintf (stderr, "Usage:test <file>\n");
    Exit (1);
    }//Register All formats and codecs av_register_all (); if (Sdl_init (Sdl_init_video | Sdl_init_audio |
        Sdl_init_timer) {fprintf (stderr, "Could not initialize SDL-%s\n", Sdl_geterror ());
    Exit (1); }//Open video file if (Avformat_open_input (&pformatctx, argv[1], NULL, NULL)!=0) return-1; Couldn ' t open File//Retrieve stream information if (Avformat_find_stream_info (PformaTctx, NULL) <0) return-1; Couldn ' t find stream information//Dump information about file onto standard error Av_dump_format (Pformat
    CTX, 0, argv[1], 0);
    Find the first video stream videostream=-1; for (i=0; i<pformatctx->nb_streams; i++) if (pformatctx->streams[i]->codec->codec_type==avmedia_type
            _video) {videostream=i;
        Break } if (videostream==-1) return-1; Didn ' t find a video stream//Get A pointer to the codec context for the video stream Pcodecctx=pformatctx
    Find the decoder for the video stream Pcodec=avcodec_find_decoder (pcodecctx->codec_id);
        if (pcodec==null) {fprintf (stderr, "Unsupported codec!\n"); return-1; Codec not Found}//Open Codec if (Avcodec_open2 (Pcodecctx, Pcodec, &optionsdict) <0) re turn-1; Could not open codec//AlLocate video frame pframe=avcodec_alloc_frame ();
    avframe* PFRAMEYUV = Avcodec_alloc_frame ();
    if (PFRAMEYUV = = NULL) return-1; Make a screens to put our Videe//#ifndef __darwin__//Screen = Sdl_setvideomode (Pcodecctx->width, pcodecctx->
Height, 0, 0);
#else//Screen = Sdl_setvideomode (Pcodecctx->width, Pcodecctx->height, 24, 0);
#endif//Sdl_wm_setcaption ("My game Window", "game"); Sdl_surface *screen = Sdl_setvideomode (640, 480, 0, Sdl_fullscreen |
                              Screen = Sdl_createwindow ("My Game Window", sdl_windowpos_undefined,
                              sdl_windowpos_undefined, Pcodecctx->width, Pcodecctx->height, Sdl_window_fullscreen |
    Sdl_renderer *renderer = sdl_createrenderer (screen,-1, 0); if (!screen) {fprintf (stderr, "Sdl:could not set video mode-exiting\ n ");
    Exit (1);                                }//Allocate a place to put our YUV image on the screen//bmp = Sdl_createyuvoverlay (pcodecctx->width,//                               Pcodecctx->height,//Sdl_yv12_overlay,//
    screen); BMP = Sdl_createtexture (renderer,sdl_pixelformat_yv12,sdl_textureaccess_streaming,pcodecctx->width,pcodecctx-
    Sdl_settextureblendmode (Bmp,sdl_blendmode_blend); Sws_ctx = Sws_getcontext (Pcodecctx->width, Pcodecctx->height, PCODECCTX-&GT;PIX_FMT, p
     Codecctx->width, Pcodecctx->height, pix_fmt_yuv420p, sws_bilinear, NULL, NULL, NULL
    ); int numbytes = Avpicture_get_size (pix_fmt_yuv420p, Pcodecctx->width, Pcodecctx->he
    uint8_t* buffer = (uint8_t *) Av_malloc (numbytes*sizeof (uint8_t)); Avpicture_fill ((Avpicture *) PFRAMEYUV, BuffEr, pix_fmt_yuv420p, pcodecctx->width, pcodecctx->height);
    Read frames and save first five frames to disk i=0;
    rect.x = 0;
    Rect.y = 0;
    RECT.W = pcodecctx->width;
    Rect.h = pcodecctx->height;
        while (Av_read_frame (Pformatctx, &packet) >=0) {//are this a packet from the video stream? if (Packet.stream_index==videostream) {//Decode video frame Avcodec_decode_video2 (Pcodecctx, Pfra
            Me, &framefinished, &packet);
            Did we get a video frame?  if (framefinished) {Sws_scale (SWS_CTX, (uint8_t const * Const *) pframe->data, pframe->linesize, 0, Pcodecctx->heigh
                T, Pframeyuv->data, pframeyuv->linesize);Ipitch calculates the number of bytes in a YUV row of data sdl_updatetexture (BMP, &rect, pframeyuv->data[0], pframeyuv->linesize
                Sdl_renderclear (renderer);
                Sdl_rendercopy (renderer, BMP, &rect, &rect);
            Sdl_renderpresent (renderer);
            } sdl_delay (50);
        Sleep (500);
        }//Free the packet, is allocated by Av_read_frame Av_free_packet (&packet);
        Sdl_pollevent (&event);
                Switch (event.type) {case sdl_quit:sdl_quit ();
                Exit (0);
    }} sdl_destroytexture (BMP);
    Free the YUV frame av_free (pframe);
    Av_free (PFRAMEYUV);
    Close the codec avcodec_close (PCODECCTX);
    Close the video file Avformat_close_input (&AMP;PFORMATCTX);
return 0;


Reference website:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.