Skip to content

AVFrame

AVFrame to IplImage

IplImage * createRgb24IplImage(AVFrame const * frame)
{
    int width = frame->width;
    int height = frame->height;

    AVPixelFormat src_format = static_cast<AVPixelFormat>(frame->format);
    AVPixelFormat dest_format = AV_PIX_FMT_BGR24;

    int flags = SWS_BILINEAR;
    SwsFilter * src_filter = nullptr;
    SwsFilter * dest_filter = nullptr;
    double const * param = nullptr;

    struct SwsContext * scaler = sws_getContext(width, height, src_format
            , width, height, dest_format, flags, src_filter, dest_filter, param);

    if (scaler == nullptr) {
        return nullptr;
    }

    IplImage * image = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, _RGB_CHANNEL_SIZE);
    int linesize[4] = { image->widthStep, 0, 0, 0 };

    sws_scale(scaler, frame->data, frame->linesize, 0, frame->height
            , (uint8_t **) & (image->imageData), linesize);

    return image;
}

Jpeg file to AVFrame

/// @brief Jpeg file to AVFrame.
AVFrame * getJpegToFrame(const char* image_path, int frame_width,
        int frame_height) {
    if (image_path == NULL || strlen(image_path) <= 0) {
        return NULL;
    }

    AVFormatContext * format_context = NULL;
    int result_code = 0;

    // Undefined reference to av_open_input_file()
    result_code = avformat_open_input(&format_context, image_path, NULL, NULL);
    if (result_code != 0) {
        DLog("Can't open image file(%s), Error code is %d.", image_path, result_code);
        av_ceil_log2_c()
        return NULL ;
    }

    // Undefined reference to dump_format()
    av_dump_format(format_context, 0, image_path, FALSE);

    AVCodecContext * image_codec_context = NULL;

    image_codec_context = format_context->streams[0]->codec;
    image_codec_context->width = frame_width;
    image_codec_context->height = frame_height;
    image_codec_context->pix_fmt = PIX_FMT_YUV420P;

    // Find the decoder for the video stream.
    AVCodec * image_codec = avcodec_find_decoder(image_codec_context->codec_id);
    if (image_codec == NULL) {
        DLog("Not found codec.");
        return NULL ;
    }

    // Open codec.
    if (avcodec_open(image_codec_context, image_codec) < 0) {
        DLog("Could not open codec.");
        return NULL ;
    }

    AVFrame * result_frame = avcodec_alloc_frame();

    if (result_frame == NULL) {
        DLog("Can't allocate memory for AVFrame.");
        return NULL ;
    }

    int frameFinished = 0;
    int numBytes = 0;

    // Determine required buffer size and allocate buffer
    numBytes = avpicture_get_size(PIX_FMT_YUVJ420P, image_codec_context->width,
            image_codec_context->height);
    uint8_t * buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));

    avpicture_fill((AVPicture *) result_frame, buffer, PIX_FMT_YUVJ420P,
            image_codec_context->width, image_codec_context->height);

    // Read frame.

    AVPacket packet;

    int framesNumber = 0;
    while (av_read_frame(format_context, &packet) >= 0) {
        if (packet.stream_index != 0) {
            continue;
        }

        int ret = avcodec_decode_video2(image_codec_context, result_frame,
                &frameFinished, &packet);

        if (ret > 0) {
            DLog("Frame is decoded, size %d.", ret);
            result_frame->quality = 4;
            return result_frame;
        } else {
            DLog("Error [%d] while decoding frame %s", ret, strerror(AVERROR(ret)));
        }
    }
}

Open JPEG

FFmpeg avcodec_encode_video() and JPEG images.

AVFrame* open_image(const char* imageFileName, int width, int height, long * bufSize)
{
    AVFormatContext *pFormatCtx;

    if(av_open_input_file(&pFormatCtx, imageFileName, NULL, 0, NULL)!=0)
    {
        printf("Can't open image file '%s'\n", imageFileName);
        return NULL;
    }       

    AVCodecContext *pCodecCtx;

    pCodecCtx = pFormatCtx->streams[0]->codec;
    pCodecCtx->width = width;
    pCodecCtx->height = height;
    pCodecCtx->pix_fmt = PIX_FMT_YUV420P;

    // Find the decoder for the video stream
    AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    if (!pCodec)
    {
        printf("Codec not found\n");
        return NULL;
    }

    // Open codec
    if(avcodec_open(pCodecCtx, pCodec)<0)
    {
        printf("Could not open codec\n");
        return NULL;
    }

    AVFrame *pFrame = avcodec_alloc_frame();
    if (!pFrame)
    {
        LOGV(TAG, "Can't allocate memory for AVFrame\n");
        return NULL;
    }

    int frameFinished;
    int numBytes;

    // Determine required buffer size and allocate buffer
    numBytes = avpicture_get_size(PIX_FMT_YUVJ420P, pCodecCtx->width, pCodecCtx->height);

    // ***
    *bufSize = numBytes;
    // ***

    uint8_t *buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));

    avpicture_fill((AVPicture *) pFrame, buffer, PIX_FMT_YUVJ420P, pCodecCtx->width, pCodecCtx->height);

    // Read frame

    AVPacket packet;

    int framesNumber = 0;
    while (av_read_frame(pFormatCtx, &packet) >= 0)
    {
        if(packet.stream_index != 0)
            continue;

        int ret = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
        if (ret > 0)
        {
            sprintf(buf, "Frame is decoded, size %d", ret);
            LOGV(TAG, buf);
            pFrame->quality = 4;
            return pFrame;
        }
        else {
            // printf("Error [%d] while decoding frame: %s\n", ret, strerror(AVERROR(ret)));
            sprintf(buf, "Error %d decoding frame: %s", ret, strerror(AVERROR(ret)));
            LOGV(TAG, buf);
        }
    }
}

Creating AVFrame from AVPicture

See also

Favorite site

References


  1. Save_avframe_to_jpeg_file.pdf