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