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