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