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