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