rtpdec_jpeg: Simplify writing of the jpeg header
[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 103 uint8_t *dht_size_ptr;
43957fcc 104 int dht_size, i;
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);
43957fcc 128 put_bits(&pbc, 16, 2 + nb_qtable * (1 + 64));
3c198154 129
43957fcc
MS
130 for (i = 0; i < nb_qtable; i++) {
131 put_bits(&pbc, 8, i);
3c198154 132
43957fcc
MS
133 /* Each table is an array of 64 values given in zig-zag
134 * order, identical to the format used in a JFIF DQT
135 * marker segment. */
136 avpriv_copy_bits(&pbc, qtable + 64 * i, 64 * 8);
3c198154
SP
137 }
138
139 /* DHT */
140 put_marker(&pbc, DHT);
7ef43234
MS
141 flush_put_bits(&pbc);
142 dht_size_ptr = put_bits_ptr(&pbc);
143 put_bits(&pbc, 16, 0);
144
145 dht_size = 2;
146 dht_size += jpeg_create_huffman_table(&pbc, 0, 0,avpriv_mjpeg_bits_dc_luminance,
147 avpriv_mjpeg_val_dc);
148 dht_size += jpeg_create_huffman_table(&pbc, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
149 avpriv_mjpeg_val_dc);
150 dht_size += jpeg_create_huffman_table(&pbc, 1, 0, avpriv_mjpeg_bits_ac_luminance,
151 avpriv_mjpeg_val_ac_luminance);
152 dht_size += jpeg_create_huffman_table(&pbc, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
153 avpriv_mjpeg_val_ac_chrominance);
154 AV_WB16(dht_size_ptr, dht_size);
3c198154
SP
155
156 /* SOF0 */
157 put_marker(&pbc, SOF0);
158 put_bits(&pbc, 16, 17);
159 put_bits(&pbc, 8, 8);
43957fcc
MS
160 put_bits(&pbc, 16, h);
161 put_bits(&pbc, 16, w);
3c198154
SP
162 put_bits(&pbc, 8, 3);
163 put_bits(&pbc, 8, 1);
164 put_bits(&pbc, 8, type ? 34 : 33);
165 put_bits(&pbc, 8, 0);
166 put_bits(&pbc, 8, 2);
167 put_bits(&pbc, 8, 17);
168 put_bits(&pbc, 8, nb_qtable == 2 ? 1 : 0);
169 put_bits(&pbc, 8, 3);
170 put_bits(&pbc, 8, 17);
171 put_bits(&pbc, 8, nb_qtable == 2 ? 1 : 0);
172
173 /* SOS */
174 put_marker(&pbc, SOS);
175 put_bits(&pbc, 16, 12);
176 put_bits(&pbc, 8, 3);
177 put_bits(&pbc, 8, 1);
178 put_bits(&pbc, 8, 0);
179 put_bits(&pbc, 8, 2);
180 put_bits(&pbc, 8, 17);
181 put_bits(&pbc, 8, 3);
182 put_bits(&pbc, 8, 17);
183 put_bits(&pbc, 8, 0);
184 put_bits(&pbc, 8, 63);
185 put_bits(&pbc, 8, 0);
186
187 /* Fill the buffer. */
188 flush_put_bits(&pbc);
189
190 /* Return the length in bytes of the JPEG header. */
191 return put_bits_count(&pbc) / 8;
192}
193
55e778be
SP
194static void create_default_qtables(uint8_t *qtables, uint8_t q)
195{
196 int factor = q;
197 int i;
198
199 factor = av_clip(q, 1, 99);
200
201 if (q < 50)
202 q = 5000 / factor;
203 else
204 q = 200 - factor * 2;
205
206 for (i = 0; i < 128; i++) {
207 int val = (default_quantizers[i] * q + 50) / 100;
208
209 /* Limit the quantizers to 1 <= q <= 255. */
210 val = av_clip(val, 1, 255);
211 qtables[i] = val;
212 }
213}
214
3c198154
SP
215static int jpeg_parse_packet(AVFormatContext *ctx, PayloadContext *jpeg,
216 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
217 const uint8_t *buf, int len, int flags)
218{
219 uint8_t type, q, width, height;
220 const uint8_t *qtables = NULL;
221 uint16_t qtable_len;
222 uint32_t off;
223 int ret;
224
225 if (len < 8) {
226 av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
227 return AVERROR_INVALIDDATA;
228 }
229
230 /* Parse the main JPEG header. */
231 off = AV_RB24(buf + 1); /* fragment byte offset */
232 type = AV_RB8(buf + 4); /* id of jpeg decoder params */
233 q = AV_RB8(buf + 5); /* quantization factor (or table id) */
234 width = AV_RB8(buf + 6); /* frame width in 8 pixel blocks */
235 height = AV_RB8(buf + 7); /* frame height in 8 pixel blocks */
236 buf += 8;
237 len -= 8;
238
239 /* Parse the restart marker header. */
240 if (type > 63) {
241 av_log(ctx, AV_LOG_ERROR,
242 "Unimplemented RTP/JPEG restart marker header.\n");
243 return AVERROR_PATCHWELCOME;
244 }
245
246 /* Parse the quantization table header. */
932d8300
MS
247 if (off == 0) {
248 /* Start of JPEG data packet. */
249 uint8_t new_qtables[128];
250 uint8_t hdr[1024];
3c198154 251
932d8300
MS
252 if (q > 127) {
253 uint8_t precision;
254 if (len < 4) {
255 av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
256 return AVERROR_INVALIDDATA;
257 }
3c198154 258
932d8300
MS
259 /* The first byte is reserved for future use. */
260 precision = AV_RB8(buf + 1); /* size of coefficients */
261 qtable_len = AV_RB16(buf + 2); /* length in bytes */
262 buf += 4;
263 len -= 4;
3c198154 264
932d8300
MS
265 if (precision)
266 av_log(ctx, AV_LOG_WARNING, "Only 8-bit precision is supported.\n");
3c198154 267
932d8300
MS
268 if (q == 255 && qtable_len == 0) {
269 av_log(ctx, AV_LOG_ERROR,
270 "Invalid RTP/JPEG packet. Quantization tables not found.\n");
3c198154
SP
271 return AVERROR_INVALIDDATA;
272 }
3c198154 273
932d8300
MS
274 if (qtable_len > 0) {
275 if (len < qtable_len) {
276 av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
277 return AVERROR_INVALIDDATA;
278 }
279 qtables = buf;
280 buf += qtable_len;
281 len -= qtable_len;
282 }
283 }
3c198154
SP
284
285 /* Skip the current frame in case of the end packet
286 * has been lost somewhere. */
287 free_frame_if_needed(jpeg);
288
289 if ((ret = avio_open_dyn_buf(&jpeg->frame)) < 0)
290 return ret;
291 jpeg->timestamp = *timestamp;
292
293 if (!qtables) {
55e778be
SP
294 create_default_qtables(new_qtables, q);
295 qtables = new_qtables;
296 qtable_len = sizeof(new_qtables);
3c198154
SP
297 }
298
299 /* Generate a frame and scan headers that can be prepended to the
300 * RTP/JPEG data payload to produce a JPEG compressed image in
301 * interchange format. */
302 jpeg->hdr_size = jpeg_create_header(hdr, sizeof(hdr), type, width,
303 height, qtables,
304 qtable_len > 64 ? 2 : 1);
305
306 /* Copy JPEG header to frame buffer. */
307 avio_write(jpeg->frame, hdr, jpeg->hdr_size);
308 }
309
310 if (!jpeg->frame) {
311 av_log(ctx, AV_LOG_ERROR,
312 "Received packet without a start chunk; dropping frame.\n");
313 return AVERROR(EAGAIN);
314 }
315
316 if (jpeg->timestamp != *timestamp) {
317 /* Skip the current frame if timestamp is incorrect.
318 * A start packet has been lost somewhere. */
319 free_frame_if_needed(jpeg);
320 av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match.\n");
321 return AVERROR_INVALIDDATA;
322 }
323
324 if (off != avio_tell(jpeg->frame) - jpeg->hdr_size) {
325 av_log(ctx, AV_LOG_ERROR,
326 "Missing packets; dropping frame.\n");
327 return AVERROR(EAGAIN);
328 }
329
330 /* Copy data to frame buffer. */
331 avio_write(jpeg->frame, buf, len);
332
333 if (flags & RTP_FLAG_MARKER) {
334 /* End of JPEG data packet. */
335 PutBitContext pbc;
336 uint8_t buf[2];
337
338 /* Put EOI marker. */
339 init_put_bits(&pbc, buf, sizeof(buf));
340 put_marker(&pbc, EOI);
341 flush_put_bits(&pbc);
342 avio_write(jpeg->frame, buf, sizeof(buf));
343
344 /* Prepare the JPEG packet. */
345 av_init_packet(pkt);
346 pkt->size = avio_close_dyn_buf(jpeg->frame, &pkt->data);
347 if (pkt->size < 0) {
348 av_log(ctx, AV_LOG_ERROR,
349 "Error occured when getting frame buffer.\n");
350 jpeg->frame = NULL;
351 return pkt->size;
352 }
353 pkt->stream_index = st->index;
354 pkt->destruct = av_destruct_packet;
355
356 /* Re-init the frame buffer. */
357 jpeg->frame = NULL;
358
359 return 0;
360 }
361
362 return AVERROR(EAGAIN);
363}
364
365RTPDynamicProtocolHandler ff_jpeg_dynamic_handler = {
366 .enc_name = "JPEG",
367 .codec_type = AVMEDIA_TYPE_VIDEO,
368 .codec_id = AV_CODEC_ID_MJPEG,
369 .alloc = jpeg_new_context,
370 .free = jpeg_free_context,
371 .parse_packet = jpeg_parse_packet,
372 .static_payload_id = 26,
373};