1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics SA 2013
4 * Author: Hugues Fruchet <hugues.fruchet@st.com> for STMicroelectronics.
8 #include "delta-mjpeg.h"
10 #define MJPEG_SOF_0 0xc0
11 #define MJPEG_SOF_1 0xc1
12 #define MJPEG_SOI 0xd8
13 #define MJPEG_MARKER 0xff
15 static char *header_str(struct mjpeg_header
*header
,
20 unsigned int left
= len
;
25 snprintf(cur
, left
, "[MJPEG header]\n"
30 "|- components = %d\n",
32 header
->sample_precision
,
35 header
->nb_of_components
);
40 static int delta_mjpeg_read_sof(struct delta_ctx
*pctx
,
41 unsigned char *data
, unsigned int size
,
42 struct mjpeg_header
*header
)
44 struct delta_dev
*delta
= pctx
->dev
;
45 unsigned int offset
= 0;
50 memset(header
, 0, sizeof(*header
));
51 header
->length
= be16_to_cpu(*(__be16
*)(data
+ offset
));
52 offset
+= sizeof(u16
);
53 header
->sample_precision
= *(u8
*)(data
+ offset
);
55 header
->frame_height
= be16_to_cpu(*(__be16
*)(data
+ offset
));
56 offset
+= sizeof(u16
);
57 header
->frame_width
= be16_to_cpu(*(__be16
*)(data
+ offset
));
58 offset
+= sizeof(u16
);
59 header
->nb_of_components
= *(u8
*)(data
+ offset
);
62 if (header
->nb_of_components
>= MJPEG_MAX_COMPONENTS
) {
64 "%s unsupported number of components (%d > %d)\n",
65 pctx
->name
, header
->nb_of_components
,
66 MJPEG_MAX_COMPONENTS
);
70 if ((offset
+ header
->nb_of_components
*
71 sizeof(header
->components
[0])) > size
)
78 "%s sof: reached end of %d size input stream\n",
83 int delta_mjpeg_read_header(struct delta_ctx
*pctx
,
84 unsigned char *data
, unsigned int size
,
85 struct mjpeg_header
*header
,
86 unsigned int *data_offset
)
88 struct delta_dev
*delta
= pctx
->dev
;
89 unsigned char str
[200];
92 unsigned int offset
= 0;
100 if (data
[offset
] == MJPEG_MARKER
)
101 switch (data
[offset
+ 1]) {
104 *data_offset
= offset
;
111 "%s wrong sequence, got SOF while SOI not seen\n",
116 ret
= delta_mjpeg_read_sof(pctx
,
130 if ((offset
+ 2) >= size
)
136 "%s found header @ offset %d:\n%s", pctx
->name
,
138 header_str(header
, str
, sizeof(str
)));
143 "%s no header found within %d bytes input stream\n",