Skip to content

Commit

Permalink
small changes to avoid memory leak and turn the code a little bit clear
Browse files Browse the repository at this point in the history
  • Loading branch information
wandenberg committed Dec 2, 2011
1 parent 7495437 commit 1a53e13
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/ngx_http_video_thumbextractor_module_utils.c
Expand Up @@ -26,7 +26,7 @@ ngx_http_video_thumbextractor_create_str(ngx_pool_t *pool, uint len)
static int
ngx_http_video_thumbextractor_get_thumb(ngx_http_video_thumbextractor_loc_conf_t *cf, const char *filename, int64_t second, ngx_uint_t width, ngx_uint_t height, caddr_t *out_buffer, size_t *out_len, ngx_pool_t *temp_pool, ngx_log_t *log)
{
int rc, videoStream, frameFinished;
int rc, videoStream, frameFinished = 0;
unsigned int i;
AVFormatContext *pFormatCtx = NULL;
AVCodecContext *pCodecCtx = NULL;
Expand Down Expand Up @@ -152,7 +152,7 @@ ngx_http_video_thumbextractor_get_thumb(ngx_http_video_thumbextractor_loc_conf_t
}

rc = NGX_ERROR;
while (av_read_frame(pFormatCtx, &packet) >= 0) {
while (!frameFinished && av_read_frame(pFormatCtx, &packet) >= 0) {
// Is this a packet from the video stream?
if (packet.stream_index == videoStream) {
// Decode video frame
Expand All @@ -169,14 +169,17 @@ ngx_http_video_thumbextractor_get_thumb(ngx_http_video_thumbextractor_loc_conf_t

if (needs_crop) {
MagickWandGenesis();
mrc = MagickTrue;

if ((m_wand = NewMagickWand()) == NULL){
ngx_log_error(NGX_LOG_ERR, log, 0, "video thumb extractor module: Could not alloc MagickWand memory");
rc = NGX_ERROR;
goto exit;
ngx_log_error(NGX_LOG_ERR, log, 0, "video thumb extractor module: Could not allocate MagickWand memory");
mrc = MagickFalse;
}

if (mrc == MagickTrue) {
mrc = MagickConstituteImage(m_wand, sws_width, sws_height, NGX_HTTP_VIDEO_THUMBEXTRACTOR_RGB, CharPixel, pFrameRGB->data[0]);
}

mrc = MagickConstituteImage(m_wand, sws_width, sws_height, NGX_HTTP_VIDEO_THUMBEXTRACTOR_RGB, CharPixel, pFrameRGB->data[0]);
if (mrc == MagickTrue) {
mrc = MagickSetImageGravity(m_wand, CenterGravity);
}
Expand All @@ -196,18 +199,17 @@ ngx_http_video_thumbextractor_get_thumb(ngx_http_video_thumbextractor_loc_conf_t

MagickWandTerminus();

if (mrc == MagickTrue) {
if (mrc != MagickTrue) {
ngx_log_error(NGX_LOG_ERR, log, 0, "video thumb extractor module: Error cropping image");
rc = NGX_ERROR;
goto exit;
/* stop the while before try to jpeg compress the image */
break;
}
}

// Compress to jpeg
if (ngx_http_video_thumbextractor_jpeg_compress(cf, pFrameRGB->data[0], pCodecCtx->width, pCodecCtx->height, width, height, out_buffer, out_len, uncompressed_size, temp_pool) == 0) {
rc = NGX_OK;
}
break;
}
}

Expand Down

0 comments on commit 1a53e13

Please sign in to comment.