LCOV - code coverage report
Current view: top level - libavcodec - mjpegdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 823 1681 49.0 %
Date: 2018-05-20 11:54:08 Functions: 22 31 71.0 %

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

Generated by: LCOV version 1.13