Skip to content

Commit

Permalink
fix to accept videos with different sample aspect ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
wandenberg committed Feb 17, 2015
1 parent a957ab7 commit 78559a8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/ngx_http_video_thumbextractor_module_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,28 @@ ngx_http_video_thumbextractor_jpeg_memory_dest (j_compress_ptr cinfo, caddr_t *o
}


float display_aspect_ratio(AVCodecContext *pCodecCtx)
{
double aspect_ratio = av_q2d(pCodecCtx->sample_aspect_ratio);
return ((float) pCodecCtx->width / pCodecCtx->height) * (aspect_ratio ? aspect_ratio : 1);
}


int display_width(AVCodecContext *pCodecCtx)
{
return pCodecCtx->height * display_aspect_ratio(pCodecCtx);
}


int setup_parameters(ngx_http_video_thumbextractor_loc_conf_t *cf, ngx_http_video_thumbextractor_ctx_t *ctx, AVFormatContext *pFormatCtx, AVCodecContext *pCodecCtx)
{
if (ctx->height == 0) {
// keep original format
ctx->width = pCodecCtx->width;
ctx->width = display_width(pCodecCtx);
ctx->height = pCodecCtx->height;
} else if (ctx->width == 0) {
// calculate width related with original aspect
ctx->width = ctx->height * pCodecCtx->width / pCodecCtx->height;
ctx->width = ctx->height * display_aspect_ratio(pCodecCtx);
}

ctx->tile_sample_interval = cf->tile_sample_interval;
Expand Down Expand Up @@ -444,18 +457,18 @@ int setup_filters(ngx_http_video_thumbextractor_loc_conf_t *cf, ngx_http_video_t
float scale = 0.0, new_scale = 0.0, scale_sws = 0.0, scale_w = 0.0, scale_h = 0.0;
int sws_width = 0, sws_height = 0;

scale = (float) pCodecCtx->width / pCodecCtx->height;
scale = display_aspect_ratio(pCodecCtx);
new_scale = (float) width / height;

sws_width = width;
sws_height = height;

if (scale != new_scale) {
scale_w = (float) width / pCodecCtx->width;
scale_w = (float) width / display_width(pCodecCtx);
scale_h = (float) height / pCodecCtx->height;
scale_sws = (scale_w > scale_h) ? scale_w : scale_h;

sws_width = pCodecCtx->width * scale_sws + 0.5;
sws_width = display_width(pCodecCtx) * scale_sws + 0.5;
sws_height = pCodecCtx->height * scale_sws + 0.5;

needs_crop = 1;
Expand Down
Binary file added test/test_video_aspect_gt_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/test_video_aspect_gt_1.mp4
Binary file not shown.
Binary file added test/test_video_aspect_lt_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/test_video_aspect_lt_1.mp4
Binary file not shown.
18 changes: 18 additions & 0 deletions test/video_thumbextractor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@
end
end

context "when the video has sample aspect ratio less than 1" do
it "should use the display dimensions as true dimensions" do
nginx_run_server(only_keyframe: 'off') do
content = image('/test_video_aspect_lt_1.mp4?second=2&height=90', {}, "200")
expect(content).to eq(IO.binread('test_video_aspect_lt_1.jpg'))
end
end
end

context "when the video has sample aspect ratio greater than 1" do
it "should use the display dimensions as true dimensions" do
nginx_run_server(only_keyframe: 'off') do
content = image('/test_video_aspect_gt_1.mp4?second=2&height=90', {}, "200")
expect(content).to eq(IO.binread('test_video_aspect_gt_1.jpg'))
end
end
end

context "when the requested second is on the end of the video" do
context "and using only_keyframe with previous time" do
it "should return a 200" do
Expand Down

0 comments on commit 78559a8

Please sign in to comment.