Re: [PATCH v8 3/8] media: v4l2-jpeg: Add a baseline JPEG header writer

From: Atanas Filipov

Date: Wed Nov 11 2026 - 10:43:13 EST


On Wed, Nov 11, 2026 at 03:30:57PM +0200, Dmitry Baryshkov wrote:
> > +/* Writes past the end only advance pos; v4l2_jpeg_write_header() checks it */
> > +static void jpeg_put_bytes(struct jpeg_hdr_buf *b, const void *src, size_t n)
> > +{
> > + if (b->pos + n <= b->len) {
> > + if (src)
> > + memcpy(b->buf + b->pos, src, n);
> > + else
> > + memset(b->buf + b->pos, 0, n);
> > + }
> > + b->pos += n;
>
> Now b->pos has advanced past the b->len. Is it expected?

Yes. Nothing is written past the end, and the final check returns
-ENOSPC. I agree it is not clear. I will use an explicit overflow flag
in the next version.

> > + jpeg_put_u8(&b, nc);
> > + for (i = 0; i < nc; i++) {
> > + jpeg_put_u8(&b, i + 1);
> > + jpeg_put_u8(&b, i ? JPEG_SAMPLING_H1V1 : params->luma_sampling);
> > + jpeg_put_u8(&b, i ? JPEG_QTABLE_CHROMA : JPEG_QTABLE_LUMA);
> > + }
>
> I think it might be better to write explicitly:
>
> jpeg_put_u8(&b, 1);
> jpeg_put_u8(&b, params->luma_sampling);
> jpeg_put_u8(&b, JPEG_QTABLE_LUMA);
>
> if (params->num_components != JPEG_COMP_MONO) {
> jpeg_put_u8(&b, 2);
> jpeg_put_u8(&b, JPEG_SAMPLING_H1V1);
> jpeg_put_u8(&b, JPEG_QTABLE_CHROMA);
> }

Ok, I will write them explicitly in the next version.

> > + jpeg_put_u8(&b, nc);
> > + for (i = 0; i < nc; i++) {
> > + jpeg_put_u8(&b, i + 1);
> > + jpeg_put_u8(&b, i ? JPEG_TABLE_SEL_CHROMA : JPEG_TABLE_SEL_LUMA);
> > + }
>
> The same.

Ok, the same for SOS.

--
Best regards,
Atanas