Skip to content

Conversation

jakethesnake420
Copy link
Contributor

@jakethesnake420 jakethesnake420 commented Jul 27, 2025

On the comma device, kill openpilot managed procs in tmux with ctrl+c, then start the ui again with ./selfdrive/ui/ui.
then run:
COMMA_CACHE=/data/cache ./tools/replay/replay --demo --ecam --dcam

Copy link
Contributor

@adeebshihadeh adeebshihadeh left a comment

Choose a reason for hiding this comment

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

Very cool! Did a quick pass and left some comments to start.

Do we really need the rotator? Decoder won't output NV12?

(To clarify, I'd like to merge the rotator support anyway. It's nice to have support for all the HW blocks in openpilot anyway.)

(V4L2_CID_MPEG_MSM_VIDC_BASE + 22)


enum v4l2_mpeg_vidc_video_decoder_multi_stream {
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like this is copy/pasted from some kernel header?

Copy link
Contributor

Choose a reason for hiding this comment

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

and a bunch of other stuff in this file

msgq_repo Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

what's this for?

@@ -0,0 +1,37 @@
#pragma once

#include <fcntl.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

does this file really use all these includes? I imagine half are unused

#define V4L2_PIX_FMT_NV12_UBWC v4l2_fourcc('Q', '1', '2', '8')
#endif

static void checked_ioctl(int fd, unsigned long request, void *argp) {
Copy link
Contributor

Choose a reason for hiding this comment

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

let's put this in common/ instead of copy/pasting everywhere

Copy link
Contributor

Choose a reason for hiding this comment

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

or better yet make check an argument to util::safe_ioctl?

Copy link
Contributor Author

@jakethesnake420 jakethesnake420 Aug 2, 2025

Choose a reason for hiding this comment

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

What if safe_ioctl accepts a message and an error callback? It is used in spi.cc so it would be like this:

 auto fail = [serial, this] {cleanup(); throw std::runtime_error("Error connecting to panda with serial: " + serial);};
 // SPI settings
 util::safe_ioctl(spi_fd, SPI_IOC_WR_MODE, &spi_mode, "failed setting SPI mode", fail);
 util::safe_ioctl(spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi_speed, "failed setting SPI speed", fail);
 util::safe_ioctl(spi_fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bits_per_word, "failed setting SPI bits per word", fail);
template<typename ErrorCallback>
int safe_ioctl(int fd, unsigned long request, void *argp, const char* error_msg, ErrorCallback error_callback) {
  int ret;
  do {
    ret = ioctl(fd, request, argp);
  } while ((ret == -1) && (errno == EINTR));

  if (ret == -1 && error_msg) {
    LOGE("ioctl failed: %s", error_msg);
    error_callback();
  }
  return ret;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines 136 to 137
if (!queued) // nothing in flight
return nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

use braces even for one liners

return &cap_buf;
}

void SdeRotator::convertStride(VisionBuf *rotated_buf, VisionBuf *user_buf) {
Copy link
Contributor

Choose a reason for hiding this comment

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

rotate can't handle this?

@jakethesnake420
Copy link
Contributor Author

jakethesnake420 commented Aug 2, 2025

I am basing this off a working example code https://github.com/jakethesnake420/v4l2-decode. With that said, I don't have a complete understanding of why certain things are done the way they are.


Do we really need the rotator? Decoder won't output NV12?

The venus driver throws an error when trying to set the capture buffer type to NV12.

msm_vidc:   fw: 
               <VFW_E:HostDr:265d:92a139e6:0> vDec_LoadResources(1228): Linear format not supported for DPB!
comma@comma-fa9d355d:/$ strings firmware/image/venus.* | grep Linear
%s(%d): Linear format not supported for DPB!
%s(%d): Linear format not supported for DPB!

Which tells me that the decoded picture buffer needs to be UBWC. The DPB format seems to get overridden when I set the capture plane format. I have tried a few things to get it working but the only way I could see is to use the rotator.


The stuff in decoder.h is copied from kernel headers to work around build issues. I think this can be cleaned up I just have not spent the time yet to solve the issue.


The change to msgq_repo is to add a modified version of the VisionBuf::allocate that maps the memory differently.

new function allocate_no_cache:

void VisionBuf::allocate_no_cache(size_t length) {
ion_alloc.len = length;
ION_IOC_ALLOC
ION_IOC_MAP
ION_IOC_FREE
mmap

versus original allocate:

void VisionBuf::allocate(size_t length) {
ion_alloc.len = length + PADDING_CL + sizeof(uint64_t);
ION_IOC_ALLOC
ION_IOC_SHARE
mmap

calls ION_IOC_FREE later

rotate can't handle this?

Another pain point. I couldn't get the driver to accept a custom fmt_cap.fmt.pix.bytesperline as its calculated by the driver and its based on the resolution and alignment. I tried to format the output to a size that the driver would accept and then scale it up/down but I faced additional issues. I am not an expert on troubleshooting v4l2 or video formats so its difficult for me to tell what is wrong when an image is deformed a certain way.

@adeebshihadeh
Copy link
Contributor

@jakethesnake420 can you rebase after the ioctl improvements and do a bit of cleanup? then I can see if I see can simplify this a bit before merging

@adeebshihadeh
Copy link
Contributor

trigger-jenkins

@jakethesnake420 jakethesnake420 force-pushed the qcom-replay branch 2 times, most recently from 7aad2e2 to 561efd8 Compare August 5, 2025 02:29
@jakethesnake420
Copy link
Contributor Author

can you rebase after the ioctl improvements and do a bit of cleanup? then I can see if I see can simplify this a bit before merging

Rebased, cleaned and further simplified.

@jakethesnake420
Copy link
Contributor Author

jakethesnake420 commented Aug 5, 2025

The last major thing to simplify is the visionbuf stuff. Maybe allocate() can be refactored slightly to work instead of adding the new allocate_no_cache function. I originally did it this way to match the example from samsung with little understanding.

Copy link
Contributor

This PR has had no activity for 9 days. It will be automatically closed in 2 days if there is no activity.

@adeebshihadeh
Copy link
Contributor

I started working on simplifying this but there's too many things that are a mess:

  • there's multiple errors
  • the msgq_repo change doesn't even seem necessary. if you replace allocate_no_cache with allocate, everything still seems to work
  • the rotator shouldn't be necessary. according to the kernel driver, non-UBWC should work fine
  • we shouldn't have to handle the alignment. the alignment at runtime is already driven by the encoder requirements

I can understand if one or two of these are left, but this feels like a PR that just started working.

@adeebshihadeh adeebshihadeh marked this pull request as draft August 24, 2025 00:47
@jakethesnake420
Copy link
Contributor Author

Ok I will take another swing at these issues.

@jakethesnake420 jakethesnake420 marked this pull request as ready for review September 2, 2025 02:43
@jakethesnake420
Copy link
Contributor Author

jakethesnake420 commented Sep 2, 2025

I was able to find solutions.

  • Can use existing visionbuf allocate function.
  • The decoder can convert to NV12 directly.
  • The decoded frames have the correct stride.
  • Error messages were not real errors.

@@ -259,7 +262,7 @@ bool MsmVidc::setDBP() {
struct v4l2_ext_control control[2] = {0};
struct v4l2_ext_controls controls = {0};
control[0].id = V4L2_CID_MPEG_VIDC_VIDEO_STREAM_OUTPUT_MODE;
control[0].value = 0; // V4L2_CID_MPEG_VIDC_VIDEO_STREAM_OUTPUT_PRIMARY
control[0].value = 1; // V4L2_CID_MPEG_VIDC_VIDEO_STREAM_OUTPUT_SECONDARY
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was the key difference to get the decoder to decode directly to linear NV12 (non-UBWC)

@adeebshihadeh
Copy link
Contributor

adeebshihadeh commented Sep 2, 2025

Nice, this is a lot simpler but now I'm getting this immediately when running ./replay --demo:

common/util.cc: safe_ioctl error: VIDIOC_STREAMON CAPTURE failed Invalid argument(22) (fd: 10 request: 4004612 argp: 0x7f5fffde14)

@jakethesnake420
Copy link
Contributor Author

jakethesnake420 commented Sep 2, 2025

Unfortunately I can't reproduce this. There will likely be information in dmesg. You can change the driver debug verbosity
echo "0xFFFF" > /sys/kernel/debug/msm_vidc/debug_level

I just did a clean install as a test and it works for me.

@jakethesnake420
Copy link
Contributor Author

It should be possible to use the ion buffers created by vipc, eliminating the copy buffer and buffer creation code. But it may not be simple.

@github-actions github-actions bot added the tools label Sep 5, 2025
@adeebshihadeh adeebshihadeh merged commit 1f1efec into commaai:master Sep 5, 2025
10 checks passed
@adeebshihadeh
Copy link
Contributor

Thanks for the PR!

Edison-CBS added a commit to Edison-CBS/openpilot that referenced this pull request Sep 6, 2025
Edison-CBS added a commit to Edison-CBS/openpilot that referenced this pull request Sep 6, 2025
@jakethesnake420
Copy link
Contributor Author

Your welcome!

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.

2 participants