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 Libav.
12  *
13  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  */
27 
33 // #define DEBUG
34 #include <assert.h>
35 
36 #include "libavutil/imgutils.h"
37 #include "libavutil/opt.h"
38 #include "avcodec.h"
39 #include "dsputil.h"
40 #include "internal.h"
41 #include "mjpeg.h"
42 #include "mjpegdec.h"
43 #include "jpeglsdec.h"
44 
45 
46 static int build_vlc(VLC *vlc, const uint8_t *bits_table,
47  const uint8_t *val_table, int nb_codes,
48  int use_static, int is_ac)
49 {
50  uint8_t huff_size[256] = { 0 };
51  uint16_t huff_code[256];
52  uint16_t huff_sym[256];
53  int i;
54 
55  assert(nb_codes <= 256);
56 
57  ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
58 
59  for (i = 0; i < 256; i++)
60  huff_sym[i] = i + 16 * is_ac;
61 
62  if (is_ac)
63  huff_sym[0] = 16 * 256;
64 
65  return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
66  huff_code, 2, 2, huff_sym, 2, 2, use_static);
67 }
68 
70 {
72  avpriv_mjpeg_val_dc, 12, 0, 0);
74  avpriv_mjpeg_val_dc, 12, 0, 0);
83 }
84 
86 {
87  MJpegDecodeContext *s = avctx->priv_data;
88 
89  if (!s->picture_ptr)
90  s->picture_ptr = &s->picture;
91 
92  s->avctx = avctx;
93  ff_dsputil_init(&s->dsp, avctx);
95  s->buffer_size = 0;
96  s->buffer = NULL;
97  s->start_code = -1;
98  s->first_picture = 1;
99  s->org_height = avctx->coded_height;
101 
103 
104  if (s->extern_huff) {
105  int ret;
106  av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
107  init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
108  if ((ret = ff_mjpeg_decode_dht(s))) {
109  av_log(avctx, AV_LOG_ERROR,
110  "mjpeg: error using external huffman table\n");
111  return ret;
112  }
113  }
114  if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
115  s->interlace_polarity = 1; /* bottom field first */
116  av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n");
117  }
118  if (avctx->codec->id == AV_CODEC_ID_AMV)
119  s->flipped = 1;
120 
121  return 0;
122 }
123 
124 
125 /* quantize tables */
127 {
128  int len, index, i, j;
129 
130  len = get_bits(&s->gb, 16) - 2;
131 
132  while (len >= 65) {
133  /* only 8 bit precision handled */
134  if (get_bits(&s->gb, 4) != 0) {
135  av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n");
136  return -1;
137  }
138  index = get_bits(&s->gb, 4);
139  if (index >= 4)
140  return -1;
141  av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
142  /* read quant table */
143  for (i = 0; i < 64; i++) {
144  j = s->scantable.permutated[i];
145  s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
146  }
147 
148  // XXX FIXME finetune, and perhaps add dc too
149  s->qscale[index] = FFMAX(s->quant_matrixes[index][s->scantable.permutated[1]],
150  s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
151  av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n",
152  index, s->qscale[index]);
153  len -= 65;
154  }
155  return 0;
156 }
157 
158 /* decode huffman tables and build VLC decoders */
160 {
161  int len, index, i, class, n, v, code_max;
162  uint8_t bits_table[17];
163  uint8_t val_table[256];
164  int ret = 0;
165 
166  len = get_bits(&s->gb, 16) - 2;
167 
168  while (len > 0) {
169  if (len < 17)
170  return AVERROR_INVALIDDATA;
171  class = get_bits(&s->gb, 4);
172  if (class >= 2)
173  return AVERROR_INVALIDDATA;
174  index = get_bits(&s->gb, 4);
175  if (index >= 4)
176  return AVERROR_INVALIDDATA;
177  n = 0;
178  for (i = 1; i <= 16; i++) {
179  bits_table[i] = get_bits(&s->gb, 8);
180  n += bits_table[i];
181  }
182  len -= 17;
183  if (len < n || n > 256)
184  return AVERROR_INVALIDDATA;
185 
186  code_max = 0;
187  for (i = 0; i < n; i++) {
188  v = get_bits(&s->gb, 8);
189  if (v > code_max)
190  code_max = v;
191  val_table[i] = v;
192  }
193  len -= n;
194 
195  /* build VLC and flush previous vlc if present */
196  ff_free_vlc(&s->vlcs[class][index]);
197  av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
198  class, index, code_max + 1);
199  if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table,
200  code_max + 1, 0, class > 0)) < 0)
201  return ret;
202 
203  if (class > 0) {
204  ff_free_vlc(&s->vlcs[2][index]);
205  if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table,
206  code_max + 1, 0, 0)) < 0)
207  return ret;
208  }
209  }
210  return 0;
211 }
212 
214 {
215  int h_count[MAX_COMPONENTS] = { 0 };
216  int v_count[MAX_COMPONENTS] = { 0 };
217  int len, nb_components, i, width, height, bits, pix_fmt_id, ret;
218 
219  /* XXX: verify len field validity */
220  len = get_bits(&s->gb, 16);
221  bits = get_bits(&s->gb, 8);
222 
223  if (s->pegasus_rct)
224  bits = 9;
225  if (bits == 9 && !s->pegasus_rct)
226  s->rct = 1; // FIXME ugly
227 
228  if (bits != 8 && !s->lossless) {
229  av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
230  return -1;
231  }
232 
233  height = get_bits(&s->gb, 16);
234  width = get_bits(&s->gb, 16);
235 
236  // HACK for odd_height.mov
237  if (s->interlaced && s->width == width && s->height == height + 1)
238  height= s->height;
239 
240  av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
241  if (av_image_check_size(width, height, 0, s->avctx))
242  return AVERROR_INVALIDDATA;
243 
244  nb_components = get_bits(&s->gb, 8);
245  if (nb_components <= 0 ||
246  nb_components > MAX_COMPONENTS)
247  return -1;
248  if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
249  if (nb_components != s->nb_components) {
251  "nb_components changing in interlaced picture\n");
252  return AVERROR_INVALIDDATA;
253  }
254  }
255  if (s->ls && !(bits <= 8 || nb_components == 1)) {
257  "For JPEG-LS anything except <= 8 bits/component"
258  " or 16-bit gray", 0);
259  return AVERROR_PATCHWELCOME;
260  }
261  s->nb_components = nb_components;
262  s->h_max = 1;
263  s->v_max = 1;
264  for (i = 0; i < nb_components; i++) {
265  /* component id */
266  s->component_id[i] = get_bits(&s->gb, 8) - 1;
267  h_count[i] = get_bits(&s->gb, 4);
268  v_count[i] = get_bits(&s->gb, 4);
269  /* compute hmax and vmax (only used in interleaved case) */
270  if (h_count[i] > s->h_max)
271  s->h_max = h_count[i];
272  if (v_count[i] > s->v_max)
273  s->v_max = v_count[i];
274  s->quant_index[i] = get_bits(&s->gb, 8);
275  if (s->quant_index[i] >= 4)
276  return AVERROR_INVALIDDATA;
277  if (!h_count[i] || !v_count[i]) {
279  "Invalid sampling factor in component %d %d:%d\n",
280  i, h_count[i], v_count[i]);
281  return AVERROR_INVALIDDATA;
282  }
283 
284  av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
285  i, h_count[i], v_count[i],
286  s->component_id[i], s->quant_index[i]);
287  }
288 
289  if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
290  av_log_missing_feature(s->avctx, "Subsampling in JPEG-LS", 0);
291  return AVERROR_PATCHWELCOME;
292  }
293 
294  if (s->v_max == 1 && s->h_max == 1 && s->lossless == 1)
295  s->rgb = 1;
296 
297  /* if different size, realloc/alloc picture */
298  if (width != s->width || height != s->height || bits != s->bits ||
299  memcmp(s->h_count, h_count, sizeof(h_count)) ||
300  memcmp(s->v_count, v_count, sizeof(v_count))) {
301  av_freep(&s->qscale_table);
302 
303  s->width = width;
304  s->height = height;
305  s->interlaced = 0;
306  s->bits = bits;
307  memcpy(s->h_count, h_count, sizeof(h_count));
308  memcpy(s->v_count, v_count, sizeof(v_count));
309 
310  /* test interlaced mode */
311  if (s->first_picture &&
312  s->org_height != 0 &&
313  s->height < ((s->org_height * 3) / 4)) {
314  s->interlaced = 1;
318  height *= 2;
319  }
320 
321  avcodec_set_dimensions(s->avctx, width, height);
322 
323  s->qscale_table = av_mallocz((s->width + 15) / 16);
324  s->first_picture = 0;
325  }
326 
327  if (!(s->interlaced && (s->bottom_field == !s->interlace_polarity))) {
328  /* XXX: not complete test ! */
329  pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
330  (s->h_count[1] << 20) | (s->v_count[1] << 16) |
331  (s->h_count[2] << 12) | (s->v_count[2] << 8) |
332  (s->h_count[3] << 4) | s->v_count[3];
333  av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
334  /* NOTE we do not allocate pictures large enough for the possible
335  * padding of h/v_count being 4 */
336  if (!(pix_fmt_id & 0xD0D0D0D0))
337  pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
338  if (!(pix_fmt_id & 0x0D0D0D0D))
339  pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
340 
341  switch (pix_fmt_id) {
342  case 0x11111100:
343  if (s->rgb)
345  else
347  assert(s->nb_components == 3);
348  break;
349  case 0x11000000:
351  break;
352  case 0x12111100:
354  break;
355  case 0x21111100:
357  break;
358  case 0x22111100:
360  break;
361  default:
362  av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
363  return AVERROR_PATCHWELCOME;
364  }
365  if (s->ls) {
366  if (s->nb_components > 1)
368  else if (s->bits <= 8)
370  else
372  }
373 
374  if (s->picture_ptr->data[0])
376 
377  if (ff_get_buffer(s->avctx, s->picture_ptr) < 0) {
378  av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
379  return -1;
380  }
382  s->picture_ptr->key_frame = 1;
383  s->got_picture = 1;
384 
385  for (i = 0; i < 3; i++)
386  s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
387 
388  av_dlog(s->avctx, "%d %d %d %d %d %d\n",
389  s->width, s->height, s->linesize[0], s->linesize[1],
390  s->interlaced, s->avctx->height);
391 
392  if (len != (8 + (3 * nb_components)))
393  av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
394  }
395 
396  /* totally blank picture as progressive JPEG will only add details to it */
397  if (s->progressive) {
398  int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8);
399  int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
400  for (i = 0; i < s->nb_components; i++) {
401  int size = bw * bh * s->h_count[i] * s->v_count[i];
402  av_freep(&s->blocks[i]);
403  av_freep(&s->last_nnz[i]);
404  s->blocks[i] = av_malloc(size * sizeof(**s->blocks));
405  s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz));
406  s->block_stride[i] = bw * s->h_count[i];
407  }
408  memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
409  }
410  return 0;
411 }
412 
413 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
414 {
415  int code;
416  code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
417  if (code < 0) {
419  "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
420  0, dc_index, &s->vlcs[0][dc_index]);
421  return 0xffff;
422  }
423 
424  if (code)
425  return get_xbits(&s->gb, code);
426  else
427  return 0;
428 }
429 
430 /* decode block and dequantize */
431 static int decode_block(MJpegDecodeContext *s, DCTELEM *block, int component,
432  int dc_index, int ac_index, int16_t *quant_matrix)
433 {
434  int code, i, j, level, val;
435 
436  /* DC coef */
437  val = mjpeg_decode_dc(s, dc_index);
438  if (val == 0xffff) {
439  av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
440  return AVERROR_INVALIDDATA;
441  }
442  val = val * quant_matrix[0] + s->last_dc[component];
443  s->last_dc[component] = val;
444  block[0] = val;
445  /* AC coefs */
446  i = 0;
447  {OPEN_READER(re, &s->gb);
448  do {
449  UPDATE_CACHE(re, &s->gb);
450  GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
451 
452  i += ((unsigned)code) >> 4;
453  code &= 0xf;
454  if (code) {
455  if (code > MIN_CACHE_BITS - 16)
456  UPDATE_CACHE(re, &s->gb);
457 
458  {
459  int cache = GET_CACHE(re, &s->gb);
460  int sign = (~cache) >> 31;
461  level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
462  }
463 
464  LAST_SKIP_BITS(re, &s->gb, code);
465 
466  if (i > 63) {
467  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
468  return AVERROR_INVALIDDATA;
469  }
470  j = s->scantable.permutated[i];
471  block[j] = level * quant_matrix[j];
472  }
473  } while (i < 63);
474  CLOSE_READER(re, &s->gb);}
475 
476  return 0;
477 }
478 
480  int component, int dc_index,
481  int16_t *quant_matrix, int Al)
482 {
483  int val;
484  s->dsp.clear_block(block);
485  val = mjpeg_decode_dc(s, dc_index);
486  if (val == 0xffff) {
487  av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
488  return AVERROR_INVALIDDATA;
489  }
490  val = (val * quant_matrix[0] << Al) + s->last_dc[component];
491  s->last_dc[component] = val;
492  block[0] = val;
493  return 0;
494 }
495 
496 /* decode block and dequantize - progressive JPEG version */
498  uint8_t *last_nnz, int ac_index,
499  int16_t *quant_matrix,
500  int ss, int se, int Al, int *EOBRUN)
501 {
502  int code, i, j, level, val, run;
503 
504  if (*EOBRUN) {
505  (*EOBRUN)--;
506  return 0;
507  }
508 
509  {
510  OPEN_READER(re, &s->gb);
511  for (i = ss; ; i++) {
512  UPDATE_CACHE(re, &s->gb);
513  GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
514 
515  run = ((unsigned) code) >> 4;
516  code &= 0xF;
517  if (code) {
518  i += run;
519  if (code > MIN_CACHE_BITS - 16)
520  UPDATE_CACHE(re, &s->gb);
521 
522  {
523  int cache = GET_CACHE(re, &s->gb);
524  int sign = (~cache) >> 31;
525  level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
526  }
527 
528  LAST_SKIP_BITS(re, &s->gb, code);
529 
530  if (i >= se) {
531  if (i == se) {
532  j = s->scantable.permutated[se];
533  block[j] = level * quant_matrix[j] << Al;
534  break;
535  }
536  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
537  return AVERROR_INVALIDDATA;
538  }
539  j = s->scantable.permutated[i];
540  block[j] = level * quant_matrix[j] << Al;
541  } else {
542  if (run == 0xF) {// ZRL - skip 15 coefficients
543  i += 15;
544  if (i >= se) {
545  av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
546  return AVERROR_INVALIDDATA;
547  }
548  } else {
549  val = (1 << run);
550  if (run) {
551  UPDATE_CACHE(re, &s->gb);
552  val += NEG_USR32(GET_CACHE(re, &s->gb), run);
553  LAST_SKIP_BITS(re, &s->gb, run);
554  }
555  *EOBRUN = val - 1;
556  break;
557  }
558  }
559  }
560  CLOSE_READER(re, &s->gb);
561  }
562 
563  if (i > *last_nnz)
564  *last_nnz = i;
565 
566  return 0;
567 }
568 
569 #define REFINE_BIT(j) { \
570  UPDATE_CACHE(re, &s->gb); \
571  sign = block[j] >> 15; \
572  block[j] += SHOW_UBITS(re, &s->gb, 1) * \
573  ((quant_matrix[j] ^ sign) - sign) << Al; \
574  LAST_SKIP_BITS(re, &s->gb, 1); \
575 }
576 
577 #define ZERO_RUN \
578 for (; ; i++) { \
579  if (i > last) { \
580  i += run; \
581  if (i > se) { \
582  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
583  return -1; \
584  } \
585  break; \
586  } \
587  j = s->scantable.permutated[i]; \
588  if (block[j]) \
589  REFINE_BIT(j) \
590  else if (run-- == 0) \
591  break; \
592 }
593 
594 /* decode block and dequantize - progressive JPEG refinement pass */
596  uint8_t *last_nnz,
597  int ac_index, int16_t *quant_matrix,
598  int ss, int se, int Al, int *EOBRUN)
599 {
600  int code, i = ss, j, sign, val, run;
601  int last = FFMIN(se, *last_nnz);
602 
603  OPEN_READER(re, &s->gb);
604  if (*EOBRUN) {
605  (*EOBRUN)--;
606  } else {
607  for (; ; i++) {
608  UPDATE_CACHE(re, &s->gb);
609  GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
610 
611  if (code & 0xF) {
612  run = ((unsigned) code) >> 4;
613  UPDATE_CACHE(re, &s->gb);
614  val = SHOW_UBITS(re, &s->gb, 1);
615  LAST_SKIP_BITS(re, &s->gb, 1);
616  ZERO_RUN;
617  j = s->scantable.permutated[i];
618  val--;
619  block[j] = ((quant_matrix[j]^val) - val) << Al;
620  if (i == se) {
621  if (i > *last_nnz)
622  *last_nnz = i;
623  CLOSE_READER(re, &s->gb);
624  return 0;
625  }
626  } else {
627  run = ((unsigned) code) >> 4;
628  if (run == 0xF) {
629  ZERO_RUN;
630  } else {
631  val = run;
632  run = (1 << run);
633  if (val) {
634  UPDATE_CACHE(re, &s->gb);
635  run += SHOW_UBITS(re, &s->gb, val);
636  LAST_SKIP_BITS(re, &s->gb, val);
637  }
638  *EOBRUN = run - 1;
639  break;
640  }
641  }
642  }
643 
644  if (i > *last_nnz)
645  *last_nnz = i;
646  }
647 
648  for (; i <= last; i++) {
649  j = s->scantable.permutated[i];
650  if (block[j])
651  REFINE_BIT(j)
652  }
653  CLOSE_READER(re, &s->gb);
654 
655  return 0;
656 }
657 #undef REFINE_BIT
658 #undef ZERO_RUN
659 
660 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor,
661  int point_transform)
662 {
663  int i, mb_x, mb_y;
664  uint16_t (*buffer)[4];
665  int left[3], top[3], topleft[3];
666  const int linesize = s->linesize[0];
667  const int mask = (1 << s->bits) - 1;
668 
670  (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
671  buffer = s->ljpeg_buffer;
672 
673  for (i = 0; i < 3; i++)
674  buffer[0][i] = 1 << (s->bits + point_transform - 1);
675 
676  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
677  const int modified_predictor = mb_y ? predictor : 1;
678  uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
679 
680  if (s->interlaced && s->bottom_field)
681  ptr += linesize >> 1;
682 
683  for (i = 0; i < 3; i++)
684  top[i] = left[i] = topleft[i] = buffer[0][i];
685 
686  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
687  if (s->restart_interval && !s->restart_count)
689 
690  for (i = 0; i < 3; i++) {
691  int pred;
692 
693  topleft[i] = top[i];
694  top[i] = buffer[mb_x][i];
695 
696  PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
697 
698  left[i] = buffer[mb_x][i] =
699  mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
700  }
701 
702  if (s->restart_interval && !--s->restart_count) {
703  align_get_bits(&s->gb);
704  skip_bits(&s->gb, 16); /* skip RSTn */
705  }
706  }
707 
708  if (s->rct) {
709  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
710  ptr[4 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
711  ptr[4 * mb_x + 0] = buffer[mb_x][1] + ptr[4 * mb_x + 1];
712  ptr[4 * mb_x + 2] = buffer[mb_x][2] + ptr[4 * mb_x + 1];
713  }
714  } else if (s->pegasus_rct) {
715  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
716  ptr[4 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
717  ptr[4 * mb_x + 0] = buffer[mb_x][1] + ptr[4 * mb_x + 1];
718  ptr[4 * mb_x + 2] = buffer[mb_x][2] + ptr[4 * mb_x + 1];
719  }
720  } else {
721  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
722  ptr[4 * mb_x + 0] = buffer[mb_x][2];
723  ptr[4 * mb_x + 1] = buffer[mb_x][1];
724  ptr[4 * mb_x + 2] = buffer[mb_x][0];
725  }
726  }
727  }
728  return 0;
729 }
730 
731 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor,
732  int point_transform, int nb_components)
733 {
734  int i, mb_x, mb_y;
735 
736  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
737  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
738  if (s->restart_interval && !s->restart_count)
740 
741  if (mb_x == 0 || mb_y == 0 || s->interlaced) {
742  for (i = 0; i < nb_components; i++) {
743  uint8_t *ptr;
744  int n, h, v, x, y, c, j, linesize;
745  n = s->nb_blocks[i];
746  c = s->comp_index[i];
747  h = s->h_scount[i];
748  v = s->v_scount[i];
749  x = 0;
750  y = 0;
751  linesize = s->linesize[c];
752 
753  for (j = 0; j < n; j++) {
754  int pred;
755  // FIXME optimize this crap
756  ptr = s->picture_ptr->data[c] +
757  (linesize * (v * mb_y + y)) +
758  (h * mb_x + x);
759  if (y == 0 && mb_y == 0) {
760  if (x == 0 && mb_x == 0)
761  pred = 128 << point_transform;
762  else
763  pred = ptr[-1];
764  } else {
765  if (x == 0 && mb_x == 0)
766  pred = ptr[-linesize];
767  else
768  PREDICT(pred, ptr[-linesize - 1],
769  ptr[-linesize], ptr[-1], predictor);
770  }
771 
772  if (s->interlaced && s->bottom_field)
773  ptr += linesize >> 1;
774  *ptr = pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
775 
776  if (++x == h) {
777  x = 0;
778  y++;
779  }
780  }
781  }
782  } else {
783  for (i = 0; i < nb_components; i++) {
784  uint8_t *ptr;
785  int n, h, v, x, y, c, j, linesize;
786  n = s->nb_blocks[i];
787  c = s->comp_index[i];
788  h = s->h_scount[i];
789  v = s->v_scount[i];
790  x = 0;
791  y = 0;
792  linesize = s->linesize[c];
793 
794  for (j = 0; j < n; j++) {
795  int pred;
796 
797  // FIXME optimize this crap
798  ptr = s->picture_ptr->data[c] +
799  (linesize * (v * mb_y + y)) +
800  (h * mb_x + x);
801  PREDICT(pred, ptr[-linesize - 1],
802  ptr[-linesize], ptr[-1], predictor);
803  *ptr = pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
804  if (++x == h) {
805  x = 0;
806  y++;
807  }
808  }
809  }
810  }
811  if (s->restart_interval && !--s->restart_count) {
812  align_get_bits(&s->gb);
813  skip_bits(&s->gb, 16); /* skip RSTn */
814  }
815  }
816  }
817  return 0;
818 }
819 
820 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
821  int Al, const uint8_t *mb_bitmask,
822  const AVFrame *reference)
823 {
824  int i, mb_x, mb_y;
826  const uint8_t *reference_data[MAX_COMPONENTS];
827  int linesize[MAX_COMPONENTS];
828  GetBitContext mb_bitmask_gb;
829 
830  if (mb_bitmask)
831  init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
832 
833  if (s->flipped && s->avctx->flags & CODEC_FLAG_EMU_EDGE) {
835  "Can not flip image with CODEC_FLAG_EMU_EDGE set!\n");
836  s->flipped = 0;
837  }
838 
839  for (i = 0; i < nb_components; i++) {
840  int c = s->comp_index[i];
841  data[c] = s->picture_ptr->data[c];
842  reference_data[c] = reference ? reference->data[c] : NULL;
843  linesize[c] = s->linesize[c];
844  s->coefs_finished[c] |= 1;
845  if (s->flipped) {
846  // picture should be flipped upside-down for this codec
847  int offset = (linesize[c] * (s->v_scount[i] *
848  (8 * s->mb_height - ((s->height / s->v_max) & 7)) - 1));
849  data[c] += offset;
850  reference_data[c] += offset;
851  linesize[c] *= -1;
852  }
853  }
854 
855  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
856  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
857  const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
858 
859  if (s->restart_interval && !s->restart_count)
861 
862  if (get_bits_left(&s->gb) < 0) {
863  av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
864  -get_bits_left(&s->gb));
865  return AVERROR_INVALIDDATA;
866  }
867  for (i = 0; i < nb_components; i++) {
868  uint8_t *ptr;
869  int n, h, v, x, y, c, j;
870  int block_offset;
871  n = s->nb_blocks[i];
872  c = s->comp_index[i];
873  h = s->h_scount[i];
874  v = s->v_scount[i];
875  x = 0;
876  y = 0;
877  for (j = 0; j < n; j++) {
878  block_offset = ((linesize[c] * (v * mb_y + y) * 8) +
879  (h * mb_x + x) * 8);
880 
881  if (s->interlaced && s->bottom_field)
882  block_offset += linesize[c] >> 1;
883  ptr = data[c] + block_offset;
884  if (!s->progressive) {
885  if (copy_mb)
886  copy_block8(ptr, reference_data[c] + block_offset,
887  linesize[c], linesize[c], 8);
888  else {
889  s->dsp.clear_block(s->block);
890  if (decode_block(s, s->block, i,
891  s->dc_index[i], s->ac_index[i],
892  s->quant_matrixes[s->quant_index[c]]) < 0) {
894  "error y=%d x=%d\n", mb_y, mb_x);
895  return AVERROR_INVALIDDATA;
896  }
897  s->dsp.idct_put(ptr, linesize[c], s->block);
898  }
899  } else {
900  int block_idx = s->block_stride[c] * (v * mb_y + y) +
901  (h * mb_x + x);
902  DCTELEM *block = s->blocks[c][block_idx];
903  if (Ah)
904  block[0] += get_bits1(&s->gb) *
905  s->quant_matrixes[s->quant_index[c]][0] << Al;
906  else if (decode_dc_progressive(s, block, i, s->dc_index[i],
907  s->quant_matrixes[s->quant_index[c]],
908  Al) < 0) {
910  "error y=%d x=%d\n", mb_y, mb_x);
911  return AVERROR_INVALIDDATA;
912  }
913  }
914  av_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
915  av_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
916  mb_x, mb_y, x, y, c, s->bottom_field,
917  (v * mb_y + y) * 8, (h * mb_x + x) * 8);
918  if (++x == h) {
919  x = 0;
920  y++;
921  }
922  }
923  }
924 
925  if (s->restart_interval) {
926  s->restart_count--;
927  i = 8 + ((-get_bits_count(&s->gb)) & 7);
928  /* skip RSTn */
929  if (show_bits(&s->gb, i) == (1 << i) - 1) {
930  int pos = get_bits_count(&s->gb);
931  align_get_bits(&s->gb);
932  while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
933  skip_bits(&s->gb, 8);
934  if ((get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
935  for (i = 0; i < nb_components; i++) /* reset dc */
936  s->last_dc[i] = 1024;
937  } else
938  skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
939  }
940  }
941  }
942  }
943  return 0;
944 }
945 
947  int se, int Ah, int Al,
948  const uint8_t *mb_bitmask,
949  const AVFrame *reference)
950 {
951  int mb_x, mb_y;
952  int EOBRUN = 0;
953  int c = s->comp_index[0];
954  uint8_t *data = s->picture_ptr->data[c];
955  const uint8_t *reference_data = reference ? reference->data[c] : NULL;
956  int linesize = s->linesize[c];
957  int last_scan = 0;
958  int16_t *quant_matrix = s->quant_matrixes[s->quant_index[c]];
959  GetBitContext mb_bitmask_gb;
960 
961  if (ss < 0 || ss >= 64 ||
962  se < ss || se >= 64 ||
963  Ah < 0 || Al < 0)
964  return AVERROR_INVALIDDATA;
965 
966  if (mb_bitmask)
967  init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
968 
969  if (!Al) {
970  s->coefs_finished[c] |= (1LL << (se + 1)) - (1LL << ss);
971  last_scan = !~s->coefs_finished[c];
972  }
973 
974  if (s->interlaced && s->bottom_field) {
975  int offset = linesize >> 1;
976  data += offset;
977  reference_data += offset;
978  }
979 
980  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
981  int block_offset = mb_y * linesize * 8;
982  uint8_t *ptr = data + block_offset;
983  int block_idx = mb_y * s->block_stride[c];
984  DCTELEM (*block)[64] = &s->blocks[c][block_idx];
985  uint8_t *last_nnz = &s->last_nnz[c][block_idx];
986  for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
987  const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
988 
989  if (!copy_mb) {
990  int ret;
991  if (Ah)
992  ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
993  quant_matrix, ss, se, Al, &EOBRUN);
994  else
995  ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
996  quant_matrix, ss, se, Al, &EOBRUN);
997  if (ret < 0) {
999  "error y=%d x=%d\n", mb_y, mb_x);
1000  return AVERROR_INVALIDDATA;
1001  }
1002  }
1003 
1004  if (last_scan) {
1005  if (copy_mb) {
1006  copy_block8(ptr, reference_data + block_offset,
1007  linesize, linesize, 8);
1008  } else {
1009  s->dsp.idct_put(ptr, linesize, *block);
1010  ptr += 8;
1011  }
1012  }
1013  }
1014  }
1015  return 0;
1016 }
1017 
1019  const AVFrame *reference)
1020 {
1021  int len, nb_components, i, h, v, predictor, point_transform;
1022  int index, id, ret;
1023  const int block_size = s->lossless ? 1 : 8;
1024  int ilv, prev_shift;
1025 
1026  /* XXX: verify len field validity */
1027  len = get_bits(&s->gb, 16);
1028  nb_components = get_bits(&s->gb, 8);
1029  if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
1031  "decode_sos: nb_components (%d) unsupported\n", nb_components);
1032  return AVERROR_PATCHWELCOME;
1033  }
1034  if (len != 6 + 2 * nb_components) {
1035  av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
1036  return AVERROR_INVALIDDATA;
1037  }
1038  for (i = 0; i < nb_components; i++) {
1039  id = get_bits(&s->gb, 8) - 1;
1040  av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1041  /* find component index */
1042  for (index = 0; index < s->nb_components; index++)
1043  if (id == s->component_id[index])
1044  break;
1045  if (index == s->nb_components) {
1047  "decode_sos: index(%d) out of components\n", index);
1048  return AVERROR_INVALIDDATA;
1049  }
1050  /* Metasoft MJPEG codec has Cb and Cr swapped */
1051  if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1052  && nb_components == 3 && s->nb_components == 3 && i)
1053  index = 3 - i;
1054 
1055  s->comp_index[i] = index;
1056 
1057  s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1058  s->h_scount[i] = s->h_count[index];
1059  s->v_scount[i] = s->v_count[index];
1060 
1061  s->dc_index[i] = get_bits(&s->gb, 4);
1062  s->ac_index[i] = get_bits(&s->gb, 4);
1063 
1064  if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1065  s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1066  goto out_of_range;
1067  if (!s->vlcs[0][s->dc_index[i]].table ||
1068  !s->vlcs[1][s->ac_index[i]].table)
1069  goto out_of_range;
1070  }
1071 
1072  predictor = get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1073  ilv = get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1074  prev_shift = get_bits(&s->gb, 4); /* Ah */
1075  point_transform = get_bits(&s->gb, 4); /* Al */
1076 
1077  if (nb_components > 1) {
1078  /* interleaved stream */
1079  s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1080  s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1081  } else if (!s->ls) { /* skip this for JPEG-LS */
1082  h = s->h_max / s->h_scount[0];
1083  v = s->v_max / s->v_scount[0];
1084  s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1085  s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1086  s->nb_blocks[0] = 1;
1087  s->h_scount[0] = 1;
1088  s->v_scount[0] = 1;
1089  }
1090 
1091  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1092  av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n",
1093  s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1094  predictor, point_transform, ilv, s->bits,
1095  s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
1096 
1097 
1098  /* mjpeg-b can have padding bytes between sos and image data, skip them */
1099  for (i = s->mjpb_skiptosod; i > 0; i--)
1100  skip_bits(&s->gb, 8);
1101 
1102 next_field:
1103  for (i = 0; i < nb_components; i++)
1104  s->last_dc[i] = 1024;
1105 
1106  if (s->lossless) {
1107  if (CONFIG_JPEGLS_DECODER && s->ls) {
1108 // for () {
1109 // reset_ls_coding_parameters(s, 0);
1110 
1111  if ((ret = ff_jpegls_decode_picture(s, predictor,
1112  point_transform, ilv)) < 0)
1113  return ret;
1114  } else {
1115  if (s->rgb) {
1116  if ((ret = ljpeg_decode_rgb_scan(s, predictor,
1117  point_transform)) < 0)
1118  return ret;
1119  } else {
1120  if ((ret = ljpeg_decode_yuv_scan(s, predictor,
1121  point_transform,
1122  nb_components)) < 0)
1123  return ret;
1124  }
1125  }
1126  } else {
1127  if (s->progressive && predictor) {
1128  if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor,
1129  ilv, prev_shift,
1130  point_transform,
1131  mb_bitmask,
1132  reference)) < 0)
1133  return ret;
1134  } else {
1135  if ((ret = mjpeg_decode_scan(s, nb_components,
1136  prev_shift, point_transform,
1137  mb_bitmask, reference)) < 0)
1138  return ret;
1139  }
1140  }
1141 
1142  if (s->interlaced &&
1143  get_bits_left(&s->gb) > 32 &&
1144  show_bits(&s->gb, 8) == 0xFF) {
1145  GetBitContext bak = s->gb;
1146  align_get_bits(&bak);
1147  if (show_bits(&bak, 16) == 0xFFD1) {
1148  av_dlog(s->avctx, "AVRn interlaced picture marker found\n");
1149  s->gb = bak;
1150  skip_bits(&s->gb, 16);
1151  s->bottom_field ^= 1;
1152 
1153  goto next_field;
1154  }
1155  }
1156 
1157  emms_c();
1158  return 0;
1159  out_of_range:
1160  av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1161  return AVERROR_INVALIDDATA;
1162 }
1163 
1165 {
1166  if (get_bits(&s->gb, 16) != 4)
1167  return AVERROR_INVALIDDATA;
1168  s->restart_interval = get_bits(&s->gb, 16);
1169  s->restart_count = 0;
1170  av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1171  s->restart_interval);
1172 
1173  return 0;
1174 }
1175 
1177 {
1178  int len, id, i;
1179 
1180  len = get_bits(&s->gb, 16);
1181  if (len < 5)
1182  return AVERROR_INVALIDDATA;
1183  if (8 * len > get_bits_left(&s->gb))
1184  return AVERROR_INVALIDDATA;
1185 
1186  id = get_bits_long(&s->gb, 32);
1187  id = av_be2ne32(id);
1188  len -= 6;
1189 
1190  if (s->avctx->debug & FF_DEBUG_STARTCODE)
1191  av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
1192 
1193  /* Buggy AVID, it puts EOI only at every 10th frame. */
1194  /* Also, this fourcc is used by non-avid files too, it holds some
1195  information, but it's always present in AVID-created files. */
1196  if (id == AV_RL32("AVI1")) {
1197  /* structure:
1198  4bytes AVI1
1199  1bytes polarity
1200  1bytes always zero
1201  4bytes field_size
1202  4bytes field_size_less_padding
1203  */
1204  s->buggy_avid = 1;
1205  i = get_bits(&s->gb, 8);
1206  if (i == 2)
1207  s->bottom_field = 1;
1208  else if (i == 1)
1209  s->bottom_field = 0;
1210 #if 0
1211  skip_bits(&s->gb, 8);
1212  skip_bits(&s->gb, 32);
1213  skip_bits(&s->gb, 32);
1214  len -= 10;
1215 #endif
1216  goto out;
1217  }
1218 
1219 // len -= 2;
1220 
1221  if (id == AV_RL32("JFIF")) {
1222  int t_w, t_h, v1, v2;
1223  skip_bits(&s->gb, 8); /* the trailing zero-byte */
1224  v1 = get_bits(&s->gb, 8);
1225  v2 = get_bits(&s->gb, 8);
1226  skip_bits(&s->gb, 8);
1227 
1228  s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1229  s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1230 
1231  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1232  av_log(s->avctx, AV_LOG_INFO,
1233  "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1234  v1, v2,
1237 
1238  t_w = get_bits(&s->gb, 8);
1239  t_h = get_bits(&s->gb, 8);
1240  if (t_w && t_h) {
1241  /* skip thumbnail */
1242  if (len -10 - (t_w * t_h * 3) > 0)
1243  len -= t_w * t_h * 3;
1244  }
1245  len -= 10;
1246  goto out;
1247  }
1248 
1249  if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e')) {
1250  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1251  av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
1252  skip_bits(&s->gb, 16); /* version */
1253  skip_bits(&s->gb, 16); /* flags0 */
1254  skip_bits(&s->gb, 16); /* flags1 */
1255  skip_bits(&s->gb, 8); /* transform */
1256  len -= 7;
1257  goto out;
1258  }
1259 
1260  if (id == AV_RL32("LJIF")) {
1261  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1262  av_log(s->avctx, AV_LOG_INFO,
1263  "Pegasus lossless jpeg header found\n");
1264  skip_bits(&s->gb, 16); /* version ? */
1265  skip_bits(&s->gb, 16); /* unknwon always 0? */
1266  skip_bits(&s->gb, 16); /* unknwon always 0? */
1267  skip_bits(&s->gb, 16); /* unknwon always 0? */
1268  switch (get_bits(&s->gb, 8)) {
1269  case 1:
1270  s->rgb = 1;
1271  s->pegasus_rct = 0;
1272  break;
1273  case 2:
1274  s->rgb = 1;
1275  s->pegasus_rct = 1;
1276  break;
1277  default:
1278  av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
1279  }
1280  len -= 9;
1281  goto out;
1282  }
1283 
1284  /* Apple MJPEG-A */
1285  if ((s->start_code == APP1) && (len > (0x28 - 8))) {
1286  id = get_bits_long(&s->gb, 32);
1287  id = av_be2ne32(id);
1288  len -= 4;
1289  /* Apple MJPEG-A */
1290  if (id == AV_RL32("mjpg")) {
1291 #if 0
1292  skip_bits(&s->gb, 32); /* field size */
1293  skip_bits(&s->gb, 32); /* pad field size */
1294  skip_bits(&s->gb, 32); /* next off */
1295  skip_bits(&s->gb, 32); /* quant off */
1296  skip_bits(&s->gb, 32); /* huff off */
1297  skip_bits(&s->gb, 32); /* image off */
1298  skip_bits(&s->gb, 32); /* scan off */
1299  skip_bits(&s->gb, 32); /* data off */
1300 #endif
1301  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1302  av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1303  }
1304  }
1305 
1306 out:
1307  /* slow but needed for extreme adobe jpegs */
1308  if (len < 0)
1310  "mjpeg: error, decode_app parser read over the end\n");
1311  while (--len > 0)
1312  skip_bits(&s->gb, 8);
1313 
1314  return 0;
1315 }
1316 
1318 {
1319  int len = get_bits(&s->gb, 16);
1320  if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
1321  char *cbuf = av_malloc(len - 1);
1322  if (cbuf) {
1323  int i;
1324  for (i = 0; i < len - 2; i++)
1325  cbuf[i] = get_bits(&s->gb, 8);
1326  if (i > 0 && cbuf[i - 1] == '\n')
1327  cbuf[i - 1] = 0;
1328  else
1329  cbuf[i] = 0;
1330 
1331  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1332  av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
1333 
1334  /* buggy avid, it puts EOI only at every 10th frame */
1335  if (!strcmp(cbuf, "AVID")) {
1336  s->buggy_avid = 1;
1337  } else if (!strcmp(cbuf, "CS=ITU601"))
1338  s->cs_itu601 = 1;
1339  else if ((len > 20 && !strncmp(cbuf, "Intel(R) JPEG Library", 21)) ||
1340  (len > 19 && !strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
1341  s->flipped = 1;
1342 
1343  av_free(cbuf);
1344  }
1345  }
1346 
1347  return 0;
1348 }
1349 
1350 /* return the 8 bit start code value and update the search
1351  state. Return -1 if no start code found */
1352 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
1353 {
1354  const uint8_t *buf_ptr;
1355  unsigned int v, v2;
1356  int val;
1357 #ifdef DEBUG
1358  int skipped = 0;
1359 #endif
1360 
1361  buf_ptr = *pbuf_ptr;
1362  while (buf_ptr < buf_end) {
1363  v = *buf_ptr++;
1364  v2 = *buf_ptr;
1365  if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1366  val = *buf_ptr++;
1367  goto found;
1368  }
1369 #ifdef DEBUG
1370  skipped++;
1371 #endif
1372  }
1373  val = -1;
1374 found:
1375  av_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
1376  *pbuf_ptr = buf_ptr;
1377  return val;
1378 }
1379 
1381  const uint8_t **buf_ptr, const uint8_t *buf_end,
1382  const uint8_t **unescaped_buf_ptr,
1383  int *unescaped_buf_size)
1384 {
1385  int start_code;
1386  start_code = find_marker(buf_ptr, buf_end);
1387 
1388  av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
1389  if (!s->buffer)
1390  return AVERROR(ENOMEM);
1391 
1392  /* unescape buffer of SOS, use special treatment for JPEG-LS */
1393  if (start_code == SOS && !s->ls) {
1394  const uint8_t *src = *buf_ptr;
1395  uint8_t *dst = s->buffer;
1396 
1397  while (src < buf_end) {
1398  uint8_t x = *(src++);
1399 
1400  *(dst++) = x;
1401  if (s->avctx->codec_id != AV_CODEC_ID_THP) {
1402  if (x == 0xff) {
1403  while (src < buf_end && x == 0xff)
1404  x = *(src++);
1405 
1406  if (x >= 0xd0 && x <= 0xd7)
1407  *(dst++) = x;
1408  else if (x)
1409  break;
1410  }
1411  }
1412  }
1413  *unescaped_buf_ptr = s->buffer;
1414  *unescaped_buf_size = dst - s->buffer;
1415  memset(s->buffer + *unescaped_buf_size, 0,
1417 
1418  av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
1419  (buf_end - *buf_ptr) - (dst - s->buffer));
1420  } else if (start_code == SOS && s->ls) {
1421  const uint8_t *src = *buf_ptr;
1422  uint8_t *dst = s->buffer;
1423  int bit_count = 0;
1424  int t = 0, b = 0;
1425  PutBitContext pb;
1426 
1427  s->cur_scan++;
1428 
1429  /* find marker */
1430  while (src + t < buf_end) {
1431  uint8_t x = src[t++];
1432  if (x == 0xff) {
1433  while ((src + t < buf_end) && x == 0xff)
1434  x = src[t++];
1435  if (x & 0x80) {
1436  t -= 2;
1437  break;
1438  }
1439  }
1440  }
1441  bit_count = t * 8;
1442  init_put_bits(&pb, dst, t);
1443 
1444  /* unescape bitstream */
1445  while (b < t) {
1446  uint8_t x = src[b++];
1447  put_bits(&pb, 8, x);
1448  if (x == 0xFF) {
1449  x = src[b++];
1450  put_bits(&pb, 7, x);
1451  bit_count--;
1452  }
1453  }
1454  flush_put_bits(&pb);
1455 
1456  *unescaped_buf_ptr = dst;
1457  *unescaped_buf_size = (bit_count + 7) >> 3;
1458  memset(s->buffer + *unescaped_buf_size, 0,
1460  } else {
1461  *unescaped_buf_ptr = *buf_ptr;
1462  *unescaped_buf_size = buf_end - *buf_ptr;
1463  }
1464 
1465  return start_code;
1466 }
1467 
1468 int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1469  AVPacket *avpkt)
1470 {
1471  const uint8_t *buf = avpkt->data;
1472  int buf_size = avpkt->size;
1473  MJpegDecodeContext *s = avctx->priv_data;
1474  const uint8_t *buf_end, *buf_ptr;
1475  const uint8_t *unescaped_buf_ptr;
1476  int unescaped_buf_size;
1477  int start_code;
1478  int ret = 0;
1479  AVFrame *picture = data;
1480 
1481  s->got_picture = 0; // picture from previous image can not be reused
1482  buf_ptr = buf;
1483  buf_end = buf + buf_size;
1484  while (buf_ptr < buf_end) {
1485  /* find start next marker */
1486  start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
1487  &unescaped_buf_ptr,
1488  &unescaped_buf_size);
1489  /* EOF */
1490  if (start_code < 0) {
1491  goto the_end;
1492  } else if (unescaped_buf_size > INT_MAX / 8) {
1493  av_log(avctx, AV_LOG_ERROR,
1494  "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
1495  start_code, unescaped_buf_size, buf_size);
1496  return AVERROR_INVALIDDATA;
1497  } else {
1498  av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n",
1499  start_code, buf_end - buf_ptr);
1500 
1501  ret = init_get_bits(&s->gb, unescaped_buf_ptr,
1502  unescaped_buf_size * 8);
1503 
1504  if (ret < 0)
1505  return ret;
1506 
1507  s->start_code = start_code;
1508  if (s->avctx->debug & FF_DEBUG_STARTCODE)
1509  av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1510 
1511  /* process markers */
1512  if (start_code >= 0xd0 && start_code <= 0xd7)
1513  av_log(avctx, AV_LOG_DEBUG,
1514  "restart marker: %d\n", start_code & 0x0f);
1515  /* APP fields */
1516  else if (start_code >= APP0 && start_code <= APP15)
1517  mjpeg_decode_app(s);
1518  /* Comment */
1519  else if (start_code == COM)
1520  mjpeg_decode_com(s);
1521 
1522  if (!CONFIG_JPEGLS_DECODER &&
1523  (start_code == SOF48 || start_code == LSE)) {
1524  av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n");
1525  return AVERROR(ENOSYS);
1526  }
1527 
1528  switch (start_code) {
1529  case SOI:
1530  s->restart_interval = 0;
1531  s->restart_count = 0;
1532  /* nothing to do on SOI */
1533  break;
1534  case DQT:
1536  break;
1537  case DHT:
1538  if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
1539  av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1540  return ret;
1541  }
1542  break;
1543  case SOF0:
1544  case SOF1:
1545  s->lossless = 0;
1546  s->ls = 0;
1547  s->progressive = 0;
1548  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1549  return ret;
1550  break;
1551  case SOF2:
1552  s->lossless = 0;
1553  s->ls = 0;
1554  s->progressive = 1;
1555  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1556  return ret;
1557  break;
1558  case SOF3:
1559  s->lossless = 1;
1560  s->ls = 0;
1561  s->progressive = 0;
1562  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1563  return ret;
1564  break;
1565  case SOF48:
1566  s->lossless = 1;
1567  s->ls = 1;
1568  s->progressive = 0;
1569  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1570  return ret;
1571  break;
1572  case LSE:
1573  if (!CONFIG_JPEGLS_DECODER ||
1574  (ret = ff_jpegls_decode_lse(s)) < 0)
1575  return ret;
1576  break;
1577  case EOI:
1578  s->cur_scan = 0;
1579  if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1580  break;
1581 eoi_parser:
1582  if (!s->got_picture) {
1583  av_log(avctx, AV_LOG_WARNING,
1584  "Found EOI before any SOF, ignoring\n");
1585  break;
1586  }
1587  if (s->interlaced) {
1588  s->bottom_field ^= 1;
1589  /* if not bottom field, do not output image yet */
1590  if (s->bottom_field == !s->interlace_polarity)
1591  goto not_the_end;
1592  }
1593  *picture = *s->picture_ptr;
1594  *got_frame = 1;
1595 
1596  if (!s->lossless) {
1597  picture->quality = FFMAX3(s->qscale[0],
1598  s->qscale[1],
1599  s->qscale[2]);
1600  picture->qstride = 0;
1601  picture->qscale_table = s->qscale_table;
1602  memset(picture->qscale_table, picture->quality,
1603  (s->width + 15) / 16);
1604  if (avctx->debug & FF_DEBUG_QP)
1605  av_log(avctx, AV_LOG_DEBUG,
1606  "QP: %d\n", picture->quality);
1607  picture->quality *= FF_QP2LAMBDA;
1608  }
1609 
1610  goto the_end;
1611  case SOS:
1612  if (!s->got_picture) {
1613  av_log(avctx, AV_LOG_WARNING,
1614  "Can not process SOS before SOF, skipping\n");
1615  break;
1616  }
1617  if ((ret = ff_mjpeg_decode_sos(s, NULL, NULL)) < 0 &&
1618  (avctx->err_recognition & AV_EF_EXPLODE))
1619  return ret;
1620  /* buggy avid puts EOI every 10-20th frame */
1621  /* if restart period is over process EOI */
1622  if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1623  goto eoi_parser;
1624  break;
1625  case DRI:
1626  mjpeg_decode_dri(s);
1627  break;
1628  case SOF5:
1629  case SOF6:
1630  case SOF7:
1631  case SOF9:
1632  case SOF10:
1633  case SOF11:
1634  case SOF13:
1635  case SOF14:
1636  case SOF15:
1637  case JPG:
1638  av_log(avctx, AV_LOG_ERROR,
1639  "mjpeg: unsupported coding type (%x)\n", start_code);
1640  break;
1641  }
1642 
1643 not_the_end:
1644  /* eof process start code */
1645  buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
1646  av_log(avctx, AV_LOG_DEBUG,
1647  "marker parser used %d bytes (%d bits)\n",
1648  (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
1649  }
1650  }
1651  if (s->got_picture) {
1652  av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
1653  goto eoi_parser;
1654  }
1655  av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
1656  return AVERROR_INVALIDDATA;
1657 the_end:
1658  av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n",
1659  buf_end - buf_ptr);
1660 // return buf_end - buf_ptr;
1661  return buf_ptr - buf;
1662 }
1663 
1665 {
1666  MJpegDecodeContext *s = avctx->priv_data;
1667  int i, j;
1668 
1669  if (s->picture_ptr && s->picture_ptr->data[0])
1670  avctx->release_buffer(avctx, s->picture_ptr);
1671 
1672  av_free(s->buffer);
1673  av_free(s->qscale_table);
1674  av_freep(&s->ljpeg_buffer);
1675  s->ljpeg_buffer_size = 0;
1676 
1677  for (i = 0; i < 3; i++) {
1678  for (j = 0; j < 4; j++)
1679  ff_free_vlc(&s->vlcs[i][j]);
1680  }
1681  for (i = 0; i < MAX_COMPONENTS; i++) {
1682  av_freep(&s->blocks[i]);
1683  av_freep(&s->last_nnz[i]);
1684  }
1685  return 0;
1686 }
1687 
1688 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
1689 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1690 static const AVOption options[] = {
1691  { "extern_huff", "Use external huffman table.",
1692  OFFSET(extern_huff), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
1693  { NULL },
1694 };
1695 
1696 static const AVClass mjpegdec_class = {
1697  .class_name = "MJPEG decoder",
1698  .item_name = av_default_item_name,
1699  .option = options,
1700  .version = LIBAVUTIL_VERSION_INT,
1701 };
1702 
1704  .name = "mjpeg",
1705  .type = AVMEDIA_TYPE_VIDEO,
1706  .id = AV_CODEC_ID_MJPEG,
1707  .priv_data_size = sizeof(MJpegDecodeContext),
1711  .capabilities = CODEC_CAP_DR1,
1712  .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
1713  .priv_class = &mjpegdec_class,
1714 };
1715 
1717  .name = "thp",
1718  .type = AVMEDIA_TYPE_VIDEO,
1719  .id = AV_CODEC_ID_THP,
1720  .priv_data_size = sizeof(MJpegDecodeContext),
1724  .capabilities = CODEC_CAP_DR1,
1725  .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
1726 };
int block_stride[MAX_COMPONENTS]
Definition: mjpegdec.h:73
Definition: mjpeg.h:115
const struct AVCodec * codec
Definition: avcodec.h:1348
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2656
int v_count[MAX_COMPONENTS]
Definition: mjpegdec.h:76
Definition: mjpeg.h:57
const uint8_t ff_zigzag_direct[64]
Definition: dsputil.c:59
int size
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
int qstride
QP store stride.
Definition: avcodec.h:1145
AVOption.
Definition: opt.h:233
enum AVCodecID id
Definition: mxfenc.c:85
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
JPEG-LS.
Definition: mjpeg.h:107
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
DCTELEM(*[MAX_COMPONENTS] blocks)[64]
intermediate sums (progressive mode)
Definition: mjpegdec.h:92
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:197
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: mjpeg.c:73
static void align_get_bits(GetBitContext *s)
Definition: get_bits.h:412
static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz, int ac_index, int16_t *quant_matrix, int ss, int se, int Al, int *EOBRUN)
Definition: mjpegdec.c:497
int h_scount[MAX_COMPONENTS]
Definition: mjpegdec.h:81
Definition: mjpeg.h:53
AVFrame picture
Definition: mjpegdec.h:86
int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
Definition: mjpegdec.c:126
static int mjpeg_decode_com(MJpegDecodeContext *s)
Definition: mjpegdec.c:1317
int num
numerator
Definition: rational.h:44
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:149
int qscale[4]
quantizer scale calculated from quant_matrixes
Definition: mjpegdec.h:51
int size
Definition: avcodec.h:916
uint8_t * buffer
Definition: mjpegdec.h:47
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:1724
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static const AVClass mjpegdec_class
Definition: mjpegdec.c:1696
Definition: mjpeg.h:74
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer with padding, reusing the given one if large enough.
Definition: utils.c:85
int dc_index[MAX_COMPONENTS]
Definition: mjpegdec.h:78
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 << interlaced
Definition: mjpegdec.h:89
uint8_t permutated[64]
Definition: dsputil.h:183
uint8_t run
Definition: svq3.c:132
#define av_be2ne32(x)
Definition: bswap.h:93
int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
Definition: mjpegdec.c:159
Definition: mjpeg.h:56
AVCodec.
Definition: avcodec.h:2960
JPEG-LS decoder.
MJPEG encoder and decoder.
int comp_index[MAX_COMPONENTS]
Definition: mjpegdec.h:77
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
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:38
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:72
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
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:259
uint8_t bits
Definition: crc.c:31
uint8_t
static int mjpeg_decode_dri(MJpegDecodeContext *s)
Definition: mjpegdec.c:1164
AVOptions.
uint16_t(* ljpeg_buffer)[4]
Definition: mjpegdec.h:110
Definition: mjpeg.h:50
#define b
Definition: input.c:52
unsigned int ljpeg_buffer_size
Definition: mjpegdec.h:111
int16_t quant_matrixes[4][64]
Definition: mjpegdec.h:49
#define emms_c()
Definition: internal.h:145
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
uint8_t * last_nnz[MAX_COMPONENTS]
Definition: mjpegdec.h:93
AVFrame * picture_ptr
Definition: mjpegdec.h:87
mpeg1, jpeg, h263
Definition: avcodec.h:586
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
#define MAX_COMPONENTS
Definition: mjpegdec.h:38
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:101
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
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:75
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:78
uint8_t idct_permutation[64]
idct input permutation.
Definition: dsputil.h:425
int interlaced_frame
The content of the picture is interlaced.
Definition: avcodec.h:1232
Definition: mjpeg.h:49
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:820
Definition: mjpeg.h:77
Definition: mjpeg.h:83
Definition: mjpeg.h:48
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2086
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
#define PREDICT(ret, topleft, top, left, predictor)
Definition: mjpeg.h:128
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:547
int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mjpegdec.c:1468
enum AVCodecID id
Definition: avcodec.h:2974
AVCodec ff_mjpeg_decoder
Definition: mjpegdec.c:1703
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:160
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
static const uint16_t mask[17]
Definition: lzw.c:38
int nb_blocks[MAX_COMPONENTS]
Definition: mjpegdec.h:80
Definition: mjpeg.h:43
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:1664
VLC vlcs[3][4]
Definition: mjpegdec.h:50
void(* idct_put)(uint8_t *dest, int line_size, DCTELEM *block)
block -> idct -> clip to unsigned 8 bit -> dest.
Definition: dsputil.h:405
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform)
Definition: mjpegdec.c:660
Definition: mjpeg.h:60
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1434
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
Definition: mjpeg.h:44
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:136
int ff_jpegls_decode_lse(MJpegDecodeContext *s)
Decode LSE block with initialization parameters.
Definition: jpeglsdec.c:52
static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
Definition: mjpegdec.c:1352
#define CLOSE_READER(name, gb)
Definition: get_bits.h:140
void(* clear_block)(DCTELEM *block)
Definition: dsputil.h:218
static void copy_block8(uint8_t *dst, const uint8_t *src, int dstStride, int srcStride, int h)
Definition: dsputil.h:642
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:46
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
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:220
ScanTable scantable
Definition: mjpegdec.h:95
static AVFrame * picture
int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
Definition: mjpegdec.c:213
static DCTELEM block[64]
Definition: dct-test.c:169
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2602
#define CONFIG_JPEGLS_DECODER
Definition: config.h:430
Definition: mjpeg.h:76
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:77
int component_id[MAX_COMPONENTS]
Definition: mjpegdec.h:74
static int mjpeg_decode_app(MJpegDecodeContext *s)
Definition: mjpegdec.c:1176
#define NEG_USR32(a, s)
Definition: mathops.h:159
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: mjpeg.c:65
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
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:251
static char buffer[20]
Definition: seek-test.c:31
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: avcodec.h:1122
int quant_index[4]
Definition: mjpegdec.h:84
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:180
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:515
#define AV_RL32
Definition: intreadwrite.h:146
int v_scount[MAX_COMPONENTS]
Definition: mjpegdec.h:82
#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:449
Definition: mjpeg.h:46
DSPContext dsp
Definition: mjpegdec.h:96
GetBitContext gb
Definition: mjpegdec.h:43
AVCodec ff_thp_decoder
Definition: mjpegdec.c:1716
#define ZERO_RUN
Definition: mjpegdec.c:577
LIBAVUTIL_VERSION_INT
Definition: eval.c:52
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:186
Definition: mjpeg.h:52
Definition: mjpeg.h:75
static const float pred[4]
Definition: siprdata.h:259
Definition: mjpeg.h:54
NULL
Definition: eval.c:52
static int width
Definition: utils.c:156
#define OFFSET(x)
Definition: mjpegdec.c:1688
external API header
enum AVCodecID codec_id
Definition: avcodec.h:1350
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
av_default_item_name
Definition: dnxhdenc.c:43
int debug
debug
Definition: avcodec.h:2568
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
int8_t * qscale_table
Definition: mjpegdec.h:90
static int get_xbits(GetBitContext *s, int n)
read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
Definition: get_bits.h:210
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1365
#define OPEN_READER(name, gb)
Definition: get_bits.h:124
int extradata_size
Definition: avcodec.h:1455
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv)
Definition: jpeglsdec.c:258
int coded_height
Definition: avcodec.h:1515
Describe the class of an AVClass context structure.
Definition: log.h:33
struct MJpegDecodeContext MJpegDecodeContext
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:260
#define VD
Definition: mjpegdec.c:1689
int index
Definition: gxfenc.c:72
static int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
Definition: mjpegdec.c:413
int ac_index[MAX_COMPONENTS]
Definition: mjpegdec.h:79
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
Log a generic warning message about a missing feature.
Definition: utils.c:2007
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
#define GET_CACHE(name, gb)
Definition: get_bits.h:190
uint64_t coefs_finished[MAX_COMPONENTS]
bitmask of which coefs have been completely decoded (progressive mode)
Definition: mjpegdec.h:94
Definition: mjpeg.h:58
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:301
short DCTELEM
Definition: dsputil.h:39
#define MIN_CACHE_BITS
Definition: get_bits.h:120
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int8_t * qscale_table
QP table.
Definition: avcodec.h:1139
uint8_t level
Definition: svq3.c:133
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:85
int height
Definition: gxfenc.c:72
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:1018
static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform, int nb_components)
Definition: mjpegdec.c:731
DCTELEM block[64]
Definition: mjpegdec.h:91
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
Y , 8bpp.
Definition: pixfmt.h:73
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:86
static void build_basic_mjpeg_vlc(MJpegDecodeContext *s)
Definition: mjpegdec.c:69
Definition: mjpeg.h:84
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:79
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:52
int den
denominator
Definition: rational.h:45
DSP utils.
AVCodecContext * avctx
Definition: mjpegdec.h:42
void * priv_data
Definition: avcodec.h:1382
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: mjpeg.c:102
static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al, const uint8_t *mb_bitmask, const AVFrame *reference)
Definition: mjpegdec.c:946
float re
Definition: fft-test.c:64
Definition: mjpeg.h:79
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: avcodec.h:1239
int got_picture
we found a SOF and picture is valid, too.
Definition: mjpegdec.h:88
int len
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: dsputil.c:122
static int decode_block_refinement(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz, int ac_index, int16_t *quant_matrix, int ss, int se, int Al, int *EOBRUN)
Definition: mjpegdec.c:595
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
JPEG-LS extension parameters.
Definition: mjpeg.h:108
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
int last_dc[MAX_COMPONENTS]
Definition: mjpegdec.h:85
#define REFINE_BIT(j)
Definition: mjpegdec.c:569
static int decode_dc_progressive(MJpegDecodeContext *s, DCTELEM *block, int component, int dc_index, int16_t *quant_matrix, int Al)
Definition: mjpegdec.c:479
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:2101
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:1380
static int decode_block(MJpegDecodeContext *s, DCTELEM *block, int component, int dc_index, int ac_index, int16_t *quant_matrix)
Definition: mjpegdec.c:431
MJPEG decoder.
static const AVOption options[]
Definition: mjpegdec.c:1690
This structure stores compressed data.
Definition: avcodec.h:898
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:322
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
Definition: mjpeg.h:51
Definition: mjpeg.h:98
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)