Guest User

Untitled

a guest
Jul 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. AVFrame* open_image(const char* imageFileName, int width, int height, long * bufSize)
  2. {
  3.     AVFormatContext *pFormatCtx;
  4.  
  5.     if(av_open_input_file(&pFormatCtx, imageFileName, NULL, 0, NULL)!=0)
  6.     {
  7.         printf("Can't open image file '%s'\n", imageFileName);
  8.         return NULL;
  9.     }      
  10.  
  11.     AVCodecContext *pCodecCtx;
  12.  
  13.     pCodecCtx = pFormatCtx->streams[0]->codec;
  14.     pCodecCtx->width = width;
  15.     pCodecCtx->height = height;
  16.     pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
  17.  
  18.     // Find the decoder for the video stream
  19.     AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
  20.     if (!pCodec)
  21.     {
  22.         printf("Codec not found\n");
  23.         return NULL;
  24.     }
  25.  
  26.     // Open codec
  27.     if(avcodec_open(pCodecCtx, pCodec)<0)
  28.     {
  29.         printf("Could not open codec\n");
  30.         return NULL;
  31.     }
  32.  
  33.     AVFrame *pFrame = avcodec_alloc_frame();
  34.     if (!pFrame)
  35.     {
  36.         LOGV(TAG, "Can't allocate memory for AVFrame\n");
  37.         return NULL;
  38.     }
  39.  
  40.     int frameFinished;
  41.     int numBytes;
  42.  
  43.     // Determine required buffer size and allocate buffer
  44.     numBytes = avpicture_get_size(PIX_FMT_YUVJ420P, pCodecCtx->width, pCodecCtx->height);
  45.  
  46.     // ***
  47.     *bufSize = numBytes;
  48.     // ***
  49.  
  50.     uint8_t *buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
  51.  
  52.     avpicture_fill((AVPicture *) pFrame, buffer, PIX_FMT_YUVJ420P, pCodecCtx->width, pCodecCtx->height);
  53.  
  54.     // Read frame
  55.  
  56.     AVPacket packet;
  57.  
  58.     int framesNumber = 0;
  59.     while (av_read_frame(pFormatCtx, &packet) >= 0)
  60.     {
  61.         if(packet.stream_index != 0)
  62.             continue;
  63.  
  64.         int ret = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
  65.         if (ret > 0)
  66.         {
  67.             sprintf(buf, "Frame is decoded, size %d", ret);
  68.             LOGV(TAG, buf);
  69.             pFrame->quality = 4;
  70.             return pFrame;
  71.         }
  72.         else {
  73.             // printf("Error [%d] while decoding frame: %s\n", ret, strerror(AVERROR(ret)));
  74.             sprintf(buf, "Error %d decoding frame: %s", ret, strerror(AVERROR(ret)));
  75.             LOGV(TAG, buf);
  76.         }
  77.     }
  78. }
Add Comment
Please, Sign In to add comment