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