Skip to content

Check buffer size when frameWidth * frameHeight bigger than allocated buffer size#21170

Merged
alalek merged 5 commits intoopencv:4.xfrom
JJJoonngg:4.x
Dec 2, 2021
Merged

Check buffer size when frameWidth * frameHeight bigger than allocated buffer size#21170
alalek merged 5 commits intoopencv:4.xfrom
JJJoonngg:4.x

Conversation

@JJJoonngg
Copy link
Copy Markdown
Contributor

@JJJoonngg JJJoonngg commented Dec 2, 2021

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake
force_builders=Android pack,docs
android_pack_config=ndk-18-api-level-24.config.py

…than allocated buffer size

    - issue nubmer opencv#21149 report, some android 12 device, allocated bufferSize is too small for the retrieve frame, so change logic for debugging
Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contribution!

Please take a look on comments below.

if (info.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM) {
if (frameWidth * frameHeight * 3 / 2 > bufferSize)
{
if (bufferSize == 3110400 && (frameWidth == 1920 && frameHeight == 1088) || frameWidth == 1088 && frameHeight == 1920)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|| frameWidth == 1088 && frameHeight == 1920

Priority brackets is necessary in condition with &&/|| mess.


Please use this work around code with shorter conditions to support both 1920x1088 / 1088x1920 cases.
Please add link to the issue for this workaround code.

// WA: https://github.com/opencv/opencv/issues/21149
if (frameWidth * frameHeight * 3 / 2 > bufferSize)
{
    if (bufferSize == 3110400 && frameWidth == 1920 && frameHeight == 1088)
    {
        frameHeight = 1080;
        LOGV(...);
    }
    else if (bufferSize == 3110400 && frameWidth == 1088 && frameHeight == 1920`)
    {
        frameWidth = 1080;
        LOGV(...);
    }
    else
    {
        LOGE(...);
        return false;
    }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Thank you

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for update!

Please remove unnecessary whitespace: http://pullrequest.opencv.org/buildbot/builders/precommit_docs/builds/33151/steps/whitespace%20opencv/logs/stdio

Also please remove double space on this line:

else if(bufferSize == 3110400 &&  frameWidth == 1088 && frameHeight == 1920)
                                ^^

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still have whitespace issue: http://pullrequest.opencv.org/buildbot/builders/precommit_docs/builds/33152/steps/whitespace%20opencv/logs/stdio

modules/videoio/src/cap_android_mediandk.cpp:108: trailing whitespace.
+                        {   
                          ^^^ 3 spaces here

Comment on lines 121 to 125
if (info.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM)
{
LOGV("output EOS");
sawOutputEOS = true;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this code block should go first.

return false; above should not block sawOutputEOS = true; flag.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Thanks your review, I will fix it and repush it!

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@alalek alalek merged commit 2da1f91 into opencv:4.x Dec 2, 2021
@alalek alalek mentioned this pull request Dec 30, 2021
@alalek alalek mentioned this pull request Feb 22, 2022
a-sajjad72 pushed a commit to a-sajjad72/opencv that referenced this pull request Mar 30, 2023
Check buffer size when frameWidth * frameHeight bigger than allocated buffer size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In Galaxy S21 and Android Version 12, VideoCapture::bool cv::VideoCapture::retrieve(Mat& image, int flag = 0) Crash erupt!

2 participants