1 // SPDX-License-Identifier: GPL-2.0-only
3 * V4L2 JPEG header parser helpers.
5 * Copyright (C) 2019 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
7 * For reference, see JPEG ITU-T.81 (ISO/IEC 10918-1) [1]
9 * [1] https://www.w3.org/Graphics/JPEG/itu-t81.pdf
12 #include <asm/unaligned.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <media/v4l2-jpeg.h>
19 MODULE_DESCRIPTION("V4L2 JPEG header parser helpers");
20 MODULE_AUTHOR("Philipp Zabel <kernel@pengutronix.de>");
21 MODULE_LICENSE("GPL");
23 /* Table B.1 - Marker code assignments */
24 #define SOF0 0xffc0 /* start of frame */
30 #define JPG 0xffc8 /* extensions */
35 #define DHT 0xffc4 /* huffman table */
36 #define DAC 0xffcc /* arithmetic coding conditioning */
37 #define RST0 0xffd0 /* restart */
39 #define SOI 0xffd8 /* start of image */
40 #define EOI 0xffd9 /* end of image */
41 #define SOS 0xffda /* start of stream */
42 #define DQT 0xffdb /* quantization table */
43 #define DNL 0xffdc /* number of lines */
44 #define DRI 0xffdd /* restart interval */
45 #define DHP 0xffde /* hierarchical progression */
46 #define EXP 0xffdf /* expand reference */
47 #define APP0 0xffe0 /* application data */
49 #define JPG0 0xfff0 /* extensions */
51 #define COM 0xfffe /* comment */
52 #define TEM 0xff01 /* temporary */
55 * struct jpeg_stream - JPEG byte stream
56 * @curr: current position in stream
57 * @end: end position, after last byte
64 /* returns a value that fits into u8, or negative error */
65 static int jpeg_get_byte(struct jpeg_stream
*stream
)
67 if (stream
->curr
>= stream
->end
)
70 return *stream
->curr
++;
73 /* returns a value that fits into u16, or negative error */
74 static int jpeg_get_word_be(struct jpeg_stream
*stream
)
78 if (stream
->curr
+ sizeof(__be16
) > stream
->end
)
81 word
= get_unaligned_be16(stream
->curr
);
82 stream
->curr
+= sizeof(__be16
);
87 static int jpeg_skip(struct jpeg_stream
*stream
, size_t len
)
89 if (stream
->curr
+ len
> stream
->end
)
97 static int jpeg_next_marker(struct jpeg_stream
*stream
)
102 while ((byte
= jpeg_get_byte(stream
)) >= 0) {
103 marker
= (marker
<< 8) | byte
;
104 /* skip stuffing bytes and REServed markers */
105 if (marker
== TEM
|| (marker
> 0xffbf && marker
< 0xffff))
112 /* this does not advance the current position in the stream */
113 static int jpeg_reference_segment(struct jpeg_stream
*stream
,
114 struct v4l2_jpeg_reference
*segment
)
118 if (stream
->curr
+ sizeof(__be16
) > stream
->end
)
121 len
= get_unaligned_be16(stream
->curr
);
122 if (stream
->curr
+ len
> stream
->end
)
125 segment
->start
= stream
->curr
;
126 segment
->length
= len
;
131 static int v4l2_jpeg_decode_subsampling(u8 nf
, u8 h_v
)
134 return V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY
;
136 /* no chroma subsampling for 4-component images */
137 if (nf
== 4 && h_v
!= 0x11)
142 return V4L2_JPEG_CHROMA_SUBSAMPLING_444
;
144 return V4L2_JPEG_CHROMA_SUBSAMPLING_422
;
146 return V4L2_JPEG_CHROMA_SUBSAMPLING_420
;
148 return V4L2_JPEG_CHROMA_SUBSAMPLING_411
;
154 static int jpeg_parse_frame_header(struct jpeg_stream
*stream
, u16 sof_marker
,
155 struct v4l2_jpeg_frame_header
*frame_header
)
157 int len
= jpeg_get_word_be(stream
);
161 /* Lf = 8 + 3 * Nf, Nf >= 1 */
166 /* Table B.2 - Frame header parameter sizes and values */
170 p
= jpeg_get_byte(stream
);
174 * Baseline DCT only supports 8-bit precision.
175 * Extended sequential DCT also supports 12-bit precision.
177 if (p
!= 8 && (p
!= 12 || sof_marker
!= SOF1
))
180 y
= jpeg_get_word_be(stream
);
186 x
= jpeg_get_word_be(stream
);
192 nf
= jpeg_get_byte(stream
);
196 * The spec allows 1 <= Nf <= 255, but we only support up to 4
199 if (nf
< 1 || nf
> V4L2_JPEG_MAX_COMPONENTS
)
201 if (len
!= 8 + 3 * nf
)
204 frame_header
->precision
= p
;
205 frame_header
->height
= y
;
206 frame_header
->width
= x
;
207 frame_header
->num_components
= nf
;
209 for (i
= 0; i
< nf
; i
++) {
210 struct v4l2_jpeg_frame_component_spec
*component
;
213 c
= jpeg_get_byte(stream
);
217 h_v
= jpeg_get_byte(stream
);
223 subs
= v4l2_jpeg_decode_subsampling(nf
, h_v
);
226 frame_header
->subsampling
= subs
;
227 } else if (h_v
!= 0x11) {
228 /* all chroma sampling factors must be 1 */
232 tq
= jpeg_get_byte(stream
);
236 component
= &frame_header
->component
[i
];
237 component
->component_identifier
= c
;
238 component
->horizontal_sampling_factor
=
240 component
->vertical_sampling_factor
= h_v
& 0xf;
241 component
->quantization_table_selector
= tq
;
244 return jpeg_skip(stream
, len
- 2);
250 static int jpeg_parse_scan_header(struct jpeg_stream
*stream
,
251 struct v4l2_jpeg_scan_header
*scan_header
)
254 int len
= jpeg_get_word_be(stream
);
258 /* Ls = 8 + 3 * Ns, Ns >= 1 */
266 ns
= jpeg_get_byte(stream
);
269 if (ns
< 1 || ns
> 4 || len
!= 6 + 2 * ns
)
272 scan_header
->num_components
= ns
;
274 for (i
= 0; i
< ns
; i
++) {
275 struct v4l2_jpeg_scan_component_spec
*component
;
278 cs
= jpeg_get_byte(stream
);
282 td_ta
= jpeg_get_byte(stream
);
286 component
= &scan_header
->component
[i
];
287 component
->component_selector
= cs
;
288 component
->dc_entropy_coding_table_selector
=
290 component
->ac_entropy_coding_table_selector
=
294 skip
= 3; /* skip Ss, Se, Ah, and Al */
299 return jpeg_skip(stream
, skip
);
302 /* B.2.4.1 Quantization table-specification syntax */
303 static int jpeg_parse_quantization_tables(struct jpeg_stream
*stream
,
305 struct v4l2_jpeg_reference
*tables
)
307 int len
= jpeg_get_word_be(stream
);
311 /* Lq = 2 + n * 65 (for baseline DCT), n >= 1 */
319 int pq_tq
= jpeg_get_byte(stream
);
324 /* quantization table element precision */
325 pq
= (pq_tq
>> 4) & 0xf;
327 * Only 8-bit Qk values for 8-bit sample precision. Extended
328 * sequential DCT with 12-bit sample precision also supports
331 if (pq
!= 0 && (pq
!= 1 || precision
!= 12))
334 /* quantization table destination identifier */
339 /* quantization table element */
341 ret
= jpeg_skip(stream
, pq
? 128 : 64);
346 tables
[tq
].start
= qk
;
347 tables
[tq
].length
= pq
? 128 : 64;
350 len
-= pq
? 129 : 65;
356 /* B.2.4.2 Huffman table-specification syntax */
357 static int jpeg_parse_huffman_tables(struct jpeg_stream
*stream
,
358 struct v4l2_jpeg_reference
*tables
)
361 int len
= jpeg_get_word_be(stream
);
365 /* Table B.5 - Huffman table specification parameter sizes and values */
369 for (len
-= 2; len
>= 17; len
-= 17 + mt
) {
371 int tc_th
= jpeg_get_byte(stream
);
377 /* table class - 0 = DC, 1 = AC */
378 tc
= (tc_th
>> 4) & 0xf;
382 /* huffman table destination identifier */
384 /* only two Huffman tables for baseline DCT */
388 /* BITS - number of Huffman codes with length i */
389 table
= stream
->curr
;
391 for (i
= 0; i
< 16; i
++) {
394 li
= jpeg_get_byte(stream
);
400 /* HUFFVAL - values associated with each Huffman code */
401 ret
= jpeg_skip(stream
, mt
);
406 tables
[(tc
<< 1) | th
].start
= table
;
407 tables
[(tc
<< 1) | th
].length
= stream
->curr
- table
;
411 return jpeg_skip(stream
, len
- 2);
414 /* B.2.4.4 Restart interval definition syntax */
415 static int jpeg_parse_restart_interval(struct jpeg_stream
*stream
,
416 u16
*restart_interval
)
418 int len
= jpeg_get_word_be(stream
);
426 ri
= jpeg_get_word_be(stream
);
430 *restart_interval
= ri
;
435 static int jpeg_skip_segment(struct jpeg_stream
*stream
)
437 int len
= jpeg_get_word_be(stream
);
444 return jpeg_skip(stream
, len
- 2);
448 * jpeg_parse_header - locate marker segments and optionally parse headers
449 * @buf: address of the JPEG buffer, should start with a SOI marker
450 * @len: length of the JPEG buffer
451 * @out: returns marker segment positions and optionally parsed headers
453 * The out->scan_header pointer must be initialized to NULL or point to a valid
454 * v4l2_jpeg_scan_header structure. The out->huffman_tables and
455 * out->quantization_tables pointers must be initialized to NULL or point to a
456 * valid array of 4 v4l2_jpeg_reference structures each.
458 * Returns 0 or negative error if parsing failed.
460 int v4l2_jpeg_parse_header(void *buf
, size_t len
, struct v4l2_jpeg_header
*out
)
462 struct jpeg_stream stream
;
467 stream
.end
= stream
.curr
+ len
;
472 /* the first marker must be SOI */
473 marker
= jpeg_next_marker(&stream
);
479 /* loop through marker segments */
480 while ((marker
= jpeg_next_marker(&stream
)) >= 0) {
482 /* baseline DCT, extended sequential DCT */
484 ret
= jpeg_reference_segment(&stream
, &out
->sof
);
487 ret
= jpeg_parse_frame_header(&stream
, marker
,
490 /* progressive, lossless */
492 /* differential coding */
494 /* arithmetic coding */
496 case SOF13
... SOF15
:
502 ret
= jpeg_reference_segment(&stream
,
503 &out
->dht
[out
->num_dht
++ % 4]);
506 ret
= jpeg_parse_huffman_tables(&stream
,
507 out
->huffman_tables
);
510 ret
= jpeg_reference_segment(&stream
,
511 &out
->dqt
[out
->num_dqt
++ % 4]);
514 ret
= jpeg_parse_quantization_tables(&stream
,
515 out
->frame
.precision
,
516 out
->quantization_tables
);
519 ret
= jpeg_parse_restart_interval(&stream
,
520 &out
->restart_interval
);
524 ret
= jpeg_reference_segment(&stream
, &out
->sos
);
527 ret
= jpeg_parse_scan_header(&stream
, out
->scan
);
529 * stop parsing, the scan header marks the beginning of
530 * the entropy coded segment
532 out
->ecs_offset
= stream
.curr
- (u8
*)buf
;
535 /* markers without parameters */
536 case RST0
... RST7
: /* restart */
537 case SOI
: /* start of image */
538 case EOI
: /* end of image */
541 /* skip unknown or unsupported marker segments */
543 ret
= jpeg_skip_segment(&stream
);
552 EXPORT_SYMBOL_GPL(v4l2_jpeg_parse_header
);
555 * v4l2_jpeg_parse_frame_header - parse frame header
556 * @buf: address of the frame header, after the SOF0 marker
557 * @len: length of the frame header
558 * @frame_header: returns the parsed frame header
560 * Returns 0 or negative error if parsing failed.
562 int v4l2_jpeg_parse_frame_header(void *buf
, size_t len
,
563 struct v4l2_jpeg_frame_header
*frame_header
)
565 struct jpeg_stream stream
;
568 stream
.end
= stream
.curr
+ len
;
569 return jpeg_parse_frame_header(&stream
, SOF0
, frame_header
);
571 EXPORT_SYMBOL_GPL(v4l2_jpeg_parse_frame_header
);
574 * v4l2_jpeg_parse_scan_header - parse scan header
575 * @buf: address of the scan header, after the SOS marker
576 * @len: length of the scan header
577 * @scan_header: returns the parsed scan header
579 * Returns 0 or negative error if parsing failed.
581 int v4l2_jpeg_parse_scan_header(void *buf
, size_t len
,
582 struct v4l2_jpeg_scan_header
*scan_header
)
584 struct jpeg_stream stream
;
587 stream
.end
= stream
.curr
+ len
;
588 return jpeg_parse_scan_header(&stream
, scan_header
);
590 EXPORT_SYMBOL_GPL(v4l2_jpeg_parse_scan_header
);
593 * v4l2_jpeg_parse_quantization_tables - parse quantization tables segment
594 * @buf: address of the quantization table segment, after the DQT marker
595 * @len: length of the quantization table segment
596 * @precision: sample precision (P) in bits per component
597 * @q_tables: returns four references into the buffer for the
598 * four possible quantization table destinations
600 * Returns 0 or negative error if parsing failed.
602 int v4l2_jpeg_parse_quantization_tables(void *buf
, size_t len
, u8 precision
,
603 struct v4l2_jpeg_reference
*q_tables
)
605 struct jpeg_stream stream
;
608 stream
.end
= stream
.curr
+ len
;
609 return jpeg_parse_quantization_tables(&stream
, precision
, q_tables
);
611 EXPORT_SYMBOL_GPL(v4l2_jpeg_parse_quantization_tables
);
614 * v4l2_jpeg_parse_huffman_tables - parse huffman tables segment
615 * @buf: address of the Huffman table segment, after the DHT marker
616 * @len: length of the Huffman table segment
617 * @huffman_tables: returns four references into the buffer for the
618 * four possible Huffman table destinations, in
619 * the order DC0, DC1, AC0, AC1
621 * Returns 0 or negative error if parsing failed.
623 int v4l2_jpeg_parse_huffman_tables(void *buf
, size_t len
,
624 struct v4l2_jpeg_reference
*huffman_tables
)
626 struct jpeg_stream stream
;
629 stream
.end
= stream
.curr
+ len
;
630 return jpeg_parse_huffman_tables(&stream
, huffman_tables
);
632 EXPORT_SYMBOL_GPL(v4l2_jpeg_parse_huffman_tables
);