Video osztály ffmpeg-el.

Video osztály ffmpeg-el.
2013-09-12T17:09:54+02:00
2013-09-12T17:28:20+02:00
2022-10-15T23:15:43+02:00
jozsi111
Sziasztok.
Abban kérném a segítségeteket, hátha valaki látja hogy mi a hiba az alábbi kódba. OGV videót akarok lejátszani amit folyamatosan játszik. A probléma az hogy a memória használat folyamatosan nő. Próbáltam valgrind-al követni hogy mi lehet a hiba de sajnos nem jutottam előrébb.

video.h
#ifndef VIDEO_H #define VIDEO_H #include "SDL2/SDL.h" #include <string> #ifndef INT64_C #define INT64_C(c) (c ## LL) #define UINT64_C(c) (c ## ULL) #endif extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> } class video { public: video(std::string filename); ~video(); void play(); void playonce(); void stop(); bool getrunning(); private: SDL_Rect pos; int x; int y; std::string filename; bool running; AVFormatContext *pFormatCtx; AVCodecContext *pCodecCtx; AVCodec *pCodec; AVPacket packet; AVFrame *pFrame; int frameDecoded; AVFrame *pFrame_YUV420P; uint8_t *buffer_YUV420P; struct SwsContext *pConvertCtx_YUV420P; uint8_t *pixels1; int pitch1, size1; }; #endif // VIDEO_H

video.cpp

#include "video.h" #include <string.h> #include "globals.h" extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> } video::video(std::string filename) { av_register_all(); avcodec_register_all(); this->filename = filename; running = false; } video::~video() { } void video::play() { if (!running) { pFormatCtx = NULL; while(avformat_open_input(&pFormatCtx, filename.c_str(), NULL, NULL) != 0) { printf("Could not open the video file\nRetrying...\n"); } pos.x = 0; pos.y = 0; pos.w = 1024; pos.h = 768; avformat_find_stream_info(pFormatCtx, NULL); av_dump_format(pFormatCtx, 0, filename.c_str(), 0); pCodecCtx = pFormatCtx->streams[1]->codec; pCodec = avcodec_find_decoder(AV_CODEC_ID_THEORA); avcodec_open2(pCodecCtx, pCodec, NULL); pFrame_YUV420P = avcodec_alloc_frame(); if(pFrame_YUV420P == NULL) { fprintf(stderr, "Could not allocate pFrame_YUV420P\n"); } buffer_YUV420P = (uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P,pCodecCtx->width, pCodecCtx->height)); avpicture_fill((AVPicture *)pFrame_YUV420P, buffer_YUV420P,PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height); pConvertCtx_YUV420P = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P,SWS_SPLINE, NULL, NULL, NULL); size1 = pCodecCtx->width * pCodecCtx->height; running = true; } SDL_Texture *bmpTex1 = SDL_CreateTexture(RENDER, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height); pFrame = avcodec_alloc_frame(); if(av_read_frame(pFormatCtx, &packet)<0) { fprintf(stderr, "Could not read frame!\n"); stop(); return; } if(avcodec_decode_video2(pCodecCtx, pFrame, &frameDecoded, &packet) < 0) { fprintf(stderr, "Could not decode frame!\n"); } if (frameDecoded) { sws_scale(pConvertCtx_YUV420P, (const uint8_t * const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrame_YUV420P->data, pFrame_YUV420P->linesize); SDL_LockTexture(bmpTex1, NULL, (void **)&pixels1, &pitch1); memcpy(pixels1, pFrame_YUV420P->data[0], size1 ); memcpy(pixels1 + size1, pFrame_YUV420P->data[2], size1/4); memcpy(pixels1 + size1*5/4, pFrame_YUV420P->data[1], size1/4); SDL_UnlockTexture(bmpTex1); SDL_UpdateTexture(bmpTex1, NULL, pixels1, pitch1); SDL_RenderCopy(RENDER, bmpTex1, NULL, &pos); av_free_packet(&packet); SDL_DestroyTexture(bmpTex1); } else { av_free_packet(&packet); SDL_DestroyTexture(bmpTex1); } } void video::playonce() { } void video::stop() { running = false; av_free(pFrame_YUV420P); av_free(buffer_YUV420P); sws_freeContext(pConvertCtx_YUV420P); avcodec_close(pFormatCtx->streams[1]->codec); avformat_close_input( &pFormatCtx ); } bool video::getrunning() { return running; }
Mutasd a teljes hozzászólást!


//Nem hiányzik? av_free(pFrame); if (frameDecoded) { // itt ? } else { // és itt ? }
Mutasd a teljes hozzászólást!

  • Amit kifelejtettem.
    globals.h

    #ifndef _GLOBALS #define _GLOBALS #include "SDL2/SDL.h" extern SDL_Window *SCREEN; extern SDL_Renderer *RENDER; extern double ROTATION; extern int SCREENWIDTH; extern int SCREENHEIGHT; extern float FPS; extern float SCREENTIME; #endif

    globals.cpp

    #include "globals.h" #include "SDL2/SDL_render.h" using namespace std; SDL_Window *SCREEN = NULL; SDL_Renderer *RENDER = NULL;

    screen.cpp

    if (SDL_Init(SDL_INIT_VIDEO)<0) { printf("SDL ERROR:%s\n",SDL_GetError()); } SCREEN = SDL_CreateWindow("Screen",0,0,1024,768,SDL_WINDOW_BORDERLESS); if (SCREEN == NULL) { printf("SDL WINDOW:%s\n",SDL_GetError()); } screenstate = true; last_sence = 0; RENDER = SDL_CreateRenderer(SCREEN,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC); if (RENDER == NULL) { printf("SDL RENDERER:%s\n",SDL_GetError()); }
    Mutasd a teljes hozzászólást!
  • Köszönöm.!!!!!
    Mutasd a teljes hozzászólást!
Tetszett amit olvastál? Szeretnél a jövőben is értesülni a hasonló érdekességekről?
abcd