fixed memory leak in grab

This commit is contained in:
jojo61 2018-10-08 22:15:10 +02:00
parent f03dde19ed
commit 391ce04657
1 changed files with 81 additions and 80 deletions

161
video.c
View File

@ -2383,6 +2383,7 @@ int get_RGB(CuvidDecoder *decoder) {
GlxCheck(); GlxCheck();
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteFramebuffers(1,&fb);
glDeleteTextures(1,&texture); glDeleteTextures(1,&texture);
} }
@ -4492,103 +4493,103 @@ uint8_t *VideoGrab(int *size, int *width, int *height, int write_header)
#ifdef USE_GRAB #ifdef USE_GRAB
if (VideoUsedModule->GrabOutput) { if (VideoUsedModule->GrabOutput) {
uint8_t *data; uint8_t *data;
uint8_t *rgb; uint8_t *rgb;
char buf[64]; char buf[64];
int i; int i;
int n; int n;
int scale_width; int scale_width;
int scale_height; int scale_height;
int x; int x;
int y; int y;
double src_x; double src_x;
double src_y; double src_y;
double scale_x; double scale_x;
double scale_y; double scale_y;
scale_width = *width; scale_width = *width;
scale_height = *height; scale_height = *height;
n = 0; n = 0;
data = VideoUsedModule->GrabOutput(size, width, height); data = VideoUsedModule->GrabOutput(size, width, height);
if (data == NULL) if (data == NULL)
return NULL;
if (scale_width <= 0) {
scale_width = *width;
}
if (scale_height <= 0) {
scale_height = *height;
}
// hardware didn't scale for us, use simple software scaler
if (scale_width != *width && scale_height != *height) {
if (write_header) {
n = snprintf(buf, sizeof(buf), "P6\n%d\n%d\n255\n",
scale_width, scale_height);
}
rgb = malloc(scale_width * scale_height * 3 + n);
if (!rgb) {
Error(_("video: out of memory\n"));
free(data);
return NULL; return NULL;
}
*size = scale_width * scale_height * 3 + n;
memcpy(rgb, buf, n); // header
scale_x = (double)*width / scale_width; if (scale_width <= 0) {
scale_y = (double)*height / scale_height; scale_width = *width;
}
if (scale_height <= 0) {
scale_height = *height;
}
// hardware didn't scale for us, use simple software scaler
if (scale_width != *width && scale_height != *height) {
if (write_header) {
n = snprintf(buf, sizeof(buf), "P6\n%d\n%d\n255\n",
scale_width, scale_height);
}
rgb = malloc(scale_width * scale_height * 3 + n);
if (!rgb) {
Error(_("video: out of memory\n"));
free(data);
return NULL;
}
*size = scale_width * scale_height * 3 + n;
memcpy(rgb, buf, n); // header
src_y = 0.0; scale_x = (double)*width / scale_width;
for (y = 0; y < scale_height; y++) { scale_y = (double)*height / scale_height;
int o;
src_x = 0.0; src_y = 0.0;
o = (int)src_y **width; for (y = 0; y < scale_height; y++) {
int o;
for (x = 0; x < scale_width; x++) { src_x = 0.0;
i = 4 * (o + (int)src_x); o = (int)src_y **width;
rgb[n + (x + y * scale_width) * 3 + 0] = data[i + 2]; for (x = 0; x < scale_width; x++) {
rgb[n + (x + y * scale_width) * 3 + 1] = data[i + 1]; i = 4 * (o + (int)src_x);
rgb[n + (x + y * scale_width) * 3 + 2] = data[i + 0];
src_x += scale_x; rgb[n + (x + y * scale_width) * 3 + 0] = data[i + 2];
rgb[n + (x + y * scale_width) * 3 + 1] = data[i + 1];
rgb[n + (x + y * scale_width) * 3 + 2] = data[i + 0];
src_x += scale_x;
}
src_y += scale_y;
} }
src_y += scale_y; *width = scale_width;
} *height = scale_height;
*width = scale_width; // grabed image of correct size convert BGRA -> RGB
*height = scale_height; } else {
if (write_header) {
n = snprintf(buf, sizeof(buf), "P6\n%d\n%d\n255\n", *width,
*height);
}
rgb = malloc(*width * *height * 3 + n);
if (!rgb) {
Error(_("video: out of memory\n"));
free(data);
return NULL;
}
memcpy(rgb, buf, n); // header
// grabed image of correct size convert BGRA -> RGB for (i = 0; i < *size / 4; ++i) { // convert bgra -> rgb
} else { rgb[n + i * 3 + 0] = data[i * 4 + 2];
if (write_header) { rgb[n + i * 3 + 1] = data[i * 4 + 1];
n = snprintf(buf, sizeof(buf), "P6\n%d\n%d\n255\n", *width, rgb[n + i * 3 + 2] = data[i * 4 + 0];
*height); }
}
rgb = malloc(*width * *height * 3 + n);
if (!rgb) {
Error(_("video: out of memory\n"));
free(data);
return NULL;
}
memcpy(rgb, buf, n); // header
for (i = 0; i < *size / 4; ++i) { // convert bgra -> rgb *size = *width * *height * 3 + n;
rgb[n + i * 3 + 0] = data[i * 4 + 2]; }
rgb[n + i * 3 + 1] = data[i * 4 + 1]; free(data);
rgb[n + i * 3 + 2] = data[i * 4 + 0];
}
*size = *width * *height * 3 + n; return rgb;
}
free(data);
return rgb;
} else } else
#endif #endif
{ {
Warning(_("softhddev: grab unsupported\n")); Warning(_("softhddev: grab unsupported\n"));
} }
(void)size; (void)size;