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