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