FFmpeg  2.1.1
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_ptr = &s->picture;
93 
94  s->avctx = avctx;
95  ff_hpeldsp_init(&s->hdsp, avctx->flags);
96  ff_dsputil_init(&s->dsp, avctx);
98  s->buffer_size = 0;
99  s->buffer = NULL;
100  s->start_code = -1;
101  s->first_picture = 1;
102  s->got_picture = 0;
103  s->org_height = avctx->coded_height;
105 
107 
108  if (s->extern_huff) {
109  av_log(avctx, AV_LOG_INFO, "using external huffman table\n");
110  init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
111  if (ff_mjpeg_decode_dht(s)) {
112  av_log(avctx, AV_LOG_ERROR,
113  "error using external huffman table, switching back to internal\n");
115  }
116  }
117  if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
118  s->interlace_polarity = 1; /* bottom field first */
119  av_log(avctx, AV_LOG_DEBUG, "bottom field first\n");
120  }
121  if (avctx->codec->id == AV_CODEC_ID_AMV)
122  s->flipped = 1;
123 
124  return 0;
125 }
126 
127 
128 /* quantize tables */
130 {
131  int len, index, i, j;
132 
133  len = get_bits(&s->gb, 16) - 2;
134 
135  while (len >= 65) {
136  int pr = get_bits(&s->gb, 4);
137  if (pr > 1) {
138  av_log(s->avctx, AV_LOG_ERROR, "dqt: invalid precision\n");
139  return AVERROR_INVALIDDATA;
140  }
141  index = get_bits(&s->gb, 4);
142  if (index >= 4)
143  return -1;
144  av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
145  /* read quant table */
146  for (i = 0; i < 64; i++) {
147  j = s->scantable.permutated[i];
148  s->quant_matrixes[index][j] = get_bits(&s->gb, pr ? 16 : 8);
149  }
150 
151  // XXX FIXME finetune, and perhaps add dc too
152  s->qscale[index] = FFMAX(s->quant_matrixes[index][s->scantable.permutated[1]],
153  s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
154  av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n",
155  index, s->qscale[index]);
156  len -= 65;
157  }
158  return 0;
159 }
160 
161 /* decode huffman tables and build VLC decoders */
163 {
164  int len, index, i, class, n, v, code_max;
165  uint8_t bits_table[17];
166  uint8_t val_table[256];
167  int ret = 0;
168 
169  len = get_bits(&s->gb, 16) - 2;
170 
171  while (len > 0) {
172  if (len < 17)
173  return AVERROR_INVALIDDATA;
174  class = get_bits(&s->gb, 4);
175  if (class >= 2)
176  return AVERROR_INVALIDDATA;
177  index = get_bits(&s->gb, 4);
178  if (index >= 4)
179  return AVERROR_INVALIDDATA;
180  n = 0;
181  for (i = 1; i <= 16; i++) {
182  bits_table[i] = get_bits(&s->gb, 8);
183  n += bits_table[i];
184  }
185  len -= 17;
186  if (len < n || n > 256)
187  return AVERROR_INVALIDDATA;
188 
189  code_max = 0;
190  for (i = 0; i < n; i++) {
191  v = get_bits(&s->gb, 8);
192  if (v > code_max)
193  code_max = v;
194  val_table[i] = v;
195  }
196  len -= n;
197 
198  /* build VLC and flush previous vlc if present */
199  ff_free_vlc(&s->vlcs[class][index]);
200  av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
201  class, index, code_max + 1);
202  if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table,
203  code_max + 1, 0, class > 0)) < 0)
204  return ret;
205 
206  if (class > 0) {
207  ff_free_vlc(&s->vlcs[2][index]);
208  if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table,
209  code_max + 1, 0, 0)) < 0)
210  return ret;
211  }
212  }
213  return 0;
214 }
215 
217 {
218  int len, nb_components, i, width, height, pix_fmt_id;
219  int h_count[MAX_COMPONENTS];
220  int v_count[MAX_COMPONENTS];
221 
222  s->cur_scan = 0;
223  s->upscale_h = s->upscale_v = 0;
224 
225  /* XXX: verify len field validity */
226  len = get_bits(&s->gb, 16);
228  s->bits = get_bits(&s->gb, 8);
229 
230  if (s->pegasus_rct)
231  s->bits = 9;
232  if (s->bits == 9 && !s->pegasus_rct)
233  s->rct = 1; // FIXME ugly
234 
235  if(s->lossless && s->avctx->lowres){
236  av_log(s->avctx, AV_LOG_ERROR, "lowres is not possible with lossless jpeg\n");
237  return -1;
238  }
239 
240  height = get_bits(&s->gb, 16);
241  width = get_bits(&s->gb, 16);
242 
243  // HACK for odd_height.mov
244  if (s->interlaced && s->width == width && s->height == height + 1)
245  height= s->height;
246 
247  av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
248  if (av_image_check_size(width, height, 0, s->avctx))
249  return AVERROR_INVALIDDATA;
250 
251  nb_components = get_bits(&s->gb, 8);
252  if (nb_components <= 0 ||
253  nb_components > MAX_COMPONENTS)
254  return -1;
255  if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
256  if (nb_components != s->nb_components) {
258  "nb_components changing in interlaced picture\n");
259  return AVERROR_INVALIDDATA;
260  }
261  }
262  if (s->ls && !(s->bits <= 8 || nb_components == 1)) {
264  "JPEG-LS that is not <= 8 "
265  "bits/component or 16-bit gray");
266  return AVERROR_PATCHWELCOME;
267  }
268  s->nb_components = nb_components;
269  s->h_max = 1;
270  s->v_max = 1;
271  memset(h_count, 0, sizeof(h_count));
272  memset(v_count, 0, sizeof(v_count));
273  for (i = 0; i < nb_components; i++) {
274  /* component id */
275  s->component_id[i] = get_bits(&s->gb, 8) - 1;
276  h_count[i] = get_bits(&s->gb, 4);
277  v_count[i] = get_bits(&s->gb, 4);
278  /* compute hmax and vmax (only used in interleaved case) */
279  if (h_count[i] > s->h_max)
280  s->h_max = h_count[i];
281  if (v_count[i] > s->v_max)
282  s->v_max = v_count[i];
283  s->quant_index[i] = get_bits(&s->gb, 8);
284  if (s->quant_index[i] >= 4) {
285  av_log(s->avctx, AV_LOG_ERROR, "quant_index is invalid\n");
286  return AVERROR_INVALIDDATA;
287  }
288  if (!h_count[i] || !v_count[i]) {
290  "Invalid sampling factor in component %d %d:%d\n",
291  i, h_count[i], v_count[i]);
292  return AVERROR_INVALIDDATA;
293  }
294 
295  av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
296  i, h_count[i], v_count[i],
297  s->component_id[i], s->quant_index[i]);
298  }
299 
300  if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
301  avpriv_report_missing_feature(s->avctx, "Subsampling in JPEG-LS");
302  return AVERROR_PATCHWELCOME;
303  }
304 
305 
306  /* if different size, realloc/alloc picture */
307  if ( width != s->width || height != s->height
308  || memcmp(s->h_count, h_count, sizeof(h_count))
309  || memcmp(s->v_count, v_count, sizeof(v_count))) {
310 
311  s->width = width;
312  s->height = height;
313  memcpy(s->h_count, h_count, sizeof(h_count));
314  memcpy(s->v_count, v_count, sizeof(v_count));
315  s->interlaced = 0;
316  s->got_picture = 0;
317 
318  /* test interlaced mode */
319  if (s->first_picture &&
320  s->org_height != 0 &&
321  s->height < ((s->org_height * 3) / 4)) {
322  s->interlaced = 1;
326  height *= 2;
327  }
328 
329  avcodec_set_dimensions(s->avctx, width, height);
330 
331  s->first_picture = 0;
332  }
333 
334  if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
335  if (s->progressive) {
336  avpriv_request_sample(s->avctx, "progressively coded interlaced picture");
337  return AVERROR_INVALIDDATA;
338  }
339  } else{
340  if (s->v_max == 1 && s->h_max == 1 && s->lossless==1 && (nb_components==3 || nb_components==4))
341  s->rgb = 1;
342  else if (!s->lossless)
343  s->rgb = 0;
344  /* XXX: not complete test ! */
345  pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
346  (s->h_count[1] << 20) | (s->v_count[1] << 16) |
347  (s->h_count[2] << 12) | (s->v_count[2] << 8) |
348  (s->h_count[3] << 4) | s->v_count[3];
349  av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
350  /* NOTE we do not allocate pictures large enough for the possible
351  * padding of h/v_count being 4 */
352  if (!(pix_fmt_id & 0xD0D0D0D0))
353  pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
354  if (!(pix_fmt_id & 0x0D0D0D0D))
355  pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
356 
357  switch (pix_fmt_id) {
358  case 0x11111100:
359  if (s->rgb)
361  else {
362  if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') {
364  } else {
368  }
369  }
370  av_assert0(s->nb_components == 3);
371  break;
372  case 0x11111111:
373  if (s->rgb)
375  else {
378  }
379  av_assert0(s->nb_components == 4);
380  break;
381  case 0x12121100:
382  case 0x22122100:
384  else
385  goto unk_pixfmt;
387  s->upscale_v = 2;
388  s->upscale_h = (pix_fmt_id == 0x22122100);
389  s->chroma_height = s->height;
390  break;
391  case 0x21211100:
392  case 0x22211200:
394  else
395  goto unk_pixfmt;
397  s->upscale_v = (pix_fmt_id == 0x22211200);
398  s->upscale_h = 2;
399  s->chroma_height = s->height;
400  break;
401  case 0x22221100:
403  else
404  goto unk_pixfmt;
406  s->upscale_v = 2;
407  s->upscale_h = 2;
408  s->chroma_height = s->height / 2;
409  break;
410  case 0x11000000:
411  case 0x13000000:
412  case 0x14000000:
413  case 0x31000000:
414  case 0x33000000:
415  case 0x34000000:
416  case 0x41000000:
417  case 0x43000000:
418  case 0x44000000:
419  if(s->bits <= 8)
421  else
423  break;
424  case 0x12111100:
425  case 0x22211100:
426  case 0x22112100:
428  else
429  goto unk_pixfmt;
431  s->upscale_h = (pix_fmt_id == 0x22211100) * 2 + (pix_fmt_id == 0x22112100);
432  s->chroma_height = s->height / 2;
433  break;
434  case 0x21111100:
438  break;
439  case 0x22121100:
440  case 0x22111200:
442  else
443  goto unk_pixfmt;
445  s->upscale_v = (pix_fmt_id == 0x22121100) + 1;
446  break;
447  case 0x22111100:
451  break;
452  case 0x41111100:
454  else
455  goto unk_pixfmt;
457  break;
458  default:
459 unk_pixfmt:
460  av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
461  return AVERROR_PATCHWELCOME;
462  }
463  if ((s->upscale_h || s->upscale_v) && s->avctx->lowres) {
464  av_log(s->avctx, AV_LOG_ERROR, "lowres not supported for weird subsampling\n");
465  return AVERROR_PATCHWELCOME;
466  }
467  if (s->ls) {
468  s->upscale_h = s->upscale_v = 0;
469  if (s->nb_components > 1)
471  else if (s->bits <= 8)
473  else
475  }
476 
479  return -1;
481  s->picture_ptr->key_frame = 1;
482  s->got_picture = 1;
483 
484  for (i = 0; i < 3; i++)
485  s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
486 
487  av_dlog(s->avctx, "%d %d %d %d %d %d\n",
488  s->width, s->height, s->linesize[0], s->linesize[1],
489  s->interlaced, s->avctx->height);
490 
491  if (len != (8 + (3 * nb_components)))
492  av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
493  }
494 
495  if (s->rgb && !s->lossless && !s->ls) {
496  av_log(s->avctx, AV_LOG_ERROR, "Unsupported coding and pixel format combination\n");
497  return AVERROR_PATCHWELCOME;
498  }
499 
500  /* totally blank picture as progressive JPEG will only add details to it */
501  if (s->progressive) {
502  int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8);
503  int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
504  for (i = 0; i < s->nb_components; i++) {
505  int size = bw * bh * s->h_count[i] * s->v_count[i];
506  av_freep(&s->blocks[i]);
507  av_freep(&s->last_nnz[i]);
508  s->blocks[i] = av_malloc(size * sizeof(**s->blocks));
509  s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz));
510  if (!s->blocks[i] || !s->last_nnz[i])
511  return AVERROR(ENOMEM);
512  s->block_stride[i] = bw * s->h_count[i];
513  }
514  memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
515  }
516  return 0;
517 }
518 
519 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
520 {
521  int code;
522  code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
523  if (code < 0 || code > 16) {
525  "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
526  0, dc_index, &s->vlcs[0][dc_index]);
527  return 0xfffff;
528  }
529 
530  if (code)
531  return get_xbits(&s->gb, code);
532  else
533  return 0;
534 }
535 
536 /* decode block and dequantize */
537 static int decode_block(MJpegDecodeContext *s, int16_t *block, int component,
538  int dc_index, int ac_index, int16_t *quant_matrix)
539 {
540  int code, i, j, level, val;
541 
542  /* DC coef */
543  val = mjpeg_decode_dc(s, dc_index);
544  if (val == 0xfffff) {
545  av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
546  return AVERROR_INVALIDDATA;
547  }
548  val = val * quant_matrix[0] + s->last_dc[component];
549  s->last_dc[component] = val;
550  block[0] = val;
551  /* AC coefs */
552  i = 0;
553  {OPEN_READER(re, &s->gb);
554  do {
555  UPDATE_CACHE(re, &s->gb);
556  GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
557 
558  i += ((unsigned)code) >> 4;
559  code &= 0xf;
560  if (code) {
561  if (code > MIN_CACHE_BITS - 16)
562  UPDATE_CACHE(re, &s->gb);
563 
564  {
565  int cache = GET_CACHE(re, &s->gb);
566  int sign = (~cache) >> 31;
567  level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
568  }
569 
570  LAST_SKIP_BITS(re, &s->gb, code);
571 
572  if (i > 63) {
573  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
574  return AVERROR_INVALIDDATA;
575  }
576  j = s->scantable.permutated[i];
577  block[j] = level * quant_matrix[j];
578  }
579  } while (i < 63);
580  CLOSE_READER(re, &s->gb);}
581 
582  return 0;
583 }
584 
586  int component, int dc_index,
587  int16_t *quant_matrix, int Al)
588 {
589  int val;
590  s->dsp.clear_block(block);
591  val = mjpeg_decode_dc(s, dc_index);
592  if (val == 0xfffff) {
593  av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
594  return AVERROR_INVALIDDATA;
595  }
596  val = (val * quant_matrix[0] << Al) + s->last_dc[component];
597  s->last_dc[component] = val;
598  block[0] = val;
599  return 0;
600 }
601 
602 /* decode block and dequantize - progressive JPEG version */
604  uint8_t *last_nnz, int ac_index,
605  int16_t *quant_matrix,
606  int ss, int se, int Al, int *EOBRUN)
607 {
608  int code, i, j, level, val, run;
609 
610  if (*EOBRUN) {
611  (*EOBRUN)--;
612  return 0;
613  }
614 
615  {
616  OPEN_READER(re, &s->gb);
617  for (i = ss; ; i++) {
618  UPDATE_CACHE(re, &s->gb);
619  GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
620 
621  run = ((unsigned) code) >> 4;
622  code &= 0xF;
623  if (code) {
624  i += run;
625  if (code > MIN_CACHE_BITS - 16)
626  UPDATE_CACHE(re, &s->gb);
627 
628  {
629  int cache = GET_CACHE(re, &s->gb);
630  int sign = (~cache) >> 31;
631  level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
632  }
633 
634  LAST_SKIP_BITS(re, &s->gb, code);
635 
636  if (i >= se) {
637  if (i == se) {
638  j = s->scantable.permutated[se];
639  block[j] = level * quant_matrix[j] << Al;
640  break;
641  }
642  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
643  return AVERROR_INVALIDDATA;
644  }
645  j = s->scantable.permutated[i];
646  block[j] = level * quant_matrix[j] << Al;
647  } else {
648  if (run == 0xF) {// ZRL - skip 15 coefficients
649  i += 15;
650  if (i >= se) {
651  av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
652  return AVERROR_INVALIDDATA;
653  }
654  } else {
655  val = (1 << run);
656  if (run) {
657  UPDATE_CACHE(re, &s->gb);
658  val += NEG_USR32(GET_CACHE(re, &s->gb), run);
659  LAST_SKIP_BITS(re, &s->gb, run);
660  }
661  *EOBRUN = val - 1;
662  break;
663  }
664  }
665  }
666  CLOSE_READER(re, &s->gb);
667  }
668 
669  if (i > *last_nnz)
670  *last_nnz = i;
671 
672  return 0;
673 }
674 
675 #define REFINE_BIT(j) { \
676  UPDATE_CACHE(re, &s->gb); \
677  sign = block[j] >> 15; \
678  block[j] += SHOW_UBITS(re, &s->gb, 1) * \
679  ((quant_matrix[j] ^ sign) - sign) << Al; \
680  LAST_SKIP_BITS(re, &s->gb, 1); \
681 }
682 
683 #define ZERO_RUN \
684 for (; ; i++) { \
685  if (i > last) { \
686  i += run; \
687  if (i > se) { \
688  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
689  return -1; \
690  } \
691  break; \
692  } \
693  j = s->scantable.permutated[i]; \
694  if (block[j]) \
695  REFINE_BIT(j) \
696  else if (run-- == 0) \
697  break; \
698 }
699 
700 /* decode block and dequantize - progressive JPEG refinement pass */
702  uint8_t *last_nnz,
703  int ac_index, int16_t *quant_matrix,
704  int ss, int se, int Al, int *EOBRUN)
705 {
706  int code, i = ss, j, sign, val, run;
707  int last = FFMIN(se, *last_nnz);
708 
709  OPEN_READER(re, &s->gb);
710  if (*EOBRUN) {
711  (*EOBRUN)--;
712  } else {
713  for (; ; i++) {
714  UPDATE_CACHE(re, &s->gb);
715  GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
716 
717  if (code & 0xF) {
718  run = ((unsigned) code) >> 4;
719  UPDATE_CACHE(re, &s->gb);
720  val = SHOW_UBITS(re, &s->gb, 1);
721  LAST_SKIP_BITS(re, &s->gb, 1);
722  ZERO_RUN;
723  j = s->scantable.permutated[i];
724  val--;
725  block[j] = ((quant_matrix[j]^val) - val) << Al;
726  if (i == se) {
727  if (i > *last_nnz)
728  *last_nnz = i;
729  CLOSE_READER(re, &s->gb);
730  return 0;
731  }
732  } else {
733  run = ((unsigned) code) >> 4;
734  if (run == 0xF) {
735  ZERO_RUN;
736  } else {
737  val = run;
738  run = (1 << run);
739  if (val) {
740  UPDATE_CACHE(re, &s->gb);
741  run += SHOW_UBITS(re, &s->gb, val);
742  LAST_SKIP_BITS(re, &s->gb, val);
743  }
744  *EOBRUN = run - 1;
745  break;
746  }
747  }
748  }
749 
750  if (i > *last_nnz)
751  *last_nnz = i;
752  }
753 
754  for (; i <= last; i++) {
755  j = s->scantable.permutated[i];
756  if (block[j])
757  REFINE_BIT(j)
758  }
759  CLOSE_READER(re, &s->gb);
760 
761  return 0;
762 }
763 #undef REFINE_BIT
764 #undef ZERO_RUN
765 
766 static int handle_rstn(MJpegDecodeContext *s, int nb_components)
767 {
768  int i;
769  int reset = 0;
770 
771  if (s->restart_interval) {
772  s->restart_count--;
773  if(s->restart_count == 0 && s->avctx->codec_id == AV_CODEC_ID_THP){
774  align_get_bits(&s->gb);
775  for (i = 0; i < nb_components; i++) /* reset dc */
776  s->last_dc[i] = (4 << s->bits);
777  }
778 
779  i = 8 + ((-get_bits_count(&s->gb)) & 7);
780  /* skip RSTn */
781  if (s->restart_count == 0) {
782  if( show_bits(&s->gb, i) == (1 << i) - 1
783  || show_bits(&s->gb, i) == 0xFF) {
784  int pos = get_bits_count(&s->gb);
785  align_get_bits(&s->gb);
786  while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
787  skip_bits(&s->gb, 8);
788  if (get_bits_left(&s->gb) >= 8 && (get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
789  for (i = 0; i < nb_components; i++) /* reset dc */
790  s->last_dc[i] = (4 << s->bits);
791  reset = 1;
792  } else
793  skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
794  }
795  }
796  }
797  return reset;
798 }
799 
800 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform)
801 {
802  int i, mb_x, mb_y;
803  uint16_t (*buffer)[4];
804  int left[4], top[4], topleft[4];
805  const int linesize = s->linesize[0];
806  const int mask = ((1 << s->bits) - 1) << point_transform;
807  int resync_mb_y = 0;
808  int resync_mb_x = 0;
809 
810  if (s->nb_components != 3 && s->nb_components != 4)
811  return AVERROR_INVALIDDATA;
812  if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
813  return AVERROR_INVALIDDATA;
814 
815 
817 
819  (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
820  buffer = s->ljpeg_buffer;
821 
822  for (i = 0; i < 4; i++)
823  buffer[0][i] = 1 << (s->bits - 1);
824 
825  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
826  uint8_t *ptr = s->picture.data[0] + (linesize * mb_y);
827 
828  if (s->interlaced && s->bottom_field)
829  ptr += linesize >> 1;
830 
831  for (i = 0; i < 4; i++)
832  top[i] = left[i] = topleft[i] = buffer[0][i];
833 
834  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
835  int modified_predictor = predictor;
836 
837  if (s->restart_interval && !s->restart_count){
839  resync_mb_x = mb_x;
840  resync_mb_y = mb_y;
841  for(i=0; i<4; i++)
842  top[i] = left[i]= topleft[i]= 1 << (s->bits - 1);
843  }
844  if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
845  modified_predictor = 1;
846 
847  for (i=0;i<nb_components;i++) {
848  int pred, dc;
849 
850  topleft[i] = top[i];
851  top[i] = buffer[mb_x][i];
852 
853  PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
854 
855  dc = mjpeg_decode_dc(s, s->dc_index[i]);
856  if(dc == 0xFFFFF)
857  return -1;
858 
859  left[i] = buffer[mb_x][i] =
860  mask & (pred + (dc << point_transform));
861  }
862 
863  if (s->restart_interval && !--s->restart_count) {
864  align_get_bits(&s->gb);
865  skip_bits(&s->gb, 16); /* skip RSTn */
866  }
867  }
868  if (s->nb_components == 4) {
869  for(i=0; i<nb_components; i++) {
870  int c= s->comp_index[i];
871  if (s->bits <= 8) {
872  for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
873  ptr[4*mb_x+3-c] = buffer[mb_x][i];
874  }
875  } else if(s->bits == 9) {
876  return AVERROR_PATCHWELCOME;
877  } else {
878  for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
879  ((uint16_t*)ptr)[4*mb_x+c] = buffer[mb_x][i];
880  }
881  }
882  }
883  } else if (s->rct) {
884  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
885  ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
886  ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
887  ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
888  }
889  } else if (s->pegasus_rct) {
890  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
891  ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
892  ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
893  ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
894  }
895  } else {
896  for(i=0; i<nb_components; i++) {
897  int c= s->comp_index[i];
898  if (s->bits <= 8) {
899  for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
900  ptr[3*mb_x+2-c] = buffer[mb_x][i];
901  }
902  } else if(s->bits == 9) {
903  return AVERROR_PATCHWELCOME;
904  } else {
905  for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
906  ((uint16_t*)ptr)[3*mb_x+2-c] = buffer[mb_x][i];
907  }
908  }
909  }
910  }
911  }
912  return 0;
913 }
914 
916  int point_transform, int nb_components)
917 {
918  int i, mb_x, mb_y, mask;
919  int bits= (s->bits+7)&~7;
920  int resync_mb_y = 0;
921  int resync_mb_x = 0;
922 
923  point_transform += bits - s->bits;
924  mask = ((1 << s->bits) - 1) << point_transform;
925 
926  av_assert0(nb_components>=1 && nb_components<=4);
927 
928  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
929  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
930  if (s->restart_interval && !s->restart_count){
932  resync_mb_x = mb_x;
933  resync_mb_y = mb_y;
934  }
935 
936  if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){
937  int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
938  int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
939  for (i = 0; i < nb_components; i++) {
940  uint8_t *ptr;
941  uint16_t *ptr16;
942  int n, h, v, x, y, c, j, linesize;
943  n = s->nb_blocks[i];
944  c = s->comp_index[i];
945  h = s->h_scount[i];
946  v = s->v_scount[i];
947  x = 0;
948  y = 0;
949  linesize= s->linesize[c];
950 
951  if(bits>8) linesize /= 2;
952 
953  for(j=0; j<n; j++) {
954  int pred, dc;
955 
956  dc = mjpeg_decode_dc(s, s->dc_index[i]);
957  if(dc == 0xFFFFF)
958  return -1;
959  if(bits<=8){
960  ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
961  if(y==0 && toprow){
962  if(x==0 && leftcol){
963  pred= 1 << (bits - 1);
964  }else{
965  pred= ptr[-1];
966  }
967  }else{
968  if(x==0 && leftcol){
969  pred= ptr[-linesize];
970  }else{
971  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
972  }
973  }
974 
975  if (s->interlaced && s->bottom_field)
976  ptr += linesize >> 1;
977  pred &= mask;
978  *ptr= pred + (dc << point_transform);
979  }else{
980  ptr16 = (uint16_t*)(s->picture.data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
981  if(y==0 && toprow){
982  if(x==0 && leftcol){
983  pred= 1 << (bits - 1);
984  }else{
985  pred= ptr16[-1];
986  }
987  }else{
988  if(x==0 && leftcol){
989  pred= ptr16[-linesize];
990  }else{
991  PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
992  }
993  }
994 
995  if (s->interlaced && s->bottom_field)
996  ptr16 += linesize >> 1;
997  pred &= mask;
998  *ptr16= pred + (dc << point_transform);
999  }
1000  if (++x == h) {
1001  x = 0;
1002  y++;
1003  }
1004  }
1005  }
1006  } else {
1007  for (i = 0; i < nb_components; i++) {
1008  uint8_t *ptr;
1009  uint16_t *ptr16;
1010  int n, h, v, x, y, c, j, linesize, dc;
1011  n = s->nb_blocks[i];
1012  c = s->comp_index[i];
1013  h = s->h_scount[i];
1014  v = s->v_scount[i];
1015  x = 0;
1016  y = 0;
1017  linesize = s->linesize[c];
1018 
1019  if(bits>8) linesize /= 2;
1020 
1021  for (j = 0; j < n; j++) {
1022  int pred;
1023 
1024  dc = mjpeg_decode_dc(s, s->dc_index[i]);
1025  if(dc == 0xFFFFF)
1026  return -1;
1027  if(bits<=8){
1028  ptr = s->picture.data[c] +
1029  (linesize * (v * mb_y + y)) +
1030  (h * mb_x + x); //FIXME optimize this crap
1031  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1032 
1033  pred &= mask;
1034  *ptr = pred + (dc << point_transform);
1035  }else{
1036  ptr16 = (uint16_t*)(s->picture.data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
1037  PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1038 
1039  pred &= mask;
1040  *ptr16= pred + (dc << point_transform);
1041  }
1042 
1043  if (++x == h) {
1044  x = 0;
1045  y++;
1046  }
1047  }
1048  }
1049  }
1050  if (s->restart_interval && !--s->restart_count) {
1051  align_get_bits(&s->gb);
1052  skip_bits(&s->gb, 16); /* skip RSTn */
1053  }
1054  }
1055  }
1056  return 0;
1057 }
1058 
1060  uint8_t *dst, const uint8_t *src,
1061  int linesize, int lowres)
1062 {
1063  switch (lowres) {
1064  case 0: s->hdsp.put_pixels_tab[1][0](dst, src, linesize, 8);
1065  break;
1066  case 1: copy_block4(dst, src, linesize, linesize, 4);
1067  break;
1068  case 2: copy_block2(dst, src, linesize, linesize, 2);
1069  break;
1070  case 3: *dst = *src;
1071  break;
1072  }
1073 }
1074 
1075 static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
1076 {
1077  int block_x, block_y;
1078  int size = 8 >> s->avctx->lowres;
1079  if (s->bits > 8) {
1080  for (block_y=0; block_y<size; block_y++)
1081  for (block_x=0; block_x<size; block_x++)
1082  *(uint16_t*)(ptr + 2*block_x + block_y*linesize) <<= 16 - s->bits;
1083  } else {
1084  for (block_y=0; block_y<size; block_y++)
1085  for (block_x=0; block_x<size; block_x++)
1086  *(ptr + block_x + block_y*linesize) <<= 8 - s->bits;
1087  }
1088 }
1089 
1090 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
1091  int Al, const uint8_t *mb_bitmask,
1092  const AVFrame *reference)
1093 {
1094  int i, mb_x, mb_y;
1096  const uint8_t *reference_data[MAX_COMPONENTS];
1097  int linesize[MAX_COMPONENTS];
1098  GetBitContext mb_bitmask_gb;
1099  int bytes_per_pixel = 1 + (s->bits > 8);
1100 
1101  if (mb_bitmask)
1102  init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
1103 
1104  if (s->flipped && s->avctx->lowres) {
1105  av_log(s->avctx, AV_LOG_ERROR, "Can not flip image with lowres\n");
1106  s->flipped = 0;
1107  }
1108  s->restart_count = 0;
1109  for (i = 0; i < nb_components; i++) {
1110  int c = s->comp_index[i];
1111  data[c] = s->picture_ptr->data[c];
1112  reference_data[c] = reference ? reference->data[c] : NULL;
1113  linesize[c] = s->linesize[c];
1114  s->coefs_finished[c] |= 1;
1115  if (s->flipped && !(s->avctx->flags & CODEC_FLAG_EMU_EDGE)) {
1116  // picture should be flipped upside-down for this codec
1117  int offset = (linesize[c] * (s->v_scount[i] *
1118  (8 * s->mb_height - ((s->height / s->v_max) & 7)) - 1));
1119  data[c] += offset;
1120  reference_data[c] += offset;
1121  linesize[c] *= -1;
1122  }
1123  }
1124 
1125  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1126  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1127  const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
1128 
1129  if (s->restart_interval && !s->restart_count)
1131 
1132  if (get_bits_left(&s->gb) < 0) {
1133  av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
1134  -get_bits_left(&s->gb));
1135  return AVERROR_INVALIDDATA;
1136  }
1137  for (i = 0; i < nb_components; i++) {
1138  uint8_t *ptr;
1139  int n, h, v, x, y, c, j;
1140  int block_offset;
1141  n = s->nb_blocks[i];
1142  c = s->comp_index[i];
1143  h = s->h_scount[i];
1144  v = s->v_scount[i];
1145  x = 0;
1146  y = 0;
1147  for (j = 0; j < n; j++) {
1148  block_offset = (((linesize[c] * (v * mb_y + y) * 8) +
1149  (h * mb_x + x) * 8 * bytes_per_pixel) >> s->avctx->lowres);
1150 
1151  if (s->interlaced && s->bottom_field)
1152  block_offset += linesize[c] >> 1;
1153  ptr = data[c] + block_offset;
1154  if (!s->progressive) {
1155  if (copy_mb)
1156  mjpeg_copy_block(s, ptr, reference_data[c] + block_offset,
1157  linesize[c], s->avctx->lowres);
1158 
1159  else {
1160  s->dsp.clear_block(s->block);
1161  if (decode_block(s, s->block, i,
1162  s->dc_index[i], s->ac_index[i],
1163  s->quant_matrixes[s->quant_sindex[i]]) < 0) {
1165  "error y=%d x=%d\n", mb_y, mb_x);
1166  return AVERROR_INVALIDDATA;
1167  }
1168  s->dsp.idct_put(ptr, linesize[c], s->block);
1169  if (s->bits & 7)
1170  shift_output(s, ptr, linesize[c]);
1171  }
1172  } else {
1173  int block_idx = s->block_stride[c] * (v * mb_y + y) +
1174  (h * mb_x + x);
1175  int16_t *block = s->blocks[c][block_idx];
1176  if (Ah)
1177  block[0] += get_bits1(&s->gb) *
1178  s->quant_matrixes[s->quant_sindex[i]][0] << Al;
1179  else if (decode_dc_progressive(s, block, i, s->dc_index[i],
1180  s->quant_matrixes[s->quant_sindex[i]],
1181  Al) < 0) {
1183  "error y=%d x=%d\n", mb_y, mb_x);
1184  return AVERROR_INVALIDDATA;
1185  }
1186  }
1187  av_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
1188  av_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
1189  mb_x, mb_y, x, y, c, s->bottom_field,
1190  (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1191  if (++x == h) {
1192  x = 0;
1193  y++;
1194  }
1195  }
1196  }
1197 
1198  handle_rstn(s, nb_components);
1199  }
1200  }
1201  return 0;
1202 }
1203 
1205  int se, int Ah, int Al)
1206 {
1207  int mb_x, mb_y;
1208  int EOBRUN = 0;
1209  int c = s->comp_index[0];
1210  uint8_t *data = s->picture.data[c];
1211  int linesize = s->linesize[c];
1212  int last_scan = 0;
1213  int16_t *quant_matrix = s->quant_matrixes[s->quant_sindex[0]];
1214  int bytes_per_pixel = 1 + (s->bits > 8);
1215 
1216  av_assert0(ss>=0 && Ah>=0 && Al>=0);
1217  if (se < ss || se > 63) {
1218  av_log(s->avctx, AV_LOG_ERROR, "SS/SE %d/%d is invalid\n", ss, se);
1219  return AVERROR_INVALIDDATA;
1220  }
1221 
1222  if (!Al) {
1223  s->coefs_finished[c] |= (1LL << (se + 1)) - (1LL << ss);
1224  last_scan = !~s->coefs_finished[c];
1225  }
1226 
1227  if (s->interlaced && s->bottom_field)
1228  data += linesize >> 1;
1229 
1230  s->restart_count = 0;
1231 
1232  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1233  uint8_t *ptr = data + (mb_y * linesize * 8 >> s->avctx->lowres);
1234  int block_idx = mb_y * s->block_stride[c];
1235  int16_t (*block)[64] = &s->blocks[c][block_idx];
1236  uint8_t *last_nnz = &s->last_nnz[c][block_idx];
1237  for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
1238  int ret;
1239  if (s->restart_interval && !s->restart_count)
1241 
1242  if (Ah)
1243  ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
1244  quant_matrix, ss, se, Al, &EOBRUN);
1245  else
1246  ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
1247  quant_matrix, ss, se, Al, &EOBRUN);
1248  if (ret < 0) {
1250  "error y=%d x=%d\n", mb_y, mb_x);
1251  return AVERROR_INVALIDDATA;
1252  }
1253 
1254  if (last_scan) {
1255  s->dsp.idct_put(ptr, linesize, *block);
1256  if (s->bits & 7)
1257  shift_output(s, ptr, linesize);
1258  ptr += bytes_per_pixel*8 >> s->avctx->lowres;
1259  }
1260  if (handle_rstn(s, 0))
1261  EOBRUN = 0;
1262  }
1263  }
1264  return 0;
1265 }
1266 
1268  const AVFrame *reference)
1269 {
1270  int len, nb_components, i, h, v, predictor, point_transform;
1271  int index, id, ret;
1272  const int block_size = s->lossless ? 1 : 8;
1273  int ilv, prev_shift;
1274 
1275  if (!s->got_picture) {
1277  "Can not process SOS before SOF, skipping\n");
1278  return -1;
1279  }
1280 
1281  av_assert0(s->picture_ptr->data[0]);
1282  /* XXX: verify len field validity */
1283  len = get_bits(&s->gb, 16);
1284  nb_components = get_bits(&s->gb, 8);
1285  if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
1287  "decode_sos: nb_components (%d) unsupported\n", nb_components);
1288  return AVERROR_PATCHWELCOME;
1289  }
1290  if (len != 6 + 2 * nb_components) {
1291  av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
1292  return AVERROR_INVALIDDATA;
1293  }
1294  for (i = 0; i < nb_components; i++) {
1295  id = get_bits(&s->gb, 8) - 1;
1296  av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1297  /* find component index */
1298  for (index = 0; index < s->nb_components; index++)
1299  if (id == s->component_id[index])
1300  break;
1301  if (index == s->nb_components) {
1303  "decode_sos: index(%d) out of components\n", index);
1304  return AVERROR_INVALIDDATA;
1305  }
1306  /* Metasoft MJPEG codec has Cb and Cr swapped */
1307  if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1308  && nb_components == 3 && s->nb_components == 3 && i)
1309  index = 3 - i;
1310 
1311  s->quant_sindex[i] = s->quant_index[index];
1312  s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1313  s->h_scount[i] = s->h_count[index];
1314  s->v_scount[i] = s->v_count[index];
1315 
1316  if(nb_components == 3 && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P)
1317  index = (i+2)%3;
1318  if(nb_components == 1 && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P)
1319  index = (index+2)%3;
1320 
1321  s->comp_index[i] = index;
1322 
1323  s->dc_index[i] = get_bits(&s->gb, 4);
1324  s->ac_index[i] = get_bits(&s->gb, 4);
1325 
1326  if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1327  s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1328  goto out_of_range;
1329  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))
1330  goto out_of_range;
1331  }
1332 
1333  predictor = get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1334  ilv = get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1335  if(s->avctx->codec_tag != AV_RL32("CJPG")){
1336  prev_shift = get_bits(&s->gb, 4); /* Ah */
1337  point_transform = get_bits(&s->gb, 4); /* Al */
1338  }else
1339  prev_shift = point_transform = 0;
1340 
1341  if (nb_components > 1) {
1342  /* interleaved stream */
1343  s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1344  s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1345  } else if (!s->ls) { /* skip this for JPEG-LS */
1346  h = s->h_max / s->h_scount[0];
1347  v = s->v_max / s->v_scount[0];
1348  s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1349  s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1350  s->nb_blocks[0] = 1;
1351  s->h_scount[0] = 1;
1352  s->v_scount[0] = 1;
1353  }
1354 
1355  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1356  av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d skip:%d %s comp:%d\n",
1357  s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1358  predictor, point_transform, ilv, s->bits, s->mjpb_skiptosod,
1359  s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""), nb_components);
1360 
1361 
1362  /* mjpeg-b can have padding bytes between sos and image data, skip them */
1363  for (i = s->mjpb_skiptosod; i > 0; i--)
1364  skip_bits(&s->gb, 8);
1365 
1366 next_field:
1367  for (i = 0; i < nb_components; i++)
1368  s->last_dc[i] = (4 << s->bits);
1369 
1370  if (s->lossless) {
1371  av_assert0(s->picture_ptr == &s->picture);
1372  if (CONFIG_JPEGLS_DECODER && s->ls) {
1373 // for () {
1374 // reset_ls_coding_parameters(s, 0);
1375 
1376  if ((ret = ff_jpegls_decode_picture(s, predictor,
1377  point_transform, ilv)) < 0)
1378  return ret;
1379  } else {
1380  if (s->rgb) {
1381  if ((ret = ljpeg_decode_rgb_scan(s, nb_components, predictor, point_transform)) < 0)
1382  return ret;
1383  } else {
1384  if ((ret = ljpeg_decode_yuv_scan(s, predictor,
1385  point_transform,
1386  nb_components)) < 0)
1387  return ret;
1388  }
1389  }
1390  } else {
1391  if (s->progressive && predictor) {
1392  av_assert0(s->picture_ptr == &s->picture);
1393  if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor,
1394  ilv, prev_shift,
1395  point_transform)) < 0)
1396  return ret;
1397  } else {
1398  if ((ret = mjpeg_decode_scan(s, nb_components,
1399  prev_shift, point_transform,
1400  mb_bitmask, reference)) < 0)
1401  return ret;
1402  }
1403  }
1404 
1405  if (s->interlaced &&
1406  get_bits_left(&s->gb) > 32 &&
1407  show_bits(&s->gb, 8) == 0xFF) {
1408  GetBitContext bak = s->gb;
1409  align_get_bits(&bak);
1410  if (show_bits(&bak, 16) == 0xFFD1) {
1411  av_log(s->avctx, AV_LOG_DEBUG, "AVRn interlaced picture marker found\n");
1412  s->gb = bak;
1413  skip_bits(&s->gb, 16);
1414  s->bottom_field ^= 1;
1415 
1416  goto next_field;
1417  }
1418  }
1419 
1420  emms_c();
1421  return 0;
1422  out_of_range:
1423  av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1424  return AVERROR_INVALIDDATA;
1425 }
1426 
1428 {
1429  if (get_bits(&s->gb, 16) != 4)
1430  return AVERROR_INVALIDDATA;
1431  s->restart_interval = get_bits(&s->gb, 16);
1432  s->restart_count = 0;
1433  av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1434  s->restart_interval);
1435 
1436  return 0;
1437 }
1438 
1440 {
1441  int len, id, i;
1442 
1443  len = get_bits(&s->gb, 16);
1444  if (len < 5)
1445  return AVERROR_INVALIDDATA;
1446  if (8 * len > get_bits_left(&s->gb))
1447  return AVERROR_INVALIDDATA;
1448 
1449  id = get_bits_long(&s->gb, 32);
1450  len -= 6;
1451 
1452  if (s->avctx->debug & FF_DEBUG_STARTCODE)
1453  av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X len=%d\n", id, len);
1454 
1455  /* Buggy AVID, it puts EOI only at every 10th frame. */
1456  /* Also, this fourcc is used by non-avid files too, it holds some
1457  information, but it's always present in AVID-created files. */
1458  if (id == AV_RB32("AVI1")) {
1459  /* structure:
1460  4bytes AVI1
1461  1bytes polarity
1462  1bytes always zero
1463  4bytes field_size
1464  4bytes field_size_less_padding
1465  */
1466  s->buggy_avid = 1;
1467  i = get_bits(&s->gb, 8); len--;
1468  av_log(s->avctx, AV_LOG_DEBUG, "polarity %d\n", i);
1469 #if 0
1470  skip_bits(&s->gb, 8);
1471  skip_bits(&s->gb, 32);
1472  skip_bits(&s->gb, 32);
1473  len -= 10;
1474 #endif
1475  goto out;
1476  }
1477 
1478 // len -= 2;
1479 
1480  if (id == AV_RB32("JFIF")) {
1481  int t_w, t_h, v1, v2;
1482  skip_bits(&s->gb, 8); /* the trailing zero-byte */
1483  v1 = get_bits(&s->gb, 8);
1484  v2 = get_bits(&s->gb, 8);
1485  skip_bits(&s->gb, 8);
1486 
1487  s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1488  s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1489 
1490  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1491  av_log(s->avctx, AV_LOG_INFO,
1492  "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1493  v1, v2,
1496 
1497  t_w = get_bits(&s->gb, 8);
1498  t_h = get_bits(&s->gb, 8);
1499  if (t_w && t_h) {
1500  /* skip thumbnail */
1501  if (len -10 - (t_w * t_h * 3) > 0)
1502  len -= t_w * t_h * 3;
1503  }
1504  len -= 10;
1505  goto out;
1506  }
1507 
1508  if (id == AV_RB32("Adob") && (get_bits(&s->gb, 8) == 'e')) {
1509  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1510  av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
1511  skip_bits(&s->gb, 16); /* version */
1512  skip_bits(&s->gb, 16); /* flags0 */
1513  skip_bits(&s->gb, 16); /* flags1 */
1514  skip_bits(&s->gb, 8); /* transform */
1515  len -= 7;
1516  goto out;
1517  }
1518 
1519  if (id == AV_RB32("LJIF")) {
1520  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1521  av_log(s->avctx, AV_LOG_INFO,
1522  "Pegasus lossless jpeg header found\n");
1523  skip_bits(&s->gb, 16); /* version ? */
1524  skip_bits(&s->gb, 16); /* unknown always 0? */
1525  skip_bits(&s->gb, 16); /* unknown always 0? */
1526  skip_bits(&s->gb, 16); /* unknown always 0? */
1527  switch (i=get_bits(&s->gb, 8)) {
1528  case 1:
1529  s->rgb = 1;
1530  s->pegasus_rct = 0;
1531  break;
1532  case 2:
1533  s->rgb = 1;
1534  s->pegasus_rct = 1;
1535  break;
1536  default:
1537  av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace %d\n", i);
1538  }
1539  len -= 9;
1540  goto out;
1541  }
1542  if (id == AV_RL32("colr") && len > 0) {
1543  s->colr = get_bits(&s->gb, 8);
1544  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1545  av_log(s->avctx, AV_LOG_INFO, "COLR %d\n", s->colr);
1546  len --;
1547  goto out;
1548  }
1549  if (id == AV_RL32("xfrm") && len > 0) {
1550  s->xfrm = get_bits(&s->gb, 8);
1551  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1552  av_log(s->avctx, AV_LOG_INFO, "XFRM %d\n", s->xfrm);
1553  len --;
1554  goto out;
1555  }
1556 
1557  /* EXIF metadata */
1558  if (s->start_code == APP1 && id == AV_RB32("Exif")) {
1559  GetByteContext gbytes;
1560  int ret, le, ifd_offset, bytes_read;
1561  const uint8_t *aligned;
1562 
1563  skip_bits(&s->gb, 16); // skip padding
1564  len -= 2;
1565 
1566  // init byte wise reading
1567  aligned = align_get_bits(&s->gb);
1568  bytestream2_init(&gbytes, aligned, len);
1569 
1570  // read TIFF header
1571  ret = ff_tdecode_header(&gbytes, &le, &ifd_offset);
1572  if (ret) {
1573  av_log(s->avctx, AV_LOG_ERROR, "mjpeg: invalid TIFF header in EXIF data\n");
1574  return ret;
1575  }
1576 
1577  bytestream2_seek(&gbytes, ifd_offset, SEEK_SET);
1578 
1579  // read 0th IFD and store the metadata
1580  // (return values > 0 indicate the presence of subimage metadata)
1581  ret = ff_exif_decode_ifd(s->avctx, &gbytes, le, 0, &s->exif_metadata);
1582  if (ret < 0) {
1583  av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error decoding EXIF data\n");
1584  return ret;
1585  }
1586 
1587  bytes_read = bytestream2_tell(&gbytes);
1588  skip_bits(&s->gb, bytes_read << 3);
1589  len -= bytes_read;
1590 
1591  goto out;
1592  }
1593 
1594  /* Apple MJPEG-A */
1595  if ((s->start_code == APP1) && (len > (0x28 - 8))) {
1596  id = get_bits_long(&s->gb, 32);
1597  len -= 4;
1598  /* Apple MJPEG-A */
1599  if (id == AV_RB32("mjpg")) {
1600 #if 0
1601  skip_bits(&s->gb, 32); /* field size */
1602  skip_bits(&s->gb, 32); /* pad field size */
1603  skip_bits(&s->gb, 32); /* next off */
1604  skip_bits(&s->gb, 32); /* quant off */
1605  skip_bits(&s->gb, 32); /* huff off */
1606  skip_bits(&s->gb, 32); /* image off */
1607  skip_bits(&s->gb, 32); /* scan off */
1608  skip_bits(&s->gb, 32); /* data off */
1609 #endif
1610  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1611  av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1612  }
1613  }
1614 
1615 out:
1616  /* slow but needed for extreme adobe jpegs */
1617  if (len < 0)
1619  "mjpeg: error, decode_app parser read over the end\n");
1620  while (--len > 0)
1621  skip_bits(&s->gb, 8);
1622 
1623  return 0;
1624 }
1625 
1627 {
1628  int len = get_bits(&s->gb, 16);
1629  if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
1630  char *cbuf = av_malloc(len - 1);
1631  if (cbuf) {
1632  int i;
1633  for (i = 0; i < len - 2; i++)
1634  cbuf[i] = get_bits(&s->gb, 8);
1635  if (i > 0 && cbuf[i - 1] == '\n')
1636  cbuf[i - 1] = 0;
1637  else
1638  cbuf[i] = 0;
1639 
1640  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1641  av_log(s->avctx, AV_LOG_INFO, "comment: '%s'\n", cbuf);
1642 
1643  /* buggy avid, it puts EOI only at every 10th frame */
1644  if (!strncmp(cbuf, "AVID", 4)) {
1645  s->buggy_avid = 1;
1646  if (len > 14 && cbuf[12] == 1) /* 1 - NTSC, 2 - PAL */
1647  s->interlace_polarity = 1;
1648  } else if (!strcmp(cbuf, "CS=ITU601"))
1649  s->cs_itu601 = 1;
1650  else if ((!strncmp(cbuf, "Intel(R) JPEG Library, version 1", 32)) ||
1651  (!strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
1652  s->flipped = 1;
1653 
1654  av_free(cbuf);
1655  }
1656  }
1657 
1658  return 0;
1659 }
1660 
1661 /* return the 8 bit start code value and update the search
1662  state. Return -1 if no start code found */
1663 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
1664 {
1665  const uint8_t *buf_ptr;
1666  unsigned int v, v2;
1667  int val;
1668  int skipped = 0;
1669 
1670  buf_ptr = *pbuf_ptr;
1671  while (buf_end - buf_ptr > 1) {
1672  v = *buf_ptr++;
1673  v2 = *buf_ptr;
1674  if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1675  val = *buf_ptr++;
1676  goto found;
1677  }
1678  skipped++;
1679  }
1680  buf_ptr = buf_end;
1681  val = -1;
1682 found:
1683  av_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
1684  *pbuf_ptr = buf_ptr;
1685  return val;
1686 }
1687 
1689  const uint8_t **buf_ptr, const uint8_t *buf_end,
1690  const uint8_t **unescaped_buf_ptr,
1691  int *unescaped_buf_size)
1692 {
1693  int start_code;
1694  start_code = find_marker(buf_ptr, buf_end);
1695 
1696  av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
1697  if (!s->buffer)
1698  return AVERROR(ENOMEM);
1699 
1700  /* unescape buffer of SOS, use special treatment for JPEG-LS */
1701  if (start_code == SOS && !s->ls) {
1702  const uint8_t *src = *buf_ptr;
1703  uint8_t *dst = s->buffer;
1704 
1705  while (src < buf_end) {
1706  uint8_t x = *(src++);
1707 
1708  *(dst++) = x;
1709  if (s->avctx->codec_id != AV_CODEC_ID_THP) {
1710  if (x == 0xff) {
1711  while (src < buf_end && x == 0xff)
1712  x = *(src++);
1713 
1714  if (x >= 0xd0 && x <= 0xd7)
1715  *(dst++) = x;
1716  else if (x)
1717  break;
1718  }
1719  }
1720  }
1721  *unescaped_buf_ptr = s->buffer;
1722  *unescaped_buf_size = dst - s->buffer;
1723  memset(s->buffer + *unescaped_buf_size, 0,
1725 
1726  av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
1727  (buf_end - *buf_ptr) - (dst - s->buffer));
1728  } else if (start_code == SOS && s->ls) {
1729  const uint8_t *src = *buf_ptr;
1730  uint8_t *dst = s->buffer;
1731  int bit_count = 0;
1732  int t = 0, b = 0;
1733  PutBitContext pb;
1734 
1735  s->cur_scan++;
1736 
1737  /* find marker */
1738  while (src + t < buf_end) {
1739  uint8_t x = src[t++];
1740  if (x == 0xff) {
1741  while ((src + t < buf_end) && x == 0xff)
1742  x = src[t++];
1743  if (x & 0x80) {
1744  t -= FFMIN(2, t);
1745  break;
1746  }
1747  }
1748  }
1749  bit_count = t * 8;
1750  init_put_bits(&pb, dst, t);
1751 
1752  /* unescape bitstream */
1753  while (b < t) {
1754  uint8_t x = src[b++];
1755  put_bits(&pb, 8, x);
1756  if (x == 0xFF) {
1757  x = src[b++];
1758  put_bits(&pb, 7, x);
1759  bit_count--;
1760  }
1761  }
1762  flush_put_bits(&pb);
1763 
1764  *unescaped_buf_ptr = dst;
1765  *unescaped_buf_size = (bit_count + 7) >> 3;
1766  memset(s->buffer + *unescaped_buf_size, 0,
1768  } else {
1769  *unescaped_buf_ptr = *buf_ptr;
1770  *unescaped_buf_size = buf_end - *buf_ptr;
1771  }
1772 
1773  return start_code;
1774 }
1775 
1776 int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1777  AVPacket *avpkt)
1778 {
1779  const uint8_t *buf = avpkt->data;
1780  int buf_size = avpkt->size;
1781  MJpegDecodeContext *s = avctx->priv_data;
1782  const uint8_t *buf_end, *buf_ptr;
1783  const uint8_t *unescaped_buf_ptr;
1784  int hshift, vshift;
1785  int unescaped_buf_size;
1786  int start_code;
1787  int i, index;
1788  int ret = 0;
1789 
1791 
1792  buf_ptr = buf;
1793  buf_end = buf + buf_size;
1794  while (buf_ptr < buf_end) {
1795  /* find start next marker */
1796  start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
1797  &unescaped_buf_ptr,
1798  &unescaped_buf_size);
1799  /* EOF */
1800  if (start_code < 0) {
1801  goto the_end;
1802  } else if (unescaped_buf_size > INT_MAX / 8) {
1803  av_log(avctx, AV_LOG_ERROR,
1804  "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
1805  start_code, unescaped_buf_size, buf_size);
1806  return AVERROR_INVALIDDATA;
1807  }
1808  av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n",
1809  start_code, buf_end - buf_ptr);
1810 
1811  ret = init_get_bits8(&s->gb, unescaped_buf_ptr, unescaped_buf_size);
1812 
1813  if (ret < 0) {
1814  av_log(avctx, AV_LOG_ERROR, "invalid buffer\n");
1815  goto fail;
1816  }
1817 
1818  s->start_code = start_code;
1819  if (s->avctx->debug & FF_DEBUG_STARTCODE)
1820  av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1821 
1822  /* process markers */
1823  if (start_code >= 0xd0 && start_code <= 0xd7)
1824  av_log(avctx, AV_LOG_DEBUG,
1825  "restart marker: %d\n", start_code & 0x0f);
1826  /* APP fields */
1827  else if (start_code >= APP0 && start_code <= APP15)
1828  mjpeg_decode_app(s);
1829  /* Comment */
1830  else if (start_code == COM)
1831  mjpeg_decode_com(s);
1832 
1833  ret = -1;
1834 
1835  if (!CONFIG_JPEGLS_DECODER &&
1836  (start_code == SOF48 || start_code == LSE)) {
1837  av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n");
1838  return AVERROR(ENOSYS);
1839  }
1840 
1841  switch (start_code) {
1842  case SOI:
1843  s->restart_interval = 0;
1844  s->restart_count = 0;
1845  /* nothing to do on SOI */
1846  break;
1847  case DQT:
1849  break;
1850  case DHT:
1851  if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
1852  av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1853  goto fail;
1854  }
1855  break;
1856  case SOF0:
1857  case SOF1:
1858  s->lossless = 0;
1859  s->ls = 0;
1860  s->progressive = 0;
1861  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1862  goto fail;
1863  break;
1864  case SOF2:
1865  s->lossless = 0;
1866  s->ls = 0;
1867  s->progressive = 1;
1868  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1869  goto fail;
1870  break;
1871  case SOF3:
1872  s->lossless = 1;
1873  s->ls = 0;
1874  s->progressive = 0;
1875  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1876  goto fail;
1877  break;
1878  case SOF48:
1879  s->lossless = 1;
1880  s->ls = 1;
1881  s->progressive = 0;
1882  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1883  goto fail;
1884  break;
1885  case LSE:
1886  if (!CONFIG_JPEGLS_DECODER ||
1887  (ret = ff_jpegls_decode_lse(s)) < 0)
1888  goto fail;
1889  break;
1890  case EOI:
1891 eoi_parser:
1892  s->cur_scan = 0;
1893  if (!s->got_picture) {
1894  av_log(avctx, AV_LOG_WARNING,
1895  "Found EOI before any SOF, ignoring\n");
1896  break;
1897  }
1898  if (s->interlaced) {
1899  s->bottom_field ^= 1;
1900  /* if not bottom field, do not output image yet */
1901  if (s->bottom_field == !s->interlace_polarity)
1902  break;
1903  }
1904  if ((ret = av_frame_ref(data, s->picture_ptr)) < 0)
1905  return ret;
1906  *got_frame = 1;
1907  s->got_picture = 0;
1908 
1909  if (!s->lossless) {
1910  int qp = FFMAX3(s->qscale[0],
1911  s->qscale[1],
1912  s->qscale[2]);
1913  int qpw = (s->width + 15) / 16;
1914  AVBufferRef *qp_table_buf = av_buffer_alloc(qpw);
1915  if (qp_table_buf) {
1916  memset(qp_table_buf->data, qp, qpw);
1917  av_frame_set_qp_table(data, qp_table_buf, 0, FF_QSCALE_TYPE_MPEG1);
1918  }
1919 
1920  if(avctx->debug & FF_DEBUG_QP)
1921  av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", qp);
1922  }
1923 
1924  goto the_end;
1925  case SOS:
1926  if ((ret = ff_mjpeg_decode_sos(s, NULL, NULL)) < 0 &&
1927  (avctx->err_recognition & AV_EF_EXPLODE))
1928  goto fail;
1929  break;
1930  case DRI:
1931  mjpeg_decode_dri(s);
1932  break;
1933  case SOF5:
1934  case SOF6:
1935  case SOF7:
1936  case SOF9:
1937  case SOF10:
1938  case SOF11:
1939  case SOF13:
1940  case SOF14:
1941  case SOF15:
1942  case JPG:
1943  av_log(avctx, AV_LOG_ERROR,
1944  "mjpeg: unsupported coding type (%x)\n", start_code);
1945  break;
1946  }
1947 
1948  /* eof process start code */
1949  buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
1950  av_log(avctx, AV_LOG_DEBUG,
1951  "marker parser used %d bytes (%d bits)\n",
1952  (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
1953  }
1954  if (s->got_picture) {
1955  av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
1956  goto eoi_parser;
1957  }
1958  av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
1959  return AVERROR_INVALIDDATA;
1960 fail:
1961  s->got_picture = 0;
1962  return ret;
1963 the_end:
1964  if (s->upscale_h) {
1965  uint8_t *line = s->picture_ptr->data[s->upscale_h];
1967  avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
1968  avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
1969  avctx->pix_fmt == AV_PIX_FMT_YUV440P);
1970  for (i = 0; i < s->chroma_height; i++) {
1971  for (index = s->width - 1; index; index--)
1972  line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
1973  line += s->linesize[s->upscale_h];
1974  }
1975  }
1976  if (s->upscale_v) {
1977  uint8_t *dst = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[(s->height - 1) * s->linesize[s->upscale_v]];
1978  int w;
1979  avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
1980  w = s->width >> hshift;
1982  avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
1983  avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
1984  avctx->pix_fmt == AV_PIX_FMT_YUV422P);
1985  for (i = s->height - 1; i; i--) {
1986  uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[i / 2 * s->linesize[s->upscale_v]];
1987  uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[(i + 1) / 2 * s->linesize[s->upscale_v]];
1988  if (src1 == src2) {
1989  memcpy(dst, src1, w);
1990  } else {
1991  for (index = 0; index < w; index++)
1992  dst[index] = (src1[index] + src2[index]) >> 1;
1993  }
1994  dst -= s->linesize[s->upscale_v];
1995  }
1996  }
1997  if (s->flipped && (s->avctx->flags & CODEC_FLAG_EMU_EDGE)) {
1998  int j;
1999  avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2000  for (index=0; index<4; index++) {
2001  uint8_t *dst = s->picture_ptr->data[index];
2002  int w = s->width;
2003  int h = s->height;
2004  if(index && index<3){
2005  w = FF_CEIL_RSHIFT(w, hshift);
2006  h = FF_CEIL_RSHIFT(h, vshift);
2007  }
2008  if(dst){
2009  uint8_t *dst2 = dst + s->linesize[index]*(h-1);
2010  for (i=0; i<h/2; i++) {
2011  for (j=0; j<w; j++)
2012  FFSWAP(int, dst[j], dst2[j]);
2013  dst += s->linesize[index];
2014  dst2 -= s->linesize[index];
2015  }
2016  }
2017  }
2018  }
2019 
2022 
2023  av_log(avctx, AV_LOG_DEBUG, "decode frame unused %td bytes\n",
2024  buf_end - buf_ptr);
2025 // return buf_end - buf_ptr;
2026  return buf_ptr - buf;
2027 }
2028 
2030 {
2031  MJpegDecodeContext *s = avctx->priv_data;
2032  int i, j;
2033 
2034  if (s->interlaced && s->bottom_field == !s->interlace_polarity && s->got_picture && !avctx->frame_number) {
2035  av_log(avctx, AV_LOG_INFO, "Single field\n");
2036  }
2037 
2038  if (s->picture_ptr)
2040 
2041  av_free(s->buffer);
2042  av_freep(&s->ljpeg_buffer);
2043  s->ljpeg_buffer_size = 0;
2044 
2045  for (i = 0; i < 3; i++) {
2046  for (j = 0; j < 4; j++)
2047  ff_free_vlc(&s->vlcs[i][j]);
2048  }
2049  for (i = 0; i < MAX_COMPONENTS; i++) {
2050  av_freep(&s->blocks[i]);
2051  av_freep(&s->last_nnz[i]);
2052  }
2054  return 0;
2055 }
2056 
2057 static void decode_flush(AVCodecContext *avctx)
2058 {
2059  MJpegDecodeContext *s = avctx->priv_data;
2060  s->got_picture = 0;
2061 }
2062 
2063 #if CONFIG_MJPEG_DECODER
2064 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
2065 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2066 static const AVOption options[] = {
2067  { "extern_huff", "Use external huffman table.",
2068  OFFSET(extern_huff), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
2069  { NULL },
2070 };
2071 
2072 static const AVClass mjpegdec_class = {
2073  .class_name = "MJPEG decoder",
2074  .item_name = av_default_item_name,
2075  .option = options,
2076  .version = LIBAVUTIL_VERSION_INT,
2077 };
2078 
2079 AVCodec ff_mjpeg_decoder = {
2080  .name = "mjpeg",
2081  .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
2082  .type = AVMEDIA_TYPE_VIDEO,
2083  .id = AV_CODEC_ID_MJPEG,
2084  .priv_data_size = sizeof(MJpegDecodeContext),
2088  .flush = decode_flush,
2089  .capabilities = CODEC_CAP_DR1,
2090  .max_lowres = 3,
2091  .priv_class = &mjpegdec_class,
2092 };
2093 #endif
2094 #if CONFIG_THP_DECODER
2095 AVCodec ff_thp_decoder = {
2096  .name = "thp",
2097  .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
2098  .type = AVMEDIA_TYPE_VIDEO,
2099  .id = AV_CODEC_ID_THP,
2100  .priv_data_size = sizeof(MJpegDecodeContext),
2104  .flush = decode_flush,
2105  .capabilities = CODEC_CAP_DR1,
2106  .max_lowres = 3,
2107 };
2108 #endif
int block_stride[MAX_COMPONENTS]
Definition: mjpegdec.h:79
#define AV_PIX_FMT_YUV422P16
Definition: avcodec.h:4958
Definition: mjpeg.h:115
#define AV_PIX_FMT_GBR24P
Definition: avcodec.h:4920
const char const char void * val
Definition: avisynth_c.h:671
float v
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2678
const char * s
Definition: avisynth_c.h:668
#define AVERROR_PATCHWELCOME
int v_count[MAX_COMPONENTS]
Definition: mjpegdec.h:82
Definition: mjpeg.h:57
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
AVOption.
Definition: opt.h:253
#define av_always_inline
Definition: attributes.h:41
enum AVCodecID id
Definition: mxfenc.c:90
the normal 2^n-1 &quot;JPEG&quot; YUV ranges
Definition: frame.h:49
JPEG-LS.
Definition: mjpeg.h:107
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:255
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
#define LIBAVUTIL_VERSION_INT
Definition: avcodec.h:820
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:212
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: mjpeg.c:73
int h_scount[MAX_COMPONENTS]
Definition: mjpegdec.h:87
Definition: mjpeg.h:53
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: avcodec.h:4536
AVFrame picture
Definition: mjpegdec.h:93
int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
Definition: mjpegdec.c:129
static int mjpeg_decode_com(MJpegDecodeContext *s)
Definition: mjpegdec.c:1626
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1848
TIFF tables.
planar YUV 4:2:2, 16bpp, (1 Cr &amp; Cb sample per 2x1 Y samples)
Definition: avcodec.h:4538
int num
numerator
Definition: rational.h:44
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:233
int qscale[4]
quantizer scale calculated from quant_matrixes
Definition: mjpegdec.h:52
int size
Definition: avcodec.h:1064
const char * b
Definition: vf_curves.c:105
int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int type)
Definition: frame.c:49
uint8_t * buffer
Definition: mjpegdec.h:48
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1517
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
Definition: mjpeg.h:74
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:131
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional FF_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:158
int dc_index[MAX_COMPONENTS]
Definition: mjpegdec.h:84
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: mjpeg.c:70
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: mjpeg.c:99
int linesize[MAX_COMPONENTS]
linesize &lt;&lt; interlaced
Definition: mjpegdec.h:96
uint8_t permutated[64]
Definition: dsputil.h:113
uint8_t run
Definition: svq3.c:145
planar YUV 4:4:4 32bpp, (1 Cr &amp; Cb sample per 1x1 Y &amp; A samples)
Definition: avcodec.h:4693
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2570
int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
Definition: mjpegdec.c:162
Definition: mjpeg.h:56
AVCodec.
Definition: avcodec.h:2922
EXIF metadata parser.
#define av_cold
Definition: avcodec.h:653
JPEG-LS decoder.
MJPEG encoder and decoder.
int comp_index[MAX_COMPONENTS]
Definition: mjpegdec.h:83
Y , 8bpp.
Definition: avcodec.h:4542
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1254
static void copy_block2(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
Definition: copy_block.h:26
HpelDSPContext hdsp
Definition: mjpegdec.h:104
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
#define FFMAX3(a, b, c)
Definition: avcodec.h:924
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
#define FF_QSCALE_TYPE_MPEG1
Definition: avcodec.h:890
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: avcodec.h:4537
int16_t block[64]
Definition: mjpegdec.h:98
Definition: mjpeg.h:45
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: utils.c:153
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
void(* idct_put)(uint8_t *dest, int line_size, int16_t *block)
block -&gt; idct -&gt; clip to unsigned 8 bit -&gt; dest.
Definition: dsputil.h:226
int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Definition: bitstream.c:265
const char * av_default_item_name(void *ctx)
Return the context name.
Definition: log.c:145
uint8_t bits
Definition: crc.c:260
uint8_t
static int mjpeg_decode_dri(MJpegDecodeContext *s)
Definition: mjpegdec.c:1427
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:55
uint16_t(* ljpeg_buffer)[4]
Definition: mjpegdec.h:118
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
Definition: mjpeg.h:50
unsigned int ljpeg_buffer_size
Definition: mjpegdec.h:119
int16_t quant_matrixes[4][64]
Definition: mjpegdec.h:50
#define emms_c()
Definition: internal.h:49
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by a given frame.
Definition: frame.c:247
uint8_t * last_nnz[MAX_COMPONENTS]
Definition: mjpegdec.h:100
#define FF_CEIL_RSHIFT(a, b)
Definition: avcodec.h:916
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:742
AVFrame * picture_ptr
Definition: mjpegdec.h:94
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: avcodec.h:4564
mpeg1, jpeg, h263
Definition: avcodec.h:649
const char data[16]
Definition: mxf.c:68
int quant_sindex[MAX_COMPONENTS]
Definition: mjpegdec.h:89
#define MAX_COMPONENTS
Definition: mjpegdec.h:39
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:207
void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, const uint8_t *bits_table, const uint8_t *val_table)
Definition: mjpeg.c:127
int h_count[MAX_COMPONENTS]
Definition: mjpegdec.h:81
uint8_t idct_permutation[64]
idct input permutation.
Definition: dsputil.h:246
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:293
Definition: mjpeg.h:49
int lowres
low resolution decoding, 1-&gt; 1/2 size, 2-&gt;1/4 size
Definition: avcodec.h:2580
#define AV_PIX_FMT_RGBA64
Definition: avcodec.h:4961
static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al, const uint8_t *mb_bitmask, const AVFrame *reference)
Definition: mjpegdec.c:1090
#define FFSWAP(type, a, b)
Definition: avcodec.h:928
Definition: mjpeg.h:77
Definition: mjpeg.h:83
Definition: mjpeg.h:48
const OptionDef options[]
Definition: ffserver.c:4682
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1855
#define VD
Definition: dvdsubdec.c:644
#define PREDICT(ret, topleft, top, left, predictor)
Definition: mjpeg.h:128
static void predictor(uint8_t *src, int size)
Definition: exr.c:192
void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:176
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:583
int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mjpegdec.c:1776
enum AVCodecID id
Definition: avcodec.h:2936
#define AV_PIX_FMT_GRAY16
Definition: avcodec.h:4935
AVDictionary * exif_metadata
Definition: mjpegdec.h:122
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:170
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
static int handle_rstn(MJpegDecodeContext *s, int nb_components)
Definition: mjpegdec.c:766
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> dc
static const uint16_t mask[17]
Definition: lzw.c:37
int nb_blocks[MAX_COMPONENTS]
Definition: mjpegdec.h:86
int ff_exif_decode_ifd(AVCodecContext *avctx, GetByteContext *gbytes, int le, int depth, AVDictionary **metadata)
Recursively decodes all IFD&#39;s and adds included TAGS into the metadata dictionary.
Definition: exif.c:110
Definition: mjpeg.h:43
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:2029
VLC vlcs[3][4]
Definition: mjpegdec.h:51
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: avcodec.h:4548
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: avcodec.h:4570
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: avcodec.h:4168
Definition: mjpeg.h:60
void av_dict_free(AVDictionary **m)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:162
struct AVCodec * codec
Definition: avcodec.h:1155
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1234
planar YUV 4:1:1, 12bpp, (1 Cr &amp; Cb sample per 4x1 Y samples)
Definition: avcodec.h:4541
Definition: graph2dot.c:48
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:2451
Definition: mjpeg.h:44
int ff_jpegls_decode_lse(MJpegDecodeContext *s)
Decode LSE block with initialization parameters.
Definition: jpeglsdec.c:50
static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
Definition: mjpegdec.c:1663
planar YUV 4:1:1, 12bpp, (1 Cr &amp; Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: avcodec.h:4715
#define CLOSE_READER(name, gb)
Definition: get_bits.h:141
Libavcodec external API header.
Definition: get_bits.h:63
static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, int nb_codes, int use_static, int is_ac)
Definition: mjpegdec.c:47
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition: hpeldsp.c:37
static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block, int component, int dc_index, int16_t *quant_matrix, int Al)
Definition: mjpegdec.c:585
goto fail
Definition: avfilter.c:963
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:580
#define AV_PIX_FMT_YUV444P16
Definition: avcodec.h:4959
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:231
ScanTable scantable
Definition: mjpegdec.h:102
static av_always_inline void mjpeg_copy_block(MJpegDecodeContext *s, uint8_t *dst, const uint8_t *src, int linesize, int lowres)
Definition: mjpegdec.c:1059
int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
Definition: mjpegdec.c:216
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:167
uint8_t * data
The data buffer.
Definition: buffer.h:89
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2476
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:351
Definition: mjpeg.h:76
float y
static int decode_block(MJpegDecodeContext *s, int16_t *block, int component, int dc_index, int ac_index, int16_t *quant_matrix)
Definition: mjpegdec.c:537
int component_id[MAX_COMPONENTS]
Definition: mjpegdec.h:80
static int mjpeg_decode_app(MJpegDecodeContext *s)
Definition: mjpegdec.c:1439
ret
Definition: avfilter.c:961
#define NEG_USR32(a, s)
Definition: mathops.h:159
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: mjpeg.c:65
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: mjpeg.c:67
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:282
#define FFMIN(a, b)
Definition: avcodec.h:925
#define AV_PIX_FMT_GBRP16
Definition: avcodec.h:4967
static char buffer[20]
Definition: seek-test.c:31
int quant_index[4]
Definition: mjpegdec.h:91
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:189
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:550
int v_scount[MAX_COMPONENTS]
Definition: mjpegdec.h:88
int n
Definition: avisynth_c.h:588
#define GET_VLC(code, name, gb, table, bits, max_depth)
If the vlc code is invalid and max_depth=1, then no bits will be removed.
Definition: get_bits.h:484
Definition: mjpeg.h:46
DSPContext dsp
Definition: mjpegdec.h:103
void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: imgconvert.c:65
GetBitContext gb
Definition: mjpegdec.h:44
#define ZERO_RUN
Definition: mjpegdec.c:683
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:201
uint8_t le
Definition: crc.c:259
Definition: mjpeg.h:52
static void flush(AVCodecContext *avctx)
Definition: aacdec.c:498
Definition: mjpeg.h:75
static const float pred[4]
Definition: siprdata.h:259
#define AV_PIX_FMT_YUVA444P16
Definition: avcodec.h:4984
Definition: mjpeg.h:54
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: avcodec.h:4546
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:186
planar YUV 4:2:0, 12bpp, (1 Cr &amp; Cb sample per 2x2 Y samples)
Definition: avcodec.h:4534
planar YUV 4:4:0 (1 Cr &amp; Cb sample per 1x2 Y samples)
Definition: avcodec.h:4569
static int width
Definition: utils.c:158
#define AV_LOG_INFO
Standard information.
Definition: avcodec.h:4158
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:65
AVS_Value src
Definition: avisynth_c.h:523
#define FFMAX(a, b)
Definition: avcodec.h:923
enum AVCodecID codec_id
Definition: avcodec.h:1157
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:436
int debug
debug
Definition: avcodec.h:2442
main external API structure.
Definition: avcodec.h:1146
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2480
static int get_xbits(GetBitContext *s, int n)
read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
Definition: get_bits.h:226
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:941
unsigned int codec_tag
fourcc (LSB first, so &quot;ABCD&quot; -&gt; (&#39;D&#39;&lt;&lt;24) + (&#39;C&#39;&lt;&lt;16) + (&#39;B&#39;&lt;&lt;8) + &#39;A&#39;).
Definition: avcodec.h:1172
planar GBR 4:4:4 24bpp
Definition: avcodec.h:4640
#define OPEN_READER(name, gb)
Definition: get_bits.h:127
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: dsputil.c:111
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)&gt;&gt;1.
Definition: hpeldsp.h:56
#define FF_DEBUG_QP
Definition: avcodec.h:2447
void * buf
Definition: avisynth_c.h:594
int extradata_size
Definition: avcodec.h:1255
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:299
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv)
Definition: jpeglsdec.c:276
int coded_height
Definition: avcodec.h:1324
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:1046
Describe the class of an AVClass context structure.
Definition: log.h:50
#define MKTAG(a, b, c, d)
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:292
int index
Definition: gxfenc.c:89
static int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
Definition: mjpegdec.c:519
int ac_index[MAX_COMPONENTS]
Definition: mjpegdec.h:85
uint8_t * data
Definition: avcodec.h:1063
static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform)
Definition: mjpegdec.c:800
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:405
#define GET_CACHE(name, gb)
Definition: get_bits.h:205
AVDictionary ** avpriv_frame_get_metadatap(AVFrame *frame)
Definition: frame.c:47
planar YUV 4:4:4, 24bpp, (1 Cr &amp; Cb sample per 1x1 Y samples)
Definition: avcodec.h:4539
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
uint64_t coefs_finished[MAX_COMPONENTS]
bitmask of which coefs have been completely decoded (progressive mode)
Definition: mjpegdec.h:101
Definition: mjpeg.h:58
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:332
#define CONFIG_JPEGLS_DECODER
Definition: config.h:541
static const uint8_t start_code[]
Definition: h264.c:4637
static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al)
Definition: mjpegdec.c:1204
#define MIN_CACHE_BITS
Definition: get_bits.h:123
void * priv_data
Definition: avcodec.h:1182
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:2443
uint8_t level
Definition: svq3.c:146
#define AV_PIX_FMT_YUV420P16
Definition: avcodec.h:4957
#define OFFSET(x)
Definition: ffmpeg_opt.c:2582
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:86
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: mjpeg.c:75
int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask, const AVFrame *reference)
Definition: mjpegdec.c:1267
static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform, int nb_components)
Definition: mjpegdec.c:915
A reference to a data buffer.
Definition: buffer.h:81
#define CODEC_FLAG_EMU_EDGE
Don&#39;t draw edges.
Definition: avcodec.h:706
static float t
Definition: muxing.c:123
common internal api header.
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:115
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:89
static void build_basic_mjpeg_vlc(MJpegDecodeContext *s)
Definition: mjpegdec.c:70
Definition: mjpeg.h:84
static double c[64]
void(* clear_block)(int16_t *block)
Definition: dsputil.h:142
static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block, uint8_t *last_nnz, int ac_index, int16_t *quant_matrix, int ss, int se, int Al, int *EOBRUN)
Definition: mjpegdec.c:701
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:54
int den
denominator
Definition: rational.h:45
static int lowres
Definition: ffplay.c:310
AVCodecContext * avctx
Definition: mjpegdec.h:43
#define AV_RB32(x)
Definition: intreadwrite.h:258
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: mjpeg.c:102
float re
Definition: fft-test.c:69
static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
Definition: mjpegdec.c:1075
#define AVERROR_INVALIDDATA
Definition: mjpeg.h:79
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:298
int got_picture
we found a SOF and picture is valid, too.
Definition: mjpegdec.h:95
#define AV_RL32(x)
Definition: intreadwrite.h:275
int len
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
int16_t(*[MAX_COMPONENTS] blocks)[64]
intermediate sums (progressive mode)
Definition: mjpegdec.h:99
static void copy_block4(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
Definition: copy_block.h:37
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
JPEG-LS extension parameters.
Definition: mjpeg.h:108
int key_frame
1 -&gt; keyframe, 0-&gt; not
Definition: frame.h:162
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:206
int last_dc[MAX_COMPONENTS]
Definition: mjpegdec.h:92
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:444
#define AVERROR(e)
#define REFINE_BIT(j)
Definition: mjpegdec.c:675
static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block, uint8_t *last_nnz, int ac_index, int16_t *quant_matrix, int ss, int se, int Al, int *EOBRUN)
Definition: mjpegdec.c:603
static void decode_flush(AVCodecContext *avctx)
Definition: mjpegdec.c:2057
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1904
int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
Decodes a TIFF header from the input bytestream and sets the endianess in *le and the offset to the f...
Definition: tiff_common.c:265
the normal 219*2^(n-8) &quot;MPEG&quot; YUV ranges
Definition: frame.h:48
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:1870
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: avcodec.h:4141
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_mjpeg_find_marker(MJpegDecodeContext *s, const uint8_t **buf_ptr, const uint8_t *buf_end, const uint8_t **unescaped_buf_ptr, int *unescaped_buf_size)
Definition: mjpegdec.c:1688
MJPEG decoder.
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: avcodec.h:4547
This structure stores compressed data.
Definition: avcodec.h:1040
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:353
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:910
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
#define AV_PIX_FMT_BGR48
Definition: avcodec.h:4940
Definition: mjpeg.h:51
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:241
Definition: mjpeg.h:98
static int16_t block[64]
Definition: dct-test.c:198