Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

int_size IS_8 takes two bytes for each uint8 #935

Open
hpsaturn opened this issue Jan 29, 2024 · 4 comments
Open

int_size IS_8 takes two bytes for each uint8 #935

hpsaturn opened this issue Jan 29, 2024 · 4 comments

Comments

@hpsaturn
Copy link

Overview

I don't know yet if this is an issue. I'm trying to reduce the current size of my payload or encoded stream, and I notice that for each insertion of each uint8, the stream add two bytes, and should be one byte, I guess. I tested different options but nothing.

My proto definition:

syntax = "proto2";

import "nanopb.proto";

message Frame {
  required uint32 lenght = 1 [(nanopb).int_size = IS_16];
  repeated uint32 data = 2 [(nanopb).int_size = IS_8, (nanopb).max_size = 1];
}

My code:

bool encode_uint8_array(pb_ostream_t *stream, const pb_field_t *field, void *const *arg) {
  for (int i = 0; i < chunk_size; i++) {
    if (!pb_encode_tag_for_field(stream, field))
      return false;
    uint8_t val = (out_jpg+chunk_pos)[i];
    if (!pb_encode_varint(stream, val))
      return false;
  }
  return true;
}

Full code

@PetteriAimonen
Copy link
Member

Varints with value >= 128 use two bytes when encoded: https://protobuf.dev/programming-guides/encoding/#varints

For data like that, you probably want to use the bytes type in protobuf, which doesn't use extra space and automatically passes the length too.

@hpsaturn
Copy link
Author

Thanks, I didn't know that, and I tried with bytes but I don't remember why it didn't work in the beginning of this exploration. Also with repeated bytes I had issues.. Maybe do you know an example or test using bytes and callback similar a repeated int that I can follow? Thanks

@PetteriAimonen
Copy link
Member

Encoding bytes is the same as encoding string here, just replace strlen() with the actual binary data length:
https://github.com/nanopb/nanopb/blob/master/tests/callbacks/encode_callbacks.c#L9-L17

@hpsaturn
Copy link
Author

hpsaturn commented Jan 29, 2024

Thanks! I did a mistake the first time that I try with encoding string, I was close. Thanks for your fast feedback and for this wonderful library. Is working better:

espnow_video.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants