build: Split JPEG-related tables off into a separate component
[libav.git] / libavcodec / mjpegdec.c
CommitLineData
4922a5b0
AJ
1/*
2 * MJPEG decoder
406792e7 3 * Copyright (c) 2000, 2001 Fabrice Bellard
4922a5b0
AJ
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
6 *
7b94177e
DB
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 *
2912e87a 11 * This file is part of Libav.
4922a5b0 12 *
2912e87a 13 * Libav is free software; you can redistribute it and/or
4922a5b0
AJ
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 *
2912e87a 18 * Libav is distributed in the hope that it will be useful,
4922a5b0
AJ
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
2912e87a 24 * License along with Libav; if not, write to the Free Software
4922a5b0 25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4922a5b0
AJ
26 */
27
28/**
ba87f080 29 * @file
4922a5b0
AJ
30 * MJPEG decoder.
31 */
32
4922a5b0
AJ
33#include <assert.h>
34
737eb597 35#include "libavutil/imgutils.h"
1f0c7020 36#include "libavutil/opt.h"
4922a5b0 37#include "avcodec.h"
e74433a8 38#include "blockdsp.h"
e3fcb143 39#include "idctdsp.h"
594d4d5d 40#include "internal.h"
4978850c 41#include "jpegtables.h"
4922a5b0 42#include "mjpeg.h"
5acd8d9e 43#include "mjpegdec.h"
4922a5b0 44#include "jpeglsdec.h"
e6e3dcba 45#include "put_bits.h"
4922a5b0
AJ
46
47
8549cf07
DH
48static int build_vlc(VLC *vlc, const uint8_t *bits_table,
49 const uint8_t *val_table, int nb_codes,
50 int use_static, int is_ac)
4922a5b0 51{
a92be9b8 52 uint8_t huff_size[256] = { 0 };
19321824
MN
53 uint16_t huff_code[256];
54 uint16_t huff_sym[256];
55 int i;
4922a5b0
AJ
56
57 assert(nb_codes <= 256);
58
944721c1 59 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
4922a5b0 60
8549cf07
DH
61 for (i = 0; i < 256; i++)
62 huff_sym[i] = i + 16 * is_ac;
4922a5b0 63
8549cf07
DH
64 if (is_ac)
65 huff_sym[0] = 16 * 256;
a2d4dfd4 66
e96b4a53
MS
67 return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
68 huff_code, 2, 2, huff_sym, 2, 2, use_static);
4922a5b0
AJ
69}
70
8549cf07
DH
71static void build_basic_mjpeg_vlc(MJpegDecodeContext *s)
72{
a0845bae
SP
73 build_vlc(&s->vlcs[0][0], avpriv_mjpeg_bits_dc_luminance,
74 avpriv_mjpeg_val_dc, 12, 0, 0);
75 build_vlc(&s->vlcs[0][1], avpriv_mjpeg_bits_dc_chrominance,
76 avpriv_mjpeg_val_dc, 12, 0, 0);
77 build_vlc(&s->vlcs[1][0], avpriv_mjpeg_bits_ac_luminance,
78 avpriv_mjpeg_val_ac_luminance, 251, 0, 1);
79 build_vlc(&s->vlcs[1][1], avpriv_mjpeg_bits_ac_chrominance,
80 avpriv_mjpeg_val_ac_chrominance, 251, 0, 1);
81 build_vlc(&s->vlcs[2][0], avpriv_mjpeg_bits_ac_luminance,
82 avpriv_mjpeg_val_ac_luminance, 251, 0, 0);
83 build_vlc(&s->vlcs[2][1], avpriv_mjpeg_bits_ac_chrominance,
84 avpriv_mjpeg_val_ac_chrominance, 251, 0, 0);
8d641aed
OS
85}
86
98a6fff9 87av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
4922a5b0
AJ
88{
89 MJpegDecodeContext *s = avctx->priv_data;
90
e2274aa5
AK
91 if (!s->picture_ptr) {
92 s->picture = av_frame_alloc();
93 if (!s->picture)
94 return AVERROR(ENOMEM);
95 s->picture_ptr = s->picture;
96 }
e0e3b8b2 97
4922a5b0 98 s->avctx = avctx;
e74433a8 99 ff_blockdsp_init(&s->bdsp, avctx);
28bc406c 100 ff_hpeldsp_init(&s->hdsp, avctx->flags);
e3fcb143
DB
101 ff_idctdsp_init(&s->idsp, avctx);
102 ff_init_scantable(s->idsp.idct_permutation, &s->scantable,
103 ff_zigzag_direct);
8549cf07
DH
104 s->buffer_size = 0;
105 s->buffer = NULL;
106 s->start_code = -1;
4922a5b0 107 s->first_picture = 1;
8549cf07 108 s->org_height = avctx->coded_height;
580a7465 109 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
2deb6142 110 avctx->colorspace = AVCOL_SPC_BT470BG;
4922a5b0 111
8d641aed 112 build_basic_mjpeg_vlc(s);
4922a5b0 113
8549cf07 114 if (s->extern_huff) {
0f64cd1e 115 int ret;
4922a5b0 116 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
8549cf07 117 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
0f64cd1e 118 if ((ret = ff_mjpeg_decode_dht(s))) {
8549cf07
DH
119 av_log(avctx, AV_LOG_ERROR,
120 "mjpeg: error using external huffman table\n");
0f64cd1e 121 return ret;
8d641aed 122 }
4922a5b0 123 }
4bf3c8f2 124 if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
8549cf07 125 s->interlace_polarity = 1; /* bottom field first */
4bf3c8f2 126 av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n");
4922a5b0 127 }
36ef5369 128 if (avctx->codec->id == AV_CODEC_ID_AMV)
643fc8f1 129 s->flipped = 1;
4922a5b0
AJ
130
131 return 0;
132}
133
134
135/* quantize tables */
0a59a18b 136int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
4922a5b0
AJ
137{
138 int len, index, i, j;
139
140 len = get_bits(&s->gb, 16) - 2;
141
142 while (len >= 65) {
143 /* only 8 bit precision handled */
8549cf07 144 if (get_bits(&s->gb, 4) != 0) {
4922a5b0
AJ
145 av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n");
146 return -1;
147 }
148 index = get_bits(&s->gb, 4);
149 if (index >= 4)
150 return -1;
151 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
152 /* read quant table */
8549cf07 153 for (i = 0; i < 64; i++) {
4922a5b0
AJ
154 j = s->scantable.permutated[i];
155 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
156 }
157
8549cf07
DH
158 // XXX FIXME finetune, and perhaps add dc too
159 s->qscale[index] = FFMAX(s->quant_matrixes[index][s->scantable.permutated[1]],
160 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
161 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n",
162 index, s->qscale[index]);
4922a5b0
AJ
163 len -= 65;
164 }
4922a5b0
AJ
165 return 0;
166}
167
168/* decode huffman tables and build VLC decoders */
0a59a18b 169int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
4922a5b0
AJ
170{
171 int len, index, i, class, n, v, code_max;
172 uint8_t bits_table[17];
173 uint8_t val_table[256];
0f64cd1e 174 int ret = 0;
4922a5b0
AJ
175
176 len = get_bits(&s->gb, 16) - 2;
177
178 while (len > 0) {
179 if (len < 17)
0f64cd1e 180 return AVERROR_INVALIDDATA;
4922a5b0
AJ
181 class = get_bits(&s->gb, 4);
182 if (class >= 2)
0f64cd1e 183 return AVERROR_INVALIDDATA;
4922a5b0
AJ
184 index = get_bits(&s->gb, 4);
185 if (index >= 4)
0f64cd1e 186 return AVERROR_INVALIDDATA;
4922a5b0 187 n = 0;
8549cf07 188 for (i = 1; i <= 16; i++) {
4922a5b0
AJ
189 bits_table[i] = get_bits(&s->gb, 8);
190 n += bits_table[i];
191 }
192 len -= 17;
193 if (len < n || n > 256)
0f64cd1e 194 return AVERROR_INVALIDDATA;
4922a5b0
AJ
195
196 code_max = 0;
8549cf07 197 for (i = 0; i < n; i++) {
4922a5b0
AJ
198 v = get_bits(&s->gb, 8);
199 if (v > code_max)
200 code_max = v;
201 val_table[i] = v;
202 }
203 len -= n;
204
205 /* build VLC and flush previous vlc if present */
e96b4a53 206 ff_free_vlc(&s->vlcs[class][index]);
4922a5b0
AJ
207 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
208 class, index, code_max + 1);
0f64cd1e
LB
209 if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table,
210 code_max + 1, 0, class > 0)) < 0)
211 return ret;
4c3267aa 212
8549cf07 213 if (class > 0) {
e96b4a53 214 ff_free_vlc(&s->vlcs[2][index]);
0f64cd1e
LB
215 if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table,
216 code_max + 1, 0, 0)) < 0)
217 return ret;
5675a11f 218 }
4922a5b0
AJ
219 }
220 return 0;
221}
222
0a59a18b 223int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
4922a5b0 224{
809c3023
AK
225 int h_count[MAX_COMPONENTS] = { 0 };
226 int v_count[MAX_COMPONENTS] = { 0 };
227 int len, nb_components, i, width, height, bits, pix_fmt_id, ret;
4922a5b0
AJ
228
229 /* XXX: verify len field validity */
8549cf07 230 len = get_bits(&s->gb, 16);
809c3023 231 bits = get_bits(&s->gb, 8);
4922a5b0 232
8549cf07 233 if (s->pegasus_rct)
809c3023
AK
234 bits = 9;
235 if (bits == 9 && !s->pegasus_rct)
8549cf07 236 s->rct = 1; // FIXME ugly
4922a5b0 237
809c3023 238 if (bits != 8 && !s->lossless) {
4922a5b0
AJ
239 av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
240 return -1;
241 }
242
243 height = get_bits(&s->gb, 16);
8549cf07 244 width = get_bits(&s->gb, 16);
4922a5b0 245
8549cf07
DH
246 // HACK for odd_height.mov
247 if (s->interlaced && s->width == width && s->height == height + 1)
4922a5b0
AJ
248 height= s->height;
249
250 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
8549cf07 251 if (av_image_check_size(width, height, 0, s->avctx))
0f64cd1e 252 return AVERROR_INVALIDDATA;
4922a5b0
AJ
253
254 nb_components = get_bits(&s->gb, 8);
255 if (nb_components <= 0 ||
256 nb_components > MAX_COMPONENTS)
257 return -1;
af11fa54
MN
258 if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
259 if (nb_components != s->nb_components) {
260 av_log(s->avctx, AV_LOG_ERROR,
261 "nb_components changing in interlaced picture\n");
262 return AVERROR_INVALIDDATA;
263 }
264 }
809c3023 265 if (s->ls && !(bits <= 8 || nb_components == 1)) {
63d744e2
DB
266 avpriv_report_missing_feature(s->avctx,
267 "JPEG-LS that is not <= 8 "
268 "bits/component or 16-bit gray");
0f64cd1e 269 return AVERROR_PATCHWELCOME;
4922a5b0
AJ
270 }
271 s->nb_components = nb_components;
8549cf07
DH
272 s->h_max = 1;
273 s->v_max = 1;
274 for (i = 0; i < nb_components; i++) {
4922a5b0
AJ
275 /* component id */
276 s->component_id[i] = get_bits(&s->gb, 8) - 1;
809c3023
AK
277 h_count[i] = get_bits(&s->gb, 4);
278 v_count[i] = get_bits(&s->gb, 4);
4922a5b0 279 /* compute hmax and vmax (only used in interleaved case) */
809c3023
AK
280 if (h_count[i] > s->h_max)
281 s->h_max = h_count[i];
282 if (v_count[i] > s->v_max)
283 s->v_max = v_count[i];
4922a5b0
AJ
284 s->quant_index[i] = get_bits(&s->gb, 8);
285 if (s->quant_index[i] >= 4)
0f64cd1e 286 return AVERROR_INVALIDDATA;
809c3023 287 if (!h_count[i] || !v_count[i]) {
8aa35009
LB
288 av_log(s->avctx, AV_LOG_ERROR,
289 "Invalid sampling factor in component %d %d:%d\n",
809c3023 290 i, h_count[i], v_count[i]);
8aa35009
LB
291 return AVERROR_INVALIDDATA;
292 }
293
8549cf07 294 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
809c3023 295 i, h_count[i], v_count[i],
8549cf07 296 s->component_id[i], s->quant_index[i]);
4922a5b0
AJ
297 }
298
8549cf07 299 if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
63d744e2 300 avpriv_report_missing_feature(s->avctx, "Subsampling in JPEG-LS");
0f64cd1e 301 return AVERROR_PATCHWELCOME;
4922a5b0
AJ
302 }
303
8549cf07
DH
304 if (s->v_max == 1 && s->h_max == 1 && s->lossless == 1)
305 s->rgb = 1;
4922a5b0
AJ
306
307 /* if different size, realloc/alloc picture */
809c3023
AK
308 if (width != s->width || height != s->height || bits != s->bits ||
309 memcmp(s->h_count, h_count, sizeof(h_count)) ||
310 memcmp(s->v_count, v_count, sizeof(v_count))) {
8549cf07
DH
311 s->width = width;
312 s->height = height;
809c3023
AK
313 s->bits = bits;
314 memcpy(s->h_count, h_count, sizeof(h_count));
315 memcpy(s->v_count, v_count, sizeof(v_count));
4922a5b0
AJ
316 s->interlaced = 0;
317
318 /* test interlaced mode */
8549cf07 319 if (s->first_picture &&
4922a5b0
AJ
320 s->org_height != 0 &&
321 s->height < ((s->org_height * 3) / 4)) {
8549cf07
DH
322 s->interlaced = 1;
323 s->bottom_field = s->interlace_polarity;
e0e3b8b2 324 s->picture_ptr->interlaced_frame = 1;
8549cf07 325 s->picture_ptr->top_field_first = !s->interlace_polarity;
4922a5b0
AJ
326 height *= 2;
327 }
328
2cd94ad5
AK
329 ret = ff_set_dimensions(s->avctx, width, height);
330 if (ret < 0)
331 return ret;
4922a5b0 332
4922a5b0
AJ
333 s->first_picture = 0;
334 }
335
5eec5a79 336 if (!(s->interlaced && (s->bottom_field == !s->interlace_polarity))) {
4922a5b0 337 /* XXX: not complete test ! */
7c1e33f9
MN
338 pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
339 (s->h_count[1] << 20) | (s->v_count[1] << 16) |
340 (s->h_count[2] << 12) | (s->v_count[2] << 8) |
341 (s->h_count[3] << 4) | s->v_count[3];
4922a5b0 342 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
8549cf07
DH
343 /* NOTE we do not allocate pictures large enough for the possible
344 * padding of h/v_count being 4 */
345 if (!(pix_fmt_id & 0xD0D0D0D0))
346 pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
347 if (!(pix_fmt_id & 0x0D0D0D0D))
348 pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
349
350 switch (pix_fmt_id) {
7c1e33f9 351 case 0x11111100:
8549cf07 352 if (s->rgb)
716d413c 353 s->avctx->pix_fmt = AV_PIX_FMT_BGRA;
1c1fbc70 354 else {
716d413c 355 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
1c1fbc70
VG
356 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
357 }
8549cf07 358 assert(s->nb_components == 3);
4922a5b0 359 break;
7c1e33f9 360 case 0x11000000:
716d413c 361 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
97be28d8 362 break;
7c1e33f9 363 case 0x12111100:
716d413c 364 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV440P : AV_PIX_FMT_YUVJ440P;
1c1fbc70 365 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
4196cfb7 366 break;
7c1e33f9 367 case 0x21111100:
716d413c 368 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P;
1c1fbc70 369 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
4922a5b0 370 break;
7c1e33f9 371 case 0x22111100:
716d413c 372 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUVJ420P;
1c1fbc70 373 s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
4922a5b0 374 break;
83c43e4d
375 default:
376 av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
0f64cd1e 377 return AVERROR_PATCHWELCOME;
4922a5b0 378 }
8549cf07
DH
379 if (s->ls) {
380 if (s->nb_components > 1)
716d413c 381 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
8549cf07 382 else if (s->bits <= 8)
716d413c 383 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
4922a5b0 384 else
716d413c 385 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
4922a5b0
AJ
386 }
387
d48132b7
AK
388 s->pix_desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
389 if (!s->pix_desc) {
390 av_log(s->avctx, AV_LOG_ERROR, "Could not get a pixel format descriptor.\n");
391 return AVERROR_BUG;
392 }
393
759001c5
AK
394 av_frame_unref(s->picture_ptr);
395 if (ff_get_buffer(s->avctx, s->picture_ptr, AV_GET_BUFFER_FLAG_REF) < 0) {
4922a5b0
AJ
396 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
397 return -1;
398 }
8549cf07
DH
399 s->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
400 s->picture_ptr->key_frame = 1;
401 s->got_picture = 1;
4922a5b0 402
8549cf07
DH
403 for (i = 0; i < 3; i++)
404 s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
4922a5b0 405
1218777f
DB
406 av_dlog(s->avctx, "%d %d %d %d %d %d\n",
407 s->width, s->height, s->linesize[0], s->linesize[1],
408 s->interlaced, s->avctx->height);
4922a5b0 409
8549cf07 410 if (len != (8 + (3 * nb_components)))
4922a5b0 411 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
5eec5a79 412 }
4922a5b0
AJ
413
414 /* totally blank picture as progressive JPEG will only add details to it */
8549cf07
DH
415 if (s->progressive) {
416 int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8);
417 int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
418 for (i = 0; i < s->nb_components; i++) {
5fac2776
LM
419 int size = bw * bh * s->h_count[i] * s->v_count[i];
420 av_freep(&s->blocks[i]);
421 av_freep(&s->last_nnz[i]);
8549cf07
DH
422 s->blocks[i] = av_malloc(size * sizeof(**s->blocks));
423 s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz));
5fac2776
LM
424 s->block_stride[i] = bw * s->h_count[i];
425 }
426 memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
4922a5b0
AJ
427 }
428 return 0;
429}
430
431static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
432{
433 int code;
434 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
8549cf07
DH
435 if (code < 0) {
436 av_log(s->avctx, AV_LOG_WARNING,
437 "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
438 0, dc_index, &s->vlcs[0][dc_index]);
4922a5b0
AJ
439 return 0xffff;
440 }
441
8549cf07 442 if (code)
4922a5b0
AJ
443 return get_xbits(&s->gb, code);
444 else
445 return 0;
446}
447
448/* decode block and dequantize */
88bd7fdc 449static int decode_block(MJpegDecodeContext *s, int16_t *block, int component,
8549cf07 450 int dc_index, int ac_index, int16_t *quant_matrix)
4922a5b0
AJ
451{
452 int code, i, j, level, val;
453
454 /* DC coef */
455 val = mjpeg_decode_dc(s, dc_index);
456 if (val == 0xffff) {
457 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
0f64cd1e 458 return AVERROR_INVALIDDATA;
4922a5b0
AJ
459 }
460 val = val * quant_matrix[0] + s->last_dc[component];
461 s->last_dc[component] = val;
462 block[0] = val;
463 /* AC coefs */
464 i = 0;
bf5f9b52 465 {OPEN_READER(re, &s->gb);
2111a191 466 do {
4922a5b0 467 UPDATE_CACHE(re, &s->gb);
bf5f9b52 468 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
4922a5b0 469
4922a5b0 470 i += ((unsigned)code) >> 4;
4922a5b0 471 code &= 0xf;
8549cf07
DH
472 if (code) {
473 if (code > MIN_CACHE_BITS - 16)
bf5f9b52 474 UPDATE_CACHE(re, &s->gb);
8549cf07 475
4922a5b0 476 {
8549cf07
DH
477 int cache = GET_CACHE(re, &s->gb);
478 int sign = (~cache) >> 31;
479 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
4922a5b0
AJ
480 }
481
bf5f9b52 482 LAST_SKIP_BITS(re, &s->gb, code);
4922a5b0 483
e4463f21 484 if (i > 63) {
4922a5b0 485 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
0f64cd1e 486 return AVERROR_INVALIDDATA;
4922a5b0 487 }
8549cf07 488 j = s->scantable.permutated[i];
4922a5b0
AJ
489 block[j] = level * quant_matrix[j];
490 }
8549cf07 491 } while (i < 63);
bf5f9b52 492 CLOSE_READER(re, &s->gb);}
4922a5b0
AJ
493
494 return 0;
495}
496
88bd7fdc 497static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block,
8549cf07
DH
498 int component, int dc_index,
499 int16_t *quant_matrix, int Al)
5fac2776
LM
500{
501 int val;
e74433a8 502 s->bdsp.clear_block(block);
5fac2776
LM
503 val = mjpeg_decode_dc(s, dc_index);
504 if (val == 0xffff) {
505 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
0f64cd1e 506 return AVERROR_INVALIDDATA;
5fac2776
LM
507 }
508 val = (val * quant_matrix[0] << Al) + s->last_dc[component];
509 s->last_dc[component] = val;
510 block[0] = val;
511 return 0;
512}
513
4922a5b0 514/* decode block and dequantize - progressive JPEG version */
88bd7fdc 515static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
8549cf07
DH
516 uint8_t *last_nnz, int ac_index,
517 int16_t *quant_matrix,
5fac2776 518 int ss, int se, int Al, int *EOBRUN)
4922a5b0
AJ
519{
520 int code, i, j, level, val, run;
521
8549cf07 522 if (*EOBRUN) {
4922a5b0
AJ
523 (*EOBRUN)--;
524 return 0;
525 }
5675a11f 526
8549cf07
DH
527 {
528 OPEN_READER(re, &s->gb);
529 for (i = ss; ; i++) {
530 UPDATE_CACHE(re, &s->gb);
531 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
4922a5b0 532
8549cf07
DH
533 run = ((unsigned) code) >> 4;
534 code &= 0xF;
535 if (code) {
536 i += run;
537 if (code > MIN_CACHE_BITS - 16)
538 UPDATE_CACHE(re, &s->gb);
4922a5b0 539
8549cf07
DH
540 {
541 int cache = GET_CACHE(re, &s->gb);
542 int sign = (~cache) >> 31;
543 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
4922a5b0 544 }
8549cf07
DH
545
546 LAST_SKIP_BITS(re, &s->gb, code);
547
2111a191 548 if (i >= se) {
8549cf07
DH
549 if (i == se) {
550 j = s->scantable.permutated[se];
551 block[j] = level * quant_matrix[j] << Al;
552 break;
553 }
554 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
0f64cd1e 555 return AVERROR_INVALIDDATA;
2111a191 556 }
8549cf07
DH
557 j = s->scantable.permutated[i];
558 block[j] = level * quant_matrix[j] << Al;
559 } else {
560 if (run == 0xF) {// ZRL - skip 15 coefficients
561 i += 15;
562 if (i >= se) {
563 av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
0f64cd1e 564 return AVERROR_INVALIDDATA;
8549cf07
DH
565 }
566 } else {
567 val = (1 << run);
568 if (run) {
569 UPDATE_CACHE(re, &s->gb);
570 val += NEG_USR32(GET_CACHE(re, &s->gb), run);
571 LAST_SKIP_BITS(re, &s->gb, run);
572 }
573 *EOBRUN = val - 1;
574 break;
7f4a1e8f 575 }
4922a5b0
AJ
576 }
577 }
8549cf07 578 CLOSE_READER(re, &s->gb);
4922a5b0 579 }
8549cf07
DH
580
581 if (i > *last_nnz)
5fac2776 582 *last_nnz = i;
8549cf07 583
5fac2776
LM
584 return 0;
585}
586
8549cf07
DH
587#define REFINE_BIT(j) { \
588 UPDATE_CACHE(re, &s->gb); \
589 sign = block[j] >> 15; \
590 block[j] += SHOW_UBITS(re, &s->gb, 1) * \
591 ((quant_matrix[j] ^ sign) - sign) << Al; \
592 LAST_SKIP_BITS(re, &s->gb, 1); \
5fac2776
LM
593}
594
8549cf07
DH
595#define ZERO_RUN \
596for (; ; i++) { \
597 if (i > last) { \
598 i += run; \
599 if (i > se) { \
600 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
601 return -1; \
602 } \
603 break; \
604 } \
605 j = s->scantable.permutated[i]; \
606 if (block[j]) \
607 REFINE_BIT(j) \
608 else if (run-- == 0) \
609 break; \
5fac2776
LM
610}
611
612/* decode block and dequantize - progressive JPEG refinement pass */
88bd7fdc 613static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block,
8549cf07
DH
614 uint8_t *last_nnz,
615 int ac_index, int16_t *quant_matrix,
616 int ss, int se, int Al, int *EOBRUN)
5fac2776 617{
8549cf07
DH
618 int code, i = ss, j, sign, val, run;
619 int last = FFMIN(se, *last_nnz);
5fac2776
LM
620
621 OPEN_READER(re, &s->gb);
8549cf07 622 if (*EOBRUN) {
5fac2776 623 (*EOBRUN)--;
8549cf07
DH
624 } else {
625 for (; ; i++) {
5fac2776 626 UPDATE_CACHE(re, &s->gb);
bf5f9b52 627 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
5675a11f 628
8549cf07 629 if (code & 0xF) {
5fac2776
LM
630 run = ((unsigned) code) >> 4;
631 UPDATE_CACHE(re, &s->gb);
632 val = SHOW_UBITS(re, &s->gb, 1);
633 LAST_SKIP_BITS(re, &s->gb, 1);
634 ZERO_RUN;
635 j = s->scantable.permutated[i];
636 val--;
8549cf07
DH
637 block[j] = ((quant_matrix[j]^val) - val) << Al;
638 if (i == se) {
639 if (i > *last_nnz)
5fac2776 640 *last_nnz = i;
bf5f9b52 641 CLOSE_READER(re, &s->gb);
5fac2776
LM
642 return 0;
643 }
8549cf07 644 } else {
5fac2776 645 run = ((unsigned) code) >> 4;
8549cf07 646 if (run == 0xF) {
5fac2776 647 ZERO_RUN;
8549cf07 648 } else {
5fac2776
LM
649 val = run;
650 run = (1 << run);
8549cf07 651 if (val) {
5fac2776
LM
652 UPDATE_CACHE(re, &s->gb);
653 run += SHOW_UBITS(re, &s->gb, val);
654 LAST_SKIP_BITS(re, &s->gb, val);
655 }
656 *EOBRUN = run - 1;
657 break;
658 }
659 }
660 }
661
8549cf07 662 if (i > *last_nnz)
5fac2776
LM
663 *last_nnz = i;
664 }
665
8549cf07 666 for (; i <= last; i++) {
5fac2776 667 j = s->scantable.permutated[i];
8549cf07 668 if (block[j])
5fac2776
LM
669 REFINE_BIT(j)
670 }
671 CLOSE_READER(re, &s->gb);
4922a5b0
AJ
672
673 return 0;
674}
5fac2776
LM
675#undef REFINE_BIT
676#undef ZERO_RUN
4922a5b0 677
8549cf07
DH
678static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor,
679 int point_transform)
680{
4922a5b0 681 int i, mb_x, mb_y;
ad9feac9 682 uint16_t (*buffer)[4];
4922a5b0 683 int left[3], top[3], topleft[3];
8549cf07
DH
684 const int linesize = s->linesize[0];
685 const int mask = (1 << s->bits) - 1;
4922a5b0 686
8549cf07
DH
687 av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
688 (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
689 buffer = s->ljpeg_buffer;
4922a5b0 690
8549cf07
DH
691 for (i = 0; i < 3; i++)
692 buffer[0][i] = 1 << (s->bits + point_transform - 1);
693
694 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
695 const int modified_predictor = mb_y ? predictor : 1;
e0e3b8b2 696 uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
4922a5b0
AJ
697
698 if (s->interlaced && s->bottom_field)
699 ptr += linesize >> 1;
700
8549cf07
DH
701 for (i = 0; i < 3; i++)
702 top[i] = left[i] = topleft[i] = buffer[0][i];
703
704 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
4922a5b0
AJ
705 if (s->restart_interval && !s->restart_count)
706 s->restart_count = s->restart_interval;
707
8549cf07 708 for (i = 0; i < 3; i++) {
4922a5b0
AJ
709 int pred;
710
8549cf07
DH
711 topleft[i] = top[i];
712 top[i] = buffer[mb_x][i];
4922a5b0
AJ
713
714 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
715
8549cf07
DH
716 left[i] = buffer[mb_x][i] =
717 mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
4922a5b0
AJ
718 }
719
720 if (s->restart_interval && !--s->restart_count) {
721 align_get_bits(&s->gb);
722 skip_bits(&s->gb, 16); /* skip RSTn */
723 }
724 }
725
8549cf07
DH
726 if (s->rct) {
727 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
728 ptr[4 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
729 ptr[4 * mb_x + 0] = buffer[mb_x][1] + ptr[4 * mb_x + 1];
730 ptr[4 * mb_x + 2] = buffer[mb_x][2] + ptr[4 * mb_x + 1];
4922a5b0 731 }
8549cf07
DH
732 } else if (s->pegasus_rct) {
733 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
734 ptr[4 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
735 ptr[4 * mb_x + 0] = buffer[mb_x][1] + ptr[4 * mb_x + 1];
736 ptr[4 * mb_x + 2] = buffer[mb_x][2] + ptr[4 * mb_x + 1];
4922a5b0 737 }
8549cf07
DH
738 } else {
739 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
740 ptr[4 * mb_x + 0] = buffer[mb_x][2];
741 ptr[4 * mb_x + 1] = buffer[mb_x][1];
742 ptr[4 * mb_x + 2] = buffer[mb_x][0];
4922a5b0
AJ
743 }
744 }
745 }
746 return 0;
747}
748
8549cf07 749static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor,
a030279a 750 int point_transform, int nb_components)
8549cf07 751{
4922a5b0 752 int i, mb_x, mb_y;
4922a5b0 753
8549cf07
DH
754 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
755 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
4922a5b0
AJ
756 if (s->restart_interval && !s->restart_count)
757 s->restart_count = s->restart_interval;
758
8549cf07
DH
759 if (mb_x == 0 || mb_y == 0 || s->interlaced) {
760 for (i = 0; i < nb_components; i++) {
4922a5b0
AJ
761 uint8_t *ptr;
762 int n, h, v, x, y, c, j, linesize;
8549cf07
DH
763 n = s->nb_blocks[i];
764 c = s->comp_index[i];
765 h = s->h_scount[i];
766 v = s->v_scount[i];
767 x = 0;
768 y = 0;
769 linesize = s->linesize[c];
770
771 for (j = 0; j < n; j++) {
4922a5b0 772 int pred;
8549cf07
DH
773 // FIXME optimize this crap
774 ptr = s->picture_ptr->data[c] +
775 (linesize * (v * mb_y + y)) +
776 (h * mb_x + x);
777 if (y == 0 && mb_y == 0) {
778 if (x == 0 && mb_x == 0)
779 pred = 128 << point_transform;
780 else
781 pred = ptr[-1];
782 } else {
783 if (x == 0 && mb_x == 0)
784 pred = ptr[-linesize];
785 else
786 PREDICT(pred, ptr[-linesize - 1],
787 ptr[-linesize], ptr[-1], predictor);
788 }
4922a5b0
AJ
789
790 if (s->interlaced && s->bottom_field)
791 ptr += linesize >> 1;
8549cf07 792 *ptr = pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
4922a5b0
AJ
793
794 if (++x == h) {
795 x = 0;
796 y++;
797 }
798 }
799 }
8549cf07
DH
800 } else {
801 for (i = 0; i < nb_components; i++) {
4922a5b0
AJ
802 uint8_t *ptr;
803 int n, h, v, x, y, c, j, linesize;
8549cf07
DH
804 n = s->nb_blocks[i];
805 c = s->comp_index[i];
806 h = s->h_scount[i];
807 v = s->v_scount[i];
808 x = 0;
809 y = 0;
810 linesize = s->linesize[c];
811
812 for (j = 0; j < n; j++) {
4922a5b0
AJ
813 int pred;
814
8549cf07
DH
815 // FIXME optimize this crap
816 ptr = s->picture_ptr->data[c] +
817 (linesize * (v * mb_y + y)) +
818 (h * mb_x + x);
819 PREDICT(pred, ptr[-linesize - 1],
820 ptr[-linesize], ptr[-1], predictor);
821 *ptr = pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
4922a5b0
AJ
822 if (++x == h) {
823 x = 0;
824 y++;
825 }
826 }
827 }
828 }
829 if (s->restart_interval && !--s->restart_count) {
830 align_get_bits(&s->gb);
831 skip_bits(&s->gb, 16); /* skip RSTn */
832 }
833 }
834 }
835 return 0;
836}
837
8549cf07
DH
838static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
839 int Al, const uint8_t *mb_bitmask,
840 const AVFrame *reference)
841{
4922a5b0 842 int i, mb_x, mb_y;
8549cf07 843 uint8_t *data[MAX_COMPONENTS];
8f0bd2c0 844 const uint8_t *reference_data[MAX_COMPONENTS];
a449faea 845 int linesize[MAX_COMPONENTS];
8f0bd2c0 846 GetBitContext mb_bitmask_gb;
847
8549cf07
DH
848 if (mb_bitmask)
849 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
4922a5b0 850
8549cf07
DH
851 for (i = 0; i < nb_components; i++) {
852 int c = s->comp_index[i];
e0e3b8b2 853 data[c] = s->picture_ptr->data[c];
8f0bd2c0 854 reference_data[c] = reference ? reference->data[c] : NULL;
8549cf07 855 linesize[c] = s->linesize[c];
5fac2776 856 s->coefs_finished[c] |= 1;
a449faea
VV
857 }
858
8549cf07
DH
859 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
860 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
8f0bd2c0 861 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
862
4922a5b0
AJ
863 if (s->restart_interval && !s->restart_count)
864 s->restart_count = s->restart_interval;
865
3574a85c 866 if (get_bits_left(&s->gb) < 0) {
8549cf07 867 av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
3574a85c 868 -get_bits_left(&s->gb));
0f64cd1e 869 return AVERROR_INVALIDDATA;
76cd98b4 870 }
8549cf07 871 for (i = 0; i < nb_components; i++) {
4922a5b0
AJ
872 uint8_t *ptr;
873 int n, h, v, x, y, c, j;
8f0bd2c0 874 int block_offset;
4922a5b0
AJ
875 n = s->nb_blocks[i];
876 c = s->comp_index[i];
877 h = s->h_scount[i];
878 v = s->v_scount[i];
879 x = 0;
880 y = 0;
8549cf07 881 for (j = 0; j < n; j++) {
2bcbd984
MR
882 block_offset = ((linesize[c] * (v * mb_y + y) * 8) +
883 (h * mb_x + x) * 8);
8f0bd2c0 884
8549cf07 885 if (s->interlaced && s->bottom_field)
8f0bd2c0 886 block_offset += linesize[c] >> 1;
887 ptr = data[c] + block_offset;
8549cf07
DH
888 if (!s->progressive) {
889 if (copy_mb)
28bc406c 890 s->hdsp.put_pixels_tab[1][0](ptr,
ed62e6e3
MR
891 reference_data[c] + block_offset,
892 linesize[c], 8);
8549cf07 893 else {
e74433a8 894 s->bdsp.clear_block(s->block);
8549cf07
DH
895 if (decode_block(s, s->block, i,
896 s->dc_index[i], s->ac_index[i],
897 s->quant_matrixes[s->quant_index[c]]) < 0) {
898 av_log(s->avctx, AV_LOG_ERROR,
899 "error y=%d x=%d\n", mb_y, mb_x);
0f64cd1e 900 return AVERROR_INVALIDDATA;
8549cf07 901 }
e3fcb143 902 s->idsp.idct_put(ptr, linesize[c], s->block);
8f0bd2c0 903 }
5fac2776 904 } else {
8549cf07
DH
905 int block_idx = s->block_stride[c] * (v * mb_y + y) +
906 (h * mb_x + x);
88bd7fdc 907 int16_t *block = s->blocks[c][block_idx];
8549cf07
DH
908 if (Ah)
909 block[0] += get_bits1(&s->gb) *
910 s->quant_matrixes[s->quant_index[c]][0] << Al;
911 else if (decode_dc_progressive(s, block, i, s->dc_index[i],
912 s->quant_matrixes[s->quant_index[c]],
913 Al) < 0) {
914 av_log(s->avctx, AV_LOG_ERROR,
915 "error y=%d x=%d\n", mb_y, mb_x);
0f64cd1e 916 return AVERROR_INVALIDDATA;
5fac2776
LM
917 }
918 }
1218777f
DB
919 av_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
920 av_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
921 mb_x, mb_y, x, y, c, s->bottom_field,
922 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
4922a5b0
AJ
923 if (++x == h) {
924 x = 0;
925 y++;
926 }
927 }
928 }
47b5b0e8 929
e268a352
JG
930 if (s->restart_interval) {
931 s->restart_count--;
932 i = 8 + ((-get_bits_count(&s->gb)) & 7);
933 /* skip RSTn */
934 if (show_bits(&s->gb, i) == (1 << i) - 1) {
935 int pos = get_bits_count(&s->gb);
936 align_get_bits(&s->gb);
937 while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
938 skip_bits(&s->gb, 8);
939 if ((get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
940 for (i = 0; i < nb_components; i++) /* reset dc */
941 s->last_dc[i] = 1024;
8549cf07 942 } else
e268a352 943 skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
e268a352 944 }
4922a5b0
AJ
945 }
946 }
947 }
948 return 0;
949}
950
8549cf07
DH
951static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss,
952 int se, int Ah, int Al,
953 const uint8_t *mb_bitmask,
954 const AVFrame *reference)
955{
5fac2776
LM
956 int mb_x, mb_y;
957 int EOBRUN = 0;
958 int c = s->comp_index[0];
8549cf07 959 uint8_t *data = s->picture_ptr->data[c];
8f0bd2c0 960 const uint8_t *reference_data = reference ? reference->data[c] : NULL;
8549cf07 961 int linesize = s->linesize[c];
5fac2776 962 int last_scan = 0;
8549cf07 963 int16_t *quant_matrix = s->quant_matrixes[s->quant_index[c]];
8f0bd2c0 964 GetBitContext mb_bitmask_gb;
965
cfbd98ab
LB
966 if (ss < 0 || ss >= 64 ||
967 se < ss || se >= 64 ||
968 Ah < 0 || Al < 0)
969 return AVERROR_INVALIDDATA;
970
8549cf07
DH
971 if (mb_bitmask)
972 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
5fac2776 973
8549cf07 974 if (!Al) {
199d9f99
VG
975 // s->coefs_finished is a bitmask for coefficients coded
976 // ss and se are parameters telling start and end coefficients
977 s->coefs_finished[c] |= (~0ULL >> (63 - (se - ss))) << ss;
5fac2776
LM
978 last_scan = !~s->coefs_finished[c];
979 }
980
8549cf07
DH
981 if (s->interlaced && s->bottom_field) {
982 int offset = linesize >> 1;
983 data += offset;
8f0bd2c0 984 reference_data += offset;
985 }
5fac2776 986
8549cf07 987 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
2bcbd984 988 int block_offset = mb_y * linesize * 8;
8549cf07
DH
989 uint8_t *ptr = data + block_offset;
990 int block_idx = mb_y * s->block_stride[c];
88bd7fdc 991 int16_t (*block)[64] = &s->blocks[c][block_idx];
8549cf07
DH
992 uint8_t *last_nnz = &s->last_nnz[c][block_idx];
993 for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
8f0bd2c0 994 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
995
996 if (!copy_mb) {
8549cf07
DH
997 int ret;
998 if (Ah)
999 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
1000 quant_matrix, ss, se, Al, &EOBRUN);
1001 else
1002 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
1003 quant_matrix, ss, se, Al, &EOBRUN);
1004 if (ret < 0) {
1005 av_log(s->avctx, AV_LOG_ERROR,
1006 "error y=%d x=%d\n", mb_y, mb_x);
0f64cd1e 1007 return AVERROR_INVALIDDATA;
8549cf07 1008 }
8f0bd2c0 1009 }
1010
8549cf07 1011 if (last_scan) {
8f0bd2c0 1012 if (copy_mb) {
28bc406c
RB
1013 s->hdsp.put_pixels_tab[1][0](ptr,
1014 reference_data + block_offset,
1015 linesize, 8);
8f0bd2c0 1016 } else {
e3fcb143 1017 s->idsp.idct_put(ptr, linesize, *block);
2bcbd984 1018 ptr += 8;
8f0bd2c0 1019 }
5fac2776
LM
1020 }
1021 }
1022 }
1023 return 0;
1024}
1025
8549cf07
DH
1026int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask,
1027 const AVFrame *reference)
4922a5b0
AJ
1028{
1029 int len, nb_components, i, h, v, predictor, point_transform;
0f64cd1e 1030 int index, id, ret;
8549cf07 1031 const int block_size = s->lossless ? 1 : 8;
4922a5b0
AJ
1032 int ilv, prev_shift;
1033
1034 /* XXX: verify len field validity */
1035 len = get_bits(&s->gb, 16);
1036 nb_components = get_bits(&s->gb, 8);
8549cf07
DH
1037 if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
1038 av_log(s->avctx, AV_LOG_ERROR,
1039 "decode_sos: nb_components (%d) unsupported\n", nb_components);
0f64cd1e 1040 return AVERROR_PATCHWELCOME;
021dccba 1041 }
8549cf07 1042 if (len != 6 + 2 * nb_components) {
4922a5b0 1043 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
0f64cd1e 1044 return AVERROR_INVALIDDATA;
4922a5b0 1045 }
8549cf07 1046 for (i = 0; i < nb_components; i++) {
4922a5b0
AJ
1047 id = get_bits(&s->gb, 8) - 1;
1048 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1049 /* find component index */
8549cf07 1050 for (index = 0; index < s->nb_components; index++)
4922a5b0
AJ
1051 if (id == s->component_id[index])
1052 break;
8549cf07
DH
1053 if (index == s->nb_components) {
1054 av_log(s->avctx, AV_LOG_ERROR,
1055 "decode_sos: index(%d) out of components\n", index);
0f64cd1e 1056 return AVERROR_INVALIDDATA;
4922a5b0 1057 }
ddee80f3
CEH
1058 /* Metasoft MJPEG codec has Cb and Cr swapped */
1059 if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1060 && nb_components == 3 && s->nb_components == 3 && i)
1061 index = 3 - i;
4922a5b0
AJ
1062
1063 s->comp_index[i] = index;
1064
1065 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
8549cf07
DH
1066 s->h_scount[i] = s->h_count[index];
1067 s->v_scount[i] = s->v_count[index];
4922a5b0
AJ
1068
1069 s->dc_index[i] = get_bits(&s->gb, 4);
1070 s->ac_index[i] = get_bits(&s->gb, 4);
1071
1072 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1073 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1074 goto out_of_range;
8549cf07
DH
1075 if (!s->vlcs[0][s->dc_index[i]].table ||
1076 !s->vlcs[1][s->ac_index[i]].table)
b27bf2a7 1077 goto out_of_range;
4922a5b0
AJ
1078 }
1079
8549cf07
DH
1080 predictor = get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1081 ilv = get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1082 prev_shift = get_bits(&s->gb, 4); /* Ah */
1083 point_transform = get_bits(&s->gb, 4); /* Al */
4922a5b0 1084
4922a5b0
AJ
1085 if (nb_components > 1) {
1086 /* interleaved stream */
1087 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1088 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
8549cf07 1089 } else if (!s->ls) { /* skip this for JPEG-LS */
4922a5b0
AJ
1090 h = s->h_max / s->h_scount[0];
1091 v = s->v_max / s->v_scount[0];
8549cf07
DH
1092 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1093 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
4922a5b0 1094 s->nb_blocks[0] = 1;
8549cf07
DH
1095 s->h_scount[0] = 1;
1096 s->v_scount[0] = 1;
4922a5b0
AJ
1097 }
1098
8549cf07
DH
1099 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1100 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n",
1101 s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
4922a5b0
AJ
1102 predictor, point_transform, ilv, s->bits,
1103 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
1104
1105
1106 /* mjpeg-b can have padding bytes between sos and image data, skip them */
1107 for (i = s->mjpb_skiptosod; i > 0; i--)
1108 skip_bits(&s->gb, 8);
1109
ac330161
MN
1110next_field:
1111 for (i = 0; i < nb_components; i++)
1112 s->last_dc[i] = 1024;
1113
8549cf07
DH
1114 if (s->lossless) {
1115 if (CONFIG_JPEGLS_DECODER && s->ls) {
1116// for () {
4922a5b0
AJ
1117// reset_ls_coding_parameters(s, 0);
1118
0f64cd1e
LB
1119 if ((ret = ff_jpegls_decode_picture(s, predictor,
1120 point_transform, ilv)) < 0)
1121 return ret;
8549cf07
DH
1122 } else {
1123 if (s->rgb) {
0f64cd1e
LB
1124 if ((ret = ljpeg_decode_rgb_scan(s, predictor,
1125 point_transform)) < 0)
1126 return ret;
8549cf07 1127 } else {
0f64cd1e 1128 if ((ret = ljpeg_decode_yuv_scan(s, predictor,
a030279a
LB
1129 point_transform,
1130 nb_components)) < 0)
0f64cd1e 1131 return ret;
4922a5b0
AJ
1132 }
1133 }
8549cf07
DH
1134 } else {
1135 if (s->progressive && predictor) {
0f64cd1e
LB
1136 if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor,
1137 ilv, prev_shift,
1138 point_transform,
1139 mb_bitmask,
1140 reference)) < 0)
1141 return ret;
5fac2776 1142 } else {
0f64cd1e
LB
1143 if ((ret = mjpeg_decode_scan(s, nb_components,
1144 prev_shift, point_transform,
1145 mb_bitmask, reference)) < 0)
1146 return ret;
5fac2776 1147 }
4922a5b0 1148 }
ac330161
MN
1149
1150 if (s->interlaced &&
1151 get_bits_left(&s->gb) > 32 &&
1152 show_bits(&s->gb, 8) == 0xFF) {
1153 GetBitContext bak = s->gb;
1154 align_get_bits(&bak);
1155 if (show_bits(&bak, 16) == 0xFFD1) {
1156 av_dlog(s->avctx, "AVRn interlaced picture marker found\n");
1157 s->gb = bak;
1158 skip_bits(&s->gb, 16);
1159 s->bottom_field ^= 1;
1160
1161 goto next_field;
1162 }
1163 }
1164
4922a5b0
AJ
1165 emms_c();
1166 return 0;
1167 out_of_range:
1168 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
0f64cd1e 1169 return AVERROR_INVALIDDATA;
4922a5b0
AJ
1170}
1171
1172static int mjpeg_decode_dri(MJpegDecodeContext *s)
1173{
1174 if (get_bits(&s->gb, 16) != 4)
0f64cd1e 1175 return AVERROR_INVALIDDATA;
4922a5b0 1176 s->restart_interval = get_bits(&s->gb, 16);
8549cf07
DH
1177 s->restart_count = 0;
1178 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1179 s->restart_interval);
4922a5b0
AJ
1180
1181 return 0;
1182}
1183
1184static int mjpeg_decode_app(MJpegDecodeContext *s)
1185{
1186 int len, id, i;
1187
1188 len = get_bits(&s->gb, 16);
1189 if (len < 5)
0f64cd1e 1190 return AVERROR_INVALIDDATA;
3574a85c 1191 if (8 * len > get_bits_left(&s->gb))
0f64cd1e 1192 return AVERROR_INVALIDDATA;
4922a5b0 1193
8549cf07
DH
1194 id = get_bits_long(&s->gb, 32);
1195 id = av_be2ne32(id);
4922a5b0
AJ
1196 len -= 6;
1197
8549cf07 1198 if (s->avctx->debug & FF_DEBUG_STARTCODE)
4922a5b0 1199 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
4922a5b0 1200
da9cea77
DB
1201 /* Buggy AVID, it puts EOI only at every 10th frame. */
1202 /* Also, this fourcc is used by non-avid files too, it holds some
1203 information, but it's always present in AVID-created files. */
8549cf07 1204 if (id == AV_RL32("AVI1")) {
4922a5b0
AJ
1205 /* structure:
1206 4bytes AVI1
1207 1bytes polarity
1208 1bytes always zero
1209 4bytes field_size
1210 4bytes field_size_less_padding
1211 */
8549cf07 1212 s->buggy_avid = 1;
4922a5b0 1213 i = get_bits(&s->gb, 8);
8549cf07
DH
1214 if (i == 2)
1215 s->bottom_field = 1;
1216 else if (i == 1)
1217 s->bottom_field = 0;
4922a5b0
AJ
1218#if 0
1219 skip_bits(&s->gb, 8);
1220 skip_bits(&s->gb, 32);
1221 skip_bits(&s->gb, 32);
1222 len -= 10;
1223#endif
4922a5b0
AJ
1224 goto out;
1225 }
1226
1227// len -= 2;
1228
8549cf07 1229 if (id == AV_RL32("JFIF")) {
4922a5b0
AJ
1230 int t_w, t_h, v1, v2;
1231 skip_bits(&s->gb, 8); /* the trailing zero-byte */
8549cf07
DH
1232 v1 = get_bits(&s->gb, 8);
1233 v2 = get_bits(&s->gb, 8);
4922a5b0
AJ
1234 skip_bits(&s->gb, 8);
1235
8549cf07
DH
1236 s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1237 s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
9e500efd 1238 ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
4922a5b0
AJ
1239
1240 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
8549cf07
DH
1241 av_log(s->avctx, AV_LOG_INFO,
1242 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1243 v1, v2,
1244 s->avctx->sample_aspect_ratio.num,
1245 s->avctx->sample_aspect_ratio.den);
4922a5b0
AJ
1246
1247 t_w = get_bits(&s->gb, 8);
1248 t_h = get_bits(&s->gb, 8);
8549cf07 1249 if (t_w && t_h) {
4922a5b0 1250 /* skip thumbnail */
8549cf07
DH
1251 if (len -10 - (t_w * t_h * 3) > 0)
1252 len -= t_w * t_h * 3;
4922a5b0
AJ
1253 }
1254 len -= 10;
1255 goto out;
1256 }
1257
8549cf07 1258 if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e')) {
4922a5b0
AJ
1259 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1260 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
1261 skip_bits(&s->gb, 16); /* version */
1262 skip_bits(&s->gb, 16); /* flags0 */
1263 skip_bits(&s->gb, 16); /* flags1 */
8549cf07 1264 skip_bits(&s->gb, 8); /* transform */
4922a5b0
AJ
1265 len -= 7;
1266 goto out;
1267 }
1268
8549cf07 1269 if (id == AV_RL32("LJIF")) {
4922a5b0 1270 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
8549cf07
DH
1271 av_log(s->avctx, AV_LOG_INFO,
1272 "Pegasus lossless jpeg header found\n");
4922a5b0
AJ
1273 skip_bits(&s->gb, 16); /* version ? */
1274 skip_bits(&s->gb, 16); /* unknwon always 0? */
1275 skip_bits(&s->gb, 16); /* unknwon always 0? */
1276 skip_bits(&s->gb, 16); /* unknwon always 0? */
8549cf07 1277 switch (get_bits(&s->gb, 8)) {
4922a5b0 1278 case 1:
8549cf07
DH
1279 s->rgb = 1;
1280 s->pegasus_rct = 0;
4922a5b0
AJ
1281 break;
1282 case 2:
8549cf07
DH
1283 s->rgb = 1;
1284 s->pegasus_rct = 1;
4922a5b0
AJ
1285 break;
1286 default:
1287 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
1288 }
1289 len -= 9;
1290 goto out;
1291 }
1292
1293 /* Apple MJPEG-A */
8549cf07
DH
1294 if ((s->start_code == APP1) && (len > (0x28 - 8))) {
1295 id = get_bits_long(&s->gb, 32);
1296 id = av_be2ne32(id);
4922a5b0 1297 len -= 4;
8549cf07
DH
1298 /* Apple MJPEG-A */
1299 if (id == AV_RL32("mjpg")) {
4922a5b0
AJ
1300#if 0
1301 skip_bits(&s->gb, 32); /* field size */
1302 skip_bits(&s->gb, 32); /* pad field size */
1303 skip_bits(&s->gb, 32); /* next off */
1304 skip_bits(&s->gb, 32); /* quant off */
1305 skip_bits(&s->gb, 32); /* huff off */
1306 skip_bits(&s->gb, 32); /* image off */
1307 skip_bits(&s->gb, 32); /* scan off */
1308 skip_bits(&s->gb, 32); /* data off */
1309#endif
1310 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1311 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1312 }
1313 }
1314
1315out:
1316 /* slow but needed for extreme adobe jpegs */
1317 if (len < 0)
8549cf07
DH
1318 av_log(s->avctx, AV_LOG_ERROR,
1319 "mjpeg: error, decode_app parser read over the end\n");
1320 while (--len > 0)
4922a5b0
AJ
1321 skip_bits(&s->gb, 8);
1322
1323 return 0;
1324}
1325
1326static int mjpeg_decode_com(MJpegDecodeContext *s)
1327{
1328 int len = get_bits(&s->gb, 16);
3574a85c 1329 if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
4922a5b0
AJ
1330 char *cbuf = av_malloc(len - 1);
1331 if (cbuf) {
1332 int i;
1333 for (i = 0; i < len - 2; i++)
1334 cbuf[i] = get_bits(&s->gb, 8);
8549cf07
DH
1335 if (i > 0 && cbuf[i - 1] == '\n')
1336 cbuf[i - 1] = 0;
4922a5b0
AJ
1337 else
1338 cbuf[i] = 0;
1339
8549cf07 1340 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
4922a5b0
AJ
1341 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
1342
1343 /* buggy avid, it puts EOI only at every 10th frame */
8549cf07 1344 if (!strcmp(cbuf, "AVID")) {
4922a5b0 1345 s->buggy_avid = 1;
8549cf07
DH
1346 } else if (!strcmp(cbuf, "CS=ITU601"))
1347 s->cs_itu601 = 1;
1348 else if ((len > 20 && !strncmp(cbuf, "Intel(R) JPEG Library", 21)) ||
1349 (len > 19 && !strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
643fc8f1 1350 s->flipped = 1;
4922a5b0
AJ
1351
1352 av_free(cbuf);
1353 }
1354 }
1355
1356 return 0;
1357}
1358
4922a5b0
AJ
1359/* return the 8 bit start code value and update the search
1360 state. Return -1 if no start code found */
2b3c8f8a 1361static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
4922a5b0 1362{
2b3c8f8a 1363 const uint8_t *buf_ptr;
4922a5b0
AJ
1364 unsigned int v, v2;
1365 int val;
1366#ifdef DEBUG
8549cf07 1367 int skipped = 0;
4922a5b0
AJ
1368#endif
1369
1370 buf_ptr = *pbuf_ptr;
1371 while (buf_ptr < buf_end) {
8549cf07 1372 v = *buf_ptr++;
4922a5b0
AJ
1373 v2 = *buf_ptr;
1374 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1375 val = *buf_ptr++;
1376 goto found;
1377 }
1378#ifdef DEBUG
1379 skipped++;
1380#endif
1381 }
1382 val = -1;
1383found:
dfd2a005 1384 av_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
4922a5b0
AJ
1385 *pbuf_ptr = buf_ptr;
1386 return val;
1387}
1388
f16055ee 1389int ff_mjpeg_find_marker(MJpegDecodeContext *s,
1390 const uint8_t **buf_ptr, const uint8_t *buf_end,
8549cf07
DH
1391 const uint8_t **unescaped_buf_ptr,
1392 int *unescaped_buf_size)
4922a5b0 1393{
4922a5b0 1394 int start_code;
f16055ee 1395 start_code = find_marker(buf_ptr, buf_end);
4922a5b0 1396
0880503f
RD
1397 av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
1398 if (!s->buffer)
1399 return AVERROR(ENOMEM);
4922a5b0 1400
8549cf07
DH
1401 /* unescape buffer of SOS, use special treatment for JPEG-LS */
1402 if (start_code == SOS && !s->ls) {
1403 const uint8_t *src = *buf_ptr;
1404 uint8_t *dst = s->buffer;
4922a5b0 1405
8549cf07
DH
1406 while (src < buf_end) {
1407 uint8_t x = *(src++);
4922a5b0 1408
8549cf07 1409 *(dst++) = x;
36ef5369 1410 if (s->avctx->codec_id != AV_CODEC_ID_THP) {
8549cf07
DH
1411 if (x == 0xff) {
1412 while (src < buf_end && x == 0xff)
1413 x = *(src++);
4922a5b0 1414
8549cf07
DH
1415 if (x >= 0xd0 && x <= 0xd7)
1416 *(dst++) = x;
1417 else if (x)
1418 break;
4922a5b0 1419 }
8549cf07
DH
1420 }
1421 }
1422 *unescaped_buf_ptr = s->buffer;
1423 *unescaped_buf_size = dst - s->buffer;
6a1aa5cb
JG
1424 memset(s->buffer + *unescaped_buf_size, 0,
1425 FF_INPUT_BUFFER_PADDING_SIZE);
8549cf07
DH
1426
1427 av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
1428 (buf_end - *buf_ptr) - (dst - s->buffer));
1429 } else if (start_code == SOS && s->ls) {
1430 const uint8_t *src = *buf_ptr;
1431 uint8_t *dst = s->buffer;
1432 int bit_count = 0;
1433 int t = 0, b = 0;
1434 PutBitContext pb;
1435
1436 s->cur_scan++;
1437
1438 /* find marker */
1439 while (src + t < buf_end) {
1440 uint8_t x = src[t++];
1441 if (x == 0xff) {
1442 while ((src + t < buf_end) && x == 0xff)
1443 x = src[t++];
1444 if (x & 0x80) {
1445 t -= 2;
1446 break;
f16055ee 1447 }
8549cf07
DH
1448 }
1449 }
1450 bit_count = t * 8;
1451 init_put_bits(&pb, dst, t);
1452
1453 /* unescape bitstream */
1454 while (b < t) {
1455 uint8_t x = src[b++];
1456 put_bits(&pb, 8, x);
1457 if (x == 0xFF) {
1458 x = src[b++];
1459 put_bits(&pb, 7, x);
1460 bit_count--;
1461 }
1462 }
1463 flush_put_bits(&pb);
1464
1465 *unescaped_buf_ptr = dst;
1466 *unescaped_buf_size = (bit_count + 7) >> 3;
6a1aa5cb
JG
1467 memset(s->buffer + *unescaped_buf_size, 0,
1468 FF_INPUT_BUFFER_PADDING_SIZE);
8549cf07
DH
1469 } else {
1470 *unescaped_buf_ptr = *buf_ptr;
1471 *unescaped_buf_size = buf_end - *buf_ptr;
1472 }
f16055ee 1473
1474 return start_code;
1475}
1476
df9b9567 1477int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
8549cf07 1478 AVPacket *avpkt)
f16055ee 1479{
d48132b7 1480 AVFrame *frame = data;
f16055ee 1481 const uint8_t *buf = avpkt->data;
8549cf07 1482 int buf_size = avpkt->size;
f16055ee 1483 MJpegDecodeContext *s = avctx->priv_data;
1484 const uint8_t *buf_end, *buf_ptr;
1485 const uint8_t *unescaped_buf_ptr;
1486 int unescaped_buf_size;
1487 int start_code;
0f64cd1e 1488 int ret = 0;
f16055ee 1489
1490 s->got_picture = 0; // picture from previous image can not be reused
1491 buf_ptr = buf;
1492 buf_end = buf + buf_size;
1493 while (buf_ptr < buf_end) {
1494 /* find start next marker */
1495 start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
8549cf07
DH
1496 &unescaped_buf_ptr,
1497 &unescaped_buf_size);
1498 /* EOF */
1499 if (start_code < 0) {
1500 goto the_end;
6765ee7b
LB
1501 } else if (unescaped_buf_size > INT_MAX / 8) {
1502 av_log(avctx, AV_LOG_ERROR,
1503 "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
1c4717be 1504 start_code, unescaped_buf_size, buf_size);
ab492ca2 1505 return AVERROR_INVALIDDATA;
7520d977 1506 }
8549cf07 1507
7520d977
LB
1508 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n",
1509 start_code, buf_end - buf_ptr);
8549cf07 1510
6765ee7b
LB
1511 ret = init_get_bits(&s->gb, unescaped_buf_ptr,
1512 unescaped_buf_size * 8);
1513 if (ret < 0)
1514 return ret;
8549cf07 1515
7520d977
LB
1516 s->start_code = start_code;
1517 if (s->avctx->debug & FF_DEBUG_STARTCODE)
1518 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
b25e49b1 1519
7520d977
LB
1520 /* process markers */
1521 if (start_code >= 0xd0 && start_code <= 0xd7)
1522 av_log(avctx, AV_LOG_DEBUG,
1523 "restart marker: %d\n", start_code & 0x0f);
1524 /* APP fields */
1525 else if (start_code >= APP0 && start_code <= APP15)
1526 mjpeg_decode_app(s);
1527 /* Comment */
1528 else if (start_code == COM)
1529 mjpeg_decode_com(s);
1530
1531 if (!CONFIG_JPEGLS_DECODER &&
1532 (start_code == SOF48 || start_code == LSE)) {
1533 av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n");
1534 return AVERROR(ENOSYS);
1535 }
050f2b3e 1536
7520d977
LB
1537 switch (start_code) {
1538 case SOI:
1539 s->restart_interval = 0;
1540 s->restart_count = 0;
1541 /* nothing to do on SOI */
1542 break;
1543 case DQT:
1544 ff_mjpeg_decode_dqt(s);
1545 break;
1546 case DHT:
1547 if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
1548 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1549 return ret;
1550 }
1551 break;
1552 case SOF0:
1553 case SOF1:
1554 s->lossless = 0;
1555 s->ls = 0;
1556 s->progressive = 0;
1557 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1558 return ret;
1559 break;
1560 case SOF2:
1561 s->lossless = 0;
1562 s->ls = 0;
1563 s->progressive = 1;
1564 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1565 return ret;
1566 break;
1567 case SOF3:
1568 s->lossless = 1;
1569 s->ls = 0;
1570 s->progressive = 0;
1571 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1572 return ret;
1573 break;
1574 case SOF48:
1575 s->lossless = 1;
1576 s->ls = 1;
1577 s->progressive = 0;
1578 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1579 return ret;
1580 break;
1581 case LSE:
1582 if (!CONFIG_JPEGLS_DECODER ||
1583 (ret = ff_jpegls_decode_lse(s)) < 0)
1584 return ret;
1585 break;
1586 case EOI:
1587 s->cur_scan = 0;
1588 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
8549cf07 1589 break;
7520d977
LB
1590eoi_parser:
1591 if (!s->got_picture) {
1592 av_log(avctx, AV_LOG_WARNING,
1593 "Found EOI before any SOF, ignoring\n");
8549cf07 1594 break;
8549cf07 1595 }
7520d977
LB
1596 if (s->interlaced) {
1597 s->bottom_field ^= 1;
1598 /* if not bottom field, do not output image yet */
1599 if (s->bottom_field == !s->interlace_polarity)
1600 goto not_the_end;
1601 }
d48132b7 1602 if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0)
7520d977 1603 return ret;
d48132b7
AK
1604 if (s->flipped) {
1605 int i;
1606 for (i = 0; frame->data[i]; i++) {
1607 int h = frame->height >> ((i == 1 || i == 2) ?
1608 s->pix_desc->log2_chroma_h : 0);
1609 frame->data[i] += frame->linesize[i] * (h - 1);
1610 frame->linesize[i] *= -1;
1611 }
1612 }
7520d977 1613 *got_frame = 1;
4922a5b0 1614
7520d977
LB
1615 if (!s->lossless &&
1616 avctx->debug & FF_DEBUG_QP) {
1617 av_log(avctx, AV_LOG_DEBUG,
1618 "QP: %d\n", FFMAX3(s->qscale[0],
1619 s->qscale[1],
1620 s->qscale[2]));
1621 }
1622
1623 goto the_end;
1624 case SOS:
1625 if (!s->got_picture) {
1626 av_log(avctx, AV_LOG_WARNING,
1627 "Can not process SOS before SOF, skipping\n");
1628 break;
1629 }
1630 if ((ret = ff_mjpeg_decode_sos(s, NULL, NULL)) < 0 &&
1631 (avctx->err_recognition & AV_EF_EXPLODE))
1632 return ret;
1633 /* buggy avid puts EOI every 10-20th frame */
1634 /* if restart period is over process EOI */
1635 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1636 goto eoi_parser;
1637 break;
1638 case DRI:
1639 mjpeg_decode_dri(s);
1640 break;
1641 case SOF5:
1642 case SOF6:
1643 case SOF7:
1644 case SOF9:
1645 case SOF10:
1646 case SOF11:
1647 case SOF13:
1648 case SOF14:
1649 case SOF15:
1650 case JPG:
1651 av_log(avctx, AV_LOG_ERROR,
1652 "mjpeg: unsupported coding type (%x)\n", start_code);
1653 break;
4922a5b0 1654 }
7520d977
LB
1655
1656not_the_end:
1657 /* eof process start code */
1658 buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
1659 av_log(avctx, AV_LOG_DEBUG,
1660 "marker parser used %d bytes (%d bits)\n",
1661 (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
4922a5b0 1662 }
82c9182f
RD
1663 if (s->got_picture) {
1664 av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
1665 goto eoi_parser;
1666 }
1496b04c 1667 av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
0f64cd1e 1668 return AVERROR_INVALIDDATA;
4922a5b0 1669the_end:
8549cf07
DH
1670 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n",
1671 buf_end - buf_ptr);
1672// return buf_end - buf_ptr;
4922a5b0
AJ
1673 return buf_ptr - buf;
1674}
1675
98a6fff9 1676av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
4922a5b0
AJ
1677{
1678 MJpegDecodeContext *s = avctx->priv_data;
1679 int i, j;
1680
e2274aa5
AK
1681 if (s->picture) {
1682 av_frame_free(&s->picture);
1683 s->picture_ptr = NULL;
1684 } else if (s->picture_ptr)
759001c5 1685 av_frame_unref(s->picture_ptr);
0b7f39c9 1686
4922a5b0 1687 av_free(s->buffer);
ad9feac9 1688 av_freep(&s->ljpeg_buffer);
8549cf07 1689 s->ljpeg_buffer_size = 0;
4922a5b0 1690
8549cf07
DH
1691 for (i = 0; i < 3; i++) {
1692 for (j = 0; j < 4; j++)
e96b4a53 1693 ff_free_vlc(&s->vlcs[i][j]);
4922a5b0 1694 }
8549cf07 1695 for (i = 0; i < MAX_COMPONENTS; i++) {
5fac2776
LM
1696 av_freep(&s->blocks[i]);
1697 av_freep(&s->last_nnz[i]);
1698 }
4922a5b0
AJ
1699 return 0;
1700}
1701
1f0c7020
AK
1702#define OFFSET(x) offsetof(MJpegDecodeContext, x)
1703#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1704static const AVOption options[] = {
8549cf07 1705 { "extern_huff", "Use external huffman table.",
e6153f17 1706 OFFSET(extern_huff), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
1f0c7020
AK
1707 { NULL },
1708};
1709
1710static const AVClass mjpegdec_class = {
1711 .class_name = "MJPEG decoder",
1712 .item_name = av_default_item_name,
1713 .option = options,
1714 .version = LIBAVUTIL_VERSION_INT,
1715};
1716
d36beb3f 1717AVCodec ff_mjpeg_decoder = {
ec6402b7 1718 .name = "mjpeg",
b2bed932 1719 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
ec6402b7 1720 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 1721 .id = AV_CODEC_ID_MJPEG,
ec6402b7
AK
1722 .priv_data_size = sizeof(MJpegDecodeContext),
1723 .init = ff_mjpeg_decode_init,
1724 .close = ff_mjpeg_decode_end,
1725 .decode = ff_mjpeg_decode_frame,
1726 .capabilities = CODEC_CAP_DR1,
1f0c7020 1727 .priv_class = &mjpegdec_class,
5a0e953c 1728 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
4922a5b0
AJ
1729};
1730
d36beb3f 1731AVCodec ff_thp_decoder = {
ec6402b7 1732 .name = "thp",
b2bed932 1733 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
ec6402b7 1734 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 1735 .id = AV_CODEC_ID_THP,
ec6402b7
AK
1736 .priv_data_size = sizeof(MJpegDecodeContext),
1737 .init = ff_mjpeg_decode_init,
1738 .close = ff_mjpeg_decode_end,
1739 .decode = ff_mjpeg_decode_frame,
1740 .capabilities = CODEC_CAP_DR1,
5a0e953c 1741 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
4922a5b0 1742};