Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QImage to QVideoFrame Conversion access violation issue

QImage to QVideoFrame Conversion access violation issue

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 676 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    iSolve_Tech
    wrote on last edited by iSolve_Tech
    #1

    QVideoFrameFormat format(image.size(), QVideoFrameFormat::pixelFormatFromImageFormat(image.format()));
    QVideoFrame frame(format);
    if (!frame.isValid()) {
    qDebug() << "Error: Invalid video frame format";
    // Handle error
    } else {
    if (!frame.map(QVideoFrame::ReadWrite)) {
    qDebug() << "Error: Failed to map video frame";
    // Handle error
    } else {
    // Check if mapping succeeded
    qDebug() << "Video frame mapped successfully";

              // Perform memcpy for each plane
              const int planeCount = frame.planeCount();
              for (int plane = 0; plane < planeCount; ++plane) {
                  uchar* frameBits = frame.bits(plane);
                  uchar* imgBits = image.bits();
                  const int planeBytes = frame.mappedBytes(plane);
    
                  // Check if sizes match for this plane
                  if (frameBits && imgBits && planeBytes >= 0) {
                      memcpy(frameBits, imgBits, planeBytes);
                      qDebug() << "Memcpy for plane" << plane << "completed successfully";
                  } else {
                      qDebug() << "Error: Invalid pointer(s) or plane size";
                      // Handle error
                  }
              }
    
              // Unmap the frame after memcpy
              frame.unmap();
          }
      }
    

    This is my code for the conversion from QImage to QVideoFrame but am facing access violation issue due to this(memcpy(frameBits, imgBits, planeBytes)) I think so. How can I solve this issue?

    jsulmJ B 2 Replies Last reply
    0
    • I iSolve_Tech

      QVideoFrameFormat format(image.size(), QVideoFrameFormat::pixelFormatFromImageFormat(image.format()));
      QVideoFrame frame(format);
      if (!frame.isValid()) {
      qDebug() << "Error: Invalid video frame format";
      // Handle error
      } else {
      if (!frame.map(QVideoFrame::ReadWrite)) {
      qDebug() << "Error: Failed to map video frame";
      // Handle error
      } else {
      // Check if mapping succeeded
      qDebug() << "Video frame mapped successfully";

                // Perform memcpy for each plane
                const int planeCount = frame.planeCount();
                for (int plane = 0; plane < planeCount; ++plane) {
                    uchar* frameBits = frame.bits(plane);
                    uchar* imgBits = image.bits();
                    const int planeBytes = frame.mappedBytes(plane);
      
                    // Check if sizes match for this plane
                    if (frameBits && imgBits && planeBytes >= 0) {
                        memcpy(frameBits, imgBits, planeBytes);
                        qDebug() << "Memcpy for plane" << plane << "completed successfully";
                    } else {
                        qDebug() << "Error: Invalid pointer(s) or plane size";
                        // Handle error
                    }
                }
      
                // Unmap the frame after memcpy
                frame.unmap();
            }
        }
      

      This is my code for the conversion from QImage to QVideoFrame but am facing access violation issue due to this(memcpy(frameBits, imgBits, planeBytes)) I think so. How can I solve this issue?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @iSolve_Tech said in QImage to QVideoFrame Conversion access violation issue:

      I think so.

      You think so or did you run in debugger to validate this assumption?
      How does stack trace look like after the crash?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      I 1 Reply Last reply
      0
      • jsulmJ jsulm

        @iSolve_Tech said in QImage to QVideoFrame Conversion access violation issue:

        I think so.

        You think so or did you run in debugger to validate this assumption?
        How does stack trace look like after the crash?

        I Offline
        I Offline
        iSolve_Tech
        wrote on last edited by
        #3

        @jsulm when executing image.bits() am getting Streaming Thread (10): EXC_BAD_ACCESS (code=1, address=0x127e98000)

        jsulmJ 1 Reply Last reply
        0
        • I iSolve_Tech

          @jsulm when executing image.bits() am getting Streaming Thread (10): EXC_BAD_ACCESS (code=1, address=0x127e98000)

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @iSolve_Tech Then check what you pass to memcpy: are both dest/src pointers valid? Is dest pointing to a memory chunk same size or bigger than src?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • I iSolve_Tech

            QVideoFrameFormat format(image.size(), QVideoFrameFormat::pixelFormatFromImageFormat(image.format()));
            QVideoFrame frame(format);
            if (!frame.isValid()) {
            qDebug() << "Error: Invalid video frame format";
            // Handle error
            } else {
            if (!frame.map(QVideoFrame::ReadWrite)) {
            qDebug() << "Error: Failed to map video frame";
            // Handle error
            } else {
            // Check if mapping succeeded
            qDebug() << "Video frame mapped successfully";

                      // Perform memcpy for each plane
                      const int planeCount = frame.planeCount();
                      for (int plane = 0; plane < planeCount; ++plane) {
                          uchar* frameBits = frame.bits(plane);
                          uchar* imgBits = image.bits();
                          const int planeBytes = frame.mappedBytes(plane);
            
                          // Check if sizes match for this plane
                          if (frameBits && imgBits && planeBytes >= 0) {
                              memcpy(frameBits, imgBits, planeBytes);
                              qDebug() << "Memcpy for plane" << plane << "completed successfully";
                          } else {
                              qDebug() << "Error: Invalid pointer(s) or plane size";
                              // Handle error
                          }
                      }
            
                      // Unmap the frame after memcpy
                      frame.unmap();
                  }
              }
            

            This is my code for the conversion from QImage to QVideoFrame but am facing access violation issue due to this(memcpy(frameBits, imgBits, planeBytes)) I think so. How can I solve this issue?

            B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #5

            @iSolve_Tech
            Some minor points:
            First, this line will make a deep copy of the image data, which is a waste.

            uchar* imgBits = image.bits();
            

            So it is better to add const.

            Second, since QImage does not support multiple planes, there will always be only plane 0.

            Then the important point, have you tried print

            qDebug() << planeBytes << image.sizeInBytes();
            

            You probably will see planeBytes is not same as image.sizeInBytes()
            That's what I suppose why memcpy would have access violation, the data size in QVideoFrame is aligned and maybe bigger than QImage. So what you should do is copy data line by line.

            I modified your code to

            QVideoFrameFormat format(image.size(), QVideoFrameFormat::pixelFormatFromImageFormat(image.format()));
            QVideoFrame frame(format);
            if (!frame.isValid() || frame.planeCount() <= 0) {
                qDebug() << "Error: Invalid video frame format";
                // Handle error
            } else {
                if (!frame.map(QVideoFrame::ReadWrite)) {
                    qDebug() << "Error: Failed to map video frame";
                    // Handle error
                } else {
                    // Check if mapping succeeded
                    qDebug() << "Video frame mapped successfully";
                    uchar* frameBits = frame.bits(0);
                    const uchar* imgBits = image.constBits();
                    const int planeBytes = frame.mappedBytes(0);
            
                    // Check if sizes match for this plane
                    if (frameBits && imgBits && planeBytes >= 0) {
                        const int lineBytesFrame = frame.bytesPerLine(0);
                        const int lineBytesImage = image.bytesPerLine();
                        const int imageHeight = image.height();
                        for(int line = 0; line < imageHeight; line++){
                            // Perform memcpy for each line
                            //you can add error handling here since memcpy_s can return errors
                            memcpy_s(frameBits, lineBytesFrame, imgBits, lineBytesImage);
                            frameBits += lineBytesFrame;
                            imgBits += lineBytesImage;
                        }
                        qDebug() << "Memcpy completed successfully";
                    } else {
                        qDebug() << "Error: Invalid pointer(s) or plane size";
                        // Handle error
                    }
            
                    // Unmap the frame after memcpy
                    frame.unmap();
                }
            
            1 Reply Last reply
            1
            • I Offline
              I Offline
              iSolve_Tech
              wrote on last edited by
              #6

              @Bonnie Thanks you so much for your time I will modify the code you provide and will check .

              I 2 Replies Last reply
              0
              • I iSolve_Tech

                @Bonnie Thanks you so much for your time I will modify the code you provide and will check .

                I Offline
                I Offline
                iSolve_Tech
                wrote on last edited by
                #7

                @Bonnie QVideoFrameFormat format(image.size(), QVideoFrameFormat::pixelFormatFromImageFormat(image.format()));
                QVideoFrame frame(format);
                if (!frame.isValid() || frame.planeCount() <= 0) {
                qDebug() << "Error: Invalid video frame format";
                // Handle error
                } else {
                if (!frame.map(QVideoFrame::ReadWrite)) {
                qDebug() << "Error: Failed to map video frame";
                // Handle error
                } else {
                // Check if mapping succeeded
                qDebug() << "Video frame mapped successfully";
                uchar* frameBits = frame.bits(0);
                const uchar* imgBits = image.constBits();
                const int planeBytes = frame.mappedBytes(0);

                            // Check if sizes match for this plane
                            if (frameBits && imgBits && planeBytes >= 0) {
                                const int lineBytesFrame = frame.bytesPerLine(0);
                                const int lineBytesImage = image.bytesPerLine();
                                const int imageHeight = image.height();
                                for(int line = 0; line < imageHeight; line++){ // srinadh
                                    // Perform memcpy for each line
                      
                
                                    memcpy(frameBits, imgBits, lineBytesImage);
                                   
                                }
                                qDebug() << "Memcpy completed successfully";
                            }
                
                            else {
                                qDebug() << "Error: Invalid pointer(s) or plane size";
                                // Handle error
                            }
                
                            // Unmap the frame after memcpy
                            frame.unmap();
                        }
                    }
                    Buffer updatedFrame = Buffer(frame);
                    updatedFrame.copyTsFrom(newFrame);
                    updatedFrame.setUnmodifiedVideoFrame(newFrame.videoFrame());
                    newFrame = updatedFrame;
                    qDebug()  << "image processed"; 
                

                this memcopy is working fine in iOS and Windows but in android after am converting the image to frame I can able to see black screen

                1 Reply Last reply
                0
                • I iSolve_Tech

                  @Bonnie Thanks you so much for your time I will modify the code you provide and will check .

                  I Offline
                  I Offline
                  iSolve_Tech
                  wrote on last edited by iSolve_Tech
                  #8

                  @Bonnie Can you please give me some idea regarding the issue in android ?

                  am getting this error - freeAllBuffers: 1 buffers were freed while being dequeued!

                  SGaistS B I 3 Replies Last reply
                  0
                  • I iSolve_Tech

                    @Bonnie Can you please give me some idea regarding the issue in android ?

                    am getting this error - freeAllBuffers: 1 buffers were freed while being dequeued!

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @iSolve_Tech Hi,

                    From old memories, I think that on Android, it's an OpenGL texture and you have to handle it accordingly.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • I iSolve_Tech

                      @Bonnie Can you please give me some idea regarding the issue in android ?

                      am getting this error - freeAllBuffers: 1 buffers were freed while being dequeued!

                      B Offline
                      B Offline
                      Bonnie
                      wrote on last edited by
                      #10

                      @iSolve_Tech Sorry I don't develop on android, also this seems not related to this topic.

                      1 Reply Last reply
                      0
                      • I iSolve_Tech

                        @Bonnie Can you please give me some idea regarding the issue in android ?

                        am getting this error - freeAllBuffers: 1 buffers were freed while being dequeued!

                        I Offline
                        I Offline
                        iSolve_Tech
                        wrote on last edited by
                        #11

                        @SGaist Thanks for your reply will check in that way

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved