| 1 | /* |
| 2 | * MJPEG decoder |
| 3 | * Copyright (c) 2000, 2001 Fabrice Bellard |
| 4 | * Copyright (c) 2003 Alex Beregszaszi |
| 5 | * Copyright (c) 2003-2004 Michael Niedermayer |
| 6 | * |
| 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 | * |
| 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 | /** |
| 29 | * @file |
| 30 | * MJPEG decoder. |
| 31 | */ |
| 32 | |
| 33 | #include "libavutil/imgutils.h" |
| 34 | #include "libavutil/avassert.h" |
| 35 | #include "libavutil/opt.h" |
| 36 | #include "avcodec.h" |
| 37 | #include "blockdsp.h" |
| 38 | #include "copy_block.h" |
| 39 | #include "idctdsp.h" |
| 40 | #include "internal.h" |
| 41 | #include "mjpeg.h" |
| 42 | #include "mjpegdec.h" |
| 43 | #include "jpeglsdec.h" |
| 44 | #include "tiff.h" |
| 45 | #include "exif.h" |
| 46 | #include "bytestream.h" |
| 47 | |
| 48 | |
| 49 | static int build_vlc(VLC *vlc, const uint8_t *bits_table, |
| 50 | const uint8_t *val_table, int nb_codes, |
| 51 | int use_static, int is_ac) |
| 52 | { |
| 53 | uint8_t huff_size[256] = { 0 }; |
| 54 | uint16_t huff_code[256]; |
| 55 | uint16_t huff_sym[256]; |
| 56 | int i; |
| 57 | |
| 58 | av_assert0(nb_codes <= 256); |
| 59 | |
| 60 | ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table); |
| 61 | |
| 62 | for (i = 0; i < 256; i++) |
| 63 | huff_sym[i] = i + 16 * is_ac; |
| 64 | |
| 65 | if (is_ac) |
| 66 | huff_sym[0] = 16 * 256; |
| 67 | |
| 68 | return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1, |
| 69 | huff_code, 2, 2, huff_sym, 2, 2, use_static); |
| 70 | } |
| 71 | |
| 72 | static void build_basic_mjpeg_vlc(MJpegDecodeContext *s) |
| 73 | { |
| 74 | build_vlc(&s->vlcs[0][0], avpriv_mjpeg_bits_dc_luminance, |
| 75 | avpriv_mjpeg_val_dc, 12, 0, 0); |
| 76 | build_vlc(&s->vlcs[0][1], avpriv_mjpeg_bits_dc_chrominance, |
| 77 | avpriv_mjpeg_val_dc, 12, 0, 0); |
| 78 | build_vlc(&s->vlcs[1][0], avpriv_mjpeg_bits_ac_luminance, |
| 79 | avpriv_mjpeg_val_ac_luminance, 251, 0, 1); |
| 80 | build_vlc(&s->vlcs[1][1], avpriv_mjpeg_bits_ac_chrominance, |
| 81 | avpriv_mjpeg_val_ac_chrominance, 251, 0, 1); |
| 82 | build_vlc(&s->vlcs[2][0], avpriv_mjpeg_bits_ac_luminance, |
| 83 | avpriv_mjpeg_val_ac_luminance, 251, 0, 0); |
| 84 | build_vlc(&s->vlcs[2][1], avpriv_mjpeg_bits_ac_chrominance, |
| 85 | avpriv_mjpeg_val_ac_chrominance, 251, 0, 0); |
| 86 | } |
| 87 | |
| 88 | static void parse_avid(MJpegDecodeContext *s, uint8_t *buf, int len) |
| 89 | { |
| 90 | s->buggy_avid = 1; |
| 91 | if (len > 14 && buf[12] == 1) /* 1 - NTSC */ |
| 92 | s->interlace_polarity = 1; |
| 93 | if (len > 14 && buf[12] == 2) /* 2 - PAL */ |
| 94 | s->interlace_polarity = 0; |
| 95 | if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
| 96 | av_log(s->avctx, AV_LOG_INFO, "AVID: len:%d %d\n", len, len > 14 ? buf[12] : -1); |
| 97 | } |
| 98 | |
| 99 | av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx) |
| 100 | { |
| 101 | MJpegDecodeContext *s = avctx->priv_data; |
| 102 | |
| 103 | if (!s->picture_ptr) { |
| 104 | s->picture = av_frame_alloc(); |
| 105 | if (!s->picture) |
| 106 | return AVERROR(ENOMEM); |
| 107 | s->picture_ptr = s->picture; |
| 108 | } |
| 109 | |
| 110 | s->avctx = avctx; |
| 111 | ff_blockdsp_init(&s->bdsp, avctx); |
| 112 | ff_hpeldsp_init(&s->hdsp, avctx->flags); |
| 113 | ff_idctdsp_init(&s->idsp, avctx); |
| 114 | ff_init_scantable(s->idsp.idct_permutation, &s->scantable, |
| 115 | ff_zigzag_direct); |
| 116 | s->buffer_size = 0; |
| 117 | s->buffer = NULL; |
| 118 | s->start_code = -1; |
| 119 | s->first_picture = 1; |
| 120 | s->got_picture = 0; |
| 121 | s->org_height = avctx->coded_height; |
| 122 | avctx->chroma_sample_location = AVCHROMA_LOC_CENTER; |
| 123 | avctx->colorspace = AVCOL_SPC_BT470BG; |
| 124 | |
| 125 | build_basic_mjpeg_vlc(s); |
| 126 | |
| 127 | if (s->extern_huff) { |
| 128 | av_log(avctx, AV_LOG_INFO, "using external huffman table\n"); |
| 129 | init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8); |
| 130 | if (ff_mjpeg_decode_dht(s)) { |
| 131 | av_log(avctx, AV_LOG_ERROR, |
| 132 | "error using external huffman table, switching back to internal\n"); |
| 133 | build_basic_mjpeg_vlc(s); |
| 134 | } |
| 135 | } |
| 136 | if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */ |
| 137 | s->interlace_polarity = 1; /* bottom field first */ |
| 138 | av_log(avctx, AV_LOG_DEBUG, "bottom field first\n"); |
| 139 | } else if (avctx->field_order == AV_FIELD_UNKNOWN) { |
| 140 | if (avctx->codec_tag == AV_RL32("MJPG")) |
| 141 | s->interlace_polarity = 1; |
| 142 | } |
| 143 | |
| 144 | if ( avctx->extradata_size > 8 |
| 145 | && AV_RL32(avctx->extradata) == 0x2C |
| 146 | && AV_RL32(avctx->extradata+4) == 0x18) { |
| 147 | parse_avid(s, avctx->extradata, avctx->extradata_size); |
| 148 | } |
| 149 | |
| 150 | if (avctx->codec->id == AV_CODEC_ID_AMV) |
| 151 | s->flipped = 1; |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /* quantize tables */ |
| 158 | int ff_mjpeg_decode_dqt(MJpegDecodeContext *s) |
| 159 | { |
| 160 | int len, index, i, j; |
| 161 | |
| 162 | len = get_bits(&s->gb, 16) - 2; |
| 163 | |
| 164 | while (len >= 65) { |
| 165 | int pr = get_bits(&s->gb, 4); |
| 166 | if (pr > 1) { |
| 167 | av_log(s->avctx, AV_LOG_ERROR, "dqt: invalid precision\n"); |
| 168 | return AVERROR_INVALIDDATA; |
| 169 | } |
| 170 | index = get_bits(&s->gb, 4); |
| 171 | if (index >= 4) |
| 172 | return -1; |
| 173 | av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index); |
| 174 | /* read quant table */ |
| 175 | for (i = 0; i < 64; i++) { |
| 176 | j = s->scantable.permutated[i]; |
| 177 | s->quant_matrixes[index][j] = get_bits(&s->gb, pr ? 16 : 8); |
| 178 | } |
| 179 | |
| 180 | // XXX FIXME finetune, and perhaps add dc too |
| 181 | s->qscale[index] = FFMAX(s->quant_matrixes[index][s->scantable.permutated[1]], |
| 182 | s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1; |
| 183 | av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n", |
| 184 | index, s->qscale[index]); |
| 185 | len -= 65; |
| 186 | } |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* decode huffman tables and build VLC decoders */ |
| 191 | int ff_mjpeg_decode_dht(MJpegDecodeContext *s) |
| 192 | { |
| 193 | int len, index, i, class, n, v, code_max; |
| 194 | uint8_t bits_table[17]; |
| 195 | uint8_t val_table[256]; |
| 196 | int ret = 0; |
| 197 | |
| 198 | len = get_bits(&s->gb, 16) - 2; |
| 199 | |
| 200 | while (len > 0) { |
| 201 | if (len < 17) |
| 202 | return AVERROR_INVALIDDATA; |
| 203 | class = get_bits(&s->gb, 4); |
| 204 | if (class >= 2) |
| 205 | return AVERROR_INVALIDDATA; |
| 206 | index = get_bits(&s->gb, 4); |
| 207 | if (index >= 4) |
| 208 | return AVERROR_INVALIDDATA; |
| 209 | n = 0; |
| 210 | for (i = 1; i <= 16; i++) { |
| 211 | bits_table[i] = get_bits(&s->gb, 8); |
| 212 | n += bits_table[i]; |
| 213 | } |
| 214 | len -= 17; |
| 215 | if (len < n || n > 256) |
| 216 | return AVERROR_INVALIDDATA; |
| 217 | |
| 218 | code_max = 0; |
| 219 | for (i = 0; i < n; i++) { |
| 220 | v = get_bits(&s->gb, 8); |
| 221 | if (v > code_max) |
| 222 | code_max = v; |
| 223 | val_table[i] = v; |
| 224 | } |
| 225 | len -= n; |
| 226 | |
| 227 | /* build VLC and flush previous vlc if present */ |
| 228 | ff_free_vlc(&s->vlcs[class][index]); |
| 229 | av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n", |
| 230 | class, index, code_max + 1); |
| 231 | if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table, |
| 232 | code_max + 1, 0, class > 0)) < 0) |
| 233 | return ret; |
| 234 | |
| 235 | if (class > 0) { |
| 236 | ff_free_vlc(&s->vlcs[2][index]); |
| 237 | if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table, |
| 238 | code_max + 1, 0, 0)) < 0) |
| 239 | return ret; |
| 240 | } |
| 241 | } |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int ff_mjpeg_decode_sof(MJpegDecodeContext *s) |
| 246 | { |
| 247 | int len, nb_components, i, width, height, bits, ret; |
| 248 | unsigned pix_fmt_id; |
| 249 | int h_count[MAX_COMPONENTS]; |
| 250 | int v_count[MAX_COMPONENTS]; |
| 251 | |
| 252 | s->cur_scan = 0; |
| 253 | s->upscale_h = s->upscale_v = 0; |
| 254 | |
| 255 | /* XXX: verify len field validity */ |
| 256 | len = get_bits(&s->gb, 16); |
| 257 | s->avctx->bits_per_raw_sample = |
| 258 | bits = get_bits(&s->gb, 8); |
| 259 | |
| 260 | if (bits > 16 || bits < 1) { |
| 261 | av_log(s->avctx, AV_LOG_ERROR, "bits %d is invalid\n", bits); |
| 262 | return AVERROR_INVALIDDATA; |
| 263 | } |
| 264 | |
| 265 | if (s->pegasus_rct) |
| 266 | bits = 9; |
| 267 | if (bits == 9 && !s->pegasus_rct) |
| 268 | s->rct = 1; // FIXME ugly |
| 269 | |
| 270 | if(s->lossless && s->avctx->lowres){ |
| 271 | av_log(s->avctx, AV_LOG_ERROR, "lowres is not possible with lossless jpeg\n"); |
| 272 | return -1; |
| 273 | } |
| 274 | |
| 275 | height = get_bits(&s->gb, 16); |
| 276 | width = get_bits(&s->gb, 16); |
| 277 | |
| 278 | if (s->avctx->codec_id == AV_CODEC_ID_AMV && (height&15)) |
| 279 | avpriv_request_sample(s->avctx, "non mod 16 height AMV\n"); |
| 280 | |
| 281 | // HACK for odd_height.mov |
| 282 | if (s->interlaced && s->width == width && s->height == height + 1) |
| 283 | height= s->height; |
| 284 | |
| 285 | av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height); |
| 286 | if (av_image_check_size(width, height, 0, s->avctx)) |
| 287 | return AVERROR_INVALIDDATA; |
| 288 | |
| 289 | nb_components = get_bits(&s->gb, 8); |
| 290 | if (nb_components <= 0 || |
| 291 | nb_components > MAX_COMPONENTS) |
| 292 | return -1; |
| 293 | if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) { |
| 294 | if (nb_components != s->nb_components) { |
| 295 | av_log(s->avctx, AV_LOG_ERROR, |
| 296 | "nb_components changing in interlaced picture\n"); |
| 297 | return AVERROR_INVALIDDATA; |
| 298 | } |
| 299 | } |
| 300 | if (s->ls && !(bits <= 8 || nb_components == 1)) { |
| 301 | avpriv_report_missing_feature(s->avctx, |
| 302 | "JPEG-LS that is not <= 8 " |
| 303 | "bits/component or 16-bit gray"); |
| 304 | return AVERROR_PATCHWELCOME; |
| 305 | } |
| 306 | s->nb_components = nb_components; |
| 307 | s->h_max = 1; |
| 308 | s->v_max = 1; |
| 309 | memset(h_count, 0, sizeof(h_count)); |
| 310 | memset(v_count, 0, sizeof(v_count)); |
| 311 | for (i = 0; i < nb_components; i++) { |
| 312 | /* component id */ |
| 313 | s->component_id[i] = get_bits(&s->gb, 8) - 1; |
| 314 | h_count[i] = get_bits(&s->gb, 4); |
| 315 | v_count[i] = get_bits(&s->gb, 4); |
| 316 | /* compute hmax and vmax (only used in interleaved case) */ |
| 317 | if (h_count[i] > s->h_max) |
| 318 | s->h_max = h_count[i]; |
| 319 | if (v_count[i] > s->v_max) |
| 320 | s->v_max = v_count[i]; |
| 321 | s->quant_index[i] = get_bits(&s->gb, 8); |
| 322 | if (s->quant_index[i] >= 4) { |
| 323 | av_log(s->avctx, AV_LOG_ERROR, "quant_index is invalid\n"); |
| 324 | return AVERROR_INVALIDDATA; |
| 325 | } |
| 326 | if (!h_count[i] || !v_count[i]) { |
| 327 | av_log(s->avctx, AV_LOG_ERROR, |
| 328 | "Invalid sampling factor in component %d %d:%d\n", |
| 329 | i, h_count[i], v_count[i]); |
| 330 | return AVERROR_INVALIDDATA; |
| 331 | } |
| 332 | |
| 333 | av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n", |
| 334 | i, h_count[i], v_count[i], |
| 335 | s->component_id[i], s->quant_index[i]); |
| 336 | } |
| 337 | |
| 338 | if (s->ls && (s->h_max > 1 || s->v_max > 1)) { |
| 339 | avpriv_report_missing_feature(s->avctx, "Subsampling in JPEG-LS"); |
| 340 | return AVERROR_PATCHWELCOME; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /* if different size, realloc/alloc picture */ |
| 345 | if ( width != s->width || height != s->height |
| 346 | || bits != s->bits |
| 347 | || memcmp(s->h_count, h_count, sizeof(h_count)) |
| 348 | || memcmp(s->v_count, v_count, sizeof(v_count))) { |
| 349 | |
| 350 | s->width = width; |
| 351 | s->height = height; |
| 352 | s->bits = bits; |
| 353 | memcpy(s->h_count, h_count, sizeof(h_count)); |
| 354 | memcpy(s->v_count, v_count, sizeof(v_count)); |
| 355 | s->interlaced = 0; |
| 356 | s->got_picture = 0; |
| 357 | |
| 358 | /* test interlaced mode */ |
| 359 | if (s->first_picture && |
| 360 | s->org_height != 0 && |
| 361 | s->height < ((s->org_height * 3) / 4)) { |
| 362 | s->interlaced = 1; |
| 363 | s->bottom_field = s->interlace_polarity; |
| 364 | s->picture_ptr->interlaced_frame = 1; |
| 365 | s->picture_ptr->top_field_first = !s->interlace_polarity; |
| 366 | height *= 2; |
| 367 | } |
| 368 | |
| 369 | ret = ff_set_dimensions(s->avctx, width, height); |
| 370 | if (ret < 0) |
| 371 | return ret; |
| 372 | |
| 373 | s->first_picture = 0; |
| 374 | } |
| 375 | |
| 376 | if (s->got_picture && s->interlaced && (s->bottom_field == !s->interlace_polarity)) { |
| 377 | if (s->progressive) { |
| 378 | avpriv_request_sample(s->avctx, "progressively coded interlaced picture"); |
| 379 | return AVERROR_INVALIDDATA; |
| 380 | } |
| 381 | } else{ |
| 382 | if (s->v_max == 1 && s->h_max == 1 && s->lossless==1 && (nb_components==3 || nb_components==4)) |
| 383 | s->rgb = 1; |
| 384 | else if (!s->lossless) |
| 385 | s->rgb = 0; |
| 386 | /* XXX: not complete test ! */ |
| 387 | pix_fmt_id = ((unsigned)s->h_count[0] << 28) | (s->v_count[0] << 24) | |
| 388 | (s->h_count[1] << 20) | (s->v_count[1] << 16) | |
| 389 | (s->h_count[2] << 12) | (s->v_count[2] << 8) | |
| 390 | (s->h_count[3] << 4) | s->v_count[3]; |
| 391 | av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id); |
| 392 | /* NOTE we do not allocate pictures large enough for the possible |
| 393 | * padding of h/v_count being 4 */ |
| 394 | if (!(pix_fmt_id & 0xD0D0D0D0)) |
| 395 | pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1; |
| 396 | if (!(pix_fmt_id & 0x0D0D0D0D)) |
| 397 | pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1; |
| 398 | |
| 399 | for (i = 0; i < 8; i++) { |
| 400 | int j = 6 + (i&1) - (i&6); |
| 401 | int is = (pix_fmt_id >> (4*i)) & 0xF; |
| 402 | int js = (pix_fmt_id >> (4*j)) & 0xF; |
| 403 | |
| 404 | if (is == 1 && js != 2 && (i < 2 || i > 5)) |
| 405 | js = (pix_fmt_id >> ( 8 + 4*(i&1))) & 0xF; |
| 406 | if (is == 1 && js != 2 && (i < 2 || i > 5)) |
| 407 | js = (pix_fmt_id >> (16 + 4*(i&1))) & 0xF; |
| 408 | |
| 409 | if (is == 1 && js == 2) { |
| 410 | if (i & 1) s->upscale_h |= 1 << (j/2); |
| 411 | else s->upscale_v |= 1 << (j/2); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | switch (pix_fmt_id) { |
| 416 | case 0x11111100: |
| 417 | if (s->rgb) |
| 418 | s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_BGR48; |
| 419 | else { |
| 420 | if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') { |
| 421 | s->avctx->pix_fmt = s->bits <= 8 ? AV_PIX_FMT_GBRP : AV_PIX_FMT_GBRP16; |
| 422 | } else { |
| 423 | if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P; |
| 424 | else s->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; |
| 425 | s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; |
| 426 | } |
| 427 | } |
| 428 | av_assert0(s->nb_components == 3); |
| 429 | break; |
| 430 | case 0x11111111: |
| 431 | if (s->rgb) |
| 432 | s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA64; |
| 433 | else { |
| 434 | if (s->adobe_transform == 0 && s->bits <= 8) { |
| 435 | s->avctx->pix_fmt = AV_PIX_FMT_GBRAP; |
| 436 | } else { |
| 437 | s->avctx->pix_fmt = s->bits <= 8 ? AV_PIX_FMT_YUVA444P : AV_PIX_FMT_YUVA444P16; |
| 438 | s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; |
| 439 | } |
| 440 | } |
| 441 | av_assert0(s->nb_components == 4); |
| 442 | break; |
| 443 | case 0x22111122: |
| 444 | case 0x22111111: |
| 445 | if (s->adobe_transform == 0 && s->bits <= 8) { |
| 446 | s->avctx->pix_fmt = AV_PIX_FMT_GBRAP; |
| 447 | s->upscale_v |= 6; |
| 448 | s->upscale_h |= 6; |
| 449 | } else if (s->adobe_transform == 2 && s->bits <= 8) { |
| 450 | s->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; |
| 451 | s->upscale_v |= 6; |
| 452 | s->upscale_h |= 6; |
| 453 | s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; |
| 454 | } else { |
| 455 | if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; |
| 456 | else s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16; |
| 457 | s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; |
| 458 | } |
| 459 | av_assert0(s->nb_components == 4); |
| 460 | break; |
| 461 | case 0x12121100: |
| 462 | case 0x22122100: |
| 463 | case 0x21211100: |
| 464 | case 0x22211200: |
| 465 | if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P; |
| 466 | else |
| 467 | goto unk_pixfmt; |
| 468 | s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; |
| 469 | break; |
| 470 | case 0x22221100: |
| 471 | case 0x22112200: |
| 472 | case 0x11222200: |
| 473 | if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P; |
| 474 | else |
| 475 | goto unk_pixfmt; |
| 476 | s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; |
| 477 | break; |
| 478 | case 0x11000000: |
| 479 | case 0x13000000: |
| 480 | case 0x14000000: |
| 481 | case 0x31000000: |
| 482 | case 0x33000000: |
| 483 | case 0x34000000: |
| 484 | case 0x41000000: |
| 485 | case 0x43000000: |
| 486 | case 0x44000000: |
| 487 | if(s->bits <= 8) |
| 488 | s->avctx->pix_fmt = AV_PIX_FMT_GRAY8; |
| 489 | else |
| 490 | s->avctx->pix_fmt = AV_PIX_FMT_GRAY16; |
| 491 | break; |
| 492 | case 0x12111100: |
| 493 | case 0x14121200: |
| 494 | case 0x14111100: |
| 495 | case 0x22211100: |
| 496 | case 0x22112100: |
| 497 | if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') { |
| 498 | if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_GBRP; |
| 499 | else |
| 500 | goto unk_pixfmt; |
| 501 | s->upscale_v |= 3; |
| 502 | } else { |
| 503 | if (pix_fmt_id == 0x14111100) |
| 504 | s->upscale_v |= 6; |
| 505 | if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV440P : AV_PIX_FMT_YUVJ440P; |
| 506 | else |
| 507 | goto unk_pixfmt; |
| 508 | s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; |
| 509 | } |
| 510 | break; |
| 511 | case 0x21111100: |
| 512 | if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') { |
| 513 | if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_GBRP; |
| 514 | else |
| 515 | goto unk_pixfmt; |
| 516 | s->upscale_h |= 3; |
| 517 | } else { |
| 518 | if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P; |
| 519 | else s->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; |
| 520 | s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; |
| 521 | } |
| 522 | break; |
| 523 | case 0x22121100: |
| 524 | case 0x22111200: |
| 525 | if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P; |
| 526 | else |
| 527 | goto unk_pixfmt; |
| 528 | s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; |
| 529 | break; |
| 530 | case 0x22111100: |
| 531 | case 0x42111100: |
| 532 | case 0x24111100: |
| 533 | if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUVJ420P; |
| 534 | else s->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; |
| 535 | s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; |
| 536 | if (pix_fmt_id == 0x42111100) { |
| 537 | if (s->bits > 8) |
| 538 | goto unk_pixfmt; |
| 539 | s->upscale_h = 6; |
| 540 | } else if (pix_fmt_id == 0x24111100) { |
| 541 | if (s->bits > 8) |
| 542 | goto unk_pixfmt; |
| 543 | s->upscale_v = 6; |
| 544 | } |
| 545 | break; |
| 546 | case 0x41111100: |
| 547 | if (s->bits <= 8) s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV411P : AV_PIX_FMT_YUVJ411P; |
| 548 | else |
| 549 | goto unk_pixfmt; |
| 550 | s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG; |
| 551 | break; |
| 552 | default: |
| 553 | unk_pixfmt: |
| 554 | av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x bits:%d\n", pix_fmt_id, s->bits); |
| 555 | s->upscale_h = s->upscale_v = 0; |
| 556 | return AVERROR_PATCHWELCOME; |
| 557 | } |
| 558 | if ((s->upscale_h || s->upscale_v) && s->avctx->lowres) { |
| 559 | av_log(s->avctx, AV_LOG_ERROR, "lowres not supported for weird subsampling\n"); |
| 560 | return AVERROR_PATCHWELCOME; |
| 561 | } |
| 562 | if (s->ls) { |
| 563 | s->upscale_h = s->upscale_v = 0; |
| 564 | if (s->nb_components > 1) |
| 565 | s->avctx->pix_fmt = AV_PIX_FMT_RGB24; |
| 566 | else if (s->palette_index && s->bits <= 8) |
| 567 | s->avctx->pix_fmt = AV_PIX_FMT_PAL8; |
| 568 | else if (s->bits <= 8) |
| 569 | s->avctx->pix_fmt = AV_PIX_FMT_GRAY8; |
| 570 | else |
| 571 | s->avctx->pix_fmt = AV_PIX_FMT_GRAY16; |
| 572 | } |
| 573 | |
| 574 | s->pix_desc = av_pix_fmt_desc_get(s->avctx->pix_fmt); |
| 575 | if (!s->pix_desc) { |
| 576 | av_log(s->avctx, AV_LOG_ERROR, "Could not get a pixel format descriptor.\n"); |
| 577 | return AVERROR_BUG; |
| 578 | } |
| 579 | |
| 580 | av_frame_unref(s->picture_ptr); |
| 581 | if (ff_get_buffer(s->avctx, s->picture_ptr, AV_GET_BUFFER_FLAG_REF) < 0) |
| 582 | return -1; |
| 583 | s->picture_ptr->pict_type = AV_PICTURE_TYPE_I; |
| 584 | s->picture_ptr->key_frame = 1; |
| 585 | s->got_picture = 1; |
| 586 | |
| 587 | for (i = 0; i < 4; i++) |
| 588 | s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced; |
| 589 | |
| 590 | av_dlog(s->avctx, "%d %d %d %d %d %d\n", |
| 591 | s->width, s->height, s->linesize[0], s->linesize[1], |
| 592 | s->interlaced, s->avctx->height); |
| 593 | |
| 594 | if (len != (8 + (3 * nb_components))) |
| 595 | av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len); |
| 596 | } |
| 597 | |
| 598 | if (s->rgb && !s->lossless && !s->ls) { |
| 599 | av_log(s->avctx, AV_LOG_ERROR, "Unsupported coding and pixel format combination\n"); |
| 600 | return AVERROR_PATCHWELCOME; |
| 601 | } |
| 602 | |
| 603 | /* totally blank picture as progressive JPEG will only add details to it */ |
| 604 | if (s->progressive) { |
| 605 | int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8); |
| 606 | int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8); |
| 607 | for (i = 0; i < s->nb_components; i++) { |
| 608 | int size = bw * bh * s->h_count[i] * s->v_count[i]; |
| 609 | av_freep(&s->blocks[i]); |
| 610 | av_freep(&s->last_nnz[i]); |
| 611 | s->blocks[i] = av_mallocz_array(size, sizeof(**s->blocks)); |
| 612 | s->last_nnz[i] = av_mallocz_array(size, sizeof(**s->last_nnz)); |
| 613 | if (!s->blocks[i] || !s->last_nnz[i]) |
| 614 | return AVERROR(ENOMEM); |
| 615 | s->block_stride[i] = bw * s->h_count[i]; |
| 616 | } |
| 617 | memset(s->coefs_finished, 0, sizeof(s->coefs_finished)); |
| 618 | } |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index) |
| 623 | { |
| 624 | int code; |
| 625 | code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2); |
| 626 | if (code < 0 || code > 16) { |
| 627 | av_log(s->avctx, AV_LOG_WARNING, |
| 628 | "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", |
| 629 | 0, dc_index, &s->vlcs[0][dc_index]); |
| 630 | return 0xfffff; |
| 631 | } |
| 632 | |
| 633 | if (code) |
| 634 | return get_xbits(&s->gb, code); |
| 635 | else |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | /* decode block and dequantize */ |
| 640 | static int decode_block(MJpegDecodeContext *s, int16_t *block, int component, |
| 641 | int dc_index, int ac_index, int16_t *quant_matrix) |
| 642 | { |
| 643 | int code, i, j, level, val; |
| 644 | |
| 645 | /* DC coef */ |
| 646 | val = mjpeg_decode_dc(s, dc_index); |
| 647 | if (val == 0xfffff) { |
| 648 | av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); |
| 649 | return AVERROR_INVALIDDATA; |
| 650 | } |
| 651 | val = val * quant_matrix[0] + s->last_dc[component]; |
| 652 | s->last_dc[component] = val; |
| 653 | block[0] = val; |
| 654 | /* AC coefs */ |
| 655 | i = 0; |
| 656 | {OPEN_READER(re, &s->gb); |
| 657 | do { |
| 658 | UPDATE_CACHE(re, &s->gb); |
| 659 | GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2); |
| 660 | |
| 661 | i += ((unsigned)code) >> 4; |
| 662 | code &= 0xf; |
| 663 | if (code) { |
| 664 | if (code > MIN_CACHE_BITS - 16) |
| 665 | UPDATE_CACHE(re, &s->gb); |
| 666 | |
| 667 | { |
| 668 | int cache = GET_CACHE(re, &s->gb); |
| 669 | int sign = (~cache) >> 31; |
| 670 | level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; |
| 671 | } |
| 672 | |
| 673 | LAST_SKIP_BITS(re, &s->gb, code); |
| 674 | |
| 675 | if (i > 63) { |
| 676 | av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); |
| 677 | return AVERROR_INVALIDDATA; |
| 678 | } |
| 679 | j = s->scantable.permutated[i]; |
| 680 | block[j] = level * quant_matrix[j]; |
| 681 | } |
| 682 | } while (i < 63); |
| 683 | CLOSE_READER(re, &s->gb);} |
| 684 | |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block, |
| 689 | int component, int dc_index, |
| 690 | int16_t *quant_matrix, int Al) |
| 691 | { |
| 692 | int val; |
| 693 | s->bdsp.clear_block(block); |
| 694 | val = mjpeg_decode_dc(s, dc_index); |
| 695 | if (val == 0xfffff) { |
| 696 | av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); |
| 697 | return AVERROR_INVALIDDATA; |
| 698 | } |
| 699 | val = (val * quant_matrix[0] << Al) + s->last_dc[component]; |
| 700 | s->last_dc[component] = val; |
| 701 | block[0] = val; |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | /* decode block and dequantize - progressive JPEG version */ |
| 706 | static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block, |
| 707 | uint8_t *last_nnz, int ac_index, |
| 708 | int16_t *quant_matrix, |
| 709 | int ss, int se, int Al, int *EOBRUN) |
| 710 | { |
| 711 | int code, i, j, level, val, run; |
| 712 | |
| 713 | if (*EOBRUN) { |
| 714 | (*EOBRUN)--; |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | { |
| 719 | OPEN_READER(re, &s->gb); |
| 720 | for (i = ss; ; i++) { |
| 721 | UPDATE_CACHE(re, &s->gb); |
| 722 | GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2); |
| 723 | |
| 724 | run = ((unsigned) code) >> 4; |
| 725 | code &= 0xF; |
| 726 | if (code) { |
| 727 | i += run; |
| 728 | if (code > MIN_CACHE_BITS - 16) |
| 729 | UPDATE_CACHE(re, &s->gb); |
| 730 | |
| 731 | { |
| 732 | int cache = GET_CACHE(re, &s->gb); |
| 733 | int sign = (~cache) >> 31; |
| 734 | level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; |
| 735 | } |
| 736 | |
| 737 | LAST_SKIP_BITS(re, &s->gb, code); |
| 738 | |
| 739 | if (i >= se) { |
| 740 | if (i == se) { |
| 741 | j = s->scantable.permutated[se]; |
| 742 | block[j] = level * quant_matrix[j] << Al; |
| 743 | break; |
| 744 | } |
| 745 | av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); |
| 746 | return AVERROR_INVALIDDATA; |
| 747 | } |
| 748 | j = s->scantable.permutated[i]; |
| 749 | block[j] = level * quant_matrix[j] << Al; |
| 750 | } else { |
| 751 | if (run == 0xF) {// ZRL - skip 15 coefficients |
| 752 | i += 15; |
| 753 | if (i >= se) { |
| 754 | av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i); |
| 755 | return AVERROR_INVALIDDATA; |
| 756 | } |
| 757 | } else { |
| 758 | val = (1 << run); |
| 759 | if (run) { |
| 760 | UPDATE_CACHE(re, &s->gb); |
| 761 | val += NEG_USR32(GET_CACHE(re, &s->gb), run); |
| 762 | LAST_SKIP_BITS(re, &s->gb, run); |
| 763 | } |
| 764 | *EOBRUN = val - 1; |
| 765 | break; |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | CLOSE_READER(re, &s->gb); |
| 770 | } |
| 771 | |
| 772 | if (i > *last_nnz) |
| 773 | *last_nnz = i; |
| 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | #define REFINE_BIT(j) { \ |
| 779 | UPDATE_CACHE(re, &s->gb); \ |
| 780 | sign = block[j] >> 15; \ |
| 781 | block[j] += SHOW_UBITS(re, &s->gb, 1) * \ |
| 782 | ((quant_matrix[j] ^ sign) - sign) << Al; \ |
| 783 | LAST_SKIP_BITS(re, &s->gb, 1); \ |
| 784 | } |
| 785 | |
| 786 | #define ZERO_RUN \ |
| 787 | for (; ; i++) { \ |
| 788 | if (i > last) { \ |
| 789 | i += run; \ |
| 790 | if (i > se) { \ |
| 791 | av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \ |
| 792 | return -1; \ |
| 793 | } \ |
| 794 | break; \ |
| 795 | } \ |
| 796 | j = s->scantable.permutated[i]; \ |
| 797 | if (block[j]) \ |
| 798 | REFINE_BIT(j) \ |
| 799 | else if (run-- == 0) \ |
| 800 | break; \ |
| 801 | } |
| 802 | |
| 803 | /* decode block and dequantize - progressive JPEG refinement pass */ |
| 804 | static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block, |
| 805 | uint8_t *last_nnz, |
| 806 | int ac_index, int16_t *quant_matrix, |
| 807 | int ss, int se, int Al, int *EOBRUN) |
| 808 | { |
| 809 | int code, i = ss, j, sign, val, run; |
| 810 | int last = FFMIN(se, *last_nnz); |
| 811 | |
| 812 | OPEN_READER(re, &s->gb); |
| 813 | if (*EOBRUN) { |
| 814 | (*EOBRUN)--; |
| 815 | } else { |
| 816 | for (; ; i++) { |
| 817 | UPDATE_CACHE(re, &s->gb); |
| 818 | GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2); |
| 819 | |
| 820 | if (code & 0xF) { |
| 821 | run = ((unsigned) code) >> 4; |
| 822 | UPDATE_CACHE(re, &s->gb); |
| 823 | val = SHOW_UBITS(re, &s->gb, 1); |
| 824 | LAST_SKIP_BITS(re, &s->gb, 1); |
| 825 | ZERO_RUN; |
| 826 | j = s->scantable.permutated[i]; |
| 827 | val--; |
| 828 | block[j] = ((quant_matrix[j]^val) - val) << Al; |
| 829 | if (i == se) { |
| 830 | if (i > *last_nnz) |
| 831 | *last_nnz = i; |
| 832 | CLOSE_READER(re, &s->gb); |
| 833 | return 0; |
| 834 | } |
| 835 | } else { |
| 836 | run = ((unsigned) code) >> 4; |
| 837 | if (run == 0xF) { |
| 838 | ZERO_RUN; |
| 839 | } else { |
| 840 | val = run; |
| 841 | run = (1 << run); |
| 842 | if (val) { |
| 843 | UPDATE_CACHE(re, &s->gb); |
| 844 | run += SHOW_UBITS(re, &s->gb, val); |
| 845 | LAST_SKIP_BITS(re, &s->gb, val); |
| 846 | } |
| 847 | *EOBRUN = run - 1; |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | if (i > *last_nnz) |
| 854 | *last_nnz = i; |
| 855 | } |
| 856 | |
| 857 | for (; i <= last; i++) { |
| 858 | j = s->scantable.permutated[i]; |
| 859 | if (block[j]) |
| 860 | REFINE_BIT(j) |
| 861 | } |
| 862 | CLOSE_READER(re, &s->gb); |
| 863 | |
| 864 | return 0; |
| 865 | } |
| 866 | #undef REFINE_BIT |
| 867 | #undef ZERO_RUN |
| 868 | |
| 869 | static int handle_rstn(MJpegDecodeContext *s, int nb_components) |
| 870 | { |
| 871 | int i; |
| 872 | int reset = 0; |
| 873 | |
| 874 | if (s->restart_interval) { |
| 875 | s->restart_count--; |
| 876 | if(s->restart_count == 0 && s->avctx->codec_id == AV_CODEC_ID_THP){ |
| 877 | align_get_bits(&s->gb); |
| 878 | for (i = 0; i < nb_components; i++) /* reset dc */ |
| 879 | s->last_dc[i] = (4 << s->bits); |
| 880 | } |
| 881 | |
| 882 | i = 8 + ((-get_bits_count(&s->gb)) & 7); |
| 883 | /* skip RSTn */ |
| 884 | if (s->restart_count == 0) { |
| 885 | if( show_bits(&s->gb, i) == (1 << i) - 1 |
| 886 | || show_bits(&s->gb, i) == 0xFF) { |
| 887 | int pos = get_bits_count(&s->gb); |
| 888 | align_get_bits(&s->gb); |
| 889 | while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF) |
| 890 | skip_bits(&s->gb, 8); |
| 891 | if (get_bits_left(&s->gb) >= 8 && (get_bits(&s->gb, 8) & 0xF8) == 0xD0) { |
| 892 | for (i = 0; i < nb_components; i++) /* reset dc */ |
| 893 | s->last_dc[i] = (4 << s->bits); |
| 894 | reset = 1; |
| 895 | } else |
| 896 | skip_bits_long(&s->gb, pos - get_bits_count(&s->gb)); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | return reset; |
| 901 | } |
| 902 | |
| 903 | static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform) |
| 904 | { |
| 905 | int i, mb_x, mb_y; |
| 906 | uint16_t (*buffer)[4]; |
| 907 | int left[4], top[4], topleft[4]; |
| 908 | const int linesize = s->linesize[0]; |
| 909 | const int mask = ((1 << s->bits) - 1) << point_transform; |
| 910 | int resync_mb_y = 0; |
| 911 | int resync_mb_x = 0; |
| 912 | |
| 913 | if (s->nb_components != 3 && s->nb_components != 4) |
| 914 | return AVERROR_INVALIDDATA; |
| 915 | if (s->v_max != 1 || s->h_max != 1 || !s->lossless) |
| 916 | return AVERROR_INVALIDDATA; |
| 917 | |
| 918 | |
| 919 | s->restart_count = s->restart_interval; |
| 920 | |
| 921 | av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, |
| 922 | (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0])); |
| 923 | buffer = s->ljpeg_buffer; |
| 924 | |
| 925 | for (i = 0; i < 4; i++) |
| 926 | buffer[0][i] = 1 << (s->bits - 1); |
| 927 | |
| 928 | for (mb_y = 0; mb_y < s->mb_height; mb_y++) { |
| 929 | uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y); |
| 930 | |
| 931 | if (s->interlaced && s->bottom_field) |
| 932 | ptr += linesize >> 1; |
| 933 | |
| 934 | for (i = 0; i < 4; i++) |
| 935 | top[i] = left[i] = topleft[i] = buffer[0][i]; |
| 936 | |
| 937 | for (mb_x = 0; mb_x < s->mb_width; mb_x++) { |
| 938 | int modified_predictor = predictor; |
| 939 | |
| 940 | if (s->restart_interval && !s->restart_count){ |
| 941 | s->restart_count = s->restart_interval; |
| 942 | resync_mb_x = mb_x; |
| 943 | resync_mb_y = mb_y; |
| 944 | for(i=0; i<4; i++) |
| 945 | top[i] = left[i]= topleft[i]= 1 << (s->bits - 1); |
| 946 | } |
| 947 | if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x) |
| 948 | modified_predictor = 1; |
| 949 | |
| 950 | for (i=0;i<nb_components;i++) { |
| 951 | int pred, dc; |
| 952 | |
| 953 | topleft[i] = top[i]; |
| 954 | top[i] = buffer[mb_x][i]; |
| 955 | |
| 956 | PREDICT(pred, topleft[i], top[i], left[i], modified_predictor); |
| 957 | |
| 958 | dc = mjpeg_decode_dc(s, s->dc_index[i]); |
| 959 | if(dc == 0xFFFFF) |
| 960 | return -1; |
| 961 | |
| 962 | left[i] = buffer[mb_x][i] = |
| 963 | mask & (pred + (dc << point_transform)); |
| 964 | } |
| 965 | |
| 966 | if (s->restart_interval && !--s->restart_count) { |
| 967 | align_get_bits(&s->gb); |
| 968 | skip_bits(&s->gb, 16); /* skip RSTn */ |
| 969 | } |
| 970 | } |
| 971 | if (s->nb_components == 4) { |
| 972 | for(i=0; i<nb_components; i++) { |
| 973 | int c= s->comp_index[i]; |
| 974 | if (s->bits <= 8) { |
| 975 | for(mb_x = 0; mb_x < s->mb_width; mb_x++) { |
| 976 | ptr[4*mb_x+3-c] = buffer[mb_x][i]; |
| 977 | } |
| 978 | } else if(s->bits == 9) { |
| 979 | return AVERROR_PATCHWELCOME; |
| 980 | } else { |
| 981 | for(mb_x = 0; mb_x < s->mb_width; mb_x++) { |
| 982 | ((uint16_t*)ptr)[4*mb_x+c] = buffer[mb_x][i]; |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | } else if (s->rct) { |
| 987 | for (mb_x = 0; mb_x < s->mb_width; mb_x++) { |
| 988 | ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2); |
| 989 | ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1]; |
| 990 | ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1]; |
| 991 | } |
| 992 | } else if (s->pegasus_rct) { |
| 993 | for (mb_x = 0; mb_x < s->mb_width; mb_x++) { |
| 994 | ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2); |
| 995 | ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1]; |
| 996 | ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1]; |
| 997 | } |
| 998 | } else { |
| 999 | for(i=0; i<nb_components; i++) { |
| 1000 | int c= s->comp_index[i]; |
| 1001 | if (s->bits <= 8) { |
| 1002 | for(mb_x = 0; mb_x < s->mb_width; mb_x++) { |
| 1003 | ptr[3*mb_x+2-c] = buffer[mb_x][i]; |
| 1004 | } |
| 1005 | } else if(s->bits == 9) { |
| 1006 | return AVERROR_PATCHWELCOME; |
| 1007 | } else { |
| 1008 | for(mb_x = 0; mb_x < s->mb_width; mb_x++) { |
| 1009 | ((uint16_t*)ptr)[3*mb_x+2-c] = buffer[mb_x][i]; |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, |
| 1019 | int point_transform, int nb_components) |
| 1020 | { |
| 1021 | int i, mb_x, mb_y, mask; |
| 1022 | int bits= (s->bits+7)&~7; |
| 1023 | int resync_mb_y = 0; |
| 1024 | int resync_mb_x = 0; |
| 1025 | |
| 1026 | point_transform += bits - s->bits; |
| 1027 | mask = ((1 << s->bits) - 1) << point_transform; |
| 1028 | |
| 1029 | av_assert0(nb_components>=1 && nb_components<=4); |
| 1030 | |
| 1031 | for (mb_y = 0; mb_y < s->mb_height; mb_y++) { |
| 1032 | for (mb_x = 0; mb_x < s->mb_width; mb_x++) { |
| 1033 | if (s->restart_interval && !s->restart_count){ |
| 1034 | s->restart_count = s->restart_interval; |
| 1035 | resync_mb_x = mb_x; |
| 1036 | resync_mb_y = mb_y; |
| 1037 | } |
| 1038 | |
| 1039 | if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){ |
| 1040 | int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x; |
| 1041 | int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x; |
| 1042 | for (i = 0; i < nb_components; i++) { |
| 1043 | uint8_t *ptr; |
| 1044 | uint16_t *ptr16; |
| 1045 | int n, h, v, x, y, c, j, linesize; |
| 1046 | n = s->nb_blocks[i]; |
| 1047 | c = s->comp_index[i]; |
| 1048 | h = s->h_scount[i]; |
| 1049 | v = s->v_scount[i]; |
| 1050 | x = 0; |
| 1051 | y = 0; |
| 1052 | linesize= s->linesize[c]; |
| 1053 | |
| 1054 | if(bits>8) linesize /= 2; |
| 1055 | |
| 1056 | for(j=0; j<n; j++) { |
| 1057 | int pred, dc; |
| 1058 | |
| 1059 | dc = mjpeg_decode_dc(s, s->dc_index[i]); |
| 1060 | if(dc == 0xFFFFF) |
| 1061 | return -1; |
| 1062 | if(bits<=8){ |
| 1063 | ptr = s->picture_ptr->data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap |
| 1064 | if(y==0 && toprow){ |
| 1065 | if(x==0 && leftcol){ |
| 1066 | pred= 1 << (bits - 1); |
| 1067 | }else{ |
| 1068 | pred= ptr[-1]; |
| 1069 | } |
| 1070 | }else{ |
| 1071 | if(x==0 && leftcol){ |
| 1072 | pred= ptr[-linesize]; |
| 1073 | }else{ |
| 1074 | PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | if (s->interlaced && s->bottom_field) |
| 1079 | ptr += linesize >> 1; |
| 1080 | pred &= mask; |
| 1081 | *ptr= pred + (dc << point_transform); |
| 1082 | }else{ |
| 1083 | ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap |
| 1084 | if(y==0 && toprow){ |
| 1085 | if(x==0 && leftcol){ |
| 1086 | pred= 1 << (bits - 1); |
| 1087 | }else{ |
| 1088 | pred= ptr16[-1]; |
| 1089 | } |
| 1090 | }else{ |
| 1091 | if(x==0 && leftcol){ |
| 1092 | pred= ptr16[-linesize]; |
| 1093 | }else{ |
| 1094 | PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor); |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | if (s->interlaced && s->bottom_field) |
| 1099 | ptr16 += linesize >> 1; |
| 1100 | pred &= mask; |
| 1101 | *ptr16= pred + (dc << point_transform); |
| 1102 | } |
| 1103 | if (++x == h) { |
| 1104 | x = 0; |
| 1105 | y++; |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | } else { |
| 1110 | for (i = 0; i < nb_components; i++) { |
| 1111 | uint8_t *ptr; |
| 1112 | uint16_t *ptr16; |
| 1113 | int n, h, v, x, y, c, j, linesize, dc; |
| 1114 | n = s->nb_blocks[i]; |
| 1115 | c = s->comp_index[i]; |
| 1116 | h = s->h_scount[i]; |
| 1117 | v = s->v_scount[i]; |
| 1118 | x = 0; |
| 1119 | y = 0; |
| 1120 | linesize = s->linesize[c]; |
| 1121 | |
| 1122 | if(bits>8) linesize /= 2; |
| 1123 | |
| 1124 | for (j = 0; j < n; j++) { |
| 1125 | int pred; |
| 1126 | |
| 1127 | dc = mjpeg_decode_dc(s, s->dc_index[i]); |
| 1128 | if(dc == 0xFFFFF) |
| 1129 | return -1; |
| 1130 | if(bits<=8){ |
| 1131 | ptr = s->picture_ptr->data[c] + |
| 1132 | (linesize * (v * mb_y + y)) + |
| 1133 | (h * mb_x + x); //FIXME optimize this crap |
| 1134 | PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); |
| 1135 | |
| 1136 | pred &= mask; |
| 1137 | *ptr = pred + (dc << point_transform); |
| 1138 | }else{ |
| 1139 | ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap |
| 1140 | PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor); |
| 1141 | |
| 1142 | pred &= mask; |
| 1143 | *ptr16= pred + (dc << point_transform); |
| 1144 | } |
| 1145 | |
| 1146 | if (++x == h) { |
| 1147 | x = 0; |
| 1148 | y++; |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | if (s->restart_interval && !--s->restart_count) { |
| 1154 | align_get_bits(&s->gb); |
| 1155 | skip_bits(&s->gb, 16); /* skip RSTn */ |
| 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | static av_always_inline void mjpeg_copy_block(MJpegDecodeContext *s, |
| 1163 | uint8_t *dst, const uint8_t *src, |
| 1164 | int linesize, int lowres) |
| 1165 | { |
| 1166 | switch (lowres) { |
| 1167 | case 0: s->hdsp.put_pixels_tab[1][0](dst, src, linesize, 8); |
| 1168 | break; |
| 1169 | case 1: copy_block4(dst, src, linesize, linesize, 4); |
| 1170 | break; |
| 1171 | case 2: copy_block2(dst, src, linesize, linesize, 2); |
| 1172 | break; |
| 1173 | case 3: *dst = *src; |
| 1174 | break; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize) |
| 1179 | { |
| 1180 | int block_x, block_y; |
| 1181 | int size = 8 >> s->avctx->lowres; |
| 1182 | if (s->bits > 8) { |
| 1183 | for (block_y=0; block_y<size; block_y++) |
| 1184 | for (block_x=0; block_x<size; block_x++) |
| 1185 | *(uint16_t*)(ptr + 2*block_x + block_y*linesize) <<= 16 - s->bits; |
| 1186 | } else { |
| 1187 | for (block_y=0; block_y<size; block_y++) |
| 1188 | for (block_x=0; block_x<size; block_x++) |
| 1189 | *(ptr + block_x + block_y*linesize) <<= 8 - s->bits; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, |
| 1194 | int Al, const uint8_t *mb_bitmask, |
| 1195 | int mb_bitmask_size, |
| 1196 | const AVFrame *reference) |
| 1197 | { |
| 1198 | int i, mb_x, mb_y; |
| 1199 | uint8_t *data[MAX_COMPONENTS]; |
| 1200 | const uint8_t *reference_data[MAX_COMPONENTS]; |
| 1201 | int linesize[MAX_COMPONENTS]; |
| 1202 | GetBitContext mb_bitmask_gb = {0}; // initialize to silence gcc warning |
| 1203 | int bytes_per_pixel = 1 + (s->bits > 8); |
| 1204 | |
| 1205 | if (mb_bitmask) { |
| 1206 | if (mb_bitmask_size != (s->mb_width * s->mb_height + 7)>>3) { |
| 1207 | av_log(s->avctx, AV_LOG_ERROR, "mb_bitmask_size mismatches\n"); |
| 1208 | return AVERROR_INVALIDDATA; |
| 1209 | } |
| 1210 | init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height); |
| 1211 | } |
| 1212 | |
| 1213 | s->restart_count = 0; |
| 1214 | |
| 1215 | for (i = 0; i < nb_components; i++) { |
| 1216 | int c = s->comp_index[i]; |
| 1217 | data[c] = s->picture_ptr->data[c]; |
| 1218 | reference_data[c] = reference ? reference->data[c] : NULL; |
| 1219 | linesize[c] = s->linesize[c]; |
| 1220 | s->coefs_finished[c] |= 1; |
| 1221 | } |
| 1222 | |
| 1223 | for (mb_y = 0; mb_y < s->mb_height; mb_y++) { |
| 1224 | for (mb_x = 0; mb_x < s->mb_width; mb_x++) { |
| 1225 | const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb); |
| 1226 | |
| 1227 | if (s->restart_interval && !s->restart_count) |
| 1228 | s->restart_count = s->restart_interval; |
| 1229 | |
| 1230 | if (get_bits_left(&s->gb) < 0) { |
| 1231 | av_log(s->avctx, AV_LOG_ERROR, "overread %d\n", |
| 1232 | -get_bits_left(&s->gb)); |
| 1233 | return AVERROR_INVALIDDATA; |
| 1234 | } |
| 1235 | for (i = 0; i < nb_components; i++) { |
| 1236 | uint8_t *ptr; |
| 1237 | int n, h, v, x, y, c, j; |
| 1238 | int block_offset; |
| 1239 | n = s->nb_blocks[i]; |
| 1240 | c = s->comp_index[i]; |
| 1241 | h = s->h_scount[i]; |
| 1242 | v = s->v_scount[i]; |
| 1243 | x = 0; |
| 1244 | y = 0; |
| 1245 | for (j = 0; j < n; j++) { |
| 1246 | block_offset = (((linesize[c] * (v * mb_y + y) * 8) + |
| 1247 | (h * mb_x + x) * 8 * bytes_per_pixel) >> s->avctx->lowres); |
| 1248 | |
| 1249 | if (s->interlaced && s->bottom_field) |
| 1250 | block_offset += linesize[c] >> 1; |
| 1251 | ptr = data[c] + block_offset; |
| 1252 | if (!s->progressive) { |
| 1253 | if (copy_mb) |
| 1254 | mjpeg_copy_block(s, ptr, reference_data[c] + block_offset, |
| 1255 | linesize[c], s->avctx->lowres); |
| 1256 | |
| 1257 | else { |
| 1258 | s->bdsp.clear_block(s->block); |
| 1259 | if (decode_block(s, s->block, i, |
| 1260 | s->dc_index[i], s->ac_index[i], |
| 1261 | s->quant_matrixes[s->quant_sindex[i]]) < 0) { |
| 1262 | av_log(s->avctx, AV_LOG_ERROR, |
| 1263 | "error y=%d x=%d\n", mb_y, mb_x); |
| 1264 | return AVERROR_INVALIDDATA; |
| 1265 | } |
| 1266 | s->idsp.idct_put(ptr, linesize[c], s->block); |
| 1267 | if (s->bits & 7) |
| 1268 | shift_output(s, ptr, linesize[c]); |
| 1269 | } |
| 1270 | } else { |
| 1271 | int block_idx = s->block_stride[c] * (v * mb_y + y) + |
| 1272 | (h * mb_x + x); |
| 1273 | int16_t *block = s->blocks[c][block_idx]; |
| 1274 | if (Ah) |
| 1275 | block[0] += get_bits1(&s->gb) * |
| 1276 | s->quant_matrixes[s->quant_sindex[i]][0] << Al; |
| 1277 | else if (decode_dc_progressive(s, block, i, s->dc_index[i], |
| 1278 | s->quant_matrixes[s->quant_sindex[i]], |
| 1279 | Al) < 0) { |
| 1280 | av_log(s->avctx, AV_LOG_ERROR, |
| 1281 | "error y=%d x=%d\n", mb_y, mb_x); |
| 1282 | return AVERROR_INVALIDDATA; |
| 1283 | } |
| 1284 | } |
| 1285 | av_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x); |
| 1286 | av_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n", |
| 1287 | mb_x, mb_y, x, y, c, s->bottom_field, |
| 1288 | (v * mb_y + y) * 8, (h * mb_x + x) * 8); |
| 1289 | if (++x == h) { |
| 1290 | x = 0; |
| 1291 | y++; |
| 1292 | } |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | handle_rstn(s, nb_components); |
| 1297 | } |
| 1298 | } |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, |
| 1303 | int se, int Ah, int Al) |
| 1304 | { |
| 1305 | int mb_x, mb_y; |
| 1306 | int EOBRUN = 0; |
| 1307 | int c = s->comp_index[0]; |
| 1308 | uint8_t *data = s->picture_ptr->data[c]; |
| 1309 | int linesize = s->linesize[c]; |
| 1310 | int last_scan = 0; |
| 1311 | int16_t *quant_matrix = s->quant_matrixes[s->quant_sindex[0]]; |
| 1312 | int bytes_per_pixel = 1 + (s->bits > 8); |
| 1313 | |
| 1314 | av_assert0(ss>=0 && Ah>=0 && Al>=0); |
| 1315 | if (se < ss || se > 63) { |
| 1316 | av_log(s->avctx, AV_LOG_ERROR, "SS/SE %d/%d is invalid\n", ss, se); |
| 1317 | return AVERROR_INVALIDDATA; |
| 1318 | } |
| 1319 | |
| 1320 | if (!Al) { |
| 1321 | // s->coefs_finished is a bitmask for coefficients coded |
| 1322 | // ss and se are parameters telling start and end coefficients |
| 1323 | s->coefs_finished[c] |= (2ULL << se) - (1ULL << ss); |
| 1324 | last_scan = !~s->coefs_finished[c]; |
| 1325 | } |
| 1326 | |
| 1327 | if (s->interlaced && s->bottom_field) |
| 1328 | data += linesize >> 1; |
| 1329 | |
| 1330 | s->restart_count = 0; |
| 1331 | |
| 1332 | for (mb_y = 0; mb_y < s->mb_height; mb_y++) { |
| 1333 | uint8_t *ptr = data + (mb_y * linesize * 8 >> s->avctx->lowres); |
| 1334 | int block_idx = mb_y * s->block_stride[c]; |
| 1335 | int16_t (*block)[64] = &s->blocks[c][block_idx]; |
| 1336 | uint8_t *last_nnz = &s->last_nnz[c][block_idx]; |
| 1337 | for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) { |
| 1338 | int ret; |
| 1339 | if (s->restart_interval && !s->restart_count) |
| 1340 | s->restart_count = s->restart_interval; |
| 1341 | |
| 1342 | if (Ah) |
| 1343 | ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0], |
| 1344 | quant_matrix, ss, se, Al, &EOBRUN); |
| 1345 | else |
| 1346 | ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0], |
| 1347 | quant_matrix, ss, se, Al, &EOBRUN); |
| 1348 | if (ret < 0) { |
| 1349 | av_log(s->avctx, AV_LOG_ERROR, |
| 1350 | "error y=%d x=%d\n", mb_y, mb_x); |
| 1351 | return AVERROR_INVALIDDATA; |
| 1352 | } |
| 1353 | |
| 1354 | if (last_scan) { |
| 1355 | s->idsp.idct_put(ptr, linesize, *block); |
| 1356 | if (s->bits & 7) |
| 1357 | shift_output(s, ptr, linesize); |
| 1358 | ptr += bytes_per_pixel*8 >> s->avctx->lowres; |
| 1359 | } |
| 1360 | if (handle_rstn(s, 0)) |
| 1361 | EOBRUN = 0; |
| 1362 | } |
| 1363 | } |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
| 1367 | int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask, |
| 1368 | int mb_bitmask_size, const AVFrame *reference) |
| 1369 | { |
| 1370 | int len, nb_components, i, h, v, predictor, point_transform; |
| 1371 | int index, id, ret; |
| 1372 | const int block_size = s->lossless ? 1 : 8; |
| 1373 | int ilv, prev_shift; |
| 1374 | |
| 1375 | if (!s->got_picture) { |
| 1376 | av_log(s->avctx, AV_LOG_WARNING, |
| 1377 | "Can not process SOS before SOF, skipping\n"); |
| 1378 | return -1; |
| 1379 | } |
| 1380 | |
| 1381 | av_assert0(s->picture_ptr->data[0]); |
| 1382 | /* XXX: verify len field validity */ |
| 1383 | len = get_bits(&s->gb, 16); |
| 1384 | nb_components = get_bits(&s->gb, 8); |
| 1385 | if (nb_components == 0 || nb_components > MAX_COMPONENTS) { |
| 1386 | av_log(s->avctx, AV_LOG_ERROR, |
| 1387 | "decode_sos: nb_components (%d) unsupported\n", nb_components); |
| 1388 | return AVERROR_PATCHWELCOME; |
| 1389 | } |
| 1390 | if (len != 6 + 2 * nb_components) { |
| 1391 | av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len); |
| 1392 | return AVERROR_INVALIDDATA; |
| 1393 | } |
| 1394 | for (i = 0; i < nb_components; i++) { |
| 1395 | id = get_bits(&s->gb, 8) - 1; |
| 1396 | av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id); |
| 1397 | /* find component index */ |
| 1398 | for (index = 0; index < s->nb_components; index++) |
| 1399 | if (id == s->component_id[index]) |
| 1400 | break; |
| 1401 | if (index == s->nb_components) { |
| 1402 | av_log(s->avctx, AV_LOG_ERROR, |
| 1403 | "decode_sos: index(%d) out of components\n", index); |
| 1404 | return AVERROR_INVALIDDATA; |
| 1405 | } |
| 1406 | /* Metasoft MJPEG codec has Cb and Cr swapped */ |
| 1407 | if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J') |
| 1408 | && nb_components == 3 && s->nb_components == 3 && i) |
| 1409 | index = 3 - i; |
| 1410 | |
| 1411 | s->quant_sindex[i] = s->quant_index[index]; |
| 1412 | s->nb_blocks[i] = s->h_count[index] * s->v_count[index]; |
| 1413 | s->h_scount[i] = s->h_count[index]; |
| 1414 | s->v_scount[i] = s->v_count[index]; |
| 1415 | |
| 1416 | if(nb_components == 3 && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P) |
| 1417 | index = (i+2)%3; |
| 1418 | if(nb_components == 1 && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P) |
| 1419 | index = (index+2)%3; |
| 1420 | |
| 1421 | s->comp_index[i] = index; |
| 1422 | |
| 1423 | s->dc_index[i] = get_bits(&s->gb, 4); |
| 1424 | s->ac_index[i] = get_bits(&s->gb, 4); |
| 1425 | |
| 1426 | if (s->dc_index[i] < 0 || s->ac_index[i] < 0 || |
| 1427 | s->dc_index[i] >= 4 || s->ac_index[i] >= 4) |
| 1428 | goto out_of_range; |
| 1429 | if (!s->vlcs[0][s->dc_index[i]].table || !(s->progressive ? s->vlcs[2][s->ac_index[0]].table : s->vlcs[1][s->ac_index[i]].table)) |
| 1430 | goto out_of_range; |
| 1431 | } |
| 1432 | |
| 1433 | predictor = get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */ |
| 1434 | ilv = get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */ |
| 1435 | if(s->avctx->codec_tag != AV_RL32("CJPG")){ |
| 1436 | prev_shift = get_bits(&s->gb, 4); /* Ah */ |
| 1437 | point_transform = get_bits(&s->gb, 4); /* Al */ |
| 1438 | }else |
| 1439 | prev_shift = point_transform = 0; |
| 1440 | |
| 1441 | if (nb_components > 1) { |
| 1442 | /* interleaved stream */ |
| 1443 | s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size); |
| 1444 | s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size); |
| 1445 | } else if (!s->ls) { /* skip this for JPEG-LS */ |
| 1446 | h = s->h_max / s->h_scount[0]; |
| 1447 | v = s->v_max / s->v_scount[0]; |
| 1448 | s->mb_width = (s->width + h * block_size - 1) / (h * block_size); |
| 1449 | s->mb_height = (s->height + v * block_size - 1) / (v * block_size); |
| 1450 | s->nb_blocks[0] = 1; |
| 1451 | s->h_scount[0] = 1; |
| 1452 | s->v_scount[0] = 1; |
| 1453 | } |
| 1454 | |
| 1455 | if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
| 1456 | av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d skip:%d %s comp:%d\n", |
| 1457 | s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "", |
| 1458 | predictor, point_transform, ilv, s->bits, s->mjpb_skiptosod, |
| 1459 | s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""), nb_components); |
| 1460 | |
| 1461 | |
| 1462 | /* mjpeg-b can have padding bytes between sos and image data, skip them */ |
| 1463 | for (i = s->mjpb_skiptosod; i > 0; i--) |
| 1464 | skip_bits(&s->gb, 8); |
| 1465 | |
| 1466 | next_field: |
| 1467 | for (i = 0; i < nb_components; i++) |
| 1468 | s->last_dc[i] = (4 << s->bits); |
| 1469 | |
| 1470 | if (s->lossless) { |
| 1471 | av_assert0(s->picture_ptr == s->picture); |
| 1472 | if (CONFIG_JPEGLS_DECODER && s->ls) { |
| 1473 | // for () { |
| 1474 | // reset_ls_coding_parameters(s, 0); |
| 1475 | |
| 1476 | if ((ret = ff_jpegls_decode_picture(s, predictor, |
| 1477 | point_transform, ilv)) < 0) |
| 1478 | return ret; |
| 1479 | } else { |
| 1480 | if (s->rgb) { |
| 1481 | if ((ret = ljpeg_decode_rgb_scan(s, nb_components, predictor, point_transform)) < 0) |
| 1482 | return ret; |
| 1483 | } else { |
| 1484 | if ((ret = ljpeg_decode_yuv_scan(s, predictor, |
| 1485 | point_transform, |
| 1486 | nb_components)) < 0) |
| 1487 | return ret; |
| 1488 | } |
| 1489 | } |
| 1490 | } else { |
| 1491 | if (s->progressive && predictor) { |
| 1492 | av_assert0(s->picture_ptr == s->picture); |
| 1493 | if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor, |
| 1494 | ilv, prev_shift, |
| 1495 | point_transform)) < 0) |
| 1496 | return ret; |
| 1497 | } else { |
| 1498 | if ((ret = mjpeg_decode_scan(s, nb_components, |
| 1499 | prev_shift, point_transform, |
| 1500 | mb_bitmask, mb_bitmask_size, reference)) < 0) |
| 1501 | return ret; |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | if (s->interlaced && |
| 1506 | get_bits_left(&s->gb) > 32 && |
| 1507 | show_bits(&s->gb, 8) == 0xFF) { |
| 1508 | GetBitContext bak = s->gb; |
| 1509 | align_get_bits(&bak); |
| 1510 | if (show_bits(&bak, 16) == 0xFFD1) { |
| 1511 | av_log(s->avctx, AV_LOG_DEBUG, "AVRn interlaced picture marker found\n"); |
| 1512 | s->gb = bak; |
| 1513 | skip_bits(&s->gb, 16); |
| 1514 | s->bottom_field ^= 1; |
| 1515 | |
| 1516 | goto next_field; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | emms_c(); |
| 1521 | return 0; |
| 1522 | out_of_range: |
| 1523 | av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n"); |
| 1524 | return AVERROR_INVALIDDATA; |
| 1525 | } |
| 1526 | |
| 1527 | static int mjpeg_decode_dri(MJpegDecodeContext *s) |
| 1528 | { |
| 1529 | if (get_bits(&s->gb, 16) != 4) |
| 1530 | return AVERROR_INVALIDDATA; |
| 1531 | s->restart_interval = get_bits(&s->gb, 16); |
| 1532 | s->restart_count = 0; |
| 1533 | av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", |
| 1534 | s->restart_interval); |
| 1535 | |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | static int mjpeg_decode_app(MJpegDecodeContext *s) |
| 1540 | { |
| 1541 | int len, id, i; |
| 1542 | |
| 1543 | len = get_bits(&s->gb, 16); |
| 1544 | if (len < 6) |
| 1545 | return AVERROR_INVALIDDATA; |
| 1546 | if (8 * len > get_bits_left(&s->gb)) |
| 1547 | return AVERROR_INVALIDDATA; |
| 1548 | |
| 1549 | id = get_bits_long(&s->gb, 32); |
| 1550 | len -= 6; |
| 1551 | |
| 1552 | if (s->avctx->debug & FF_DEBUG_STARTCODE) { |
| 1553 | char id_str[32]; |
| 1554 | av_get_codec_tag_string(id_str, sizeof(id_str), av_bswap32(id)); |
| 1555 | av_log(s->avctx, AV_LOG_DEBUG, "APPx (%s / %8X) len=%d\n", id_str, id, len); |
| 1556 | } |
| 1557 | |
| 1558 | /* Buggy AVID, it puts EOI only at every 10th frame. */ |
| 1559 | /* Also, this fourcc is used by non-avid files too, it holds some |
| 1560 | information, but it's always present in AVID-created files. */ |
| 1561 | if (id == AV_RB32("AVI1")) { |
| 1562 | /* structure: |
| 1563 | 4bytes AVI1 |
| 1564 | 1bytes polarity |
| 1565 | 1bytes always zero |
| 1566 | 4bytes field_size |
| 1567 | 4bytes field_size_less_padding |
| 1568 | */ |
| 1569 | s->buggy_avid = 1; |
| 1570 | i = get_bits(&s->gb, 8); len--; |
| 1571 | av_log(s->avctx, AV_LOG_DEBUG, "polarity %d\n", i); |
| 1572 | #if 0 |
| 1573 | skip_bits(&s->gb, 8); |
| 1574 | skip_bits(&s->gb, 32); |
| 1575 | skip_bits(&s->gb, 32); |
| 1576 | len -= 10; |
| 1577 | #endif |
| 1578 | goto out; |
| 1579 | } |
| 1580 | |
| 1581 | // len -= 2; |
| 1582 | |
| 1583 | if (id == AV_RB32("JFIF")) { |
| 1584 | int t_w, t_h, v1, v2; |
| 1585 | skip_bits(&s->gb, 8); /* the trailing zero-byte */ |
| 1586 | v1 = get_bits(&s->gb, 8); |
| 1587 | v2 = get_bits(&s->gb, 8); |
| 1588 | skip_bits(&s->gb, 8); |
| 1589 | |
| 1590 | s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16); |
| 1591 | s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16); |
| 1592 | ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio); |
| 1593 | |
| 1594 | if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
| 1595 | av_log(s->avctx, AV_LOG_INFO, |
| 1596 | "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n", |
| 1597 | v1, v2, |
| 1598 | s->avctx->sample_aspect_ratio.num, |
| 1599 | s->avctx->sample_aspect_ratio.den); |
| 1600 | |
| 1601 | t_w = get_bits(&s->gb, 8); |
| 1602 | t_h = get_bits(&s->gb, 8); |
| 1603 | if (t_w && t_h) { |
| 1604 | /* skip thumbnail */ |
| 1605 | if (len -10 - (t_w * t_h * 3) > 0) |
| 1606 | len -= t_w * t_h * 3; |
| 1607 | } |
| 1608 | len -= 10; |
| 1609 | goto out; |
| 1610 | } |
| 1611 | |
| 1612 | if (id == AV_RB32("Adob") && (get_bits(&s->gb, 8) == 'e')) { |
| 1613 | skip_bits(&s->gb, 16); /* version */ |
| 1614 | skip_bits(&s->gb, 16); /* flags0 */ |
| 1615 | skip_bits(&s->gb, 16); /* flags1 */ |
| 1616 | s->adobe_transform = get_bits(&s->gb, 8); |
| 1617 | if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
| 1618 | av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found, transform=%d\n", s->adobe_transform); |
| 1619 | len -= 7; |
| 1620 | goto out; |
| 1621 | } |
| 1622 | |
| 1623 | if (id == AV_RB32("LJIF")) { |
| 1624 | int rgb = s->rgb; |
| 1625 | int pegasus_rct = s->pegasus_rct; |
| 1626 | if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
| 1627 | av_log(s->avctx, AV_LOG_INFO, |
| 1628 | "Pegasus lossless jpeg header found\n"); |
| 1629 | skip_bits(&s->gb, 16); /* version ? */ |
| 1630 | skip_bits(&s->gb, 16); /* unknown always 0? */ |
| 1631 | skip_bits(&s->gb, 16); /* unknown always 0? */ |
| 1632 | skip_bits(&s->gb, 16); /* unknown always 0? */ |
| 1633 | switch (i=get_bits(&s->gb, 8)) { |
| 1634 | case 1: |
| 1635 | rgb = 1; |
| 1636 | pegasus_rct = 0; |
| 1637 | break; |
| 1638 | case 2: |
| 1639 | rgb = 1; |
| 1640 | pegasus_rct = 1; |
| 1641 | break; |
| 1642 | default: |
| 1643 | av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace %d\n", i); |
| 1644 | } |
| 1645 | |
| 1646 | len -= 9; |
| 1647 | if (s->got_picture) |
| 1648 | if (rgb != s->rgb || pegasus_rct != s->pegasus_rct) { |
| 1649 | av_log(s->avctx, AV_LOG_WARNING, "Mismatching LJIF tag\n"); |
| 1650 | goto out; |
| 1651 | } |
| 1652 | |
| 1653 | s->rgb = rgb; |
| 1654 | s->pegasus_rct = pegasus_rct; |
| 1655 | |
| 1656 | goto out; |
| 1657 | } |
| 1658 | if (id == AV_RL32("colr") && len > 0) { |
| 1659 | s->colr = get_bits(&s->gb, 8); |
| 1660 | if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
| 1661 | av_log(s->avctx, AV_LOG_INFO, "COLR %d\n", s->colr); |
| 1662 | len --; |
| 1663 | goto out; |
| 1664 | } |
| 1665 | if (id == AV_RL32("xfrm") && len > 0) { |
| 1666 | s->xfrm = get_bits(&s->gb, 8); |
| 1667 | if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
| 1668 | av_log(s->avctx, AV_LOG_INFO, "XFRM %d\n", s->xfrm); |
| 1669 | len --; |
| 1670 | goto out; |
| 1671 | } |
| 1672 | |
| 1673 | /* JPS extension by VRex */ |
| 1674 | if (s->start_code == APP3 && id == AV_RB32("_JPS") && len >= 10) { |
| 1675 | int flags, layout, type; |
| 1676 | if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
| 1677 | av_log(s->avctx, AV_LOG_INFO, "_JPSJPS_\n"); |
| 1678 | |
| 1679 | skip_bits(&s->gb, 32); len -= 4; /* JPS_ */ |
| 1680 | skip_bits(&s->gb, 16); len -= 2; /* block length */ |
| 1681 | skip_bits(&s->gb, 8); /* reserved */ |
| 1682 | flags = get_bits(&s->gb, 8); |
| 1683 | layout = get_bits(&s->gb, 8); |
| 1684 | type = get_bits(&s->gb, 8); |
| 1685 | len -= 4; |
| 1686 | |
| 1687 | s->stereo3d = av_stereo3d_alloc(); |
| 1688 | if (!s->stereo3d) { |
| 1689 | goto out; |
| 1690 | } |
| 1691 | if (type == 0) { |
| 1692 | s->stereo3d->type = AV_STEREO3D_2D; |
| 1693 | } else if (type == 1) { |
| 1694 | switch (layout) { |
| 1695 | case 0x01: |
| 1696 | s->stereo3d->type = AV_STEREO3D_LINES; |
| 1697 | break; |
| 1698 | case 0x02: |
| 1699 | s->stereo3d->type = AV_STEREO3D_SIDEBYSIDE; |
| 1700 | break; |
| 1701 | case 0x03: |
| 1702 | s->stereo3d->type = AV_STEREO3D_TOPBOTTOM; |
| 1703 | break; |
| 1704 | } |
| 1705 | if (!(flags & 0x04)) { |
| 1706 | s->stereo3d->flags = AV_STEREO3D_FLAG_INVERT; |
| 1707 | } |
| 1708 | } |
| 1709 | goto out; |
| 1710 | } |
| 1711 | |
| 1712 | /* EXIF metadata */ |
| 1713 | if (s->start_code == APP1 && id == AV_RB32("Exif") && len >= 2) { |
| 1714 | GetByteContext gbytes; |
| 1715 | int ret, le, ifd_offset, bytes_read; |
| 1716 | const uint8_t *aligned; |
| 1717 | |
| 1718 | skip_bits(&s->gb, 16); // skip padding |
| 1719 | len -= 2; |
| 1720 | |
| 1721 | // init byte wise reading |
| 1722 | aligned = align_get_bits(&s->gb); |
| 1723 | bytestream2_init(&gbytes, aligned, len); |
| 1724 | |
| 1725 | // read TIFF header |
| 1726 | ret = ff_tdecode_header(&gbytes, &le, &ifd_offset); |
| 1727 | if (ret) { |
| 1728 | av_log(s->avctx, AV_LOG_ERROR, "mjpeg: invalid TIFF header in EXIF data\n"); |
| 1729 | } else { |
| 1730 | bytestream2_seek(&gbytes, ifd_offset, SEEK_SET); |
| 1731 | |
| 1732 | // read 0th IFD and store the metadata |
| 1733 | // (return values > 0 indicate the presence of subimage metadata) |
| 1734 | ret = avpriv_exif_decode_ifd(s->avctx, &gbytes, le, 0, &s->exif_metadata); |
| 1735 | if (ret < 0) { |
| 1736 | av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error decoding EXIF data\n"); |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | bytes_read = bytestream2_tell(&gbytes); |
| 1741 | skip_bits(&s->gb, bytes_read << 3); |
| 1742 | len -= bytes_read; |
| 1743 | |
| 1744 | goto out; |
| 1745 | } |
| 1746 | |
| 1747 | /* Apple MJPEG-A */ |
| 1748 | if ((s->start_code == APP1) && (len > (0x28 - 8))) { |
| 1749 | id = get_bits_long(&s->gb, 32); |
| 1750 | len -= 4; |
| 1751 | /* Apple MJPEG-A */ |
| 1752 | if (id == AV_RB32("mjpg")) { |
| 1753 | #if 0 |
| 1754 | skip_bits(&s->gb, 32); /* field size */ |
| 1755 | skip_bits(&s->gb, 32); /* pad field size */ |
| 1756 | skip_bits(&s->gb, 32); /* next off */ |
| 1757 | skip_bits(&s->gb, 32); /* quant off */ |
| 1758 | skip_bits(&s->gb, 32); /* huff off */ |
| 1759 | skip_bits(&s->gb, 32); /* image off */ |
| 1760 | skip_bits(&s->gb, 32); /* scan off */ |
| 1761 | skip_bits(&s->gb, 32); /* data off */ |
| 1762 | #endif |
| 1763 | if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
| 1764 | av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n"); |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | out: |
| 1769 | /* slow but needed for extreme adobe jpegs */ |
| 1770 | if (len < 0) |
| 1771 | av_log(s->avctx, AV_LOG_ERROR, |
| 1772 | "mjpeg: error, decode_app parser read over the end\n"); |
| 1773 | while (--len > 0) |
| 1774 | skip_bits(&s->gb, 8); |
| 1775 | |
| 1776 | return 0; |
| 1777 | } |
| 1778 | |
| 1779 | static int mjpeg_decode_com(MJpegDecodeContext *s) |
| 1780 | { |
| 1781 | int len = get_bits(&s->gb, 16); |
| 1782 | if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) { |
| 1783 | char *cbuf = av_malloc(len - 1); |
| 1784 | if (cbuf) { |
| 1785 | int i; |
| 1786 | for (i = 0; i < len - 2; i++) |
| 1787 | cbuf[i] = get_bits(&s->gb, 8); |
| 1788 | if (i > 0 && cbuf[i - 1] == '\n') |
| 1789 | cbuf[i - 1] = 0; |
| 1790 | else |
| 1791 | cbuf[i] = 0; |
| 1792 | |
| 1793 | if (s->avctx->debug & FF_DEBUG_PICT_INFO) |
| 1794 | av_log(s->avctx, AV_LOG_INFO, "comment: '%s'\n", cbuf); |
| 1795 | |
| 1796 | /* buggy avid, it puts EOI only at every 10th frame */ |
| 1797 | if (!strncmp(cbuf, "AVID", 4)) { |
| 1798 | parse_avid(s, cbuf, len); |
| 1799 | } else if (!strcmp(cbuf, "CS=ITU601")) |
| 1800 | s->cs_itu601 = 1; |
| 1801 | else if ((!strncmp(cbuf, "Intel(R) JPEG Library, version 1", 32) && s->avctx->codec_tag) || |
| 1802 | (!strncmp(cbuf, "Metasoft MJPEG Codec", 20))) |
| 1803 | s->flipped = 1; |
| 1804 | |
| 1805 | av_free(cbuf); |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | return 0; |
| 1810 | } |
| 1811 | |
| 1812 | /* return the 8 bit start code value and update the search |
| 1813 | state. Return -1 if no start code found */ |
| 1814 | static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end) |
| 1815 | { |
| 1816 | const uint8_t *buf_ptr; |
| 1817 | unsigned int v, v2; |
| 1818 | int val; |
| 1819 | int skipped = 0; |
| 1820 | |
| 1821 | buf_ptr = *pbuf_ptr; |
| 1822 | while (buf_end - buf_ptr > 1) { |
| 1823 | v = *buf_ptr++; |
| 1824 | v2 = *buf_ptr; |
| 1825 | if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) { |
| 1826 | val = *buf_ptr++; |
| 1827 | goto found; |
| 1828 | } |
| 1829 | skipped++; |
| 1830 | } |
| 1831 | buf_ptr = buf_end; |
| 1832 | val = -1; |
| 1833 | found: |
| 1834 | av_dlog(NULL, "find_marker skipped %d bytes\n", skipped); |
| 1835 | *pbuf_ptr = buf_ptr; |
| 1836 | return val; |
| 1837 | } |
| 1838 | |
| 1839 | int ff_mjpeg_find_marker(MJpegDecodeContext *s, |
| 1840 | const uint8_t **buf_ptr, const uint8_t *buf_end, |
| 1841 | const uint8_t **unescaped_buf_ptr, |
| 1842 | int *unescaped_buf_size) |
| 1843 | { |
| 1844 | int start_code; |
| 1845 | start_code = find_marker(buf_ptr, buf_end); |
| 1846 | |
| 1847 | av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr); |
| 1848 | if (!s->buffer) |
| 1849 | return AVERROR(ENOMEM); |
| 1850 | |
| 1851 | /* unescape buffer of SOS, use special treatment for JPEG-LS */ |
| 1852 | if (start_code == SOS && !s->ls) { |
| 1853 | const uint8_t *src = *buf_ptr; |
| 1854 | uint8_t *dst = s->buffer; |
| 1855 | |
| 1856 | while (src < buf_end) { |
| 1857 | uint8_t x = *(src++); |
| 1858 | |
| 1859 | *(dst++) = x; |
| 1860 | if (s->avctx->codec_id != AV_CODEC_ID_THP) { |
| 1861 | if (x == 0xff) { |
| 1862 | while (src < buf_end && x == 0xff) |
| 1863 | x = *(src++); |
| 1864 | |
| 1865 | if (x >= 0xd0 && x <= 0xd7) |
| 1866 | *(dst++) = x; |
| 1867 | else if (x) |
| 1868 | break; |
| 1869 | } |
| 1870 | } |
| 1871 | } |
| 1872 | *unescaped_buf_ptr = s->buffer; |
| 1873 | *unescaped_buf_size = dst - s->buffer; |
| 1874 | memset(s->buffer + *unescaped_buf_size, 0, |
| 1875 | FF_INPUT_BUFFER_PADDING_SIZE); |
| 1876 | |
| 1877 | av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %"PTRDIFF_SPECIFIER" bytes\n", |
| 1878 | (buf_end - *buf_ptr) - (dst - s->buffer)); |
| 1879 | } else if (start_code == SOS && s->ls) { |
| 1880 | const uint8_t *src = *buf_ptr; |
| 1881 | uint8_t *dst = s->buffer; |
| 1882 | int bit_count = 0; |
| 1883 | int t = 0, b = 0; |
| 1884 | PutBitContext pb; |
| 1885 | |
| 1886 | /* find marker */ |
| 1887 | while (src + t < buf_end) { |
| 1888 | uint8_t x = src[t++]; |
| 1889 | if (x == 0xff) { |
| 1890 | while ((src + t < buf_end) && x == 0xff) |
| 1891 | x = src[t++]; |
| 1892 | if (x & 0x80) { |
| 1893 | t -= FFMIN(2, t); |
| 1894 | break; |
| 1895 | } |
| 1896 | } |
| 1897 | } |
| 1898 | bit_count = t * 8; |
| 1899 | init_put_bits(&pb, dst, t); |
| 1900 | |
| 1901 | /* unescape bitstream */ |
| 1902 | while (b < t) { |
| 1903 | uint8_t x = src[b++]; |
| 1904 | put_bits(&pb, 8, x); |
| 1905 | if (x == 0xFF) { |
| 1906 | x = src[b++]; |
| 1907 | put_bits(&pb, 7, x); |
| 1908 | bit_count--; |
| 1909 | } |
| 1910 | } |
| 1911 | flush_put_bits(&pb); |
| 1912 | |
| 1913 | *unescaped_buf_ptr = dst; |
| 1914 | *unescaped_buf_size = (bit_count + 7) >> 3; |
| 1915 | memset(s->buffer + *unescaped_buf_size, 0, |
| 1916 | FF_INPUT_BUFFER_PADDING_SIZE); |
| 1917 | } else { |
| 1918 | *unescaped_buf_ptr = *buf_ptr; |
| 1919 | *unescaped_buf_size = buf_end - *buf_ptr; |
| 1920 | } |
| 1921 | |
| 1922 | return start_code; |
| 1923 | } |
| 1924 | |
| 1925 | int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, |
| 1926 | AVPacket *avpkt) |
| 1927 | { |
| 1928 | AVFrame *frame = data; |
| 1929 | const uint8_t *buf = avpkt->data; |
| 1930 | int buf_size = avpkt->size; |
| 1931 | MJpegDecodeContext *s = avctx->priv_data; |
| 1932 | const uint8_t *buf_end, *buf_ptr; |
| 1933 | const uint8_t *unescaped_buf_ptr; |
| 1934 | int hshift, vshift; |
| 1935 | int unescaped_buf_size; |
| 1936 | int start_code; |
| 1937 | int i, index; |
| 1938 | int ret = 0; |
| 1939 | int is16bit; |
| 1940 | |
| 1941 | av_dict_free(&s->exif_metadata); |
| 1942 | av_freep(&s->stereo3d); |
| 1943 | s->adobe_transform = -1; |
| 1944 | |
| 1945 | buf_ptr = buf; |
| 1946 | buf_end = buf + buf_size; |
| 1947 | while (buf_ptr < buf_end) { |
| 1948 | /* find start next marker */ |
| 1949 | start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end, |
| 1950 | &unescaped_buf_ptr, |
| 1951 | &unescaped_buf_size); |
| 1952 | /* EOF */ |
| 1953 | if (start_code < 0) { |
| 1954 | break; |
| 1955 | } else if (unescaped_buf_size > INT_MAX / 8) { |
| 1956 | av_log(avctx, AV_LOG_ERROR, |
| 1957 | "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n", |
| 1958 | start_code, unescaped_buf_size, buf_size); |
| 1959 | return AVERROR_INVALIDDATA; |
| 1960 | } |
| 1961 | av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%"PTRDIFF_SPECIFIER"\n", |
| 1962 | start_code, buf_end - buf_ptr); |
| 1963 | |
| 1964 | ret = init_get_bits8(&s->gb, unescaped_buf_ptr, unescaped_buf_size); |
| 1965 | |
| 1966 | if (ret < 0) { |
| 1967 | av_log(avctx, AV_LOG_ERROR, "invalid buffer\n"); |
| 1968 | goto fail; |
| 1969 | } |
| 1970 | |
| 1971 | s->start_code = start_code; |
| 1972 | if (s->avctx->debug & FF_DEBUG_STARTCODE) |
| 1973 | av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code); |
| 1974 | |
| 1975 | /* process markers */ |
| 1976 | if (start_code >= 0xd0 && start_code <= 0xd7) |
| 1977 | av_log(avctx, AV_LOG_DEBUG, |
| 1978 | "restart marker: %d\n", start_code & 0x0f); |
| 1979 | /* APP fields */ |
| 1980 | else if (start_code >= APP0 && start_code <= APP15) |
| 1981 | mjpeg_decode_app(s); |
| 1982 | /* Comment */ |
| 1983 | else if (start_code == COM) |
| 1984 | mjpeg_decode_com(s); |
| 1985 | |
| 1986 | ret = -1; |
| 1987 | |
| 1988 | if (!CONFIG_JPEGLS_DECODER && |
| 1989 | (start_code == SOF48 || start_code == LSE)) { |
| 1990 | av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n"); |
| 1991 | return AVERROR(ENOSYS); |
| 1992 | } |
| 1993 | |
| 1994 | switch (start_code) { |
| 1995 | case SOI: |
| 1996 | s->restart_interval = 0; |
| 1997 | s->restart_count = 0; |
| 1998 | /* nothing to do on SOI */ |
| 1999 | break; |
| 2000 | case DQT: |
| 2001 | ff_mjpeg_decode_dqt(s); |
| 2002 | break; |
| 2003 | case DHT: |
| 2004 | if ((ret = ff_mjpeg_decode_dht(s)) < 0) { |
| 2005 | av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n"); |
| 2006 | goto fail; |
| 2007 | } |
| 2008 | break; |
| 2009 | case SOF0: |
| 2010 | case SOF1: |
| 2011 | s->lossless = 0; |
| 2012 | s->ls = 0; |
| 2013 | s->progressive = 0; |
| 2014 | if ((ret = ff_mjpeg_decode_sof(s)) < 0) |
| 2015 | goto fail; |
| 2016 | break; |
| 2017 | case SOF2: |
| 2018 | s->lossless = 0; |
| 2019 | s->ls = 0; |
| 2020 | s->progressive = 1; |
| 2021 | if ((ret = ff_mjpeg_decode_sof(s)) < 0) |
| 2022 | goto fail; |
| 2023 | break; |
| 2024 | case SOF3: |
| 2025 | s->lossless = 1; |
| 2026 | s->ls = 0; |
| 2027 | s->progressive = 0; |
| 2028 | if ((ret = ff_mjpeg_decode_sof(s)) < 0) |
| 2029 | goto fail; |
| 2030 | break; |
| 2031 | case SOF48: |
| 2032 | s->lossless = 1; |
| 2033 | s->ls = 1; |
| 2034 | s->progressive = 0; |
| 2035 | if ((ret = ff_mjpeg_decode_sof(s)) < 0) |
| 2036 | goto fail; |
| 2037 | break; |
| 2038 | case LSE: |
| 2039 | if (!CONFIG_JPEGLS_DECODER || |
| 2040 | (ret = ff_jpegls_decode_lse(s)) < 0) |
| 2041 | goto fail; |
| 2042 | break; |
| 2043 | case EOI: |
| 2044 | eoi_parser: |
| 2045 | s->cur_scan = 0; |
| 2046 | if (!s->got_picture) { |
| 2047 | av_log(avctx, AV_LOG_WARNING, |
| 2048 | "Found EOI before any SOF, ignoring\n"); |
| 2049 | break; |
| 2050 | } |
| 2051 | if (s->interlaced) { |
| 2052 | s->bottom_field ^= 1; |
| 2053 | /* if not bottom field, do not output image yet */ |
| 2054 | if (s->bottom_field == !s->interlace_polarity) |
| 2055 | break; |
| 2056 | } |
| 2057 | if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0) |
| 2058 | return ret; |
| 2059 | *got_frame = 1; |
| 2060 | s->got_picture = 0; |
| 2061 | |
| 2062 | if (!s->lossless) { |
| 2063 | int qp = FFMAX3(s->qscale[0], |
| 2064 | s->qscale[1], |
| 2065 | s->qscale[2]); |
| 2066 | int qpw = (s->width + 15) / 16; |
| 2067 | AVBufferRef *qp_table_buf = av_buffer_alloc(qpw); |
| 2068 | if (qp_table_buf) { |
| 2069 | memset(qp_table_buf->data, qp, qpw); |
| 2070 | av_frame_set_qp_table(data, qp_table_buf, 0, FF_QSCALE_TYPE_MPEG1); |
| 2071 | } |
| 2072 | |
| 2073 | if(avctx->debug & FF_DEBUG_QP) |
| 2074 | av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", qp); |
| 2075 | } |
| 2076 | |
| 2077 | goto the_end; |
| 2078 | case SOS: |
| 2079 | s->cur_scan++; |
| 2080 | if ((ret = ff_mjpeg_decode_sos(s, NULL, 0, NULL)) < 0 && |
| 2081 | (avctx->err_recognition & AV_EF_EXPLODE)) |
| 2082 | goto fail; |
| 2083 | break; |
| 2084 | case DRI: |
| 2085 | mjpeg_decode_dri(s); |
| 2086 | break; |
| 2087 | case SOF5: |
| 2088 | case SOF6: |
| 2089 | case SOF7: |
| 2090 | case SOF9: |
| 2091 | case SOF10: |
| 2092 | case SOF11: |
| 2093 | case SOF13: |
| 2094 | case SOF14: |
| 2095 | case SOF15: |
| 2096 | case JPG: |
| 2097 | av_log(avctx, AV_LOG_ERROR, |
| 2098 | "mjpeg: unsupported coding type (%x)\n", start_code); |
| 2099 | break; |
| 2100 | } |
| 2101 | |
| 2102 | /* eof process start code */ |
| 2103 | buf_ptr += (get_bits_count(&s->gb) + 7) / 8; |
| 2104 | av_log(avctx, AV_LOG_DEBUG, |
| 2105 | "marker parser used %d bytes (%d bits)\n", |
| 2106 | (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb)); |
| 2107 | } |
| 2108 | if (s->got_picture && s->cur_scan) { |
| 2109 | av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n"); |
| 2110 | goto eoi_parser; |
| 2111 | } |
| 2112 | av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n"); |
| 2113 | return AVERROR_INVALIDDATA; |
| 2114 | fail: |
| 2115 | s->got_picture = 0; |
| 2116 | return ret; |
| 2117 | the_end: |
| 2118 | |
| 2119 | is16bit = av_pix_fmt_desc_get(s->avctx->pix_fmt)->comp[0].step_minus1; |
| 2120 | |
| 2121 | if (s->upscale_h) { |
| 2122 | int p; |
| 2123 | av_assert0(avctx->pix_fmt == AV_PIX_FMT_YUVJ444P || |
| 2124 | avctx->pix_fmt == AV_PIX_FMT_YUV444P || |
| 2125 | avctx->pix_fmt == AV_PIX_FMT_YUVJ440P || |
| 2126 | avctx->pix_fmt == AV_PIX_FMT_YUV440P || |
| 2127 | avctx->pix_fmt == AV_PIX_FMT_YUVA444P || |
| 2128 | avctx->pix_fmt == AV_PIX_FMT_YUVJ420P || |
| 2129 | avctx->pix_fmt == AV_PIX_FMT_YUV420P || |
| 2130 | avctx->pix_fmt == AV_PIX_FMT_YUV420P16|| |
| 2131 | avctx->pix_fmt == AV_PIX_FMT_YUVA420P || |
| 2132 | avctx->pix_fmt == AV_PIX_FMT_YUVA420P16|| |
| 2133 | avctx->pix_fmt == AV_PIX_FMT_GBRP || |
| 2134 | avctx->pix_fmt == AV_PIX_FMT_GBRAP |
| 2135 | ); |
| 2136 | avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift); |
| 2137 | for (p = 0; p<4; p++) { |
| 2138 | uint8_t *line = s->picture_ptr->data[p]; |
| 2139 | int w = s->width; |
| 2140 | int h = s->height; |
| 2141 | if (!(s->upscale_h & (1<<p))) |
| 2142 | continue; |
| 2143 | if (p==1 || p==2) { |
| 2144 | w = FF_CEIL_RSHIFT(w, hshift); |
| 2145 | h = FF_CEIL_RSHIFT(h, vshift); |
| 2146 | } |
| 2147 | if (s->upscale_v & (1<<p)) |
| 2148 | h = (h+1)>>1; |
| 2149 | av_assert0(w > 0); |
| 2150 | for (i = 0; i < h; i++) { |
| 2151 | if (is16bit) ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 2]; |
| 2152 | else line[w - 1] = line[(w - 1) / 2]; |
| 2153 | for (index = w - 2; index > 0; index--) { |
| 2154 | if (is16bit) |
| 2155 | ((uint16_t*)line)[index] = (((uint16_t*)line)[index / 2] + ((uint16_t*)line)[(index + 1) / 2]) >> 1; |
| 2156 | else |
| 2157 | line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1; |
| 2158 | } |
| 2159 | line += s->linesize[p]; |
| 2160 | } |
| 2161 | } |
| 2162 | } |
| 2163 | if (s->upscale_v) { |
| 2164 | int p; |
| 2165 | av_assert0(avctx->pix_fmt == AV_PIX_FMT_YUVJ444P || |
| 2166 | avctx->pix_fmt == AV_PIX_FMT_YUV444P || |
| 2167 | avctx->pix_fmt == AV_PIX_FMT_YUVJ422P || |
| 2168 | avctx->pix_fmt == AV_PIX_FMT_YUV422P || |
| 2169 | avctx->pix_fmt == AV_PIX_FMT_YUVJ420P || |
| 2170 | avctx->pix_fmt == AV_PIX_FMT_YUV420P || |
| 2171 | avctx->pix_fmt == AV_PIX_FMT_YUV440P || |
| 2172 | avctx->pix_fmt == AV_PIX_FMT_YUVJ440P || |
| 2173 | avctx->pix_fmt == AV_PIX_FMT_YUVA444P || |
| 2174 | avctx->pix_fmt == AV_PIX_FMT_YUVA420P || |
| 2175 | avctx->pix_fmt == AV_PIX_FMT_YUVA420P16|| |
| 2176 | avctx->pix_fmt == AV_PIX_FMT_GBRP || |
| 2177 | avctx->pix_fmt == AV_PIX_FMT_GBRAP |
| 2178 | ); |
| 2179 | avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift); |
| 2180 | for (p = 0; p < 4; p++) { |
| 2181 | uint8_t *dst; |
| 2182 | int w = s->width; |
| 2183 | int h = s->height; |
| 2184 | if (!(s->upscale_v & (1<<p))) |
| 2185 | continue; |
| 2186 | if (p==1 || p==2) { |
| 2187 | w = FF_CEIL_RSHIFT(w, hshift); |
| 2188 | h = FF_CEIL_RSHIFT(h, vshift); |
| 2189 | } |
| 2190 | dst = &((uint8_t *)s->picture_ptr->data[p])[(h - 1) * s->linesize[p]]; |
| 2191 | for (i = h - 1; i; i--) { |
| 2192 | uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[p])[i / 2 * s->linesize[p]]; |
| 2193 | uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[p])[(i + 1) / 2 * s->linesize[p]]; |
| 2194 | if (src1 == src2 || i == h - 1) { |
| 2195 | memcpy(dst, src1, w); |
| 2196 | } else { |
| 2197 | for (index = 0; index < w; index++) |
| 2198 | dst[index] = (src1[index] + src2[index]) >> 1; |
| 2199 | } |
| 2200 | dst -= s->linesize[p]; |
| 2201 | } |
| 2202 | } |
| 2203 | } |
| 2204 | if (s->flipped) { |
| 2205 | int j; |
| 2206 | avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift); |
| 2207 | for (index=0; index<4; index++) { |
| 2208 | uint8_t *dst = s->picture_ptr->data[index]; |
| 2209 | int w = s->picture_ptr->width; |
| 2210 | int h = s->picture_ptr->height; |
| 2211 | if(index && index<3){ |
| 2212 | w = FF_CEIL_RSHIFT(w, hshift); |
| 2213 | h = FF_CEIL_RSHIFT(h, vshift); |
| 2214 | } |
| 2215 | if(dst){ |
| 2216 | uint8_t *dst2 = dst + s->picture_ptr->linesize[index]*(h-1); |
| 2217 | for (i=0; i<h/2; i++) { |
| 2218 | for (j=0; j<w; j++) |
| 2219 | FFSWAP(int, dst[j], dst2[j]); |
| 2220 | dst += s->picture_ptr->linesize[index]; |
| 2221 | dst2 -= s->picture_ptr->linesize[index]; |
| 2222 | } |
| 2223 | } |
| 2224 | } |
| 2225 | } |
| 2226 | if (s->adobe_transform == 0 && s->avctx->pix_fmt == AV_PIX_FMT_GBRAP) { |
| 2227 | int w = s->picture_ptr->width; |
| 2228 | int h = s->picture_ptr->height; |
| 2229 | for (i=0; i<h; i++) { |
| 2230 | int j; |
| 2231 | uint8_t *dst[4]; |
| 2232 | for (index=0; index<4; index++) { |
| 2233 | dst[index] = s->picture_ptr->data[index] |
| 2234 | + s->picture_ptr->linesize[index]*i; |
| 2235 | } |
| 2236 | for (j=0; j<w; j++) { |
| 2237 | int k = dst[3][j]; |
| 2238 | int r = dst[0][j] * k; |
| 2239 | int g = dst[1][j] * k; |
| 2240 | int b = dst[2][j] * k; |
| 2241 | dst[0][j] = g*257 >> 16; |
| 2242 | dst[1][j] = b*257 >> 16; |
| 2243 | dst[2][j] = r*257 >> 16; |
| 2244 | dst[3][j] = 255; |
| 2245 | } |
| 2246 | } |
| 2247 | } |
| 2248 | if (s->adobe_transform == 2 && s->avctx->pix_fmt == AV_PIX_FMT_YUVA444P) { |
| 2249 | int w = s->picture_ptr->width; |
| 2250 | int h = s->picture_ptr->height; |
| 2251 | for (i=0; i<h; i++) { |
| 2252 | int j; |
| 2253 | uint8_t *dst[4]; |
| 2254 | for (index=0; index<4; index++) { |
| 2255 | dst[index] = s->picture_ptr->data[index] |
| 2256 | + s->picture_ptr->linesize[index]*i; |
| 2257 | } |
| 2258 | for (j=0; j<w; j++) { |
| 2259 | int k = dst[3][j]; |
| 2260 | int r = (255 - dst[0][j]) * k; |
| 2261 | int g = (128 - dst[1][j]) * k; |
| 2262 | int b = (128 - dst[2][j]) * k; |
| 2263 | dst[0][j] = r*257 >> 16; |
| 2264 | dst[1][j] = (g*257 >> 16) + 128; |
| 2265 | dst[2][j] = (b*257 >> 16) + 128; |
| 2266 | dst[3][j] = 255; |
| 2267 | } |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | if (s->stereo3d) { |
| 2272 | AVStereo3D *stereo = av_stereo3d_create_side_data(data); |
| 2273 | if (stereo) { |
| 2274 | stereo->type = s->stereo3d->type; |
| 2275 | stereo->flags = s->stereo3d->flags; |
| 2276 | } |
| 2277 | av_freep(&s->stereo3d); |
| 2278 | } |
| 2279 | |
| 2280 | av_dict_copy(avpriv_frame_get_metadatap(data), s->exif_metadata, 0); |
| 2281 | av_dict_free(&s->exif_metadata); |
| 2282 | |
| 2283 | av_log(avctx, AV_LOG_DEBUG, "decode frame unused %"PTRDIFF_SPECIFIER" bytes\n", |
| 2284 | buf_end - buf_ptr); |
| 2285 | // return buf_end - buf_ptr; |
| 2286 | return buf_ptr - buf; |
| 2287 | } |
| 2288 | |
| 2289 | av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx) |
| 2290 | { |
| 2291 | MJpegDecodeContext *s = avctx->priv_data; |
| 2292 | int i, j; |
| 2293 | |
| 2294 | if (s->interlaced && s->bottom_field == !s->interlace_polarity && s->got_picture && !avctx->frame_number) { |
| 2295 | av_log(avctx, AV_LOG_INFO, "Single field\n"); |
| 2296 | } |
| 2297 | |
| 2298 | if (s->picture) { |
| 2299 | av_frame_free(&s->picture); |
| 2300 | s->picture_ptr = NULL; |
| 2301 | } else if (s->picture_ptr) |
| 2302 | av_frame_unref(s->picture_ptr); |
| 2303 | |
| 2304 | av_freep(&s->buffer); |
| 2305 | av_freep(&s->stereo3d); |
| 2306 | av_freep(&s->ljpeg_buffer); |
| 2307 | s->ljpeg_buffer_size = 0; |
| 2308 | |
| 2309 | for (i = 0; i < 3; i++) { |
| 2310 | for (j = 0; j < 4; j++) |
| 2311 | ff_free_vlc(&s->vlcs[i][j]); |
| 2312 | } |
| 2313 | for (i = 0; i < MAX_COMPONENTS; i++) { |
| 2314 | av_freep(&s->blocks[i]); |
| 2315 | av_freep(&s->last_nnz[i]); |
| 2316 | } |
| 2317 | av_dict_free(&s->exif_metadata); |
| 2318 | return 0; |
| 2319 | } |
| 2320 | |
| 2321 | static void decode_flush(AVCodecContext *avctx) |
| 2322 | { |
| 2323 | MJpegDecodeContext *s = avctx->priv_data; |
| 2324 | s->got_picture = 0; |
| 2325 | } |
| 2326 | |
| 2327 | #if CONFIG_MJPEG_DECODER |
| 2328 | #define OFFSET(x) offsetof(MJpegDecodeContext, x) |
| 2329 | #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM |
| 2330 | static const AVOption options[] = { |
| 2331 | { "extern_huff", "Use external huffman table.", |
| 2332 | OFFSET(extern_huff), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD }, |
| 2333 | { NULL }, |
| 2334 | }; |
| 2335 | |
| 2336 | static const AVClass mjpegdec_class = { |
| 2337 | .class_name = "MJPEG decoder", |
| 2338 | .item_name = av_default_item_name, |
| 2339 | .option = options, |
| 2340 | .version = LIBAVUTIL_VERSION_INT, |
| 2341 | }; |
| 2342 | |
| 2343 | AVCodec ff_mjpeg_decoder = { |
| 2344 | .name = "mjpeg", |
| 2345 | .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"), |
| 2346 | .type = AVMEDIA_TYPE_VIDEO, |
| 2347 | .id = AV_CODEC_ID_MJPEG, |
| 2348 | .priv_data_size = sizeof(MJpegDecodeContext), |
| 2349 | .init = ff_mjpeg_decode_init, |
| 2350 | .close = ff_mjpeg_decode_end, |
| 2351 | .decode = ff_mjpeg_decode_frame, |
| 2352 | .flush = decode_flush, |
| 2353 | .capabilities = CODEC_CAP_DR1, |
| 2354 | .max_lowres = 3, |
| 2355 | .priv_class = &mjpegdec_class, |
| 2356 | }; |
| 2357 | #endif |
| 2358 | #if CONFIG_THP_DECODER |
| 2359 | AVCodec ff_thp_decoder = { |
| 2360 | .name = "thp", |
| 2361 | .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"), |
| 2362 | .type = AVMEDIA_TYPE_VIDEO, |
| 2363 | .id = AV_CODEC_ID_THP, |
| 2364 | .priv_data_size = sizeof(MJpegDecodeContext), |
| 2365 | .init = ff_mjpeg_decode_init, |
| 2366 | .close = ff_mjpeg_decode_end, |
| 2367 | .decode = ff_mjpeg_decode_frame, |
| 2368 | .flush = decode_flush, |
| 2369 | .capabilities = CODEC_CAP_DR1, |
| 2370 | .max_lowres = 3, |
| 2371 | }; |
| 2372 | #endif |