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