1 /*
2 * RTP JPEG-compressed video Packetizer, RFC 2435
3 * Copyright (c) 2012 Samuel Pitoiset
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
27
29 {
31 const uint8_t *qtables[4] = {
NULL };
32 int nb_qtables = 0;
35 uint8_t *p;
36 int off = 0; /* fragment offset of the current JPEG frame */
39 int default_huffman_tables = 0;
40
42 s->timestamp =
s->cur_timestamp;
43
44 /* convert video pixel dimensions from pixels to blocks */
47
48 /* get the pixel format type or fail */
57 } else {
59 return;
60 }
61
62 /* preparse the header for getting some info */
65 continue;
66
67 if (buf[
i + 1] ==
DQT) {
69 if (buf[
i + 4] & 0xF0)
71 "Only 8-bit precision is supported.\n");
72
73 /* a quantization table is 64 bytes long */
77 return;
78 }
79 if (nb_qtables +
tables > 4) {
81 return;
82 }
83
84 for (j = 0; j <
tables; j++)
85 qtables[nb_qtables + j] = buf +
i + 5 + j * 65;
87 }
else if (buf[
i + 1] ==
SOF0) {
88 if (buf[
i + 14] != 17 || buf[
i + 17] != 17) {
90 "Only 1x1 chroma blocks are supported. Aborted!\n");
91 return;
92 }
93 }
else if (buf[
i + 1] ==
DHT) {
95 default_huffman_tables |= 1 << 4;
97 dht_size -= 2;
98 if (
i + dht_size >=
size)
99 continue;
100 while (dht_size > 0)
101 switch (buf[
i + 1]) {
102 case 0x00:
103 if ( dht_size >= 29
106 default_huffman_tables |= 1;
108 dht_size -= 29;
109 } else {
111 dht_size = 0;
112 }
113 break;
114 case 0x01:
115 if ( dht_size >= 29
118 default_huffman_tables |= 1 << 1;
120 dht_size -= 29;
121 } else {
123 dht_size = 0;
124 }
125 break;
126 case 0x10:
127 if ( dht_size >= 179
130 default_huffman_tables |= 1 << 2;
132 dht_size -= 179;
133 } else {
135 dht_size = 0;
136 }
137 break;
138 case 0x11:
139 if ( dht_size >= 179
142 default_huffman_tables |= 1 << 3;
144 dht_size -= 179;
145 } else {
147 dht_size = 0;
148 }
149 break;
150 default:
152 dht_size = 0;
153 continue;
154 }
155 }
else if (buf[
i + 1] ==
SOS) {
156 /* SOS is last marker in the header */
160 "Insufficient data. Aborted!\n");
161 return;
162 }
163 break;
164 }
165 }
166 if (default_huffman_tables && default_huffman_tables != 31) {
168 "RFC 2435 requires standard Huffman tables for jpeg\n");
169 return;
170 }
171 if (nb_qtables && nb_qtables != 2)
173 "RFC 2435 suggests two quantization tables, %d provided\n",
174 nb_qtables);
175
176 /* skip JPEG header */
179
180 for (
i =
size - 2;
i >= 0;
i--) {
181 if (buf[
i] == 0xff && buf[
i + 1] ==
EOI) {
182 /* Remove the EOI marker */
184 break;
185 }
186 }
187
190 int hdr_size = 8;
191
192 if (off == 0 && nb_qtables)
193 hdr_size += 4 + 64 * nb_qtables;
194
195 /* payload max in one packet */
197
198 /* set main header */
199 bytestream_put_byte(&p, 0);
200 bytestream_put_be24(&p, off);
201 bytestream_put_byte(&p,
type);
202 bytestream_put_byte(&p, 255);
203 bytestream_put_byte(&p,
w);
204 bytestream_put_byte(&p,
h);
205
206 if (off == 0 && nb_qtables) {
207 /* set quantization tables header */
208 bytestream_put_byte(&p, 0);
209 bytestream_put_byte(&p, 0);
210 bytestream_put_be16(&p, 64 * nb_qtables);
211
212 for (
i = 0;
i < nb_qtables;
i++)
214 }
215
216 /* copy payload data */
218
219 /* marker bit is last packet in frame */
221
226 }
227 }