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