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