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 len, nb_components, i, width, height, pix_fmt_id;
216 
217  /* XXX: verify len field validity */
218  len = get_bits(&s->gb, 16);
219  s->bits = get_bits(&s->gb, 8);
220 
221  if (s->pegasus_rct)
222  s->bits = 9;
223  if (s->bits == 9 && !s->pegasus_rct)
224  s->rct = 1; // FIXME ugly
225 
226  if (s->bits != 8 && !s->lossless) {
227  av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
228  return -1;
229  }
230 
231  height = get_bits(&s->gb, 16);
232  width = get_bits(&s->gb, 16);
233 
234  // HACK for odd_height.mov
235  if (s->interlaced && s->width == width && s->height == height + 1)
236  height= s->height;
237 
238  av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
239  if (av_image_check_size(width, height, 0, s->avctx))
240  return AVERROR_INVALIDDATA;
241 
242  nb_components = get_bits(&s->gb, 8);
243  if (nb_components <= 0 ||
244  nb_components > MAX_COMPONENTS)
245  return -1;
246  if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
247  if (nb_components != s->nb_components) {
249  "nb_components changing in interlaced picture\n");
250  return AVERROR_INVALIDDATA;
251  }
252  }
253  if (s->ls && !(s->bits <= 8 || nb_components == 1)) {
255  "For JPEG-LS anything except <= 8 bits/component"
256  " or 16-bit gray", 0);
257  return AVERROR_PATCHWELCOME;
258  }
259  s->nb_components = nb_components;
260  s->h_max = 1;
261  s->v_max = 1;
262  for (i = 0; i < nb_components; i++) {
263  /* component id */
264  s->component_id[i] = get_bits(&s->gb, 8) - 1;
265  s->h_count[i] = get_bits(&s->gb, 4);
266  s->v_count[i] = get_bits(&s->gb, 4);
267  /* compute hmax and vmax (only used in interleaved case) */
268  if (s->h_count[i] > s->h_max)
269  s->h_max = s->h_count[i];
270  if (s->v_count[i] > s->v_max)
271  s->v_max = s->v_count[i];
272  s->quant_index[i] = get_bits(&s->gb, 8);
273  if (s->quant_index[i] >= 4)
274  return AVERROR_INVALIDDATA;
275  if (!s->h_count[i] || !s->v_count[i]) {
277  "Invalid sampling factor in component %d %d:%d\n",
278  i, s->h_count[i], s->v_count[i]);
279  return AVERROR_INVALIDDATA;
280  }
281 
282  av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
283  i, s->h_count[i], s->v_count[i],
284  s->component_id[i], s->quant_index[i]);
285  }
286 
287  if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
288  av_log_missing_feature(s->avctx, "Subsampling in JPEG-LS", 0);
289  return AVERROR_PATCHWELCOME;
290  }
291 
292  if (s->v_max == 1 && s->h_max == 1 && s->lossless == 1)
293  s->rgb = 1;
294 
295  /* if different size, realloc/alloc picture */
296  /* XXX: also check h_count and v_count */
297  if (width != s->width || height != s->height) {
298  av_freep(&s->qscale_table);
299 
300  s->width = width;
301  s->height = height;
302  s->interlaced = 0;
303 
304  /* test interlaced mode */
305  if (s->first_picture &&
306  s->org_height != 0 &&
307  s->height < ((s->org_height * 3) / 4)) {
308  s->interlaced = 1;
312  height *= 2;
313  }
314 
315  avcodec_set_dimensions(s->avctx, width, height);
316 
317  s->qscale_table = av_mallocz((s->width + 15) / 16);
318  s->first_picture = 0;
319  }
320 
321  if (!(s->interlaced && (s->bottom_field == !s->interlace_polarity))) {
322  /* XXX: not complete test ! */
323  pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
324  (s->h_count[1] << 20) | (s->v_count[1] << 16) |
325  (s->h_count[2] << 12) | (s->v_count[2] << 8) |
326  (s->h_count[3] << 4) | s->v_count[3];
327  av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
328  /* NOTE we do not allocate pictures large enough for the possible
329  * padding of h/v_count being 4 */
330  if (!(pix_fmt_id & 0xD0D0D0D0))
331  pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
332  if (!(pix_fmt_id & 0x0D0D0D0D))
333  pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
334 
335  switch (pix_fmt_id) {
336  case 0x11111100:
337  if (s->rgb)
339  else
341  assert(s->nb_components == 3);
342  break;
343  case 0x11000000:
345  break;
346  case 0x12111100:
348  break;
349  case 0x21111100:
351  break;
352  case 0x22111100:
354  break;
355  default:
356  av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
357  return AVERROR_PATCHWELCOME;
358  }
359  if (s->ls) {
360  if (s->nb_components > 1)
362  else if (s->bits <= 8)
364  else
366  }
367 
368  if (s->picture_ptr->data[0])
370 
371  if (ff_get_buffer(s->avctx, s->picture_ptr) < 0) {
372  av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
373  return -1;
374  }
376  s->picture_ptr->key_frame = 1;
377  s->got_picture = 1;
378 
379  for (i = 0; i < 3; i++)
380  s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
381 
382  av_dlog(s->avctx, "%d %d %d %d %d %d\n",
383  s->width, s->height, s->linesize[0], s->linesize[1],
384  s->interlaced, s->avctx->height);
385 
386  if (len != (8 + (3 * nb_components)))
387  av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
388  }
389 
390  /* totally blank picture as progressive JPEG will only add details to it */
391  if (s->progressive) {
392  int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8);
393  int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
394  for (i = 0; i < s->nb_components; i++) {
395  int size = bw * bh * s->h_count[i] * s->v_count[i];
396  av_freep(&s->blocks[i]);
397  av_freep(&s->last_nnz[i]);
398  s->blocks[i] = av_malloc(size * sizeof(**s->blocks));
399  s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz));
400  s->block_stride[i] = bw * s->h_count[i];
401  }
402  memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
403  }
404  return 0;
405 }
406 
407 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
408 {
409  int code;
410  code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
411  if (code < 0) {
413  "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
414  0, dc_index, &s->vlcs[0][dc_index]);
415  return 0xffff;
416  }
417 
418  if (code)
419  return get_xbits(&s->gb, code);
420  else
421  return 0;
422 }
423 
424 /* decode block and dequantize */
425 static int decode_block(MJpegDecodeContext *s, DCTELEM *block, int component,
426  int dc_index, int ac_index, int16_t *quant_matrix)
427 {
428  int code, i, j, level, val;
429 
430  /* DC coef */
431  val = mjpeg_decode_dc(s, dc_index);
432  if (val == 0xffff) {
433  av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
434  return AVERROR_INVALIDDATA;
435  }
436  val = val * quant_matrix[0] + s->last_dc[component];
437  s->last_dc[component] = val;
438  block[0] = val;
439  /* AC coefs */
440  i = 0;
441  {OPEN_READER(re, &s->gb);
442  do {
443  UPDATE_CACHE(re, &s->gb);
444  GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
445 
446  i += ((unsigned)code) >> 4;
447  code &= 0xf;
448  if (code) {
449  if (code > MIN_CACHE_BITS - 16)
450  UPDATE_CACHE(re, &s->gb);
451 
452  {
453  int cache = GET_CACHE(re, &s->gb);
454  int sign = (~cache) >> 31;
455  level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
456  }
457 
458  LAST_SKIP_BITS(re, &s->gb, code);
459 
460  if (i > 63) {
461  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
462  return AVERROR_INVALIDDATA;
463  }
464  j = s->scantable.permutated[i];
465  block[j] = level * quant_matrix[j];
466  }
467  } while (i < 63);
468  CLOSE_READER(re, &s->gb);}
469 
470  return 0;
471 }
472 
474  int component, int dc_index,
475  int16_t *quant_matrix, int Al)
476 {
477  int val;
478  s->dsp.clear_block(block);
479  val = mjpeg_decode_dc(s, dc_index);
480  if (val == 0xffff) {
481  av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
482  return AVERROR_INVALIDDATA;
483  }
484  val = (val * quant_matrix[0] << Al) + s->last_dc[component];
485  s->last_dc[component] = val;
486  block[0] = val;
487  return 0;
488 }
489 
490 /* decode block and dequantize - progressive JPEG version */
492  uint8_t *last_nnz, int ac_index,
493  int16_t *quant_matrix,
494  int ss, int se, int Al, int *EOBRUN)
495 {
496  int code, i, j, level, val, run;
497 
498  if (*EOBRUN) {
499  (*EOBRUN)--;
500  return 0;
501  }
502 
503  {
504  OPEN_READER(re, &s->gb);
505  for (i = ss; ; i++) {
506  UPDATE_CACHE(re, &s->gb);
507  GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
508 
509  run = ((unsigned) code) >> 4;
510  code &= 0xF;
511  if (code) {
512  i += run;
513  if (code > MIN_CACHE_BITS - 16)
514  UPDATE_CACHE(re, &s->gb);
515 
516  {
517  int cache = GET_CACHE(re, &s->gb);
518  int sign = (~cache) >> 31;
519  level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
520  }
521 
522  LAST_SKIP_BITS(re, &s->gb, code);
523 
524  if (i >= se) {
525  if (i == se) {
526  j = s->scantable.permutated[se];
527  block[j] = level * quant_matrix[j] << Al;
528  break;
529  }
530  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
531  return AVERROR_INVALIDDATA;
532  }
533  j = s->scantable.permutated[i];
534  block[j] = level * quant_matrix[j] << Al;
535  } else {
536  if (run == 0xF) {// ZRL - skip 15 coefficients
537  i += 15;
538  if (i >= se) {
539  av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
540  return AVERROR_INVALIDDATA;
541  }
542  } else {
543  val = (1 << run);
544  if (run) {
545  UPDATE_CACHE(re, &s->gb);
546  val += NEG_USR32(GET_CACHE(re, &s->gb), run);
547  LAST_SKIP_BITS(re, &s->gb, run);
548  }
549  *EOBRUN = val - 1;
550  break;
551  }
552  }
553  }
554  CLOSE_READER(re, &s->gb);
555  }
556 
557  if (i > *last_nnz)
558  *last_nnz = i;
559 
560  return 0;
561 }
562 
563 #define REFINE_BIT(j) { \
564  UPDATE_CACHE(re, &s->gb); \
565  sign = block[j] >> 15; \
566  block[j] += SHOW_UBITS(re, &s->gb, 1) * \
567  ((quant_matrix[j] ^ sign) - sign) << Al; \
568  LAST_SKIP_BITS(re, &s->gb, 1); \
569 }
570 
571 #define ZERO_RUN \
572 for (; ; i++) { \
573  if (i > last) { \
574  i += run; \
575  if (i > se) { \
576  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
577  return -1; \
578  } \
579  break; \
580  } \
581  j = s->scantable.permutated[i]; \
582  if (block[j]) \
583  REFINE_BIT(j) \
584  else if (run-- == 0) \
585  break; \
586 }
587 
588 /* decode block and dequantize - progressive JPEG refinement pass */
590  uint8_t *last_nnz,
591  int ac_index, int16_t *quant_matrix,
592  int ss, int se, int Al, int *EOBRUN)
593 {
594  int code, i = ss, j, sign, val, run;
595  int last = FFMIN(se, *last_nnz);
596 
597  OPEN_READER(re, &s->gb);
598  if (*EOBRUN) {
599  (*EOBRUN)--;
600  } else {
601  for (; ; i++) {
602  UPDATE_CACHE(re, &s->gb);
603  GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
604 
605  if (code & 0xF) {
606  run = ((unsigned) code) >> 4;
607  UPDATE_CACHE(re, &s->gb);
608  val = SHOW_UBITS(re, &s->gb, 1);
609  LAST_SKIP_BITS(re, &s->gb, 1);
610  ZERO_RUN;
611  j = s->scantable.permutated[i];
612  val--;
613  block[j] = ((quant_matrix[j]^val) - val) << Al;
614  if (i == se) {
615  if (i > *last_nnz)
616  *last_nnz = i;
617  CLOSE_READER(re, &s->gb);
618  return 0;
619  }
620  } else {
621  run = ((unsigned) code) >> 4;
622  if (run == 0xF) {
623  ZERO_RUN;
624  } else {
625  val = run;
626  run = (1 << run);
627  if (val) {
628  UPDATE_CACHE(re, &s->gb);
629  run += SHOW_UBITS(re, &s->gb, val);
630  LAST_SKIP_BITS(re, &s->gb, val);
631  }
632  *EOBRUN = run - 1;
633  break;
634  }
635  }
636  }
637 
638  if (i > *last_nnz)
639  *last_nnz = i;
640  }
641 
642  for (; i <= last; i++) {
643  j = s->scantable.permutated[i];
644  if (block[j])
645  REFINE_BIT(j)
646  }
647  CLOSE_READER(re, &s->gb);
648 
649  return 0;
650 }
651 #undef REFINE_BIT
652 #undef ZERO_RUN
653 
654 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor,
655  int point_transform)
656 {
657  int i, mb_x, mb_y;
658  uint16_t (*buffer)[4];
659  int left[3], top[3], topleft[3];
660  const int linesize = s->linesize[0];
661  const int mask = (1 << s->bits) - 1;
662 
664  (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
665  buffer = s->ljpeg_buffer;
666 
667  for (i = 0; i < 3; i++)
668  buffer[0][i] = 1 << (s->bits + point_transform - 1);
669 
670  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
671  const int modified_predictor = mb_y ? predictor : 1;
672  uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
673 
674  if (s->interlaced && s->bottom_field)
675  ptr += linesize >> 1;
676 
677  for (i = 0; i < 3; i++)
678  top[i] = left[i] = topleft[i] = buffer[0][i];
679 
680  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
681  if (s->restart_interval && !s->restart_count)
683 
684  for (i = 0; i < 3; i++) {
685  int pred;
686 
687  topleft[i] = top[i];
688  top[i] = buffer[mb_x][i];
689 
690  PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
691 
692  left[i] = buffer[mb_x][i] =
693  mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
694  }
695 
696  if (s->restart_interval && !--s->restart_count) {
697  align_get_bits(&s->gb);
698  skip_bits(&s->gb, 16); /* skip RSTn */
699  }
700  }
701 
702  if (s->rct) {
703  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
704  ptr[4 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
705  ptr[4 * mb_x + 0] = buffer[mb_x][1] + ptr[4 * mb_x + 1];
706  ptr[4 * mb_x + 2] = buffer[mb_x][2] + ptr[4 * mb_x + 1];
707  }
708  } else if (s->pegasus_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]) >> 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 {
715  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
716  ptr[4 * mb_x + 0] = buffer[mb_x][2];
717  ptr[4 * mb_x + 1] = buffer[mb_x][1];
718  ptr[4 * mb_x + 2] = buffer[mb_x][0];
719  }
720  }
721  }
722  return 0;
723 }
724 
725 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor,
726  int point_transform, int nb_components)
727 {
728  int i, mb_x, mb_y;
729 
730  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
731  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
732  if (s->restart_interval && !s->restart_count)
734 
735  if (mb_x == 0 || mb_y == 0 || s->interlaced) {
736  for (i = 0; i < nb_components; i++) {
737  uint8_t *ptr;
738  int n, h, v, x, y, c, j, linesize;
739  n = s->nb_blocks[i];
740  c = s->comp_index[i];
741  h = s->h_scount[i];
742  v = s->v_scount[i];
743  x = 0;
744  y = 0;
745  linesize = s->linesize[c];
746 
747  for (j = 0; j < n; j++) {
748  int pred;
749  // FIXME optimize this crap
750  ptr = s->picture_ptr->data[c] +
751  (linesize * (v * mb_y + y)) +
752  (h * mb_x + x);
753  if (y == 0 && mb_y == 0) {
754  if (x == 0 && mb_x == 0)
755  pred = 128 << point_transform;
756  else
757  pred = ptr[-1];
758  } else {
759  if (x == 0 && mb_x == 0)
760  pred = ptr[-linesize];
761  else
762  PREDICT(pred, ptr[-linesize - 1],
763  ptr[-linesize], ptr[-1], predictor);
764  }
765 
766  if (s->interlaced && s->bottom_field)
767  ptr += linesize >> 1;
768  *ptr = pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
769 
770  if (++x == h) {
771  x = 0;
772  y++;
773  }
774  }
775  }
776  } else {
777  for (i = 0; i < nb_components; i++) {
778  uint8_t *ptr;
779  int n, h, v, x, y, c, j, linesize;
780  n = s->nb_blocks[i];
781  c = s->comp_index[i];
782  h = s->h_scount[i];
783  v = s->v_scount[i];
784  x = 0;
785  y = 0;
786  linesize = s->linesize[c];
787 
788  for (j = 0; j < n; j++) {
789  int pred;
790 
791  // FIXME optimize this crap
792  ptr = s->picture_ptr->data[c] +
793  (linesize * (v * mb_y + y)) +
794  (h * mb_x + x);
795  PREDICT(pred, ptr[-linesize - 1],
796  ptr[-linesize], ptr[-1], predictor);
797  *ptr = pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
798  if (++x == h) {
799  x = 0;
800  y++;
801  }
802  }
803  }
804  }
805  if (s->restart_interval && !--s->restart_count) {
806  align_get_bits(&s->gb);
807  skip_bits(&s->gb, 16); /* skip RSTn */
808  }
809  }
810  }
811  return 0;
812 }
813 
814 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
815  int Al, const uint8_t *mb_bitmask,
816  const AVFrame *reference)
817 {
818  int i, mb_x, mb_y;
820  const uint8_t *reference_data[MAX_COMPONENTS];
821  int linesize[MAX_COMPONENTS];
822  GetBitContext mb_bitmask_gb;
823 
824  if (mb_bitmask)
825  init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
826 
827  if (s->flipped && s->avctx->flags & CODEC_FLAG_EMU_EDGE) {
829  "Can not flip image with CODEC_FLAG_EMU_EDGE set!\n");
830  s->flipped = 0;
831  }
832 
833  for (i = 0; i < nb_components; i++) {
834  int c = s->comp_index[i];
835  data[c] = s->picture_ptr->data[c];
836  reference_data[c] = reference ? reference->data[c] : NULL;
837  linesize[c] = s->linesize[c];
838  s->coefs_finished[c] |= 1;
839  if (s->flipped) {
840  // picture should be flipped upside-down for this codec
841  int offset = (linesize[c] * (s->v_scount[i] *
842  (8 * s->mb_height - ((s->height / s->v_max) & 7)) - 1));
843  data[c] += offset;
844  reference_data[c] += offset;
845  linesize[c] *= -1;
846  }
847  }
848 
849  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
850  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
851  const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
852 
853  if (s->restart_interval && !s->restart_count)
855 
856  if (get_bits_left(&s->gb) < 0) {
857  av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
858  -get_bits_left(&s->gb));
859  return AVERROR_INVALIDDATA;
860  }
861  for (i = 0; i < nb_components; i++) {
862  uint8_t *ptr;
863  int n, h, v, x, y, c, j;
864  int block_offset;
865  n = s->nb_blocks[i];
866  c = s->comp_index[i];
867  h = s->h_scount[i];
868  v = s->v_scount[i];
869  x = 0;
870  y = 0;
871  for (j = 0; j < n; j++) {
872  block_offset = ((linesize[c] * (v * mb_y + y) * 8) +
873  (h * mb_x + x) * 8);
874 
875  if (s->interlaced && s->bottom_field)
876  block_offset += linesize[c] >> 1;
877  ptr = data[c] + block_offset;
878  if (!s->progressive) {
879  if (copy_mb)
880  copy_block8(ptr, reference_data[c] + block_offset,
881  linesize[c], linesize[c], 8);
882  else {
883  s->dsp.clear_block(s->block);
884  if (decode_block(s, s->block, i,
885  s->dc_index[i], s->ac_index[i],
886  s->quant_matrixes[s->quant_index[c]]) < 0) {
888  "error y=%d x=%d\n", mb_y, mb_x);
889  return AVERROR_INVALIDDATA;
890  }
891  s->dsp.idct_put(ptr, linesize[c], s->block);
892  }
893  } else {
894  int block_idx = s->block_stride[c] * (v * mb_y + y) +
895  (h * mb_x + x);
896  DCTELEM *block = s->blocks[c][block_idx];
897  if (Ah)
898  block[0] += get_bits1(&s->gb) *
899  s->quant_matrixes[s->quant_index[c]][0] << Al;
900  else if (decode_dc_progressive(s, block, i, s->dc_index[i],
901  s->quant_matrixes[s->quant_index[c]],
902  Al) < 0) {
904  "error y=%d x=%d\n", mb_y, mb_x);
905  return AVERROR_INVALIDDATA;
906  }
907  }
908  av_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
909  av_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
910  mb_x, mb_y, x, y, c, s->bottom_field,
911  (v * mb_y + y) * 8, (h * mb_x + x) * 8);
912  if (++x == h) {
913  x = 0;
914  y++;
915  }
916  }
917  }
918 
919  if (s->restart_interval) {
920  s->restart_count--;
921  i = 8 + ((-get_bits_count(&s->gb)) & 7);
922  /* skip RSTn */
923  if (show_bits(&s->gb, i) == (1 << i) - 1) {
924  int pos = get_bits_count(&s->gb);
925  align_get_bits(&s->gb);
926  while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
927  skip_bits(&s->gb, 8);
928  if ((get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
929  for (i = 0; i < nb_components; i++) /* reset dc */
930  s->last_dc[i] = 1024;
931  } else
932  skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
933  }
934  }
935  }
936  }
937  return 0;
938 }
939 
941  int se, int Ah, int Al,
942  const uint8_t *mb_bitmask,
943  const AVFrame *reference)
944 {
945  int mb_x, mb_y;
946  int EOBRUN = 0;
947  int c = s->comp_index[0];
948  uint8_t *data = s->picture_ptr->data[c];
949  const uint8_t *reference_data = reference ? reference->data[c] : NULL;
950  int linesize = s->linesize[c];
951  int last_scan = 0;
952  int16_t *quant_matrix = s->quant_matrixes[s->quant_index[c]];
953  GetBitContext mb_bitmask_gb;
954 
955  if (ss < 0 || ss >= 64 ||
956  se < ss || se >= 64 ||
957  Ah < 0 || Al < 0)
958  return AVERROR_INVALIDDATA;
959 
960  if (mb_bitmask)
961  init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
962 
963  if (!Al) {
964  s->coefs_finished[c] |= (1LL << (se + 1)) - (1LL << ss);
965  last_scan = !~s->coefs_finished[c];
966  }
967 
968  if (s->interlaced && s->bottom_field) {
969  int offset = linesize >> 1;
970  data += offset;
971  reference_data += offset;
972  }
973 
974  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
975  int block_offset = mb_y * linesize * 8;
976  uint8_t *ptr = data + block_offset;
977  int block_idx = mb_y * s->block_stride[c];
978  DCTELEM (*block)[64] = &s->blocks[c][block_idx];
979  uint8_t *last_nnz = &s->last_nnz[c][block_idx];
980  for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
981  const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
982 
983  if (!copy_mb) {
984  int ret;
985  if (Ah)
986  ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
987  quant_matrix, ss, se, Al, &EOBRUN);
988  else
989  ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
990  quant_matrix, ss, se, Al, &EOBRUN);
991  if (ret < 0) {
993  "error y=%d x=%d\n", mb_y, mb_x);
994  return AVERROR_INVALIDDATA;
995  }
996  }
997 
998  if (last_scan) {
999  if (copy_mb) {
1000  copy_block8(ptr, reference_data + block_offset,
1001  linesize, linesize, 8);
1002  } else {
1003  s->dsp.idct_put(ptr, linesize, *block);
1004  ptr += 8;
1005  }
1006  }
1007  }
1008  }
1009  return 0;
1010 }
1011 
1013  const AVFrame *reference)
1014 {
1015  int len, nb_components, i, h, v, predictor, point_transform;
1016  int index, id, ret;
1017  const int block_size = s->lossless ? 1 : 8;
1018  int ilv, prev_shift;
1019 
1020  /* XXX: verify len field validity */
1021  len = get_bits(&s->gb, 16);
1022  nb_components = get_bits(&s->gb, 8);
1023  if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
1025  "decode_sos: nb_components (%d) unsupported\n", nb_components);
1026  return AVERROR_PATCHWELCOME;
1027  }
1028  if (len != 6 + 2 * nb_components) {
1029  av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
1030  return AVERROR_INVALIDDATA;
1031  }
1032  for (i = 0; i < nb_components; i++) {
1033  id = get_bits(&s->gb, 8) - 1;
1034  av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1035  /* find component index */
1036  for (index = 0; index < s->nb_components; index++)
1037  if (id == s->component_id[index])
1038  break;
1039  if (index == s->nb_components) {
1041  "decode_sos: index(%d) out of components\n", index);
1042  return AVERROR_INVALIDDATA;
1043  }
1044  /* Metasoft MJPEG codec has Cb and Cr swapped */
1045  if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1046  && nb_components == 3 && s->nb_components == 3 && i)
1047  index = 3 - i;
1048 
1049  s->comp_index[i] = index;
1050 
1051  s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1052  s->h_scount[i] = s->h_count[index];
1053  s->v_scount[i] = s->v_count[index];
1054 
1055  s->dc_index[i] = get_bits(&s->gb, 4);
1056  s->ac_index[i] = get_bits(&s->gb, 4);
1057 
1058  if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1059  s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1060  goto out_of_range;
1061  if (!s->vlcs[0][s->dc_index[i]].table ||
1062  !s->vlcs[1][s->ac_index[i]].table)
1063  goto out_of_range;
1064  }
1065 
1066  predictor = get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1067  ilv = get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1068  prev_shift = get_bits(&s->gb, 4); /* Ah */
1069  point_transform = get_bits(&s->gb, 4); /* Al */
1070 
1071  if (nb_components > 1) {
1072  /* interleaved stream */
1073  s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1074  s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1075  } else if (!s->ls) { /* skip this for JPEG-LS */
1076  h = s->h_max / s->h_scount[0];
1077  v = s->v_max / s->v_scount[0];
1078  s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1079  s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1080  s->nb_blocks[0] = 1;
1081  s->h_scount[0] = 1;
1082  s->v_scount[0] = 1;
1083  }
1084 
1085  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1086  av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n",
1087  s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1088  predictor, point_transform, ilv, s->bits,
1089  s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
1090 
1091 
1092  /* mjpeg-b can have padding bytes between sos and image data, skip them */
1093  for (i = s->mjpb_skiptosod; i > 0; i--)
1094  skip_bits(&s->gb, 8);
1095 
1096 next_field:
1097  for (i = 0; i < nb_components; i++)
1098  s->last_dc[i] = 1024;
1099 
1100  if (s->lossless) {
1101  if (CONFIG_JPEGLS_DECODER && s->ls) {
1102 // for () {
1103 // reset_ls_coding_parameters(s, 0);
1104 
1105  if ((ret = ff_jpegls_decode_picture(s, predictor,
1106  point_transform, ilv)) < 0)
1107  return ret;
1108  } else {
1109  if (s->rgb) {
1110  if ((ret = ljpeg_decode_rgb_scan(s, predictor,
1111  point_transform)) < 0)
1112  return ret;
1113  } else {
1114  if ((ret = ljpeg_decode_yuv_scan(s, predictor,
1115  point_transform,
1116  nb_components)) < 0)
1117  return ret;
1118  }
1119  }
1120  } else {
1121  if (s->progressive && predictor) {
1122  if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor,
1123  ilv, prev_shift,
1124  point_transform,
1125  mb_bitmask,
1126  reference)) < 0)
1127  return ret;
1128  } else {
1129  if ((ret = mjpeg_decode_scan(s, nb_components,
1130  prev_shift, point_transform,
1131  mb_bitmask, reference)) < 0)
1132  return ret;
1133  }
1134  }
1135 
1136  if (s->interlaced &&
1137  get_bits_left(&s->gb) > 32 &&
1138  show_bits(&s->gb, 8) == 0xFF) {
1139  GetBitContext bak = s->gb;
1140  align_get_bits(&bak);
1141  if (show_bits(&bak, 16) == 0xFFD1) {
1142  av_dlog(s->avctx, "AVRn interlaced picture marker found\n");
1143  s->gb = bak;
1144  skip_bits(&s->gb, 16);
1145  s->bottom_field ^= 1;
1146 
1147  goto next_field;
1148  }
1149  }
1150 
1151  emms_c();
1152  return 0;
1153  out_of_range:
1154  av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1155  return AVERROR_INVALIDDATA;
1156 }
1157 
1159 {
1160  if (get_bits(&s->gb, 16) != 4)
1161  return AVERROR_INVALIDDATA;
1162  s->restart_interval = get_bits(&s->gb, 16);
1163  s->restart_count = 0;
1164  av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1165  s->restart_interval);
1166 
1167  return 0;
1168 }
1169 
1171 {
1172  int len, id, i;
1173 
1174  len = get_bits(&s->gb, 16);
1175  if (len < 5)
1176  return AVERROR_INVALIDDATA;
1177  if (8 * len > get_bits_left(&s->gb))
1178  return AVERROR_INVALIDDATA;
1179 
1180  id = get_bits_long(&s->gb, 32);
1181  id = av_be2ne32(id);
1182  len -= 6;
1183 
1184  if (s->avctx->debug & FF_DEBUG_STARTCODE)
1185  av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
1186 
1187  /* Buggy AVID, it puts EOI only at every 10th frame. */
1188  /* Also, this fourcc is used by non-avid files too, it holds some
1189  information, but it's always present in AVID-created files. */
1190  if (id == AV_RL32("AVI1")) {
1191  /* structure:
1192  4bytes AVI1
1193  1bytes polarity
1194  1bytes always zero
1195  4bytes field_size
1196  4bytes field_size_less_padding
1197  */
1198  s->buggy_avid = 1;
1199  i = get_bits(&s->gb, 8);
1200  if (i == 2)
1201  s->bottom_field = 1;
1202  else if (i == 1)
1203  s->bottom_field = 0;
1204 #if 0
1205  skip_bits(&s->gb, 8);
1206  skip_bits(&s->gb, 32);
1207  skip_bits(&s->gb, 32);
1208  len -= 10;
1209 #endif
1210  goto out;
1211  }
1212 
1213 // len -= 2;
1214 
1215  if (id == AV_RL32("JFIF")) {
1216  int t_w, t_h, v1, v2;
1217  skip_bits(&s->gb, 8); /* the trailing zero-byte */
1218  v1 = get_bits(&s->gb, 8);
1219  v2 = get_bits(&s->gb, 8);
1220  skip_bits(&s->gb, 8);
1221 
1222  s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1223  s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1224 
1225  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1226  av_log(s->avctx, AV_LOG_INFO,
1227  "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1228  v1, v2,
1231 
1232  t_w = get_bits(&s->gb, 8);
1233  t_h = get_bits(&s->gb, 8);
1234  if (t_w && t_h) {
1235  /* skip thumbnail */
1236  if (len -10 - (t_w * t_h * 3) > 0)
1237  len -= t_w * t_h * 3;
1238  }
1239  len -= 10;
1240  goto out;
1241  }
1242 
1243  if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e')) {
1244  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1245  av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
1246  skip_bits(&s->gb, 16); /* version */
1247  skip_bits(&s->gb, 16); /* flags0 */
1248  skip_bits(&s->gb, 16); /* flags1 */
1249  skip_bits(&s->gb, 8); /* transform */
1250  len -= 7;
1251  goto out;
1252  }
1253 
1254  if (id == AV_RL32("LJIF")) {
1255  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1256  av_log(s->avctx, AV_LOG_INFO,
1257  "Pegasus lossless jpeg header found\n");
1258  skip_bits(&s->gb, 16); /* version ? */
1259  skip_bits(&s->gb, 16); /* unknwon always 0? */
1260  skip_bits(&s->gb, 16); /* unknwon always 0? */
1261  skip_bits(&s->gb, 16); /* unknwon always 0? */
1262  switch (get_bits(&s->gb, 8)) {
1263  case 1:
1264  s->rgb = 1;
1265  s->pegasus_rct = 0;
1266  break;
1267  case 2:
1268  s->rgb = 1;
1269  s->pegasus_rct = 1;
1270  break;
1271  default:
1272  av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
1273  }
1274  len -= 9;
1275  goto out;
1276  }
1277 
1278  /* Apple MJPEG-A */
1279  if ((s->start_code == APP1) && (len > (0x28 - 8))) {
1280  id = get_bits_long(&s->gb, 32);
1281  id = av_be2ne32(id);
1282  len -= 4;
1283  /* Apple MJPEG-A */
1284  if (id == AV_RL32("mjpg")) {
1285 #if 0
1286  skip_bits(&s->gb, 32); /* field size */
1287  skip_bits(&s->gb, 32); /* pad field size */
1288  skip_bits(&s->gb, 32); /* next off */
1289  skip_bits(&s->gb, 32); /* quant off */
1290  skip_bits(&s->gb, 32); /* huff off */
1291  skip_bits(&s->gb, 32); /* image off */
1292  skip_bits(&s->gb, 32); /* scan off */
1293  skip_bits(&s->gb, 32); /* data off */
1294 #endif
1295  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1296  av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1297  }
1298  }
1299 
1300 out:
1301  /* slow but needed for extreme adobe jpegs */
1302  if (len < 0)
1304  "mjpeg: error, decode_app parser read over the end\n");
1305  while (--len > 0)
1306  skip_bits(&s->gb, 8);
1307 
1308  return 0;
1309 }
1310 
1312 {
1313  int len = get_bits(&s->gb, 16);
1314  if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
1315  char *cbuf = av_malloc(len - 1);
1316  if (cbuf) {
1317  int i;
1318  for (i = 0; i < len - 2; i++)
1319  cbuf[i] = get_bits(&s->gb, 8);
1320  if (i > 0 && cbuf[i - 1] == '\n')
1321  cbuf[i - 1] = 0;
1322  else
1323  cbuf[i] = 0;
1324 
1325  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1326  av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
1327 
1328  /* buggy avid, it puts EOI only at every 10th frame */
1329  if (!strcmp(cbuf, "AVID")) {
1330  s->buggy_avid = 1;
1331  } else if (!strcmp(cbuf, "CS=ITU601"))
1332  s->cs_itu601 = 1;
1333  else if ((len > 20 && !strncmp(cbuf, "Intel(R) JPEG Library", 21)) ||
1334  (len > 19 && !strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
1335  s->flipped = 1;
1336 
1337  av_free(cbuf);
1338  }
1339  }
1340 
1341  return 0;
1342 }
1343 
1344 /* return the 8 bit start code value and update the search
1345  state. Return -1 if no start code found */
1346 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
1347 {
1348  const uint8_t *buf_ptr;
1349  unsigned int v, v2;
1350  int val;
1351 #ifdef DEBUG
1352  int skipped = 0;
1353 #endif
1354 
1355  buf_ptr = *pbuf_ptr;
1356  while (buf_ptr < buf_end) {
1357  v = *buf_ptr++;
1358  v2 = *buf_ptr;
1359  if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1360  val = *buf_ptr++;
1361  goto found;
1362  }
1363 #ifdef DEBUG
1364  skipped++;
1365 #endif
1366  }
1367  val = -1;
1368 found:
1369  av_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
1370  *pbuf_ptr = buf_ptr;
1371  return val;
1372 }
1373 
1375  const uint8_t **buf_ptr, const uint8_t *buf_end,
1376  const uint8_t **unescaped_buf_ptr,
1377  int *unescaped_buf_size)
1378 {
1379  int start_code;
1380  start_code = find_marker(buf_ptr, buf_end);
1381 
1382  av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
1383  if (!s->buffer)
1384  return AVERROR(ENOMEM);
1385 
1386  /* unescape buffer of SOS, use special treatment for JPEG-LS */
1387  if (start_code == SOS && !s->ls) {
1388  const uint8_t *src = *buf_ptr;
1389  uint8_t *dst = s->buffer;
1390 
1391  while (src < buf_end) {
1392  uint8_t x = *(src++);
1393 
1394  *(dst++) = x;
1395  if (s->avctx->codec_id != AV_CODEC_ID_THP) {
1396  if (x == 0xff) {
1397  while (src < buf_end && x == 0xff)
1398  x = *(src++);
1399 
1400  if (x >= 0xd0 && x <= 0xd7)
1401  *(dst++) = x;
1402  else if (x)
1403  break;
1404  }
1405  }
1406  }
1407  *unescaped_buf_ptr = s->buffer;
1408  *unescaped_buf_size = dst - s->buffer;
1409  memset(s->buffer + *unescaped_buf_size, 0,
1411 
1412  av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
1413  (buf_end - *buf_ptr) - (dst - s->buffer));
1414  } else if (start_code == SOS && s->ls) {
1415  const uint8_t *src = *buf_ptr;
1416  uint8_t *dst = s->buffer;
1417  int bit_count = 0;
1418  int t = 0, b = 0;
1419  PutBitContext pb;
1420 
1421  s->cur_scan++;
1422 
1423  /* find marker */
1424  while (src + t < buf_end) {
1425  uint8_t x = src[t++];
1426  if (x == 0xff) {
1427  while ((src + t < buf_end) && x == 0xff)
1428  x = src[t++];
1429  if (x & 0x80) {
1430  t -= 2;
1431  break;
1432  }
1433  }
1434  }
1435  bit_count = t * 8;
1436  init_put_bits(&pb, dst, t);
1437 
1438  /* unescape bitstream */
1439  while (b < t) {
1440  uint8_t x = src[b++];
1441  put_bits(&pb, 8, x);
1442  if (x == 0xFF) {
1443  x = src[b++];
1444  put_bits(&pb, 7, x);
1445  bit_count--;
1446  }
1447  }
1448  flush_put_bits(&pb);
1449 
1450  *unescaped_buf_ptr = dst;
1451  *unescaped_buf_size = (bit_count + 7) >> 3;
1452  memset(s->buffer + *unescaped_buf_size, 0,
1454  } else {
1455  *unescaped_buf_ptr = *buf_ptr;
1456  *unescaped_buf_size = buf_end - *buf_ptr;
1457  }
1458 
1459  return start_code;
1460 }
1461 
1462 int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1463  AVPacket *avpkt)
1464 {
1465  const uint8_t *buf = avpkt->data;
1466  int buf_size = avpkt->size;
1467  MJpegDecodeContext *s = avctx->priv_data;
1468  const uint8_t *buf_end, *buf_ptr;
1469  const uint8_t *unescaped_buf_ptr;
1470  int unescaped_buf_size;
1471  int start_code;
1472  int ret = 0;
1473  AVFrame *picture = data;
1474 
1475  s->got_picture = 0; // picture from previous image can not be reused
1476  buf_ptr = buf;
1477  buf_end = buf + buf_size;
1478  while (buf_ptr < buf_end) {
1479  /* find start next marker */
1480  start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
1481  &unescaped_buf_ptr,
1482  &unescaped_buf_size);
1483  /* EOF */
1484  if (start_code < 0) {
1485  goto the_end;
1486  } else if (unescaped_buf_size > INT_MAX / 8) {
1487  av_log(avctx, AV_LOG_ERROR,
1488  "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
1489  start_code, unescaped_buf_size, buf_size);
1490  return AVERROR_INVALIDDATA;
1491  } else {
1492  av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n",
1493  start_code, buf_end - buf_ptr);
1494 
1495  ret = init_get_bits(&s->gb, unescaped_buf_ptr,
1496  unescaped_buf_size * 8);
1497 
1498  if (ret < 0)
1499  return ret;
1500 
1501  s->start_code = start_code;
1502  if (s->avctx->debug & FF_DEBUG_STARTCODE)
1503  av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1504 
1505  /* process markers */
1506  if (start_code >= 0xd0 && start_code <= 0xd7)
1507  av_log(avctx, AV_LOG_DEBUG,
1508  "restart marker: %d\n", start_code & 0x0f);
1509  /* APP fields */
1510  else if (start_code >= APP0 && start_code <= APP15)
1511  mjpeg_decode_app(s);
1512  /* Comment */
1513  else if (start_code == COM)
1514  mjpeg_decode_com(s);
1515 
1516  if (!CONFIG_JPEGLS_DECODER &&
1517  (start_code == SOF48 || start_code == LSE)) {
1518  av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n");
1519  return AVERROR(ENOSYS);
1520  }
1521 
1522  switch (start_code) {
1523  case SOI:
1524  s->restart_interval = 0;
1525  s->restart_count = 0;
1526  /* nothing to do on SOI */
1527  break;
1528  case DQT:
1530  break;
1531  case DHT:
1532  if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
1533  av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1534  return ret;
1535  }
1536  break;
1537  case SOF0:
1538  case SOF1:
1539  s->lossless = 0;
1540  s->ls = 0;
1541  s->progressive = 0;
1542  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1543  return ret;
1544  break;
1545  case SOF2:
1546  s->lossless = 0;
1547  s->ls = 0;
1548  s->progressive = 1;
1549  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1550  return ret;
1551  break;
1552  case SOF3:
1553  s->lossless = 1;
1554  s->ls = 0;
1555  s->progressive = 0;
1556  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1557  return ret;
1558  break;
1559  case SOF48:
1560  s->lossless = 1;
1561  s->ls = 1;
1562  s->progressive = 0;
1563  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1564  return ret;
1565  break;
1566  case LSE:
1567  if (!CONFIG_JPEGLS_DECODER ||
1568  (ret = ff_jpegls_decode_lse(s)) < 0)
1569  return ret;
1570  break;
1571  case EOI:
1572  s->cur_scan = 0;
1573  if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1574  break;
1575 eoi_parser:
1576  if (!s->got_picture) {
1577  av_log(avctx, AV_LOG_WARNING,
1578  "Found EOI before any SOF, ignoring\n");
1579  break;
1580  }
1581  if (s->interlaced) {
1582  s->bottom_field ^= 1;
1583  /* if not bottom field, do not output image yet */
1584  if (s->bottom_field == !s->interlace_polarity)
1585  goto not_the_end;
1586  }
1587  *picture = *s->picture_ptr;
1588  *got_frame = 1;
1589 
1590  if (!s->lossless) {
1591  picture->quality = FFMAX3(s->qscale[0],
1592  s->qscale[1],
1593  s->qscale[2]);
1594  picture->qstride = 0;
1595  picture->qscale_table = s->qscale_table;
1596  memset(picture->qscale_table, picture->quality,
1597  (s->width + 15) / 16);
1598  if (avctx->debug & FF_DEBUG_QP)
1599  av_log(avctx, AV_LOG_DEBUG,
1600  "QP: %d\n", picture->quality);
1601  picture->quality *= FF_QP2LAMBDA;
1602  }
1603 
1604  goto the_end;
1605  case SOS:
1606  if (!s->got_picture) {
1607  av_log(avctx, AV_LOG_WARNING,
1608  "Can not process SOS before SOF, skipping\n");
1609  break;
1610  }
1611  if ((ret = ff_mjpeg_decode_sos(s, NULL, NULL)) < 0 &&
1612  (avctx->err_recognition & AV_EF_EXPLODE))
1613  return ret;
1614  /* buggy avid puts EOI every 10-20th frame */
1615  /* if restart period is over process EOI */
1616  if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1617  goto eoi_parser;
1618  break;
1619  case DRI:
1620  mjpeg_decode_dri(s);
1621  break;
1622  case SOF5:
1623  case SOF6:
1624  case SOF7:
1625  case SOF9:
1626  case SOF10:
1627  case SOF11:
1628  case SOF13:
1629  case SOF14:
1630  case SOF15:
1631  case JPG:
1632  av_log(avctx, AV_LOG_ERROR,
1633  "mjpeg: unsupported coding type (%x)\n", start_code);
1634  break;
1635  }
1636 
1637 not_the_end:
1638  /* eof process start code */
1639  buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
1640  av_log(avctx, AV_LOG_DEBUG,
1641  "marker parser used %d bytes (%d bits)\n",
1642  (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
1643  }
1644  }
1645  if (s->got_picture) {
1646  av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
1647  goto eoi_parser;
1648  }
1649  av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
1650  return AVERROR_INVALIDDATA;
1651 the_end:
1652  av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n",
1653  buf_end - buf_ptr);
1654 // return buf_end - buf_ptr;
1655  return buf_ptr - buf;
1656 }
1657 
1659 {
1660  MJpegDecodeContext *s = avctx->priv_data;
1661  int i, j;
1662 
1663  if (s->picture_ptr && s->picture_ptr->data[0])
1664  avctx->release_buffer(avctx, s->picture_ptr);
1665 
1666  av_free(s->buffer);
1667  av_free(s->qscale_table);
1668  av_freep(&s->ljpeg_buffer);
1669  s->ljpeg_buffer_size = 0;
1670 
1671  for (i = 0; i < 3; i++) {
1672  for (j = 0; j < 4; j++)
1673  ff_free_vlc(&s->vlcs[i][j]);
1674  }
1675  for (i = 0; i < MAX_COMPONENTS; i++) {
1676  av_freep(&s->blocks[i]);
1677  av_freep(&s->last_nnz[i]);
1678  }
1679  return 0;
1680 }
1681 
1682 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
1683 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1684 static const AVOption options[] = {
1685  { "extern_huff", "Use external huffman table.",
1686  OFFSET(extern_huff), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
1687  { NULL },
1688 };
1689 
1690 static const AVClass mjpegdec_class = {
1691  .class_name = "MJPEG decoder",
1692  .item_name = av_default_item_name,
1693  .option = options,
1694  .version = LIBAVUTIL_VERSION_INT,
1695 };
1696 
1698  .name = "mjpeg",
1699  .type = AVMEDIA_TYPE_VIDEO,
1700  .id = AV_CODEC_ID_MJPEG,
1701  .priv_data_size = sizeof(MJpegDecodeContext),
1705  .capabilities = CODEC_CAP_DR1,
1706  .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
1707  .priv_class = &mjpegdec_class,
1708 };
1709 
1711  .name = "thp",
1712  .type = AVMEDIA_TYPE_VIDEO,
1713  .id = AV_CODEC_ID_THP,
1714  .priv_data_size = sizeof(MJpegDecodeContext),
1718  .capabilities = CODEC_CAP_DR1,
1719  .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
1720 };
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:491
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:1311
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:1690
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
static int mjpeg_decode_dri(MJpegDecodeContext *s)
Definition: mjpegdec.c:1158
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:814
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:1462
enum AVCodecID id
Definition: avcodec.h:2974
AVCodec ff_mjpeg_decoder
Definition: mjpegdec.c:1697
#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:1658
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:654
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:1346
#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:1170
#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:1710
#define ZERO_RUN
Definition: mjpegdec.c:571
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:1682
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:1683
int index
Definition: gxfenc.c:72
static int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
Definition: mjpegdec.c:407
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:1012
static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform, int nb_components)
Definition: mjpegdec.c:725
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:940
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:589
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:563
static int decode_dc_progressive(MJpegDecodeContext *s, DCTELEM *block, int component, int dc_index, int16_t *quant_matrix, int Al)
Definition: mjpegdec.c:473
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:1374
static int decode_block(MJpegDecodeContext *s, DCTELEM *block, int component, int dc_index, int ac_index, int16_t *quant_matrix)
Definition: mjpegdec.c:425
MJPEG decoder.
static const AVOption options[]
Definition: mjpegdec.c:1684
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)