Skip to content

Commit

Permalink
Add constructor and assign direct for avframe
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Connor committed Jan 25, 2025
1 parent 18f3e24 commit fe4765a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/zm_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ Image::Image(const AVFrame *frame, int p_width, int p_height) :
this->Assign(frame);
}

Image::Image(const AVFrame *frame) {
AssignDirect(frame);
}

static void dont_free(void *opaque, uint8_t *data) {
}

Expand Down Expand Up @@ -649,6 +653,27 @@ uint8_t* Image::WriteBuffer(
return buffer;
}

void Image::AssignDirect(const AVFrame *frame) {
width = frame->width;
height = frame->height;
buffer = frame->data[0];
linesize = frame->linesize[0];
allocation = size = av_image_get_buffer_size(static_cast<AVPixelFormat>(frame->format), frame->width, frame->height, 32);
switch(static_cast<AVPixelFormat>(frame->format)) {
case AV_PIX_FMT_RGBA:
subpixelorder = ZM_SUBPIX_ORDER_RGBA;
colours = ZM_COLOUR_RGB32;
break;
case AV_PIX_FMT_YUV420P:
colours = ZM_COLOUR_GRAY8;
default:
break;
}
buffertype = ZM_BUFTYPE_DONTFREE;
pixels = width * height;
}


/* Assign an existing buffer to the image instead of copying from a source buffer.
The goal is to reduce the amount of memory copying and increase efficiency and buffer reusing.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/zm_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ class Image {
Image(int p_width, int p_height, int p_colours, int p_subpixelorder, uint8_t *p_buffer=0, unsigned int padding=0);
Image(int p_width, int p_linesize, int p_height, int p_colours, int p_subpixelorder, uint8_t *p_buffer=0, unsigned int padding=0);
explicit Image(const Image &p_image);
explicit Image(const AVFrame *frame, int p_width=-1, int p_height=-1);
explicit Image(const AVFrame *frame, int p_width, int p_height);
explicit Image(const AVFrame *frame);

~Image();

Expand Down Expand Up @@ -206,6 +207,7 @@ class Image {
uint8_t *new_buffer,
const size_t buffer_size,
const int p_buffertype);
void AssignDirect(const AVFrame *frame);

int PopulateFrame(AVFrame *frame) const;

Expand Down

0 comments on commit fe4765a

Please sign in to comment.