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