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