Mercurial > libavcodec.hg
annotate mjpegdec.c @ 12494:94eaea836bf4 libavcodec
Check avctx width/height more thoroughly (e.g. all values 0 except width would
have been accepted before).
Also do not fail if they are invalid but instead override them to 0.
This allows decoding e.g. MPEG video when only the container values are corrupted.
For encoding a value of 0,0 of course makes no sense, but was allowed
through before and will be caught by an extra check in the encode function.
author | reimar |
---|---|
date | Wed, 15 Sep 2010 04:46:55 +0000 |
parents | ffb3668ff7af |
children |
rev | line source |
---|---|
5020 | 1 /* |
2 * MJPEG decoder | |
8629
04423b2f6e0b
cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents:
8612
diff
changeset
|
3 * Copyright (c) 2000, 2001 Fabrice Bellard |
5020 | 4 * Copyright (c) 2003 Alex Beregszaszi |
5 * Copyright (c) 2003-2004 Michael Niedermayer | |
6 * | |
5214 | 7 * Support for external huffman table, various fixes (AVID workaround), |
8 * aspecting, new decode_frame mechanism and apple mjpeg-b support | |
9 * by Alex Beregszaszi | |
10 * | |
5020 | 11 * This file is part of FFmpeg. |
12 * | |
13 * FFmpeg is free software; you can redistribute it and/or | |
14 * modify it under the terms of the GNU Lesser General Public | |
15 * License as published by the Free Software Foundation; either | |
16 * version 2.1 of the License, or (at your option) any later version. | |
17 * | |
18 * FFmpeg is distributed in the hope that it will be useful, | |
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
21 * Lesser General Public License for more details. | |
22 * | |
23 * You should have received a copy of the GNU Lesser General Public | |
24 * License along with FFmpeg; if not, write to the Free Software | |
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
26 */ | |
27 | |
28 /** | |
11644
7dd2a45249a9
Remove explicit filename from Doxygen @file commands.
diego
parents:
11560
diff
changeset
|
29 * @file |
5020 | 30 * MJPEG decoder. |
31 */ | |
32 | |
33 //#define DEBUG | |
34 #include <assert.h> | |
35 | |
12372
914f484bb476
Remove use of the deprecated function avcodec_check_dimensions(), use
stefano
parents:
12129
diff
changeset
|
36 #include "libavcore/imgutils.h" |
5020 | 37 #include "avcodec.h" |
38 #include "dsputil.h" | |
39 #include "mjpeg.h" | |
5041 | 40 #include "mjpegdec.h" |
5020 | 41 #include "jpeglsdec.h" |
42 | |
43 | |
44 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, | |
45 int nb_codes, int use_static, int is_ac) | |
46 { | |
47 uint8_t huff_size[256+16]; | |
48 uint16_t huff_code[256+16]; | |
49 | |
50 assert(nb_codes <= 256); | |
51 | |
52 memset(huff_size, 0, sizeof(huff_size)); | |
5021 | 53 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table); |
5020 | 54 |
55 if(is_ac){ | |
56 memmove(huff_size+16, huff_size, sizeof(uint8_t)*nb_codes); | |
57 memmove(huff_code+16, huff_code, sizeof(uint16_t)*nb_codes); | |
58 memset(huff_size, 0, sizeof(uint8_t)*16); | |
59 memset(huff_code, 0, sizeof(uint16_t)*16); | |
60 nb_codes += 16; | |
61 } | |
62 | |
63 return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_static); | |
64 } | |
65 | |
5068
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
66 static void build_basic_mjpeg_vlc(MJpegDecodeContext * s) { |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
67 build_vlc(&s->vlcs[0][0], ff_mjpeg_bits_dc_luminance, |
7136 | 68 ff_mjpeg_val_dc, 12, 0, 0); |
5068
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
69 build_vlc(&s->vlcs[0][1], ff_mjpeg_bits_dc_chrominance, |
7136 | 70 ff_mjpeg_val_dc, 12, 0, 0); |
5068
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
71 build_vlc(&s->vlcs[1][0], ff_mjpeg_bits_ac_luminance, |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
72 ff_mjpeg_val_ac_luminance, 251, 0, 1); |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
73 build_vlc(&s->vlcs[1][1], ff_mjpeg_bits_ac_chrominance, |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
74 ff_mjpeg_val_ac_chrominance, 251, 0, 1); |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
75 } |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
76 |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6448
diff
changeset
|
77 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx) |
5020 | 78 { |
79 MJpegDecodeContext *s = avctx->priv_data; | |
80 | |
81 s->avctx = avctx; | |
82 dsputil_init(&s->dsp, avctx); | |
83 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct); | |
84 s->buffer_size = 0; | |
85 s->buffer = NULL; | |
86 s->start_code = -1; | |
87 s->first_picture = 1; | |
88 s->org_height = avctx->coded_height; | |
9626
bd3e11b60ccd
Add a chroma_sample_location field to define positioning of chroma samples
conrad
parents:
9459
diff
changeset
|
89 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER; |
5020 | 90 |
5068
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
91 build_basic_mjpeg_vlc(s); |
5020 | 92 |
93 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF) | |
94 { | |
95 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n"); | |
96 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8); | |
5068
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
97 if (ff_mjpeg_decode_dht(s)) { |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
98 av_log(avctx, AV_LOG_ERROR, "mjpeg: error using external huffman table, switching back to internal\n"); |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
99 build_basic_mjpeg_vlc(s); |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
100 } |
5020 | 101 } |
102 if (avctx->extradata_size > 9 && | |
103 AV_RL32(avctx->extradata + 4) == MKTAG('f','i','e','l')) { | |
104 if (avctx->extradata[9] == 6) { /* quicktime icefloe 019 */ | |
105 s->interlace_polarity = 1; /* bottom field first */ | |
106 av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n"); | |
107 } | |
108 } | |
10438
710e226783f0
Flip (M)JPEG frames encoded by Intel JPEG library.
cehoyos
parents:
10412
diff
changeset
|
109 if (avctx->codec->id == CODEC_ID_AMV) |
710e226783f0
Flip (M)JPEG frames encoded by Intel JPEG library.
cehoyos
parents:
10412
diff
changeset
|
110 s->flipped = 1; |
5020 | 111 |
112 return 0; | |
113 } | |
114 | |
115 | |
116 /* quantize tables */ | |
5044 | 117 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s) |
5020 | 118 { |
119 int len, index, i, j; | |
120 | |
121 len = get_bits(&s->gb, 16) - 2; | |
122 | |
123 while (len >= 65) { | |
124 /* only 8 bit precision handled */ | |
125 if (get_bits(&s->gb, 4) != 0) | |
126 { | |
127 av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n"); | |
128 return -1; | |
129 } | |
130 index = get_bits(&s->gb, 4); | |
131 if (index >= 4) | |
132 return -1; | |
133 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index); | |
134 /* read quant table */ | |
135 for(i=0;i<64;i++) { | |
136 j = s->scantable.permutated[i]; | |
137 s->quant_matrixes[index][j] = get_bits(&s->gb, 8); | |
138 } | |
139 | |
140 //XXX FIXME finetune, and perhaps add dc too | |
141 s->qscale[index]= FFMAX( | |
142 s->quant_matrixes[index][s->scantable.permutated[1]], | |
143 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1; | |
144 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n", index, s->qscale[index]); | |
145 len -= 65; | |
146 } | |
147 | |
148 return 0; | |
149 } | |
150 | |
151 /* decode huffman tables and build VLC decoders */ | |
5044 | 152 int ff_mjpeg_decode_dht(MJpegDecodeContext *s) |
5020 | 153 { |
154 int len, index, i, class, n, v, code_max; | |
155 uint8_t bits_table[17]; | |
156 uint8_t val_table[256]; | |
157 | |
158 len = get_bits(&s->gb, 16) - 2; | |
159 | |
160 while (len > 0) { | |
161 if (len < 17) | |
162 return -1; | |
163 class = get_bits(&s->gb, 4); | |
164 if (class >= 2) | |
165 return -1; | |
166 index = get_bits(&s->gb, 4); | |
167 if (index >= 4) | |
168 return -1; | |
169 n = 0; | |
170 for(i=1;i<=16;i++) { | |
171 bits_table[i] = get_bits(&s->gb, 8); | |
172 n += bits_table[i]; | |
173 } | |
174 len -= 17; | |
175 if (len < n || n > 256) | |
176 return -1; | |
177 | |
178 code_max = 0; | |
179 for(i=0;i<n;i++) { | |
180 v = get_bits(&s->gb, 8); | |
181 if (v > code_max) | |
182 code_max = v; | |
183 val_table[i] = v; | |
184 } | |
185 len -= n; | |
186 | |
187 /* build VLC and flush previous vlc if present */ | |
188 free_vlc(&s->vlcs[class][index]); | |
189 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n", | |
190 class, index, code_max + 1); | |
191 if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){ | |
192 return -1; | |
193 } | |
194 } | |
195 return 0; | |
196 } | |
197 | |
5044 | 198 int ff_mjpeg_decode_sof(MJpegDecodeContext *s) |
5020 | 199 { |
200 int len, nb_components, i, width, height, pix_fmt_id; | |
201 | |
202 /* XXX: verify len field validity */ | |
203 len = get_bits(&s->gb, 16); | |
204 s->bits= get_bits(&s->gb, 8); | |
205 | |
206 if(s->pegasus_rct) s->bits=9; | |
207 if(s->bits==9 && !s->pegasus_rct) s->rct=1; //FIXME ugly | |
208 | |
209 if (s->bits != 8 && !s->lossless){ | |
210 av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n"); | |
211 return -1; | |
212 } | |
213 | |
214 height = get_bits(&s->gb, 16); | |
215 width = get_bits(&s->gb, 16); | |
216 | |
217 //HACK for odd_height.mov | |
218 if(s->interlaced && s->width == width && s->height == height + 1) | |
219 height= s->height; | |
220 | |
221 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height); | |
12462
ffb3668ff7af
Use new imgutils.h API names, fix deprecation warnings.
stefano
parents:
12412
diff
changeset
|
222 if(av_image_check_size(width, height, 0, s->avctx)) |
5020 | 223 return -1; |
224 | |
225 nb_components = get_bits(&s->gb, 8); | |
226 if (nb_components <= 0 || | |
227 nb_components > MAX_COMPONENTS) | |
228 return -1; | |
229 if (s->ls && !(s->bits <= 8 || nb_components == 1)){ | |
230 av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n"); | |
231 return -1; | |
232 } | |
233 s->nb_components = nb_components; | |
234 s->h_max = 1; | |
235 s->v_max = 1; | |
236 for(i=0;i<nb_components;i++) { | |
237 /* component id */ | |
238 s->component_id[i] = get_bits(&s->gb, 8) - 1; | |
239 s->h_count[i] = get_bits(&s->gb, 4); | |
240 s->v_count[i] = get_bits(&s->gb, 4); | |
241 /* compute hmax and vmax (only used in interleaved case) */ | |
242 if (s->h_count[i] > s->h_max) | |
243 s->h_max = s->h_count[i]; | |
244 if (s->v_count[i] > s->v_max) | |
245 s->v_max = s->v_count[i]; | |
246 s->quant_index[i] = get_bits(&s->gb, 8); | |
247 if (s->quant_index[i] >= 4) | |
248 return -1; | |
249 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n", i, s->h_count[i], | |
250 s->v_count[i], s->component_id[i], s->quant_index[i]); | |
251 } | |
252 | |
253 if(s->ls && (s->h_max > 1 || s->v_max > 1)) { | |
254 av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n"); | |
255 return -1; | |
256 } | |
257 | |
258 if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1; | |
259 | |
260 /* if different size, realloc/alloc picture */ | |
261 /* XXX: also check h_count and v_count */ | |
262 if (width != s->width || height != s->height) { | |
263 av_freep(&s->qscale_table); | |
264 | |
265 s->width = width; | |
266 s->height = height; | |
267 s->interlaced = 0; | |
268 | |
269 /* test interlaced mode */ | |
270 if (s->first_picture && | |
271 s->org_height != 0 && | |
272 s->height < ((s->org_height * 3) / 4)) { | |
273 s->interlaced = 1; | |
274 s->bottom_field = s->interlace_polarity; | |
275 s->picture.interlaced_frame = 1; | |
276 s->picture.top_field_first = !s->interlace_polarity; | |
277 height *= 2; | |
278 } | |
279 | |
280 avcodec_set_dimensions(s->avctx, width, height); | |
281 | |
282 s->qscale_table= av_mallocz((s->width+15)/16); | |
283 | |
284 s->first_picture = 0; | |
285 } | |
286 | |
287 if(s->interlaced && (s->bottom_field == !s->interlace_polarity)) | |
288 return 0; | |
289 | |
290 /* XXX: not complete test ! */ | |
7930
fae5e74eef1b
Check the 4th plane too when selecting the pixfmt.
michael
parents:
7478
diff
changeset
|
291 pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) | |
fae5e74eef1b
Check the 4th plane too when selecting the pixfmt.
michael
parents:
7478
diff
changeset
|
292 (s->h_count[1] << 20) | (s->v_count[1] << 16) | |
fae5e74eef1b
Check the 4th plane too when selecting the pixfmt.
michael
parents:
7478
diff
changeset
|
293 (s->h_count[2] << 12) | (s->v_count[2] << 8) | |
fae5e74eef1b
Check the 4th plane too when selecting the pixfmt.
michael
parents:
7478
diff
changeset
|
294 (s->h_count[3] << 4) | s->v_count[3]; |
5020 | 295 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id); |
10549 | 296 //NOTE we do not allocate pictures large enough for the possible padding of h/v_count being 4 |
297 if(!(pix_fmt_id & 0xD0D0D0D0)) | |
7931
fd6c05a225aa
Simplify pix_fmt_id instead of listing a subset of non-simplified ones.
michael
parents:
7930
diff
changeset
|
298 pix_fmt_id-= (pix_fmt_id & 0xF0F0F0F0)>>1; |
10549 | 299 if(!(pix_fmt_id & 0x0D0D0D0D)) |
7931
fd6c05a225aa
Simplify pix_fmt_id instead of listing a subset of non-simplified ones.
michael
parents:
7930
diff
changeset
|
300 pix_fmt_id-= (pix_fmt_id & 0x0F0F0F0F)>>1; |
fd6c05a225aa
Simplify pix_fmt_id instead of listing a subset of non-simplified ones.
michael
parents:
7930
diff
changeset
|
301 |
5020 | 302 switch(pix_fmt_id){ |
7930
fae5e74eef1b
Check the 4th plane too when selecting the pixfmt.
michael
parents:
7478
diff
changeset
|
303 case 0x11111100: |
5020 | 304 if(s->rgb){ |
10666
7127645ee791
Lossless jpeg expects and uses BGRA not RGB32 (this probably caused a problem on
michael
parents:
10549
diff
changeset
|
305 s->avctx->pix_fmt = PIX_FMT_BGRA; |
7932
7729da3e4866
Replace apparently always true condition by assert().
michael
parents:
7931
diff
changeset
|
306 }else |
5020 | 307 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P; |
7932
7729da3e4866
Replace apparently always true condition by assert().
michael
parents:
7931
diff
changeset
|
308 assert(s->nb_components==3); |
5020 | 309 break; |
7930
fae5e74eef1b
Check the 4th plane too when selecting the pixfmt.
michael
parents:
7478
diff
changeset
|
310 case 0x11000000: |
5478
41103dc22ad5
Add support for grayscale MJPEG streams sent by Axis cameras such as the
diego
parents:
5381
diff
changeset
|
311 s->avctx->pix_fmt = PIX_FMT_GRAY8; |
41103dc22ad5
Add support for grayscale MJPEG streams sent by Axis cameras such as the
diego
parents:
5381
diff
changeset
|
312 break; |
7930
fae5e74eef1b
Check the 4th plane too when selecting the pixfmt.
michael
parents:
7478
diff
changeset
|
313 case 0x12111100: |
5363 | 314 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV440P : PIX_FMT_YUVJ440P; |
315 break; | |
7930
fae5e74eef1b
Check the 4th plane too when selecting the pixfmt.
michael
parents:
7478
diff
changeset
|
316 case 0x21111100: |
5020 | 317 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P; |
318 break; | |
7930
fae5e74eef1b
Check the 4th plane too when selecting the pixfmt.
michael
parents:
7478
diff
changeset
|
319 case 0x22111100: |
5020 | 320 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P; |
321 break; | |
5381
4cac2cfe2745
bail out on unknown jpeg pixel format instead of silently decoding data incorrectly
gpoirier
parents:
5363
diff
changeset
|
322 default: |
4cac2cfe2745
bail out on unknown jpeg pixel format instead of silently decoding data incorrectly
gpoirier
parents:
5363
diff
changeset
|
323 av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id); |
4cac2cfe2745
bail out on unknown jpeg pixel format instead of silently decoding data incorrectly
gpoirier
parents:
5363
diff
changeset
|
324 return -1; |
5020 | 325 } |
326 if(s->ls){ | |
327 if(s->nb_components > 1) | |
328 s->avctx->pix_fmt = PIX_FMT_RGB24; | |
329 else if(s->bits <= 8) | |
330 s->avctx->pix_fmt = PIX_FMT_GRAY8; | |
331 else | |
332 s->avctx->pix_fmt = PIX_FMT_GRAY16; | |
333 } | |
334 | |
335 if(s->picture.data[0]) | |
336 s->avctx->release_buffer(s->avctx, &s->picture); | |
337 | |
338 s->picture.reference= 0; | |
339 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){ | |
340 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
341 return -1; | |
342 } | |
6448
7fa807dd7958
remove #include "mpegvideo.h" where it is not needed
aurel
parents:
6222
diff
changeset
|
343 s->picture.pict_type= FF_I_TYPE; |
5020 | 344 s->picture.key_frame= 1; |
9913
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
345 s->got_picture = 1; |
5020 | 346 |
347 for(i=0; i<3; i++){ | |
348 s->linesize[i]= s->picture.linesize[i] << s->interlaced; | |
349 } | |
350 | |
351 // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height); | |
352 | |
353 if (len != (8+(3*nb_components))) | |
354 { | |
355 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len); | |
356 } | |
357 | |
358 /* totally blank picture as progressive JPEG will only add details to it */ | |
359 if(s->progressive){ | |
8287 | 360 int bw = (width + s->h_max*8-1) / (s->h_max*8); |
361 int bh = (height + s->v_max*8-1) / (s->v_max*8); | |
362 for(i=0; i<s->nb_components; i++) { | |
363 int size = bw * bh * s->h_count[i] * s->v_count[i]; | |
364 av_freep(&s->blocks[i]); | |
365 av_freep(&s->last_nnz[i]); | |
366 s->blocks[i] = av_malloc(size * sizeof(**s->blocks)); | |
367 s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz)); | |
368 s->block_stride[i] = bw * s->h_count[i]; | |
369 } | |
370 memset(s->coefs_finished, 0, sizeof(s->coefs_finished)); | |
5020 | 371 } |
372 return 0; | |
373 } | |
374 | |
375 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index) | |
376 { | |
377 int code; | |
378 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2); | |
379 if (code < 0) | |
380 { | |
381 av_log(s->avctx, AV_LOG_WARNING, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index, | |
382 &s->vlcs[0][dc_index]); | |
383 return 0xffff; | |
384 } | |
385 | |
386 if(code) | |
387 return get_xbits(&s->gb, code); | |
388 else | |
389 return 0; | |
390 } | |
391 | |
392 /* decode block and dequantize */ | |
393 static int decode_block(MJpegDecodeContext *s, DCTELEM *block, | |
394 int component, int dc_index, int ac_index, int16_t *quant_matrix) | |
395 { | |
396 int code, i, j, level, val; | |
397 | |
398 /* DC coef */ | |
399 val = mjpeg_decode_dc(s, dc_index); | |
400 if (val == 0xffff) { | |
401 av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); | |
402 return -1; | |
403 } | |
404 val = val * quant_matrix[0] + s->last_dc[component]; | |
405 s->last_dc[component] = val; | |
406 block[0] = val; | |
407 /* AC coefs */ | |
408 i = 0; | |
409 {OPEN_READER(re, &s->gb) | |
410 for(;;) { | |
411 UPDATE_CACHE(re, &s->gb); | |
412 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) | |
413 | |
414 /* EOB */ | |
415 if (code == 0x10) | |
416 break; | |
417 i += ((unsigned)code) >> 4; | |
418 if(code != 0x100){ | |
419 code &= 0xf; | |
420 if(code > MIN_CACHE_BITS - 16){ | |
421 UPDATE_CACHE(re, &s->gb) | |
422 } | |
423 { | |
424 int cache=GET_CACHE(re,&s->gb); | |
425 int sign=(~cache)>>31; | |
426 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; | |
427 } | |
428 | |
429 LAST_SKIP_BITS(re, &s->gb, code) | |
430 | |
431 if (i >= 63) { | |
432 if(i == 63){ | |
433 j = s->scantable.permutated[63]; | |
434 block[j] = level * quant_matrix[j]; | |
435 break; | |
436 } | |
437 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); | |
438 return -1; | |
439 } | |
440 j = s->scantable.permutated[i]; | |
441 block[j] = level * quant_matrix[j]; | |
442 } | |
443 } | |
444 CLOSE_READER(re, &s->gb)} | |
445 | |
446 return 0; | |
447 } | |
448 | |
8287 | 449 static int decode_dc_progressive(MJpegDecodeContext *s, DCTELEM *block, int component, |
450 int dc_index, int16_t *quant_matrix, int Al) | |
451 { | |
452 int val; | |
8288 | 453 s->dsp.clear_block(block); |
8287 | 454 val = mjpeg_decode_dc(s, dc_index); |
455 if (val == 0xffff) { | |
456 av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); | |
457 return -1; | |
458 } | |
459 val = (val * quant_matrix[0] << Al) + s->last_dc[component]; | |
460 s->last_dc[component] = val; | |
461 block[0] = val; | |
462 return 0; | |
463 } | |
464 | |
5020 | 465 /* decode block and dequantize - progressive JPEG version */ |
8287 | 466 static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz, |
467 int ac_index, int16_t *quant_matrix, | |
468 int ss, int se, int Al, int *EOBRUN) | |
5020 | 469 { |
470 int code, i, j, level, val, run; | |
471 | |
472 if(*EOBRUN){ | |
473 (*EOBRUN)--; | |
474 return 0; | |
475 } | |
476 {OPEN_READER(re, &s->gb) | |
477 for(i=ss;;i++) { | |
478 UPDATE_CACHE(re, &s->gb); | |
479 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) | |
480 /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */ | |
481 code -= 16; | |
482 if(code & 0xF) { | |
483 i += ((unsigned) code) >> 4; | |
484 code &= 0xf; | |
485 if(code > MIN_CACHE_BITS - 16){ | |
486 UPDATE_CACHE(re, &s->gb) | |
487 } | |
488 { | |
489 int cache=GET_CACHE(re,&s->gb); | |
490 int sign=(~cache)>>31; | |
491 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; | |
492 } | |
493 | |
494 LAST_SKIP_BITS(re, &s->gb, code) | |
495 | |
496 if (i >= se) { | |
497 if(i == se){ | |
498 j = s->scantable.permutated[se]; | |
499 block[j] = level * quant_matrix[j] << Al; | |
500 break; | |
501 } | |
502 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); | |
503 return -1; | |
504 } | |
505 j = s->scantable.permutated[i]; | |
506 block[j] = level * quant_matrix[j] << Al; | |
507 }else{ | |
508 run = ((unsigned) code) >> 4; | |
509 if(run == 0xF){// ZRL - skip 15 coefficients | |
510 i += 15; | |
511 }else{ | |
512 val = run; | |
513 run = (1 << run); | |
514 UPDATE_CACHE(re, &s->gb); | |
515 run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1); | |
516 if(val) | |
517 LAST_SKIP_BITS(re, &s->gb, val); | |
518 *EOBRUN = run - 1; | |
519 break; | |
520 } | |
521 } | |
522 } | |
523 CLOSE_READER(re, &s->gb)} | |
8287 | 524 if(i > *last_nnz) |
525 *last_nnz = i; | |
526 return 0; | |
527 } | |
528 | |
529 #define REFINE_BIT(j) {\ | |
530 UPDATE_CACHE(re, &s->gb);\ | |
531 sign = block[j]>>15;\ | |
532 block[j] += SHOW_UBITS(re, &s->gb, 1) * ((quant_matrix[j]^sign)-sign) << Al;\ | |
533 LAST_SKIP_BITS(re, &s->gb, 1);\ | |
534 } | |
535 | |
536 #define ZERO_RUN \ | |
537 for(;;i++) {\ | |
538 if(i > last) {\ | |
539 i += run;\ | |
540 if(i > se) {\ | |
541 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);\ | |
542 return -1;\ | |
543 }\ | |
544 break;\ | |
545 }\ | |
546 j = s->scantable.permutated[i];\ | |
547 if(block[j])\ | |
548 REFINE_BIT(j)\ | |
549 else if(run-- == 0)\ | |
550 break;\ | |
551 } | |
552 | |
553 /* decode block and dequantize - progressive JPEG refinement pass */ | |
554 static int decode_block_refinement(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz, | |
555 int ac_index, int16_t *quant_matrix, | |
556 int ss, int se, int Al, int *EOBRUN) | |
557 { | |
558 int code, i=ss, j, sign, val, run; | |
559 int last = FFMIN(se, *last_nnz); | |
560 | |
561 OPEN_READER(re, &s->gb); | |
562 if(*EOBRUN) | |
563 (*EOBRUN)--; | |
564 else { | |
565 for(;;i++) { | |
566 UPDATE_CACHE(re, &s->gb); | |
567 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) | |
568 /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */ | |
569 code -= 16; | |
570 if(code & 0xF) { | |
571 run = ((unsigned) code) >> 4; | |
572 UPDATE_CACHE(re, &s->gb); | |
573 val = SHOW_UBITS(re, &s->gb, 1); | |
574 LAST_SKIP_BITS(re, &s->gb, 1); | |
575 ZERO_RUN; | |
576 j = s->scantable.permutated[i]; | |
577 val--; | |
578 block[j] = ((quant_matrix[j]^val)-val) << Al; | |
579 if(i == se) { | |
580 if(i > *last_nnz) | |
581 *last_nnz = i; | |
582 CLOSE_READER(re, &s->gb) | |
583 return 0; | |
584 } | |
585 }else{ | |
586 run = ((unsigned) code) >> 4; | |
587 if(run == 0xF){ | |
588 ZERO_RUN; | |
589 }else{ | |
590 val = run; | |
591 run = (1 << run); | |
592 if(val) { | |
593 UPDATE_CACHE(re, &s->gb); | |
594 run += SHOW_UBITS(re, &s->gb, val); | |
595 LAST_SKIP_BITS(re, &s->gb, val); | |
596 } | |
597 *EOBRUN = run - 1; | |
598 break; | |
599 } | |
600 } | |
601 } | |
602 | |
603 if(i > *last_nnz) | |
604 *last_nnz = i; | |
605 } | |
606 | |
607 for(;i<=last;i++) { | |
608 j = s->scantable.permutated[i]; | |
609 if(block[j]) | |
610 REFINE_BIT(j) | |
611 } | |
612 CLOSE_READER(re, &s->gb); | |
5020 | 613 |
614 return 0; | |
615 } | |
8287 | 616 #undef REFINE_BIT |
617 #undef ZERO_RUN | |
5020 | 618 |
619 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){ | |
620 int i, mb_x, mb_y; | |
10487 | 621 uint16_t (*buffer)[4]; |
5020 | 622 int left[3], top[3], topleft[3]; |
623 const int linesize= s->linesize[0]; | |
624 const int mask= (1<<s->bits)-1; | |
625 | |
10487 | 626 av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0])); |
627 buffer= s->ljpeg_buffer; | |
5020 | 628 |
629 for(i=0; i<3; i++){ | |
630 buffer[0][i]= 1 << (s->bits + point_transform - 1); | |
631 } | |
632 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { | |
633 const int modified_predictor= mb_y ? predictor : 1; | |
634 uint8_t *ptr = s->picture.data[0] + (linesize * mb_y); | |
635 | |
636 if (s->interlaced && s->bottom_field) | |
637 ptr += linesize >> 1; | |
638 | |
639 for(i=0; i<3; i++){ | |
640 top[i]= left[i]= topleft[i]= buffer[0][i]; | |
641 } | |
642 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
643 if (s->restart_interval && !s->restart_count) | |
644 s->restart_count = s->restart_interval; | |
645 | |
646 for(i=0;i<3;i++) { | |
647 int pred; | |
648 | |
649 topleft[i]= top[i]; | |
650 top[i]= buffer[mb_x][i]; | |
651 | |
652 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor); | |
653 | |
654 left[i]= | |
655 buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform)); | |
656 } | |
657 | |
658 if (s->restart_interval && !--s->restart_count) { | |
659 align_get_bits(&s->gb); | |
660 skip_bits(&s->gb, 16); /* skip RSTn */ | |
661 } | |
662 } | |
663 | |
664 if(s->rct){ | |
665 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
666 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2); | |
667 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1]; | |
668 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1]; | |
669 } | |
670 }else if(s->pegasus_rct){ | |
671 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
672 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2); | |
673 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1]; | |
674 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1]; | |
675 } | |
676 }else{ | |
677 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
10667 | 678 ptr[4*mb_x+0] = buffer[mb_x][2]; |
5020 | 679 ptr[4*mb_x+1] = buffer[mb_x][1]; |
10667 | 680 ptr[4*mb_x+2] = buffer[mb_x][0]; |
5020 | 681 } |
682 } | |
683 } | |
684 return 0; | |
685 } | |
686 | |
687 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){ | |
688 int i, mb_x, mb_y; | |
689 const int nb_components=3; | |
690 | |
691 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { | |
692 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
693 if (s->restart_interval && !s->restart_count) | |
694 s->restart_count = s->restart_interval; | |
695 | |
696 if(mb_x==0 || mb_y==0 || s->interlaced){ | |
697 for(i=0;i<nb_components;i++) { | |
698 uint8_t *ptr; | |
699 int n, h, v, x, y, c, j, linesize; | |
700 n = s->nb_blocks[i]; | |
701 c = s->comp_index[i]; | |
702 h = s->h_scount[i]; | |
703 v = s->v_scount[i]; | |
704 x = 0; | |
705 y = 0; | |
706 linesize= s->linesize[c]; | |
707 | |
708 for(j=0; j<n; j++) { | |
709 int pred; | |
710 | |
711 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap | |
712 if(y==0 && mb_y==0){ | |
713 if(x==0 && mb_x==0){ | |
714 pred= 128 << point_transform; | |
715 }else{ | |
716 pred= ptr[-1]; | |
717 } | |
718 }else{ | |
719 if(x==0 && mb_x==0){ | |
720 pred= ptr[-linesize]; | |
721 }else{ | |
722 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); | |
723 } | |
724 } | |
725 | |
726 if (s->interlaced && s->bottom_field) | |
727 ptr += linesize >> 1; | |
728 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform); | |
729 | |
730 if (++x == h) { | |
731 x = 0; | |
732 y++; | |
733 } | |
734 } | |
735 } | |
736 }else{ | |
737 for(i=0;i<nb_components;i++) { | |
738 uint8_t *ptr; | |
739 int n, h, v, x, y, c, j, linesize; | |
740 n = s->nb_blocks[i]; | |
741 c = s->comp_index[i]; | |
742 h = s->h_scount[i]; | |
743 v = s->v_scount[i]; | |
744 x = 0; | |
745 y = 0; | |
746 linesize= s->linesize[c]; | |
747 | |
748 for(j=0; j<n; j++) { | |
749 int pred; | |
750 | |
751 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap | |
752 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); | |
753 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform); | |
754 if (++x == h) { | |
755 x = 0; | |
756 y++; | |
757 } | |
758 } | |
759 } | |
760 } | |
761 if (s->restart_interval && !--s->restart_count) { | |
762 align_get_bits(&s->gb); | |
763 skip_bits(&s->gb, 16); /* skip RSTn */ | |
764 } | |
765 } | |
766 } | |
767 return 0; | |
768 } | |
769 | |
8287 | 770 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al){ |
5020 | 771 int i, mb_x, mb_y; |
5734 | 772 uint8_t* data[MAX_COMPONENTS]; |
773 int linesize[MAX_COMPONENTS]; | |
5020 | 774 |
10539
8d8fcc20dd30
Disable image flipping during JPEG decoding if CODEC_FLAG_EMU_EDGE is set
reimar
parents:
10487
diff
changeset
|
775 if(s->flipped && s->avctx->flags & CODEC_FLAG_EMU_EDGE) { |
8d8fcc20dd30
Disable image flipping during JPEG decoding if CODEC_FLAG_EMU_EDGE is set
reimar
parents:
10487
diff
changeset
|
776 av_log(s->avctx, AV_LOG_ERROR, "Can not flip image with CODEC_FLAG_EMU_EDGE set!\n"); |
8d8fcc20dd30
Disable image flipping during JPEG decoding if CODEC_FLAG_EMU_EDGE is set
reimar
parents:
10487
diff
changeset
|
777 s->flipped = 0; |
8d8fcc20dd30
Disable image flipping during JPEG decoding if CODEC_FLAG_EMU_EDGE is set
reimar
parents:
10487
diff
changeset
|
778 } |
5734 | 779 for(i=0; i < nb_components; i++) { |
780 int c = s->comp_index[i]; | |
781 data[c] = s->picture.data[c]; | |
782 linesize[c]=s->linesize[c]; | |
8287 | 783 s->coefs_finished[c] |= 1; |
10438
710e226783f0
Flip (M)JPEG frames encoded by Intel JPEG library.
cehoyos
parents:
10412
diff
changeset
|
784 if(s->flipped) { |
5736 | 785 //picture should be flipped upside-down for this codec |
5818 | 786 data[c] += (linesize[c] * (s->v_scount[i] * (8 * s->mb_height -((s->height/s->v_max)&7)) - 1 )); |
5736 | 787 linesize[c] *= -1; |
788 } | |
5734 | 789 } |
790 | |
5020 | 791 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { |
792 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
793 if (s->restart_interval && !s->restart_count) | |
794 s->restart_count = s->restart_interval; | |
795 | |
796 for(i=0;i<nb_components;i++) { | |
797 uint8_t *ptr; | |
798 int n, h, v, x, y, c, j; | |
799 n = s->nb_blocks[i]; | |
800 c = s->comp_index[i]; | |
801 h = s->h_scount[i]; | |
802 v = s->v_scount[i]; | |
803 x = 0; | |
804 y = 0; | |
805 for(j=0;j<n;j++) { | |
5734 | 806 ptr = data[c] + |
807 (((linesize[c] * (v * mb_y + y) * 8) + | |
5020 | 808 (h * mb_x + x) * 8) >> s->avctx->lowres); |
8287 | 809 if(s->interlaced && s->bottom_field) |
5734 | 810 ptr += linesize[c] >> 1; |
8287 | 811 if(!s->progressive) { |
8288 | 812 s->dsp.clear_block(s->block); |
8287 | 813 if(decode_block(s, s->block, i, |
814 s->dc_index[i], s->ac_index[i], | |
815 s->quant_matrixes[ s->quant_index[c] ]) < 0) { | |
816 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); | |
817 return -1; | |
818 } | |
819 s->dsp.idct_put(ptr, linesize[c], s->block); | |
820 } else { | |
821 int block_idx = s->block_stride[c] * (v * mb_y + y) + (h * mb_x + x); | |
822 DCTELEM *block = s->blocks[c][block_idx]; | |
823 if(Ah) | |
824 block[0] += get_bits1(&s->gb) * s->quant_matrixes[ s->quant_index[c] ][0] << Al; | |
825 else if(decode_dc_progressive(s, block, i, s->dc_index[i], s->quant_matrixes[ s->quant_index[c] ], Al) < 0) { | |
826 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); | |
827 return -1; | |
828 } | |
829 } | |
830 // av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x); | |
5020 | 831 //av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d %d %d %d %d \n", mb_x, mb_y, x, y, c, s->bottom_field, (v * mb_y + y) * 8, (h * mb_x + x) * 8); |
832 if (++x == h) { | |
833 x = 0; | |
834 y++; | |
835 } | |
836 } | |
837 } | |
9706
473fbc242b43
honor restart interval in mjpeg, fix #861, SpectralFan.mov still decodes correctly
bcoudurier
parents:
9626
diff
changeset
|
838 |
473fbc242b43
honor restart interval in mjpeg, fix #861, SpectralFan.mov still decodes correctly
bcoudurier
parents:
9626
diff
changeset
|
839 if (s->restart_interval && !--s->restart_count) { |
5020 | 840 align_get_bits(&s->gb); |
841 skip_bits(&s->gb, 16); /* skip RSTn */ | |
842 for (i=0; i<nb_components; i++) /* reset dc */ | |
843 s->last_dc[i] = 1024; | |
844 } | |
845 } | |
846 } | |
847 return 0; | |
848 } | |
849 | |
8287 | 850 static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al){ |
851 int mb_x, mb_y; | |
852 int EOBRUN = 0; | |
853 int c = s->comp_index[0]; | |
854 uint8_t* data = s->picture.data[c]; | |
855 int linesize = s->linesize[c]; | |
856 int last_scan = 0; | |
857 int16_t *quant_matrix = s->quant_matrixes[ s->quant_index[c] ]; | |
858 | |
859 if(!Al) { | |
860 s->coefs_finished[c] |= (1LL<<(se+1))-(1LL<<ss); | |
861 last_scan = !~s->coefs_finished[c]; | |
862 } | |
863 | |
864 if(s->interlaced && s->bottom_field) | |
865 data += linesize >> 1; | |
866 | |
867 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { | |
868 uint8_t *ptr = data + (mb_y*linesize*8 >> s->avctx->lowres); | |
869 int block_idx = mb_y * s->block_stride[c]; | |
870 DCTELEM (*block)[64] = &s->blocks[c][block_idx]; | |
871 uint8_t *last_nnz = &s->last_nnz[c][block_idx]; | |
872 for(mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) { | |
873 int ret; | |
874 if(Ah) | |
875 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0], | |
876 quant_matrix, ss, se, Al, &EOBRUN); | |
877 else | |
878 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0], | |
879 quant_matrix, ss, se, Al, &EOBRUN); | |
880 if(ret < 0) { | |
881 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); | |
882 return -1; | |
883 } | |
884 if(last_scan) { | |
885 s->dsp.idct_put(ptr, linesize, *block); | |
886 ptr += 8 >> s->avctx->lowres; | |
887 } | |
888 } | |
889 } | |
890 return 0; | |
891 } | |
892 | |
5044 | 893 int ff_mjpeg_decode_sos(MJpegDecodeContext *s) |
5020 | 894 { |
895 int len, nb_components, i, h, v, predictor, point_transform; | |
9459
84932ac233a5
Remove unused variables from ff_mjpeg_decode_sos() found by CSA.
michael
parents:
9355
diff
changeset
|
896 int index, id; |
5020 | 897 const int block_size= s->lossless ? 1 : 8; |
898 int ilv, prev_shift; | |
899 | |
900 /* XXX: verify len field validity */ | |
901 len = get_bits(&s->gb, 16); | |
902 nb_components = get_bits(&s->gb, 8); | |
11006
d99420b73262
Fix heap overflow due to lack of nb_components check.
michael
parents:
10672
diff
changeset
|
903 if (nb_components == 0 || nb_components > MAX_COMPONENTS){ |
d99420b73262
Fix heap overflow due to lack of nb_components check.
michael
parents:
10672
diff
changeset
|
904 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: nb_components (%d) unsupported\n", nb_components); |
d99420b73262
Fix heap overflow due to lack of nb_components check.
michael
parents:
10672
diff
changeset
|
905 return -1; |
d99420b73262
Fix heap overflow due to lack of nb_components check.
michael
parents:
10672
diff
changeset
|
906 } |
5020 | 907 if (len != 6+2*nb_components) |
908 { | |
909 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len); | |
910 return -1; | |
911 } | |
912 for(i=0;i<nb_components;i++) { | |
913 id = get_bits(&s->gb, 8) - 1; | |
914 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id); | |
915 /* find component index */ | |
916 for(index=0;index<s->nb_components;index++) | |
917 if (id == s->component_id[index]) | |
918 break; | |
919 if (index == s->nb_components) | |
920 { | |
921 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index); | |
922 return -1; | |
923 } | |
11007
8d4ae55fdada
Metasoft MJPEG codec has Cb and Cr swapped, fixes issue 1611.
cehoyos
parents:
11006
diff
changeset
|
924 /* Metasoft MJPEG codec has Cb and Cr swapped */ |
8d4ae55fdada
Metasoft MJPEG codec has Cb and Cr swapped, fixes issue 1611.
cehoyos
parents:
11006
diff
changeset
|
925 if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J') |
8d4ae55fdada
Metasoft MJPEG codec has Cb and Cr swapped, fixes issue 1611.
cehoyos
parents:
11006
diff
changeset
|
926 && nb_components == 3 && s->nb_components == 3 && i) |
8d4ae55fdada
Metasoft MJPEG codec has Cb and Cr swapped, fixes issue 1611.
cehoyos
parents:
11006
diff
changeset
|
927 index = 3 - i; |
5020 | 928 |
929 s->comp_index[i] = index; | |
930 | |
931 s->nb_blocks[i] = s->h_count[index] * s->v_count[index]; | |
932 s->h_scount[i] = s->h_count[index]; | |
933 s->v_scount[i] = s->v_count[index]; | |
934 | |
935 s->dc_index[i] = get_bits(&s->gb, 4); | |
936 s->ac_index[i] = get_bits(&s->gb, 4); | |
937 | |
938 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 || | |
939 s->dc_index[i] >= 4 || s->ac_index[i] >= 4) | |
940 goto out_of_range; | |
9912
9502108caadf
mjpegdec: check that the coded dc_index and ac_index have a valid associated VLC table.
reimar
parents:
9706
diff
changeset
|
941 if (!s->vlcs[0][s->dc_index[i]].table || !s->vlcs[1][s->ac_index[i]].table) |
9502108caadf
mjpegdec: check that the coded dc_index and ac_index have a valid associated VLC table.
reimar
parents:
9706
diff
changeset
|
942 goto out_of_range; |
5020 | 943 } |
944 | |
945 predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */ | |
946 ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */ | |
947 prev_shift = get_bits(&s->gb, 4); /* Ah */ | |
948 point_transform= get_bits(&s->gb, 4); /* Al */ | |
949 | |
950 for(i=0;i<nb_components;i++) | |
951 s->last_dc[i] = 1024; | |
952 | |
953 if (nb_components > 1) { | |
954 /* interleaved stream */ | |
955 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size); | |
956 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size); | |
957 } else if(!s->ls) { /* skip this for JPEG-LS */ | |
958 h = s->h_max / s->h_scount[0]; | |
959 v = s->v_max / s->v_scount[0]; | |
960 s->mb_width = (s->width + h * block_size - 1) / (h * block_size); | |
961 s->mb_height = (s->height + v * block_size - 1) / (v * block_size); | |
962 s->nb_blocks[0] = 1; | |
963 s->h_scount[0] = 1; | |
964 s->v_scount[0] = 1; | |
965 } | |
966 | |
967 if(s->avctx->debug & FF_DEBUG_PICT_INFO) | |
11024 | 968 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n", s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "", |
5020 | 969 predictor, point_transform, ilv, s->bits, |
970 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : "")); | |
971 | |
972 | |
973 /* mjpeg-b can have padding bytes between sos and image data, skip them */ | |
974 for (i = s->mjpb_skiptosod; i > 0; i--) | |
975 skip_bits(&s->gb, 8); | |
976 | |
977 if(s->lossless){ | |
8596
68e959302527
replace all occurrence of ENABLE_ by the corresponding CONFIG_, HAVE_ or ARCH_
aurel
parents:
8288
diff
changeset
|
978 if(CONFIG_JPEGLS_DECODER && s->ls){ |
5020 | 979 // for(){ |
980 // reset_ls_coding_parameters(s, 0); | |
981 | |
7478
12d42593e467
Return an error when ff_jpegls_decode_picture fails.
benoit
parents:
7136
diff
changeset
|
982 if(ff_jpegls_decode_picture(s, predictor, point_transform, ilv) < 0) |
12d42593e467
Return an error when ff_jpegls_decode_picture fails.
benoit
parents:
7136
diff
changeset
|
983 return -1; |
5020 | 984 }else{ |
985 if(s->rgb){ | |
986 if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0) | |
987 return -1; | |
988 }else{ | |
989 if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0) | |
990 return -1; | |
991 } | |
992 } | |
993 }else{ | |
8287 | 994 if(s->progressive && predictor) { |
995 if(mjpeg_decode_scan_progressive_ac(s, predictor, ilv, prev_shift, point_transform) < 0) | |
996 return -1; | |
997 } else { | |
998 if(mjpeg_decode_scan(s, nb_components, prev_shift, point_transform) < 0) | |
999 return -1; | |
1000 } | |
5020 | 1001 } |
1002 emms_c(); | |
1003 return 0; | |
1004 out_of_range: | |
1005 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n"); | |
1006 return -1; | |
1007 } | |
1008 | |
1009 static int mjpeg_decode_dri(MJpegDecodeContext *s) | |
1010 { | |
1011 if (get_bits(&s->gb, 16) != 4) | |
1012 return -1; | |
1013 s->restart_interval = get_bits(&s->gb, 16); | |
1014 s->restart_count = 0; | |
1015 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval); | |
1016 | |
1017 return 0; | |
1018 } | |
1019 | |
1020 static int mjpeg_decode_app(MJpegDecodeContext *s) | |
1021 { | |
1022 int len, id, i; | |
1023 | |
1024 len = get_bits(&s->gb, 16); | |
1025 if (len < 5) | |
1026 return -1; | |
1027 if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits) | |
1028 return -1; | |
1029 | |
12412
b31e6ace12f6
Fix undefined expressions that use multiple calls to get_bits().
alexc
parents:
12372
diff
changeset
|
1030 id = get_bits_long(&s->gb, 32); |
12129 | 1031 id = av_be2ne32(id); |
5020 | 1032 len -= 6; |
1033 | |
1034 if(s->avctx->debug & FF_DEBUG_STARTCODE){ | |
1035 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id); | |
1036 } | |
1037 | |
1038 /* buggy AVID, it puts EOI only at every 10th frame */ | |
1039 /* also this fourcc is used by non-avid files too, it holds some | |
1040 informations, but it's always present in AVID creates files */ | |
8612 | 1041 if (id == AV_RL32("AVI1")) |
5020 | 1042 { |
1043 /* structure: | |
1044 4bytes AVI1 | |
1045 1bytes polarity | |
1046 1bytes always zero | |
1047 4bytes field_size | |
1048 4bytes field_size_less_padding | |
1049 */ | |
1050 s->buggy_avid = 1; | |
1051 // if (s->first_picture) | |
1052 // printf("mjpeg: workarounding buggy AVID\n"); | |
1053 i = get_bits(&s->gb, 8); | |
1054 if (i==2) s->bottom_field= 1; | |
1055 else if(i==1) s->bottom_field= 0; | |
1056 #if 0 | |
1057 skip_bits(&s->gb, 8); | |
1058 skip_bits(&s->gb, 32); | |
1059 skip_bits(&s->gb, 32); | |
1060 len -= 10; | |
1061 #endif | |
1062 // if (s->interlace_polarity) | |
1063 // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity); | |
1064 goto out; | |
1065 } | |
1066 | |
1067 // len -= 2; | |
1068 | |
8612 | 1069 if (id == AV_RL32("JFIF")) |
5020 | 1070 { |
1071 int t_w, t_h, v1, v2; | |
1072 skip_bits(&s->gb, 8); /* the trailing zero-byte */ | |
1073 v1= get_bits(&s->gb, 8); | |
1074 v2= get_bits(&s->gb, 8); | |
1075 skip_bits(&s->gb, 8); | |
1076 | |
1077 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16); | |
1078 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16); | |
1079 | |
1080 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
1081 av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n", | |
1082 v1, v2, | |
1083 s->avctx->sample_aspect_ratio.num, | |
1084 s->avctx->sample_aspect_ratio.den | |
1085 ); | |
1086 | |
1087 t_w = get_bits(&s->gb, 8); | |
1088 t_h = get_bits(&s->gb, 8); | |
1089 if (t_w && t_h) | |
1090 { | |
1091 /* skip thumbnail */ | |
1092 if (len-10-(t_w*t_h*3) > 0) | |
1093 len -= t_w*t_h*3; | |
1094 } | |
1095 len -= 10; | |
1096 goto out; | |
1097 } | |
1098 | |
8612 | 1099 if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e')) |
5020 | 1100 { |
1101 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
1102 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n"); | |
1103 skip_bits(&s->gb, 16); /* version */ | |
1104 skip_bits(&s->gb, 16); /* flags0 */ | |
1105 skip_bits(&s->gb, 16); /* flags1 */ | |
1106 skip_bits(&s->gb, 8); /* transform */ | |
1107 len -= 7; | |
1108 goto out; | |
1109 } | |
1110 | |
8612 | 1111 if (id == AV_RL32("LJIF")){ |
5020 | 1112 if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
1113 av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n"); | |
1114 skip_bits(&s->gb, 16); /* version ? */ | |
1115 skip_bits(&s->gb, 16); /* unknwon always 0? */ | |
1116 skip_bits(&s->gb, 16); /* unknwon always 0? */ | |
1117 skip_bits(&s->gb, 16); /* unknwon always 0? */ | |
1118 switch( get_bits(&s->gb, 8)){ | |
1119 case 1: | |
1120 s->rgb= 1; | |
1121 s->pegasus_rct=0; | |
1122 break; | |
1123 case 2: | |
1124 s->rgb= 1; | |
1125 s->pegasus_rct=1; | |
1126 break; | |
1127 default: | |
1128 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n"); | |
1129 } | |
1130 len -= 9; | |
1131 goto out; | |
1132 } | |
1133 | |
1134 /* Apple MJPEG-A */ | |
1135 if ((s->start_code == APP1) && (len > (0x28 - 8))) | |
1136 { | |
12412
b31e6ace12f6
Fix undefined expressions that use multiple calls to get_bits().
alexc
parents:
12372
diff
changeset
|
1137 id = get_bits_long(&s->gb, 32); |
12129 | 1138 id = av_be2ne32(id); |
5020 | 1139 len -= 4; |
8612 | 1140 if (id == AV_RL32("mjpg")) /* Apple MJPEG-A */ |
5020 | 1141 { |
1142 #if 0 | |
1143 skip_bits(&s->gb, 32); /* field size */ | |
1144 skip_bits(&s->gb, 32); /* pad field size */ | |
1145 skip_bits(&s->gb, 32); /* next off */ | |
1146 skip_bits(&s->gb, 32); /* quant off */ | |
1147 skip_bits(&s->gb, 32); /* huff off */ | |
1148 skip_bits(&s->gb, 32); /* image off */ | |
1149 skip_bits(&s->gb, 32); /* scan off */ | |
1150 skip_bits(&s->gb, 32); /* data off */ | |
1151 #endif | |
1152 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
1153 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n"); | |
1154 } | |
1155 } | |
1156 | |
1157 out: | |
1158 /* slow but needed for extreme adobe jpegs */ | |
1159 if (len < 0) | |
1160 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n"); | |
1161 while(--len > 0) | |
1162 skip_bits(&s->gb, 8); | |
1163 | |
1164 return 0; | |
1165 } | |
1166 | |
1167 static int mjpeg_decode_com(MJpegDecodeContext *s) | |
1168 { | |
1169 int len = get_bits(&s->gb, 16); | |
1170 if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) { | |
1171 char *cbuf = av_malloc(len - 1); | |
1172 if (cbuf) { | |
1173 int i; | |
1174 for (i = 0; i < len - 2; i++) | |
1175 cbuf[i] = get_bits(&s->gb, 8); | |
1176 if (i > 0 && cbuf[i-1] == '\n') | |
1177 cbuf[i-1] = 0; | |
1178 else | |
1179 cbuf[i] = 0; | |
1180 | |
1181 if(s->avctx->debug & FF_DEBUG_PICT_INFO) | |
1182 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf); | |
1183 | |
1184 /* buggy avid, it puts EOI only at every 10th frame */ | |
1185 if (!strcmp(cbuf, "AVID")) | |
1186 { | |
1187 s->buggy_avid = 1; | |
1188 // if (s->first_picture) | |
1189 // printf("mjpeg: workarounding buggy AVID\n"); | |
1190 } | |
1191 else if(!strcmp(cbuf, "CS=ITU601")){ | |
1192 s->cs_itu601= 1; | |
1193 } | |
10672
2aab93afc826
(Partly) support Metasoft MJPEG Codec, fixes half of issue 1611.
cehoyos
parents:
10667
diff
changeset
|
1194 else if((len > 20 && !strncmp(cbuf, "Intel(R) JPEG Library", 21)) || |
2aab93afc826
(Partly) support Metasoft MJPEG Codec, fixes half of issue 1611.
cehoyos
parents:
10667
diff
changeset
|
1195 (len > 19 && !strncmp(cbuf, "Metasoft MJPEG Codec", 20))){ |
10438
710e226783f0
Flip (M)JPEG frames encoded by Intel JPEG library.
cehoyos
parents:
10412
diff
changeset
|
1196 s->flipped = 1; |
710e226783f0
Flip (M)JPEG frames encoded by Intel JPEG library.
cehoyos
parents:
10412
diff
changeset
|
1197 } |
5020 | 1198 |
1199 av_free(cbuf); | |
1200 } | |
1201 } | |
1202 | |
1203 return 0; | |
1204 } | |
1205 | |
1206 #if 0 | |
1207 static int valid_marker_list[] = | |
1208 { | |
1209 /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */ | |
1210 /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1211 /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1212 /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1213 /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1214 /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1215 /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1216 /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1217 /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1218 /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1219 /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1220 /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1221 /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1222 /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
1223 /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
1224 /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
1225 /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, | |
1226 } | |
1227 #endif | |
1228 | |
1229 /* return the 8 bit start code value and update the search | |
1230 state. Return -1 if no start code found */ | |
6222 | 1231 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end) |
5020 | 1232 { |
6222 | 1233 const uint8_t *buf_ptr; |
5020 | 1234 unsigned int v, v2; |
1235 int val; | |
1236 #ifdef DEBUG | |
1237 int skipped=0; | |
1238 #endif | |
1239 | |
1240 buf_ptr = *pbuf_ptr; | |
1241 while (buf_ptr < buf_end) { | |
1242 v = *buf_ptr++; | |
1243 v2 = *buf_ptr; | |
1244 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) { | |
1245 val = *buf_ptr++; | |
1246 goto found; | |
1247 } | |
1248 #ifdef DEBUG | |
1249 skipped++; | |
1250 #endif | |
1251 } | |
1252 val = -1; | |
1253 found: | |
9999
c78fd9154378
Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents:
9915
diff
changeset
|
1254 dprintf(NULL, "find_marker skipped %d bytes\n", skipped); |
5020 | 1255 *pbuf_ptr = buf_ptr; |
1256 return val; | |
1257 } | |
1258 | |
5041 | 1259 int ff_mjpeg_decode_frame(AVCodecContext *avctx, |
5020 | 1260 void *data, int *data_size, |
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8718
diff
changeset
|
1261 AVPacket *avpkt) |
5020 | 1262 { |
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8718
diff
changeset
|
1263 const uint8_t *buf = avpkt->data; |
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8718
diff
changeset
|
1264 int buf_size = avpkt->size; |
5020 | 1265 MJpegDecodeContext *s = avctx->priv_data; |
6222 | 1266 const uint8_t *buf_end, *buf_ptr; |
5020 | 1267 int start_code; |
1268 AVFrame *picture = data; | |
1269 | |
9913
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1270 s->got_picture = 0; // picture from previous image can not be reused |
5020 | 1271 buf_ptr = buf; |
1272 buf_end = buf + buf_size; | |
1273 while (buf_ptr < buf_end) { | |
1274 /* find start next marker */ | |
1275 start_code = find_marker(&buf_ptr, buf_end); | |
1276 { | |
1277 /* EOF */ | |
1278 if (start_code < 0) { | |
1279 goto the_end; | |
1280 } else { | |
5153 | 1281 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n", start_code, buf_end - buf_ptr); |
5020 | 1282 |
1283 if ((buf_end - buf_ptr) > s->buffer_size) | |
1284 { | |
1285 av_free(s->buffer); | |
1286 s->buffer_size = buf_end-buf_ptr; | |
1287 s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
1288 av_log(avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n", | |
1289 s->buffer_size); | |
1290 } | |
1291 | |
1292 /* unescape buffer of SOS, use special treatment for JPEG-LS */ | |
1293 if (start_code == SOS && !s->ls) | |
1294 { | |
6222 | 1295 const uint8_t *src = buf_ptr; |
5020 | 1296 uint8_t *dst = s->buffer; |
1297 | |
1298 while (src<buf_end) | |
1299 { | |
1300 uint8_t x = *(src++); | |
1301 | |
1302 *(dst++) = x; | |
1303 if (avctx->codec_id != CODEC_ID_THP) | |
1304 { | |
1305 if (x == 0xff) { | |
1306 while (src < buf_end && x == 0xff) | |
1307 x = *(src++); | |
1308 | |
1309 if (x >= 0xd0 && x <= 0xd7) | |
1310 *(dst++) = x; | |
1311 else if (x) | |
1312 break; | |
1313 } | |
1314 } | |
1315 } | |
1316 init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8); | |
1317 | |
5153 | 1318 av_log(avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n", |
5020 | 1319 (buf_end - buf_ptr) - (dst - s->buffer)); |
1320 } | |
1321 else if(start_code == SOS && s->ls){ | |
6222 | 1322 const uint8_t *src = buf_ptr; |
5020 | 1323 uint8_t *dst = s->buffer; |
1324 int bit_count = 0; | |
1325 int t = 0, b = 0; | |
1326 PutBitContext pb; | |
1327 | |
1328 s->cur_scan++; | |
1329 | |
1330 /* find marker */ | |
1331 while (src + t < buf_end){ | |
1332 uint8_t x = src[t++]; | |
1333 if (x == 0xff){ | |
1334 while((src + t < buf_end) && x == 0xff) | |
1335 x = src[t++]; | |
1336 if (x & 0x80) { | |
1337 t -= 2; | |
1338 break; | |
1339 } | |
1340 } | |
1341 } | |
1342 bit_count = t * 8; | |
1343 | |
1344 init_put_bits(&pb, dst, t); | |
1345 | |
1346 /* unescape bitstream */ | |
1347 while(b < t){ | |
1348 uint8_t x = src[b++]; | |
1349 put_bits(&pb, 8, x); | |
1350 if(x == 0xFF){ | |
1351 x = src[b++]; | |
1352 put_bits(&pb, 7, x); | |
1353 bit_count--; | |
1354 } | |
1355 } | |
1356 flush_put_bits(&pb); | |
1357 | |
1358 init_get_bits(&s->gb, dst, bit_count); | |
1359 } | |
1360 else | |
1361 init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8); | |
1362 | |
1363 s->start_code = start_code; | |
1364 if(s->avctx->debug & FF_DEBUG_STARTCODE){ | |
1365 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code); | |
1366 } | |
1367 | |
1368 /* process markers */ | |
1369 if (start_code >= 0xd0 && start_code <= 0xd7) { | |
1370 av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f); | |
1371 /* APP fields */ | |
1372 } else if (start_code >= APP0 && start_code <= APP15) { | |
1373 mjpeg_decode_app(s); | |
1374 /* Comment */ | |
1375 } else if (start_code == COM){ | |
1376 mjpeg_decode_com(s); | |
1377 } | |
1378 | |
1379 switch(start_code) { | |
1380 case SOI: | |
1381 s->restart_interval = 0; | |
1382 | |
1383 s->restart_count = 0; | |
1384 /* nothing to do on SOI */ | |
1385 break; | |
1386 case DQT: | |
5044 | 1387 ff_mjpeg_decode_dqt(s); |
5020 | 1388 break; |
1389 case DHT: | |
5044 | 1390 if(ff_mjpeg_decode_dht(s) < 0){ |
5020 | 1391 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n"); |
1392 return -1; | |
1393 } | |
1394 break; | |
1395 case SOF0: | |
10336
a554d7e29e99
lavc MJPEG decoder is capable of decoding some extended sequential
kostya
parents:
9999
diff
changeset
|
1396 case SOF1: |
5020 | 1397 s->lossless=0; |
1398 s->ls=0; | |
1399 s->progressive=0; | |
5044 | 1400 if (ff_mjpeg_decode_sof(s) < 0) |
5020 | 1401 return -1; |
1402 break; | |
1403 case SOF2: | |
1404 s->lossless=0; | |
1405 s->ls=0; | |
1406 s->progressive=1; | |
5044 | 1407 if (ff_mjpeg_decode_sof(s) < 0) |
5020 | 1408 return -1; |
1409 break; | |
1410 case SOF3: | |
1411 s->lossless=1; | |
1412 s->ls=0; | |
1413 s->progressive=0; | |
5044 | 1414 if (ff_mjpeg_decode_sof(s) < 0) |
5020 | 1415 return -1; |
1416 break; | |
1417 case SOF48: | |
1418 s->lossless=1; | |
1419 s->ls=1; | |
1420 s->progressive=0; | |
5044 | 1421 if (ff_mjpeg_decode_sof(s) < 0) |
5020 | 1422 return -1; |
1423 break; | |
1424 case LSE: | |
8596
68e959302527
replace all occurrence of ENABLE_ by the corresponding CONFIG_, HAVE_ or ARCH_
aurel
parents:
8288
diff
changeset
|
1425 if (!CONFIG_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0) |
5020 | 1426 return -1; |
1427 break; | |
1428 case EOI: | |
1429 s->cur_scan = 0; | |
1430 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) | |
1431 break; | |
1432 eoi_parser: | |
9913
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1433 if (!s->got_picture) { |
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1434 av_log(avctx, AV_LOG_WARNING, "Found EOI before any SOF, ignoring\n"); |
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1435 break; |
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1436 } |
5020 | 1437 { |
1438 if (s->interlaced) { | |
1439 s->bottom_field ^= 1; | |
1440 /* if not bottom field, do not output image yet */ | |
1441 if (s->bottom_field == !s->interlace_polarity) | |
1442 goto not_the_end; | |
1443 } | |
1444 *picture = s->picture; | |
1445 *data_size = sizeof(AVFrame); | |
1446 | |
1447 if(!s->lossless){ | |
6655
22cca5d3173a
Implement FFMAX3(a,b,c) - maximum over three arguments.
voroshil
parents:
6517
diff
changeset
|
1448 picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]); |
5020 | 1449 picture->qstride= 0; |
1450 picture->qscale_table= s->qscale_table; | |
1451 memset(picture->qscale_table, picture->quality, (s->width+15)/16); | |
1452 if(avctx->debug & FF_DEBUG_QP) | |
1453 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality); | |
1454 picture->quality*= FF_QP2LAMBDA; | |
1455 } | |
1456 | |
1457 goto the_end; | |
1458 } | |
1459 break; | |
1460 case SOS: | |
9913
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1461 if (!s->got_picture) { |
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1462 av_log(avctx, AV_LOG_WARNING, "Can not process SOS before SOF, skipping\n"); |
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1463 break; |
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1464 } |
5044 | 1465 ff_mjpeg_decode_sos(s); |
5020 | 1466 /* buggy avid puts EOI every 10-20th frame */ |
1467 /* if restart period is over process EOI */ | |
1468 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) | |
1469 goto eoi_parser; | |
1470 break; | |
1471 case DRI: | |
1472 mjpeg_decode_dri(s); | |
1473 break; | |
1474 case SOF5: | |
1475 case SOF6: | |
1476 case SOF7: | |
1477 case SOF9: | |
1478 case SOF10: | |
1479 case SOF11: | |
1480 case SOF13: | |
1481 case SOF14: | |
1482 case SOF15: | |
1483 case JPG: | |
1484 av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code); | |
1485 break; | |
1486 // default: | |
1487 // printf("mjpeg: unsupported marker (%x)\n", start_code); | |
1488 // break; | |
1489 } | |
1490 | |
1491 not_the_end: | |
1492 /* eof process start code */ | |
1493 buf_ptr += (get_bits_count(&s->gb)+7)/8; | |
1494 av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n", | |
1495 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb)); | |
1496 } | |
1497 } | |
1498 } | |
9915
f1707434e40b
If the end of the input buffer is reached while decoding MJPEG and at least
reimar
parents:
9914
diff
changeset
|
1499 if (s->got_picture) { |
f1707434e40b
If the end of the input buffer is reached while decoding MJPEG and at least
reimar
parents:
9914
diff
changeset
|
1500 av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n"); |
f1707434e40b
If the end of the input buffer is reached while decoding MJPEG and at least
reimar
parents:
9914
diff
changeset
|
1501 goto eoi_parser; |
f1707434e40b
If the end of the input buffer is reached while decoding MJPEG and at least
reimar
parents:
9914
diff
changeset
|
1502 } |
9914
e1aaf6216769
Make the MJPEG decoder return -1 when no image was decoded so that decode
reimar
parents:
9913
diff
changeset
|
1503 av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n"); |
e1aaf6216769
Make the MJPEG decoder return -1 when no image was decoded so that decode
reimar
parents:
9913
diff
changeset
|
1504 return -1; |
5020 | 1505 the_end: |
5153 | 1506 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n", buf_end - buf_ptr); |
5020 | 1507 // return buf_end - buf_ptr; |
1508 return buf_ptr - buf; | |
1509 } | |
1510 | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6448
diff
changeset
|
1511 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx) |
5020 | 1512 { |
1513 MJpegDecodeContext *s = avctx->priv_data; | |
1514 int i, j; | |
1515 | |
10412 | 1516 if (s->picture.data[0]) |
1517 avctx->release_buffer(avctx, &s->picture); | |
1518 | |
5020 | 1519 av_free(s->buffer); |
1520 av_free(s->qscale_table); | |
10487 | 1521 av_freep(&s->ljpeg_buffer); |
1522 s->ljpeg_buffer_size=0; | |
5020 | 1523 |
1524 for(i=0;i<2;i++) { | |
1525 for(j=0;j<4;j++) | |
1526 free_vlc(&s->vlcs[i][j]); | |
1527 } | |
8287 | 1528 for(i=0; i<MAX_COMPONENTS; i++) { |
1529 av_freep(&s->blocks[i]); | |
1530 av_freep(&s->last_nnz[i]); | |
1531 } | |
5020 | 1532 return 0; |
1533 } | |
1534 | |
1535 AVCodec mjpeg_decoder = { | |
1536 "mjpeg", | |
11560
8a4984c5cacc
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
11024
diff
changeset
|
1537 AVMEDIA_TYPE_VIDEO, |
5020 | 1538 CODEC_ID_MJPEG, |
1539 sizeof(MJpegDecodeContext), | |
5041 | 1540 ff_mjpeg_decode_init, |
5020 | 1541 NULL, |
5041 | 1542 ff_mjpeg_decode_end, |
1543 ff_mjpeg_decode_frame, | |
5020 | 1544 CODEC_CAP_DR1, |
6710 | 1545 NULL, |
12108
c35d7bc64882
Add new decoder property max_lowres and do not init decoder if requested value is higher.
cehoyos
parents:
11644
diff
changeset
|
1546 .max_lowres = 8, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6710
diff
changeset
|
1547 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"), |
5020 | 1548 }; |
1549 | |
1550 AVCodec thp_decoder = { | |
1551 "thp", | |
11560
8a4984c5cacc
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
11024
diff
changeset
|
1552 AVMEDIA_TYPE_VIDEO, |
5020 | 1553 CODEC_ID_THP, |
1554 sizeof(MJpegDecodeContext), | |
5041 | 1555 ff_mjpeg_decode_init, |
5020 | 1556 NULL, |
5041 | 1557 ff_mjpeg_decode_end, |
1558 ff_mjpeg_decode_frame, | |
5020 | 1559 CODEC_CAP_DR1, |
6710 | 1560 NULL, |
12108
c35d7bc64882
Add new decoder property max_lowres and do not init decoder if requested value is higher.
cehoyos
parents:
11644
diff
changeset
|
1561 .max_lowres = 3, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6710
diff
changeset
|
1562 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"), |
5020 | 1563 }; |