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