rtpdec_jpeg: Write the DHT section properly
[libav.git] / libavformat / rtpdec_jpeg.c
CommitLineData
3c198154
SP
1/*
2 * RTP JPEG-compressed Video Depacketizer, RFC 2435
3 * Copyright (c) 2012 Samuel Pitoiset
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avformat.h"
23#include "rtpdec_formats.h"
24#include "libavutil/intreadwrite.h"
25#include "libavcodec/mjpeg.h"
26
27/**
28 * RTP/JPEG specific private data.
29 */
30struct PayloadContext {
31 AVIOContext *frame; ///< current frame buffer
32 uint32_t timestamp; ///< current frame timestamp
33 int hdr_size; ///< size of the current frame header
34};
35
55e778be
SP
36static const uint8_t default_quantizers[128] = {
37 /* luma table */
38 16, 11, 12, 14, 12, 10, 16, 14,
39 13, 14, 18, 17, 16, 19, 24, 40,
40 26, 24, 22, 22, 24, 49, 35, 37,
41 29, 40, 58, 51, 61, 60, 57, 51,
42 56, 55, 64, 72, 92, 78, 64, 68,
43 87, 69, 55, 56, 80, 109, 81, 87,
44 95, 98, 103, 104, 103, 62, 77, 113,
45 121, 112, 100, 120, 92, 101, 103, 99,
46
47 /* chroma table */
48 17, 18, 18, 24, 21, 24, 47, 26,
49 26, 47, 99, 66, 56, 66, 99, 99,
50 99, 99, 99, 99, 99, 99, 99, 99,
51 99, 99, 99, 99, 99, 99, 99, 99,
52 99, 99, 99, 99, 99, 99, 99, 99,
53 99, 99, 99, 99, 99, 99, 99, 99,
54 99, 99, 99, 99, 99, 99, 99, 99,
55 99, 99, 99, 99, 99, 99, 99, 99
56};
57
3c198154
SP
58static PayloadContext *jpeg_new_context(void)
59{
60 return av_mallocz(sizeof(PayloadContext));
61}
62
63static inline void free_frame_if_needed(PayloadContext *jpeg)
64{
65 if (jpeg->frame) {
66 uint8_t *p;
67 avio_close_dyn_buf(jpeg->frame, &p);
68 av_free(p);
69 jpeg->frame = NULL;
70 }
71}
72
73static void jpeg_free_context(PayloadContext *jpeg)
74{
75 free_frame_if_needed(jpeg);
76 av_free(jpeg);
77}
78
7ef43234
MS
79static int jpeg_create_huffman_table(PutBitContext *p, int table_class,
80 int table_id, const uint8_t *bits_table,
81 const uint8_t *value_table)
3c198154
SP
82{
83 int i, n = 0;
84
3c198154
SP
85 put_bits(p, 4, table_class);
86 put_bits(p, 4, table_id);
87
88 for (i = 1; i <= 16; i++) {
89 n += bits_table[i];
90 put_bits(p, 8, bits_table[i]);
91 }
92
93 for (i = 0; i < n; i++) {
94 put_bits(p, 8, value_table[i]);
95 }
7ef43234 96 return n + 17;
3c198154
SP
97}
98
99static int jpeg_create_header(uint8_t *buf, int size, uint32_t type, uint32_t w,
100 uint32_t h, const uint8_t *qtable, int nb_qtable)
101{
102 PutBitContext pbc;
7ef43234
MS
103 uint8_t *dht_size_ptr;
104 int dht_size;
3c198154
SP
105
106 init_put_bits(&pbc, buf, size);
107
108 /* Convert from blocks to pixels. */
109 w <<= 3;
110 h <<= 3;
111
112 /* SOI */
113 put_marker(&pbc, SOI);
114
115 /* JFIF header */
116 put_marker(&pbc, APP0);
117 put_bits(&pbc, 16, 16);
118 avpriv_put_string(&pbc, "JFIF", 1);
119 put_bits(&pbc, 16, 0x0201);
120 put_bits(&pbc, 8, 0);
121 put_bits(&pbc, 16, 1);
122 put_bits(&pbc, 16, 1);
123 put_bits(&pbc, 8, 0);
124 put_bits(&pbc, 8, 0);
125
126 /* DQT */
127 put_marker(&pbc, DQT);
128 if (nb_qtable == 2) {
129 put_bits(&pbc, 16, 2 + 2 * (1 + 64));
130 } else {
131 put_bits(&pbc, 16, 2 + 1 * (1 + 64));
132 }
133 put_bits(&pbc, 8, 0);
134
135 /* Each table is an array of 64 values given in zig-zag
136 * order, identical to the format used in a JFIF DQT
137 * marker segment. */
138 avpriv_copy_bits(&pbc, qtable, 64 * 8);
139
140 if (nb_qtable == 2) {
141 put_bits(&pbc, 8, 1);
142 avpriv_copy_bits(&pbc, qtable + 64, 64 * 8);
143 }
144
145 /* DHT */
146 put_marker(&pbc, DHT);
7ef43234
MS
147 flush_put_bits(&pbc);
148 dht_size_ptr = put_bits_ptr(&pbc);
149 put_bits(&pbc, 16, 0);
150
151 dht_size = 2;
152 dht_size += jpeg_create_huffman_table(&pbc, 0, 0,avpriv_mjpeg_bits_dc_luminance,
153 avpriv_mjpeg_val_dc);
154 dht_size += jpeg_create_huffman_table(&pbc, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
155 avpriv_mjpeg_val_dc);
156 dht_size += jpeg_create_huffman_table(&pbc, 1, 0, avpriv_mjpeg_bits_ac_luminance,
157 avpriv_mjpeg_val_ac_luminance);
158 dht_size += jpeg_create_huffman_table(&pbc, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
159 avpriv_mjpeg_val_ac_chrominance);
160 AV_WB16(dht_size_ptr, dht_size);
3c198154
SP
161
162 /* SOF0 */
163 put_marker(&pbc, SOF0);
164 put_bits(&pbc, 16, 17);
165 put_bits(&pbc, 8, 8);
166 put_bits(&pbc, 8, h >> 8);
167 put_bits(&pbc, 8, h);
168 put_bits(&pbc, 8, w >> 8);
169 put_bits(&pbc, 8, w);
170 put_bits(&pbc, 8, 3);
171 put_bits(&pbc, 8, 1);
172 put_bits(&pbc, 8, type ? 34 : 33);
173 put_bits(&pbc, 8, 0);
174 put_bits(&pbc, 8, 2);
175 put_bits(&pbc, 8, 17);
176 put_bits(&pbc, 8, nb_qtable == 2 ? 1 : 0);
177 put_bits(&pbc, 8, 3);
178 put_bits(&pbc, 8, 17);
179 put_bits(&pbc, 8, nb_qtable == 2 ? 1 : 0);
180
181 /* SOS */
182 put_marker(&pbc, SOS);
183 put_bits(&pbc, 16, 12);
184 put_bits(&pbc, 8, 3);
185 put_bits(&pbc, 8, 1);
186 put_bits(&pbc, 8, 0);
187 put_bits(&pbc, 8, 2);
188 put_bits(&pbc, 8, 17);
189 put_bits(&pbc, 8, 3);
190 put_bits(&pbc, 8, 17);
191 put_bits(&pbc, 8, 0);
192 put_bits(&pbc, 8, 63);
193 put_bits(&pbc, 8, 0);
194
195 /* Fill the buffer. */
196 flush_put_bits(&pbc);
197
198 /* Return the length in bytes of the JPEG header. */
199 return put_bits_count(&pbc) / 8;
200}
201
55e778be
SP
202static void create_default_qtables(uint8_t *qtables, uint8_t q)
203{
204 int factor = q;
205 int i;
206
207 factor = av_clip(q, 1, 99);
208
209 if (q < 50)
210 q = 5000 / factor;
211 else
212 q = 200 - factor * 2;
213
214 for (i = 0; i < 128; i++) {
215 int val = (default_quantizers[i] * q + 50) / 100;
216
217 /* Limit the quantizers to 1 <= q <= 255. */
218 val = av_clip(val, 1, 255);
219 qtables[i] = val;
220 }
221}
222
3c198154
SP
223static int jpeg_parse_packet(AVFormatContext *ctx, PayloadContext *jpeg,
224 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
225 const uint8_t *buf, int len, int flags)
226{
227 uint8_t type, q, width, height;
228 const uint8_t *qtables = NULL;
229 uint16_t qtable_len;
230 uint32_t off;
231 int ret;
232
233 if (len < 8) {
234 av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
235 return AVERROR_INVALIDDATA;
236 }
237
238 /* Parse the main JPEG header. */
239 off = AV_RB24(buf + 1); /* fragment byte offset */
240 type = AV_RB8(buf + 4); /* id of jpeg decoder params */
241 q = AV_RB8(buf + 5); /* quantization factor (or table id) */
242 width = AV_RB8(buf + 6); /* frame width in 8 pixel blocks */
243 height = AV_RB8(buf + 7); /* frame height in 8 pixel blocks */
244 buf += 8;
245 len -= 8;
246
247 /* Parse the restart marker header. */
248 if (type > 63) {
249 av_log(ctx, AV_LOG_ERROR,
250 "Unimplemented RTP/JPEG restart marker header.\n");
251 return AVERROR_PATCHWELCOME;
252 }
253
254 /* Parse the quantization table header. */
255 if (q > 127 && off == 0) {
256 uint8_t precision;
257
258 if (len < 4) {
259 av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
260 return AVERROR_INVALIDDATA;
261 }
262
263 /* The first byte is reserved for future use. */
264 precision = AV_RB8(buf + 1); /* size of coefficients */
265 qtable_len = AV_RB16(buf + 2); /* length in bytes */
266 buf += 4;
267 len -= 4;
268
269 if (precision)
270 av_log(ctx, AV_LOG_WARNING, "Only 8-bit precision is supported.\n");
271
272 if (q == 255 && qtable_len == 0) {
273 av_log(ctx, AV_LOG_ERROR,
274 "Invalid RTP/JPEG packet. Quantization tables not found.\n");
275 return AVERROR_INVALIDDATA;
276 }
277
278 if (qtable_len > 0) {
279 if (len < qtable_len) {
280 av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
281 return AVERROR_INVALIDDATA;
282 }
283 qtables = buf;
284 buf += qtable_len;
285 len -= qtable_len;
286 }
287 }
288
289 if (off == 0) {
290 /* Start of JPEG data packet. */
55e778be 291 uint8_t new_qtables[128];
3c198154
SP
292 uint8_t hdr[1024];
293
294 /* Skip the current frame in case of the end packet
295 * has been lost somewhere. */
296 free_frame_if_needed(jpeg);
297
298 if ((ret = avio_open_dyn_buf(&jpeg->frame)) < 0)
299 return ret;
300 jpeg->timestamp = *timestamp;
301
302 if (!qtables) {
55e778be
SP
303 create_default_qtables(new_qtables, q);
304 qtables = new_qtables;
305 qtable_len = sizeof(new_qtables);
3c198154
SP
306 }
307
308 /* Generate a frame and scan headers that can be prepended to the
309 * RTP/JPEG data payload to produce a JPEG compressed image in
310 * interchange format. */
311 jpeg->hdr_size = jpeg_create_header(hdr, sizeof(hdr), type, width,
312 height, qtables,
313 qtable_len > 64 ? 2 : 1);
314
315 /* Copy JPEG header to frame buffer. */
316 avio_write(jpeg->frame, hdr, jpeg->hdr_size);
317 }
318
319 if (!jpeg->frame) {
320 av_log(ctx, AV_LOG_ERROR,
321 "Received packet without a start chunk; dropping frame.\n");
322 return AVERROR(EAGAIN);
323 }
324
325 if (jpeg->timestamp != *timestamp) {
326 /* Skip the current frame if timestamp is incorrect.
327 * A start packet has been lost somewhere. */
328 free_frame_if_needed(jpeg);
329 av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match.\n");
330 return AVERROR_INVALIDDATA;
331 }
332
333 if (off != avio_tell(jpeg->frame) - jpeg->hdr_size) {
334 av_log(ctx, AV_LOG_ERROR,
335 "Missing packets; dropping frame.\n");
336 return AVERROR(EAGAIN);
337 }
338
339 /* Copy data to frame buffer. */
340 avio_write(jpeg->frame, buf, len);
341
342 if (flags & RTP_FLAG_MARKER) {
343 /* End of JPEG data packet. */
344 PutBitContext pbc;
345 uint8_t buf[2];
346
347 /* Put EOI marker. */
348 init_put_bits(&pbc, buf, sizeof(buf));
349 put_marker(&pbc, EOI);
350 flush_put_bits(&pbc);
351 avio_write(jpeg->frame, buf, sizeof(buf));
352
353 /* Prepare the JPEG packet. */
354 av_init_packet(pkt);
355 pkt->size = avio_close_dyn_buf(jpeg->frame, &pkt->data);
356 if (pkt->size < 0) {
357 av_log(ctx, AV_LOG_ERROR,
358 "Error occured when getting frame buffer.\n");
359 jpeg->frame = NULL;
360 return pkt->size;
361 }
362 pkt->stream_index = st->index;
363 pkt->destruct = av_destruct_packet;
364
365 /* Re-init the frame buffer. */
366 jpeg->frame = NULL;
367
368 return 0;
369 }
370
371 return AVERROR(EAGAIN);
372}
373
374RTPDynamicProtocolHandler ff_jpeg_dynamic_handler = {
375 .enc_name = "JPEG",
376 .codec_type = AVMEDIA_TYPE_VIDEO,
377 .codec_id = AV_CODEC_ID_MJPEG,
378 .alloc = jpeg_new_context,
379 .free = jpeg_free_context,
380 .parse_packet = jpeg_parse_packet,
381 .static_payload_id = 26,
382};