Mercurial > libavcodec.hg
annotate mjpegdec.c @ 10539:8d8fcc20dd30 libavcodec
Disable image flipping during JPEG decoding if CODEC_FLAG_EMU_EDGE is set
instead of either aborting on the assert or crashing due to writing beyond
the array due to insufficient padding.
| author | reimar |
|---|---|
| date | Thu, 19 Nov 2009 11:49:03 +0000 |
| parents | 3e404d2520a7 |
| children | 981e7720fc03 |
| 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 |
|
10539
8d8fcc20dd30
Disable image flipping during JPEG decoding if CODEC_FLAG_EMU_EDGE is set
reimar
parents:
10487
diff
changeset
|
773 if(s->flipped && s->avctx->flags & CODEC_FLAG_EMU_EDGE) { |
|
8d8fcc20dd30
Disable image flipping during JPEG decoding if CODEC_FLAG_EMU_EDGE is set
reimar
parents:
10487
diff
changeset
|
774 av_log(s->avctx, AV_LOG_ERROR, "Can not flip image with CODEC_FLAG_EMU_EDGE set!\n"); |
|
8d8fcc20dd30
Disable image flipping during JPEG decoding if CODEC_FLAG_EMU_EDGE is set
reimar
parents:
10487
diff
changeset
|
775 s->flipped = 0; |
|
8d8fcc20dd30
Disable image flipping during JPEG decoding if CODEC_FLAG_EMU_EDGE is set
reimar
parents:
10487
diff
changeset
|
776 } |
| 5734 | 777 for(i=0; i < nb_components; i++) { |
| 778 int c = s->comp_index[i]; | |
| 779 data[c] = s->picture.data[c]; | |
| 780 linesize[c]=s->linesize[c]; | |
| 8287 | 781 s->coefs_finished[c] |= 1; |
|
10438
710e226783f0
Flip (M)JPEG frames encoded by Intel JPEG library.
cehoyos
parents:
10412
diff
changeset
|
782 if(s->flipped) { |
| 5736 | 783 //picture should be flipped upside-down for this codec |
| 5818 | 784 data[c] += (linesize[c] * (s->v_scount[i] * (8 * s->mb_height -((s->height/s->v_max)&7)) - 1 )); |
| 5736 | 785 linesize[c] *= -1; |
| 786 } | |
| 5734 | 787 } |
| 788 | |
| 5020 | 789 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { |
| 790 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
| 791 if (s->restart_interval && !s->restart_count) | |
| 792 s->restart_count = s->restart_interval; | |
| 793 | |
| 794 for(i=0;i<nb_components;i++) { | |
| 795 uint8_t *ptr; | |
| 796 int n, h, v, x, y, c, j; | |
| 797 n = s->nb_blocks[i]; | |
| 798 c = s->comp_index[i]; | |
| 799 h = s->h_scount[i]; | |
| 800 v = s->v_scount[i]; | |
| 801 x = 0; | |
| 802 y = 0; | |
| 803 for(j=0;j<n;j++) { | |
| 5734 | 804 ptr = data[c] + |
| 805 (((linesize[c] * (v * mb_y + y) * 8) + | |
| 5020 | 806 (h * mb_x + x) * 8) >> s->avctx->lowres); |
| 8287 | 807 if(s->interlaced && s->bottom_field) |
| 5734 | 808 ptr += linesize[c] >> 1; |
| 8287 | 809 if(!s->progressive) { |
| 8288 | 810 s->dsp.clear_block(s->block); |
| 8287 | 811 if(decode_block(s, s->block, i, |
| 812 s->dc_index[i], s->ac_index[i], | |
| 813 s->quant_matrixes[ s->quant_index[c] ]) < 0) { | |
| 814 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); | |
| 815 return -1; | |
| 816 } | |
| 817 s->dsp.idct_put(ptr, linesize[c], s->block); | |
| 818 } else { | |
| 819 int block_idx = s->block_stride[c] * (v * mb_y + y) + (h * mb_x + x); | |
| 820 DCTELEM *block = s->blocks[c][block_idx]; | |
| 821 if(Ah) | |
| 822 block[0] += get_bits1(&s->gb) * s->quant_matrixes[ s->quant_index[c] ][0] << Al; | |
| 823 else if(decode_dc_progressive(s, block, i, s->dc_index[i], s->quant_matrixes[ s->quant_index[c] ], Al) < 0) { | |
| 824 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); | |
| 825 return -1; | |
| 826 } | |
| 827 } | |
| 828 // av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x); | |
| 5020 | 829 //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); |
| 830 if (++x == h) { | |
| 831 x = 0; | |
| 832 y++; | |
| 833 } | |
| 834 } | |
| 835 } | |
|
9706
473fbc242b43
honor restart interval in mjpeg, fix #861, SpectralFan.mov still decodes correctly
bcoudurier
parents:
9626
diff
changeset
|
836 |
|
473fbc242b43
honor restart interval in mjpeg, fix #861, SpectralFan.mov still decodes correctly
bcoudurier
parents:
9626
diff
changeset
|
837 if (s->restart_interval && !--s->restart_count) { |
| 5020 | 838 align_get_bits(&s->gb); |
| 839 skip_bits(&s->gb, 16); /* skip RSTn */ | |
| 840 for (i=0; i<nb_components; i++) /* reset dc */ | |
| 841 s->last_dc[i] = 1024; | |
| 842 } | |
| 843 } | |
| 844 } | |
| 845 return 0; | |
| 846 } | |
| 847 | |
| 8287 | 848 static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al){ |
| 849 int mb_x, mb_y; | |
| 850 int EOBRUN = 0; | |
| 851 int c = s->comp_index[0]; | |
| 852 uint8_t* data = s->picture.data[c]; | |
| 853 int linesize = s->linesize[c]; | |
| 854 int last_scan = 0; | |
| 855 int16_t *quant_matrix = s->quant_matrixes[ s->quant_index[c] ]; | |
| 856 | |
| 857 if(!Al) { | |
| 858 s->coefs_finished[c] |= (1LL<<(se+1))-(1LL<<ss); | |
| 859 last_scan = !~s->coefs_finished[c]; | |
| 860 } | |
| 861 | |
| 862 if(s->interlaced && s->bottom_field) | |
| 863 data += linesize >> 1; | |
| 864 | |
| 865 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { | |
| 866 uint8_t *ptr = data + (mb_y*linesize*8 >> s->avctx->lowres); | |
| 867 int block_idx = mb_y * s->block_stride[c]; | |
| 868 DCTELEM (*block)[64] = &s->blocks[c][block_idx]; | |
| 869 uint8_t *last_nnz = &s->last_nnz[c][block_idx]; | |
| 870 for(mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) { | |
| 871 int ret; | |
| 872 if(Ah) | |
| 873 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0], | |
| 874 quant_matrix, ss, se, Al, &EOBRUN); | |
| 875 else | |
| 876 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0], | |
| 877 quant_matrix, ss, se, Al, &EOBRUN); | |
| 878 if(ret < 0) { | |
| 879 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); | |
| 880 return -1; | |
| 881 } | |
| 882 if(last_scan) { | |
| 883 s->dsp.idct_put(ptr, linesize, *block); | |
| 884 ptr += 8 >> s->avctx->lowres; | |
| 885 } | |
| 886 } | |
| 887 } | |
| 888 return 0; | |
| 889 } | |
| 890 | |
| 5044 | 891 int ff_mjpeg_decode_sos(MJpegDecodeContext *s) |
| 5020 | 892 { |
| 893 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
|
894 int index, id; |
| 5020 | 895 const int block_size= s->lossless ? 1 : 8; |
| 896 int ilv, prev_shift; | |
| 897 | |
| 898 /* XXX: verify len field validity */ | |
| 899 len = get_bits(&s->gb, 16); | |
| 900 nb_components = get_bits(&s->gb, 8); | |
| 901 if (len != 6+2*nb_components) | |
| 902 { | |
| 903 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len); | |
| 904 return -1; | |
| 905 } | |
| 906 for(i=0;i<nb_components;i++) { | |
| 907 id = get_bits(&s->gb, 8) - 1; | |
| 908 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id); | |
| 909 /* find component index */ | |
| 910 for(index=0;index<s->nb_components;index++) | |
| 911 if (id == s->component_id[index]) | |
| 912 break; | |
| 913 if (index == s->nb_components) | |
| 914 { | |
| 915 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index); | |
| 916 return -1; | |
| 917 } | |
| 918 | |
| 919 s->comp_index[i] = index; | |
| 920 | |
| 921 s->nb_blocks[i] = s->h_count[index] * s->v_count[index]; | |
| 922 s->h_scount[i] = s->h_count[index]; | |
| 923 s->v_scount[i] = s->v_count[index]; | |
| 924 | |
| 925 s->dc_index[i] = get_bits(&s->gb, 4); | |
| 926 s->ac_index[i] = get_bits(&s->gb, 4); | |
| 927 | |
| 928 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 || | |
| 929 s->dc_index[i] >= 4 || s->ac_index[i] >= 4) | |
| 930 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
|
931 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
|
932 goto out_of_range; |
| 5020 | 933 } |
| 934 | |
| 935 predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */ | |
| 936 ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */ | |
| 937 prev_shift = get_bits(&s->gb, 4); /* Ah */ | |
| 938 point_transform= get_bits(&s->gb, 4); /* Al */ | |
| 939 | |
| 940 for(i=0;i<nb_components;i++) | |
| 941 s->last_dc[i] = 1024; | |
| 942 | |
| 943 if (nb_components > 1) { | |
| 944 /* interleaved stream */ | |
| 945 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size); | |
| 946 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size); | |
| 947 } else if(!s->ls) { /* skip this for JPEG-LS */ | |
| 948 h = s->h_max / s->h_scount[0]; | |
| 949 v = s->v_max / s->v_scount[0]; | |
| 950 s->mb_width = (s->width + h * block_size - 1) / (h * block_size); | |
| 951 s->mb_height = (s->height + v * block_size - 1) / (v * block_size); | |
| 952 s->nb_blocks[0] = 1; | |
| 953 s->h_scount[0] = 1; | |
| 954 s->v_scount[0] = 1; | |
| 955 } | |
| 956 | |
| 957 if(s->avctx->debug & FF_DEBUG_PICT_INFO) | |
| 958 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" : "", | |
| 959 predictor, point_transform, ilv, s->bits, | |
| 960 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : "")); | |
| 961 | |
| 962 | |
| 963 /* mjpeg-b can have padding bytes between sos and image data, skip them */ | |
| 964 for (i = s->mjpb_skiptosod; i > 0; i--) | |
| 965 skip_bits(&s->gb, 8); | |
| 966 | |
| 967 if(s->lossless){ | |
|
8596
68e959302527
replace all occurrence of ENABLE_ by the corresponding CONFIG_, HAVE_ or ARCH_
aurel
parents:
8288
diff
changeset
|
968 if(CONFIG_JPEGLS_DECODER && s->ls){ |
| 5020 | 969 // for(){ |
| 970 // reset_ls_coding_parameters(s, 0); | |
| 971 | |
|
7478
12d42593e467
Return an error when ff_jpegls_decode_picture fails.
benoit
parents:
7136
diff
changeset
|
972 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
|
973 return -1; |
| 5020 | 974 }else{ |
| 975 if(s->rgb){ | |
| 976 if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0) | |
| 977 return -1; | |
| 978 }else{ | |
| 979 if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0) | |
| 980 return -1; | |
| 981 } | |
| 982 } | |
| 983 }else{ | |
| 8287 | 984 if(s->progressive && predictor) { |
| 985 if(mjpeg_decode_scan_progressive_ac(s, predictor, ilv, prev_shift, point_transform) < 0) | |
| 986 return -1; | |
| 987 } else { | |
| 988 if(mjpeg_decode_scan(s, nb_components, prev_shift, point_transform) < 0) | |
| 989 return -1; | |
| 990 } | |
| 5020 | 991 } |
| 992 emms_c(); | |
| 993 return 0; | |
| 994 out_of_range: | |
| 995 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n"); | |
| 996 return -1; | |
| 997 } | |
| 998 | |
| 999 static int mjpeg_decode_dri(MJpegDecodeContext *s) | |
| 1000 { | |
| 1001 if (get_bits(&s->gb, 16) != 4) | |
| 1002 return -1; | |
| 1003 s->restart_interval = get_bits(&s->gb, 16); | |
| 1004 s->restart_count = 0; | |
| 1005 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval); | |
| 1006 | |
| 1007 return 0; | |
| 1008 } | |
| 1009 | |
| 1010 static int mjpeg_decode_app(MJpegDecodeContext *s) | |
| 1011 { | |
| 1012 int len, id, i; | |
| 1013 | |
| 1014 len = get_bits(&s->gb, 16); | |
| 1015 if (len < 5) | |
| 1016 return -1; | |
| 1017 if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits) | |
| 1018 return -1; | |
| 1019 | |
| 1020 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); | |
| 1021 id = be2me_32(id); | |
| 1022 len -= 6; | |
| 1023 | |
| 1024 if(s->avctx->debug & FF_DEBUG_STARTCODE){ | |
| 1025 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id); | |
| 1026 } | |
| 1027 | |
| 1028 /* buggy AVID, it puts EOI only at every 10th frame */ | |
| 1029 /* also this fourcc is used by non-avid files too, it holds some | |
| 1030 informations, but it's always present in AVID creates files */ | |
| 8612 | 1031 if (id == AV_RL32("AVI1")) |
| 5020 | 1032 { |
| 1033 /* structure: | |
| 1034 4bytes AVI1 | |
| 1035 1bytes polarity | |
| 1036 1bytes always zero | |
| 1037 4bytes field_size | |
| 1038 4bytes field_size_less_padding | |
| 1039 */ | |
| 1040 s->buggy_avid = 1; | |
| 1041 // if (s->first_picture) | |
| 1042 // printf("mjpeg: workarounding buggy AVID\n"); | |
| 1043 i = get_bits(&s->gb, 8); | |
| 1044 if (i==2) s->bottom_field= 1; | |
| 1045 else if(i==1) s->bottom_field= 0; | |
| 1046 #if 0 | |
| 1047 skip_bits(&s->gb, 8); | |
| 1048 skip_bits(&s->gb, 32); | |
| 1049 skip_bits(&s->gb, 32); | |
| 1050 len -= 10; | |
| 1051 #endif | |
| 1052 // if (s->interlace_polarity) | |
| 1053 // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity); | |
| 1054 goto out; | |
| 1055 } | |
| 1056 | |
| 1057 // len -= 2; | |
| 1058 | |
| 8612 | 1059 if (id == AV_RL32("JFIF")) |
| 5020 | 1060 { |
| 1061 int t_w, t_h, v1, v2; | |
| 1062 skip_bits(&s->gb, 8); /* the trailing zero-byte */ | |
| 1063 v1= get_bits(&s->gb, 8); | |
| 1064 v2= get_bits(&s->gb, 8); | |
| 1065 skip_bits(&s->gb, 8); | |
| 1066 | |
| 1067 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16); | |
| 1068 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16); | |
| 1069 | |
| 1070 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
| 1071 av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n", | |
| 1072 v1, v2, | |
| 1073 s->avctx->sample_aspect_ratio.num, | |
| 1074 s->avctx->sample_aspect_ratio.den | |
| 1075 ); | |
| 1076 | |
| 1077 t_w = get_bits(&s->gb, 8); | |
| 1078 t_h = get_bits(&s->gb, 8); | |
| 1079 if (t_w && t_h) | |
| 1080 { | |
| 1081 /* skip thumbnail */ | |
| 1082 if (len-10-(t_w*t_h*3) > 0) | |
| 1083 len -= t_w*t_h*3; | |
| 1084 } | |
| 1085 len -= 10; | |
| 1086 goto out; | |
| 1087 } | |
| 1088 | |
| 8612 | 1089 if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e')) |
| 5020 | 1090 { |
| 1091 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
| 1092 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n"); | |
| 1093 skip_bits(&s->gb, 16); /* version */ | |
| 1094 skip_bits(&s->gb, 16); /* flags0 */ | |
| 1095 skip_bits(&s->gb, 16); /* flags1 */ | |
| 1096 skip_bits(&s->gb, 8); /* transform */ | |
| 1097 len -= 7; | |
| 1098 goto out; | |
| 1099 } | |
| 1100 | |
| 8612 | 1101 if (id == AV_RL32("LJIF")){ |
| 5020 | 1102 if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
| 1103 av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n"); | |
| 1104 skip_bits(&s->gb, 16); /* version ? */ | |
| 1105 skip_bits(&s->gb, 16); /* unknwon always 0? */ | |
| 1106 skip_bits(&s->gb, 16); /* unknwon always 0? */ | |
| 1107 skip_bits(&s->gb, 16); /* unknwon always 0? */ | |
| 1108 switch( get_bits(&s->gb, 8)){ | |
| 1109 case 1: | |
| 1110 s->rgb= 1; | |
| 1111 s->pegasus_rct=0; | |
| 1112 break; | |
| 1113 case 2: | |
| 1114 s->rgb= 1; | |
| 1115 s->pegasus_rct=1; | |
| 1116 break; | |
| 1117 default: | |
| 1118 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n"); | |
| 1119 } | |
| 1120 len -= 9; | |
| 1121 goto out; | |
| 1122 } | |
| 1123 | |
| 1124 /* Apple MJPEG-A */ | |
| 1125 if ((s->start_code == APP1) && (len > (0x28 - 8))) | |
| 1126 { | |
| 1127 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); | |
| 1128 id = be2me_32(id); | |
| 1129 len -= 4; | |
| 8612 | 1130 if (id == AV_RL32("mjpg")) /* Apple MJPEG-A */ |
| 5020 | 1131 { |
| 1132 #if 0 | |
| 1133 skip_bits(&s->gb, 32); /* field size */ | |
| 1134 skip_bits(&s->gb, 32); /* pad field size */ | |
| 1135 skip_bits(&s->gb, 32); /* next off */ | |
| 1136 skip_bits(&s->gb, 32); /* quant off */ | |
| 1137 skip_bits(&s->gb, 32); /* huff off */ | |
| 1138 skip_bits(&s->gb, 32); /* image off */ | |
| 1139 skip_bits(&s->gb, 32); /* scan off */ | |
| 1140 skip_bits(&s->gb, 32); /* data off */ | |
| 1141 #endif | |
| 1142 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
| 1143 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n"); | |
| 1144 } | |
| 1145 } | |
| 1146 | |
| 1147 out: | |
| 1148 /* slow but needed for extreme adobe jpegs */ | |
| 1149 if (len < 0) | |
| 1150 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n"); | |
| 1151 while(--len > 0) | |
| 1152 skip_bits(&s->gb, 8); | |
| 1153 | |
| 1154 return 0; | |
| 1155 } | |
| 1156 | |
| 1157 static int mjpeg_decode_com(MJpegDecodeContext *s) | |
| 1158 { | |
| 1159 int len = get_bits(&s->gb, 16); | |
| 1160 if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) { | |
| 1161 char *cbuf = av_malloc(len - 1); | |
| 1162 if (cbuf) { | |
| 1163 int i; | |
| 1164 for (i = 0; i < len - 2; i++) | |
| 1165 cbuf[i] = get_bits(&s->gb, 8); | |
| 1166 if (i > 0 && cbuf[i-1] == '\n') | |
| 1167 cbuf[i-1] = 0; | |
| 1168 else | |
| 1169 cbuf[i] = 0; | |
| 1170 | |
| 1171 if(s->avctx->debug & FF_DEBUG_PICT_INFO) | |
| 1172 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf); | |
| 1173 | |
| 1174 /* buggy avid, it puts EOI only at every 10th frame */ | |
| 1175 if (!strcmp(cbuf, "AVID")) | |
| 1176 { | |
| 1177 s->buggy_avid = 1; | |
| 1178 // if (s->first_picture) | |
| 1179 // printf("mjpeg: workarounding buggy AVID\n"); | |
| 1180 } | |
| 1181 else if(!strcmp(cbuf, "CS=ITU601")){ | |
| 1182 s->cs_itu601= 1; | |
| 1183 } | |
|
10438
710e226783f0
Flip (M)JPEG frames encoded by Intel JPEG library.
cehoyos
parents:
10412
diff
changeset
|
1184 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
|
1185 s->flipped = 1; |
|
710e226783f0
Flip (M)JPEG frames encoded by Intel JPEG library.
cehoyos
parents:
10412
diff
changeset
|
1186 } |
| 5020 | 1187 |
| 1188 av_free(cbuf); | |
| 1189 } | |
| 1190 } | |
| 1191 | |
| 1192 return 0; | |
| 1193 } | |
| 1194 | |
| 1195 #if 0 | |
| 1196 static int valid_marker_list[] = | |
| 1197 { | |
| 1198 /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */ | |
| 1199 /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 1200 /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 1201 /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 1202 /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 1203 /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 1204 /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 1205 /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 1206 /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 1207 /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 1208 /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 1209 /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 1210 /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 1211 /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
| 1212 /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
| 1213 /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
| 1214 /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, | |
| 1215 } | |
| 1216 #endif | |
| 1217 | |
| 1218 /* return the 8 bit start code value and update the search | |
| 1219 state. Return -1 if no start code found */ | |
| 6222 | 1220 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end) |
| 5020 | 1221 { |
| 6222 | 1222 const uint8_t *buf_ptr; |
| 5020 | 1223 unsigned int v, v2; |
| 1224 int val; | |
| 1225 #ifdef DEBUG | |
| 1226 int skipped=0; | |
| 1227 #endif | |
| 1228 | |
| 1229 buf_ptr = *pbuf_ptr; | |
| 1230 while (buf_ptr < buf_end) { | |
| 1231 v = *buf_ptr++; | |
| 1232 v2 = *buf_ptr; | |
| 1233 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) { | |
| 1234 val = *buf_ptr++; | |
| 1235 goto found; | |
| 1236 } | |
| 1237 #ifdef DEBUG | |
| 1238 skipped++; | |
| 1239 #endif | |
| 1240 } | |
| 1241 val = -1; | |
| 1242 found: | |
|
9999
c78fd9154378
Change av_log() calls surrounded by '#ifdef DEBUG' into dprintf macros.
diego
parents:
9915
diff
changeset
|
1243 dprintf(NULL, "find_marker skipped %d bytes\n", skipped); |
| 5020 | 1244 *pbuf_ptr = buf_ptr; |
| 1245 return val; | |
| 1246 } | |
| 1247 | |
| 5041 | 1248 int ff_mjpeg_decode_frame(AVCodecContext *avctx, |
| 5020 | 1249 void *data, int *data_size, |
|
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8718
diff
changeset
|
1250 AVPacket *avpkt) |
| 5020 | 1251 { |
|
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8718
diff
changeset
|
1252 const uint8_t *buf = avpkt->data; |
|
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8718
diff
changeset
|
1253 int buf_size = avpkt->size; |
| 5020 | 1254 MJpegDecodeContext *s = avctx->priv_data; |
| 6222 | 1255 const uint8_t *buf_end, *buf_ptr; |
| 5020 | 1256 int start_code; |
| 1257 AVFrame *picture = data; | |
| 1258 | |
|
9913
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1259 s->got_picture = 0; // picture from previous image can not be reused |
| 5020 | 1260 buf_ptr = buf; |
| 1261 buf_end = buf + buf_size; | |
| 1262 while (buf_ptr < buf_end) { | |
| 1263 /* find start next marker */ | |
| 1264 start_code = find_marker(&buf_ptr, buf_end); | |
| 1265 { | |
| 1266 /* EOF */ | |
| 1267 if (start_code < 0) { | |
| 1268 goto the_end; | |
| 1269 } else { | |
| 5153 | 1270 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n", start_code, buf_end - buf_ptr); |
| 5020 | 1271 |
| 1272 if ((buf_end - buf_ptr) > s->buffer_size) | |
| 1273 { | |
| 1274 av_free(s->buffer); | |
| 1275 s->buffer_size = buf_end-buf_ptr; | |
| 1276 s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
| 1277 av_log(avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n", | |
| 1278 s->buffer_size); | |
| 1279 } | |
| 1280 | |
| 1281 /* unescape buffer of SOS, use special treatment for JPEG-LS */ | |
| 1282 if (start_code == SOS && !s->ls) | |
| 1283 { | |
| 6222 | 1284 const uint8_t *src = buf_ptr; |
| 5020 | 1285 uint8_t *dst = s->buffer; |
| 1286 | |
| 1287 while (src<buf_end) | |
| 1288 { | |
| 1289 uint8_t x = *(src++); | |
| 1290 | |
| 1291 *(dst++) = x; | |
| 1292 if (avctx->codec_id != CODEC_ID_THP) | |
| 1293 { | |
| 1294 if (x == 0xff) { | |
| 1295 while (src < buf_end && x == 0xff) | |
| 1296 x = *(src++); | |
| 1297 | |
| 1298 if (x >= 0xd0 && x <= 0xd7) | |
| 1299 *(dst++) = x; | |
| 1300 else if (x) | |
| 1301 break; | |
| 1302 } | |
| 1303 } | |
| 1304 } | |
| 1305 init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8); | |
| 1306 | |
| 5153 | 1307 av_log(avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n", |
| 5020 | 1308 (buf_end - buf_ptr) - (dst - s->buffer)); |
| 1309 } | |
| 1310 else if(start_code == SOS && s->ls){ | |
| 6222 | 1311 const uint8_t *src = buf_ptr; |
| 5020 | 1312 uint8_t *dst = s->buffer; |
| 1313 int bit_count = 0; | |
| 1314 int t = 0, b = 0; | |
| 1315 PutBitContext pb; | |
| 1316 | |
| 1317 s->cur_scan++; | |
| 1318 | |
| 1319 /* find marker */ | |
| 1320 while (src + t < buf_end){ | |
| 1321 uint8_t x = src[t++]; | |
| 1322 if (x == 0xff){ | |
| 1323 while((src + t < buf_end) && x == 0xff) | |
| 1324 x = src[t++]; | |
| 1325 if (x & 0x80) { | |
| 1326 t -= 2; | |
| 1327 break; | |
| 1328 } | |
| 1329 } | |
| 1330 } | |
| 1331 bit_count = t * 8; | |
| 1332 | |
| 1333 init_put_bits(&pb, dst, t); | |
| 1334 | |
| 1335 /* unescape bitstream */ | |
| 1336 while(b < t){ | |
| 1337 uint8_t x = src[b++]; | |
| 1338 put_bits(&pb, 8, x); | |
| 1339 if(x == 0xFF){ | |
| 1340 x = src[b++]; | |
| 1341 put_bits(&pb, 7, x); | |
| 1342 bit_count--; | |
| 1343 } | |
| 1344 } | |
| 1345 flush_put_bits(&pb); | |
| 1346 | |
| 1347 init_get_bits(&s->gb, dst, bit_count); | |
| 1348 } | |
| 1349 else | |
| 1350 init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8); | |
| 1351 | |
| 1352 s->start_code = start_code; | |
| 1353 if(s->avctx->debug & FF_DEBUG_STARTCODE){ | |
| 1354 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code); | |
| 1355 } | |
| 1356 | |
| 1357 /* process markers */ | |
| 1358 if (start_code >= 0xd0 && start_code <= 0xd7) { | |
| 1359 av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f); | |
| 1360 /* APP fields */ | |
| 1361 } else if (start_code >= APP0 && start_code <= APP15) { | |
| 1362 mjpeg_decode_app(s); | |
| 1363 /* Comment */ | |
| 1364 } else if (start_code == COM){ | |
| 1365 mjpeg_decode_com(s); | |
| 1366 } | |
| 1367 | |
| 1368 switch(start_code) { | |
| 1369 case SOI: | |
| 1370 s->restart_interval = 0; | |
| 1371 | |
| 1372 s->restart_count = 0; | |
| 1373 /* nothing to do on SOI */ | |
| 1374 break; | |
| 1375 case DQT: | |
| 5044 | 1376 ff_mjpeg_decode_dqt(s); |
| 5020 | 1377 break; |
| 1378 case DHT: | |
| 5044 | 1379 if(ff_mjpeg_decode_dht(s) < 0){ |
| 5020 | 1380 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n"); |
| 1381 return -1; | |
| 1382 } | |
| 1383 break; | |
| 1384 case SOF0: | |
|
10336
a554d7e29e99
lavc MJPEG decoder is capable of decoding some extended sequential
kostya
parents:
9999
diff
changeset
|
1385 case SOF1: |
| 5020 | 1386 s->lossless=0; |
| 1387 s->ls=0; | |
| 1388 s->progressive=0; | |
| 5044 | 1389 if (ff_mjpeg_decode_sof(s) < 0) |
| 5020 | 1390 return -1; |
| 1391 break; | |
| 1392 case SOF2: | |
| 1393 s->lossless=0; | |
| 1394 s->ls=0; | |
| 1395 s->progressive=1; | |
| 5044 | 1396 if (ff_mjpeg_decode_sof(s) < 0) |
| 5020 | 1397 return -1; |
| 1398 break; | |
| 1399 case SOF3: | |
| 1400 s->lossless=1; | |
| 1401 s->ls=0; | |
| 1402 s->progressive=0; | |
| 5044 | 1403 if (ff_mjpeg_decode_sof(s) < 0) |
| 5020 | 1404 return -1; |
| 1405 break; | |
| 1406 case SOF48: | |
| 1407 s->lossless=1; | |
| 1408 s->ls=1; | |
| 1409 s->progressive=0; | |
| 5044 | 1410 if (ff_mjpeg_decode_sof(s) < 0) |
| 5020 | 1411 return -1; |
| 1412 break; | |
| 1413 case LSE: | |
|
8596
68e959302527
replace all occurrence of ENABLE_ by the corresponding CONFIG_, HAVE_ or ARCH_
aurel
parents:
8288
diff
changeset
|
1414 if (!CONFIG_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0) |
| 5020 | 1415 return -1; |
| 1416 break; | |
| 1417 case EOI: | |
| 1418 s->cur_scan = 0; | |
| 1419 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) | |
| 1420 break; | |
| 1421 eoi_parser: | |
|
9913
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1422 if (!s->got_picture) { |
|
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1423 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
|
1424 break; |
|
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1425 } |
| 5020 | 1426 { |
| 1427 if (s->interlaced) { | |
| 1428 s->bottom_field ^= 1; | |
| 1429 /* if not bottom field, do not output image yet */ | |
| 1430 if (s->bottom_field == !s->interlace_polarity) | |
| 1431 goto not_the_end; | |
| 1432 } | |
| 1433 *picture = s->picture; | |
| 1434 *data_size = sizeof(AVFrame); | |
| 1435 | |
| 1436 if(!s->lossless){ | |
|
6655
22cca5d3173a
Implement FFMAX3(a,b,c) - maximum over three arguments.
voroshil
parents:
6517
diff
changeset
|
1437 picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]); |
| 5020 | 1438 picture->qstride= 0; |
| 1439 picture->qscale_table= s->qscale_table; | |
| 1440 memset(picture->qscale_table, picture->quality, (s->width+15)/16); | |
| 1441 if(avctx->debug & FF_DEBUG_QP) | |
| 1442 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality); | |
| 1443 picture->quality*= FF_QP2LAMBDA; | |
| 1444 } | |
| 1445 | |
| 1446 goto the_end; | |
| 1447 } | |
| 1448 break; | |
| 1449 case SOS: | |
|
9913
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1450 if (!s->got_picture) { |
|
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1451 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
|
1452 break; |
|
b73796e93571
Add a got_picture flag to MJpegDecodeContext which indicates if its picture
reimar
parents:
9912
diff
changeset
|
1453 } |
| 5044 | 1454 ff_mjpeg_decode_sos(s); |
| 5020 | 1455 /* buggy avid puts EOI every 10-20th frame */ |
| 1456 /* if restart period is over process EOI */ | |
| 1457 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) | |
| 1458 goto eoi_parser; | |
| 1459 break; | |
| 1460 case DRI: | |
| 1461 mjpeg_decode_dri(s); | |
| 1462 break; | |
| 1463 case SOF5: | |
| 1464 case SOF6: | |
| 1465 case SOF7: | |
| 1466 case SOF9: | |
| 1467 case SOF10: | |
| 1468 case SOF11: | |
| 1469 case SOF13: | |
| 1470 case SOF14: | |
| 1471 case SOF15: | |
| 1472 case JPG: | |
| 1473 av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code); | |
| 1474 break; | |
| 1475 // default: | |
| 1476 // printf("mjpeg: unsupported marker (%x)\n", start_code); | |
| 1477 // break; | |
| 1478 } | |
| 1479 | |
| 1480 not_the_end: | |
| 1481 /* eof process start code */ | |
| 1482 buf_ptr += (get_bits_count(&s->gb)+7)/8; | |
| 1483 av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n", | |
| 1484 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb)); | |
| 1485 } | |
| 1486 } | |
| 1487 } | |
|
9915
f1707434e40b
If the end of the input buffer is reached while decoding MJPEG and at least
reimar
parents:
9914
diff
changeset
|
1488 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
|
1489 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
|
1490 goto eoi_parser; |
|
f1707434e40b
If the end of the input buffer is reached while decoding MJPEG and at least
reimar
parents:
9914
diff
changeset
|
1491 } |
|
9914
e1aaf6216769
Make the MJPEG decoder return -1 when no image was decoded so that decode
reimar
parents:
9913
diff
changeset
|
1492 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
|
1493 return -1; |
| 5020 | 1494 the_end: |
| 5153 | 1495 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n", buf_end - buf_ptr); |
| 5020 | 1496 // return buf_end - buf_ptr; |
| 1497 return buf_ptr - buf; | |
| 1498 } | |
| 1499 | |
|
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6448
diff
changeset
|
1500 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx) |
| 5020 | 1501 { |
| 1502 MJpegDecodeContext *s = avctx->priv_data; | |
| 1503 int i, j; | |
| 1504 | |
| 10412 | 1505 if (s->picture.data[0]) |
| 1506 avctx->release_buffer(avctx, &s->picture); | |
| 1507 | |
| 5020 | 1508 av_free(s->buffer); |
| 1509 av_free(s->qscale_table); | |
| 10487 | 1510 av_freep(&s->ljpeg_buffer); |
| 1511 s->ljpeg_buffer_size=0; | |
| 5020 | 1512 |
| 1513 for(i=0;i<2;i++) { | |
| 1514 for(j=0;j<4;j++) | |
| 1515 free_vlc(&s->vlcs[i][j]); | |
| 1516 } | |
| 8287 | 1517 for(i=0; i<MAX_COMPONENTS; i++) { |
| 1518 av_freep(&s->blocks[i]); | |
| 1519 av_freep(&s->last_nnz[i]); | |
| 1520 } | |
| 5020 | 1521 return 0; |
| 1522 } | |
| 1523 | |
| 1524 AVCodec mjpeg_decoder = { | |
| 1525 "mjpeg", | |
| 1526 CODEC_TYPE_VIDEO, | |
| 1527 CODEC_ID_MJPEG, | |
| 1528 sizeof(MJpegDecodeContext), | |
| 5041 | 1529 ff_mjpeg_decode_init, |
| 5020 | 1530 NULL, |
| 5041 | 1531 ff_mjpeg_decode_end, |
| 1532 ff_mjpeg_decode_frame, | |
| 5020 | 1533 CODEC_CAP_DR1, |
| 6710 | 1534 NULL, |
|
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6710
diff
changeset
|
1535 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"), |
| 5020 | 1536 }; |
| 1537 | |
| 1538 AVCodec thp_decoder = { | |
| 1539 "thp", | |
| 1540 CODEC_TYPE_VIDEO, | |
| 1541 CODEC_ID_THP, | |
| 1542 sizeof(MJpegDecodeContext), | |
| 5041 | 1543 ff_mjpeg_decode_init, |
| 5020 | 1544 NULL, |
| 5041 | 1545 ff_mjpeg_decode_end, |
| 1546 ff_mjpeg_decode_frame, | |
| 5020 | 1547 CODEC_CAP_DR1, |
| 6710 | 1548 NULL, |
|
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6710
diff
changeset
|
1549 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"), |
| 5020 | 1550 }; |
