From 0d3f3c8ed2079a1890a519d79de3180f95e385d9 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Fri, 2 Aug 2024 12:19:41 +0200 Subject: [PATCH] rtpdec_jpeg: compute Q tables for 0-127 code taken directly from RFC 2435 text --- src/rtp/rtpdec_jpeg.c | 68 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/src/rtp/rtpdec_jpeg.c b/src/rtp/rtpdec_jpeg.c index 4da206278..7447841d4 100644 --- a/src/rtp/rtpdec_jpeg.c +++ b/src/rtp/rtpdec_jpeg.c @@ -4,9 +4,7 @@ * @author Martin Pulec * * @todo - * * handle predefined (0-127) quantization tables * * handle precision=1 (?) - * * error meesage for zero-len quantization tables */ /* * Copyright (c) 2024 CESNET @@ -191,6 +189,65 @@ skip_jpeg_headers(unsigned char **pckt_data) } } +/* + * Table K.1 from JPEG spec. + */ +static const int jpeg_luma_quantizer[64] = { + 16, 11, 10, 16, 24, 40, 51, 61, + 12, 12, 14, 19, 26, 58, 60, 55, + 14, 13, 16, 24, 40, 57, 69, 56, + 14, 17, 22, 29, 51, 87, 80, 62, + 18, 22, 37, 56, 68, 109, 103, 77, + 24, 35, 55, 64, 81, 104, 113, 92, + 49, 64, 78, 87, 103, 121, 120, 101, + 72, 92, 95, 98, 112, 100, 103, 99 +}; + +/* + * Table K.2 from JPEG spec. + */ +static const int jpeg_chroma_quantizer[64] = { + 17, 18, 24, 47, 99, 99, 99, 99, + 18, 21, 26, 66, 99, 99, 99, 99, + 24, 26, 56, 99, 99, 99, 99, 99, + 47, 66, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99 +}; + +/* + * Call MakeTables with the Q factor and two u_char[64] return arrays + */ +static void +MakeTables(int q, u_char *lqt, u_char *cqt) +{ + int i; + int factor = q; + + if (q < 1) factor = 1; + if (q > 99) factor = 99; + if (q < 50) + q = 5000 / factor; + else + q = 200 - factor*2; + + for (i=0; i < 64; i++) { + int lq = (jpeg_luma_quantizer[i] * q + 50) / 100; + int cq = (jpeg_chroma_quantizer[i] * q + 50) / 100; + + /* Limit the quantizers to 1 <= q <= 255 */ + if (lq < 1) lq = 1; + else if (lq > 255) lq = 255; + lqt[i] = lq; + + if (cq < 1) cq = 1; + else if (cq > 255) cq = 255; + cqt[i] = cq; + } +} + int decode_frame_jpeg(struct coded_data *cdata, void *decode_data) { @@ -236,6 +293,13 @@ decode_frame_jpeg(struct coded_data *cdata, void *decode_data) cdata = cdata->nxt; } + if (!dec_data->jpeg.quantization_table_set[q] && + q < QUANT_TAB_T_FIRST_STATIC) { + MakeTables(q, dec_data->jpeg.quantization_tables[q][0], + dec_data->jpeg.quantization_tables[q][1]); + dec_data->jpeg.quantization_table_set[q] = true; + } + if (!dec_data->jpeg.quantization_table_set[q]) { MSG(WARNING, "Dropping frame - missing quantization tables for Q=%d!\n",