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