Lines Matching +full:10918 +full:- +full:1
1 // SPDX-License-Identifier: GPL-2.0-only
7 * For reference, see JPEG ITU-T.81 (ISO/IEC 10918-1) [1]
9 * [1] https://www.w3.org/Graphics/JPEG/itu-t81.pdf
17 #include <media/v4l2-jpeg.h>
23 /* Table B.1 - Marker code assignments */
56 * This is as per example in Annex K.1 of ITU-T.81
84 0, 1, 8, 16, 9, 2, 3, 10,
99 * coefficient differences. The table represents Table K.3 of ITU-T.81
112 * coefficients. The table represents Table K.5 of ITU-T.81
137 * The table represents Table K.4 of ITU-T.81
150 * AC coefficients. The table represents Table K.6 of ITU-T.81
172 * struct jpeg_stream - JPEG byte stream
184 if (stream->curr >= stream->end) in jpeg_get_byte()
185 return -EINVAL; in jpeg_get_byte()
187 return *stream->curr++; in jpeg_get_byte()
195 if (stream->curr + sizeof(__be16) > stream->end) in jpeg_get_word_be()
196 return -EINVAL; in jpeg_get_word_be()
198 word = get_unaligned_be16(stream->curr); in jpeg_get_word_be()
199 stream->curr += sizeof(__be16); in jpeg_get_word_be()
206 if (stream->curr + len > stream->end) in jpeg_skip()
207 return -EINVAL; in jpeg_skip()
209 stream->curr += len; in jpeg_skip()
235 if (stream->curr + sizeof(__be16) > stream->end) in jpeg_reference_segment()
236 return -EINVAL; in jpeg_reference_segment()
238 len = get_unaligned_be16(stream->curr); in jpeg_reference_segment()
239 if (stream->curr + len > stream->end) in jpeg_reference_segment()
240 return -EINVAL; in jpeg_reference_segment()
242 segment->start = stream->curr; in jpeg_reference_segment()
243 segment->length = len; in jpeg_reference_segment()
250 if (nf == 1) in v4l2_jpeg_decode_subsampling()
253 /* no chroma subsampling for 4-component images */ in v4l2_jpeg_decode_subsampling()
255 return -EINVAL; in v4l2_jpeg_decode_subsampling()
267 return -EINVAL; in v4l2_jpeg_decode_subsampling()
278 /* Lf = 8 + 3 * Nf, Nf >= 1 */ in jpeg_parse_frame_header()
280 return -EINVAL; in jpeg_parse_frame_header()
283 /* Table B.2 - Frame header parameter sizes and values */ in jpeg_parse_frame_header()
291 * Baseline DCT only supports 8-bit precision. in jpeg_parse_frame_header()
292 * Extended sequential DCT also supports 12-bit precision. in jpeg_parse_frame_header()
295 return -EINVAL; in jpeg_parse_frame_header()
301 return -EINVAL; in jpeg_parse_frame_header()
307 return -EINVAL; in jpeg_parse_frame_header()
313 * The spec allows 1 <= Nf <= 255, but we only support up to 4 in jpeg_parse_frame_header()
316 if (nf < 1 || nf > V4L2_JPEG_MAX_COMPONENTS) in jpeg_parse_frame_header()
317 return -EINVAL; in jpeg_parse_frame_header()
319 return -EINVAL; in jpeg_parse_frame_header()
321 frame_header->precision = p; in jpeg_parse_frame_header()
322 frame_header->height = y; in jpeg_parse_frame_header()
323 frame_header->width = x; in jpeg_parse_frame_header()
324 frame_header->num_components = nf; in jpeg_parse_frame_header()
343 frame_header->subsampling = subs; in jpeg_parse_frame_header()
345 /* all chroma sampling factors must be 1 */ in jpeg_parse_frame_header()
346 return -EINVAL; in jpeg_parse_frame_header()
353 component = &frame_header->component[i]; in jpeg_parse_frame_header()
354 component->component_identifier = c; in jpeg_parse_frame_header()
355 component->horizontal_sampling_factor = in jpeg_parse_frame_header()
357 component->vertical_sampling_factor = h_v & 0xf; in jpeg_parse_frame_header()
358 component->quantization_table_selector = tq; in jpeg_parse_frame_header()
361 return jpeg_skip(stream, len - 2); in jpeg_parse_frame_header()
375 /* Ls = 8 + 3 * Ns, Ns >= 1 */ in jpeg_parse_scan_header()
377 return -EINVAL; in jpeg_parse_scan_header()
386 if (ns < 1 || ns > 4 || len != 6 + 2 * ns) in jpeg_parse_scan_header()
387 return -EINVAL; in jpeg_parse_scan_header()
389 scan_header->num_components = ns; in jpeg_parse_scan_header()
403 component = &scan_header->component[i]; in jpeg_parse_scan_header()
404 component->component_selector = cs; in jpeg_parse_scan_header()
405 component->dc_entropy_coding_table_selector = in jpeg_parse_scan_header()
407 component->ac_entropy_coding_table_selector = in jpeg_parse_scan_header()
413 skip = len - 2; in jpeg_parse_scan_header()
419 /* B.2.4.1 Quantization table-specification syntax */
428 /* Lq = 2 + n * 65 (for baseline DCT), n >= 1 */ in jpeg_parse_quantization_tables()
430 return -EINVAL; in jpeg_parse_quantization_tables()
432 len -= 2; in jpeg_parse_quantization_tables()
444 * Only 8-bit Qk values for 8-bit sample precision. Extended in jpeg_parse_quantization_tables()
445 * sequential DCT with 12-bit sample precision also supports in jpeg_parse_quantization_tables()
446 * 16-bit Qk values. in jpeg_parse_quantization_tables()
448 if (pq != 0 && (pq != 1 || precision != 12)) in jpeg_parse_quantization_tables()
449 return -EINVAL; in jpeg_parse_quantization_tables()
454 return -EINVAL; in jpeg_parse_quantization_tables()
457 qk = stream->curr; in jpeg_parse_quantization_tables()
460 return -EINVAL; in jpeg_parse_quantization_tables()
467 len -= pq ? 129 : 65; in jpeg_parse_quantization_tables()
473 /* B.2.4.2 Huffman table-specification syntax */
482 /* Table B.5 - Huffman table specification parameter sizes and values */ in jpeg_parse_huffman_tables()
484 return -EINVAL; in jpeg_parse_huffman_tables()
486 for (len -= 2; len >= 17; len -= 17 + mt) { in jpeg_parse_huffman_tables()
494 /* table class - 0 = DC, 1 = AC */ in jpeg_parse_huffman_tables()
496 if (tc > 1) in jpeg_parse_huffman_tables()
497 return -EINVAL; in jpeg_parse_huffman_tables()
502 if (th > 1) in jpeg_parse_huffman_tables()
503 return -EINVAL; in jpeg_parse_huffman_tables()
505 /* BITS - number of Huffman codes with length i */ in jpeg_parse_huffman_tables()
506 table = stream->curr; in jpeg_parse_huffman_tables()
517 /* HUFFVAL - values associated with each Huffman code */ in jpeg_parse_huffman_tables()
523 tables[(tc << 1) | th].start = table; in jpeg_parse_huffman_tables()
524 tables[(tc << 1) | th].length = stream->curr - table; in jpeg_parse_huffman_tables()
528 return jpeg_skip(stream, len - 2); in jpeg_parse_huffman_tables()
541 return -EINVAL; in jpeg_parse_restart_interval()
559 return -EINVAL; in jpeg_skip_segment()
561 return jpeg_skip(stream, len - 2); in jpeg_skip_segment()
564 /* Rec. ITU-T T.872 (06/2012) 6.5.3 */
577 if (stream->curr + 6 > stream->end || in jpeg_parse_app14_data()
578 strncmp(stream->curr, "Adobe\0", 6)) in jpeg_parse_app14_data()
579 return jpeg_skip(stream, lp - 2); in jpeg_parse_app14_data()
593 skip = lp - 2 - 11 - 1; in jpeg_parse_app14_data()
598 * v4l2_jpeg_parse_header - locate marker segments and optionally parse headers
603 * The out->scan_header pointer must be initialized to NULL or point to a valid
604 * v4l2_jpeg_scan_header structure. The out->huffman_tables and
605 * out->quantization_tables pointers must be initialized to NULL or point to a
619 out->num_dht = 0; in v4l2_jpeg_parse_header()
620 out->num_dqt = 0; in v4l2_jpeg_parse_header()
622 /* the first bytes must be SOI, B.2.1 High-level syntax */ in v4l2_jpeg_parse_header()
624 return -EINVAL; in v4l2_jpeg_parse_header()
627 out->app14_tf = V4L2_JPEG_APP14_TF_UNKNOWN; in v4l2_jpeg_parse_header()
634 ret = jpeg_reference_segment(&stream, &out->sof); in v4l2_jpeg_parse_header()
638 &out->frame); in v4l2_jpeg_parse_header()
649 return -EINVAL; in v4l2_jpeg_parse_header()
653 &out->dht[out->num_dht++ % 4]); in v4l2_jpeg_parse_header()
656 if (!out->huffman_tables) { in v4l2_jpeg_parse_header()
661 out->huffman_tables); in v4l2_jpeg_parse_header()
665 &out->dqt[out->num_dqt++ % 4]); in v4l2_jpeg_parse_header()
668 if (!out->quantization_tables) { in v4l2_jpeg_parse_header()
673 out->frame.precision, in v4l2_jpeg_parse_header()
674 out->quantization_tables); in v4l2_jpeg_parse_header()
678 &out->restart_interval); in v4l2_jpeg_parse_header()
682 &out->app14_tf); in v4l2_jpeg_parse_header()
685 ret = jpeg_reference_segment(&stream, &out->sos); in v4l2_jpeg_parse_header()
688 ret = jpeg_parse_scan_header(&stream, out->scan); in v4l2_jpeg_parse_header()
693 out->ecs_offset = stream.curr - (u8 *)buf; in v4l2_jpeg_parse_header()
716 * v4l2_jpeg_parse_frame_header - parse frame header
735 * v4l2_jpeg_parse_scan_header - parse scan header
754 * v4l2_jpeg_parse_quantization_tables - parse quantization tables segment
775 * v4l2_jpeg_parse_huffman_tables - parse huffman tables segment