FFmpeg
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 FFmpeg.
12  *
13  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  */
27 
28 /**
29  * @file
30  * MJPEG decoder.
31  */
32 
33 #include "libavutil/imgutils.h"
34 #include "libavutil/avassert.h"
35 #include "libavutil/opt.h"
36 #include "avcodec.h"
37 #include "blockdsp.h"
38 #include "copy_block.h"
39 #include "hwconfig.h"
40 #include "idctdsp.h"
41 #include "internal.h"
42 #include "jpegtables.h"
43 #include "mjpeg.h"
44 #include "mjpegdec.h"
45 #include "jpeglsdec.h"
46 #include "profiles.h"
47 #include "put_bits.h"
48 #include "tiff.h"
49 #include "exif.h"
50 #include "bytestream.h"
51 
52 
53 static void build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
54  const uint8_t *bits_table)
55 {
56  for (int i = 1, code = 0, k = 0; i <= 16; i++) {
57  int nb = bits_table[i];
58  for (int j = 0; j < nb;j++) {
59  huff_size[k] = i;
60  huff_code[k] = code;
61  code++;
62  k++;
63  }
64  code <<= 1;
65  }
66 }
67 
68 static int build_vlc(VLC *vlc, const uint8_t *bits_table,
69  const uint8_t *val_table, int nb_codes,
70  int is_ac)
71 {
72  uint8_t huff_size[256];
73  uint16_t huff_code[256];
74  uint16_t huff_sym[256];
75  int i;
76 
77  av_assert0(nb_codes <= 256);
78 
79  build_huffman_codes(huff_size, huff_code, bits_table);
80 
81  for (i = 0; i < nb_codes; i++) {
82  huff_sym[i] = val_table[i] + 16 * is_ac;
83 
84  if (is_ac && !val_table[i])
85  huff_sym[i] = 16 * 256;
86  }
87 
88  return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
89  huff_code, 2, 2, huff_sym, 2, 2, 0);
90 }
91 
93 {
94  static const struct {
95  int class;
96  int index;
97  const uint8_t *bits;
98  const uint8_t *values;
99  int length;
100  } ht[] = {
102  avpriv_mjpeg_val_dc, 12 },
104  avpriv_mjpeg_val_dc, 12 },
113  };
114  int i, ret;
115 
116  for (i = 0; i < FF_ARRAY_ELEMS(ht); i++) {
117  ret = build_vlc(&s->vlcs[ht[i].class][ht[i].index],
118  ht[i].bits, ht[i].values, ht[i].length,
119  ht[i].class == 1);
120  if (ret < 0)
121  return ret;
122 
123  if (ht[i].class < 2) {
124  memcpy(s->raw_huffman_lengths[ht[i].class][ht[i].index],
125  ht[i].bits + 1, 16);
126  memcpy(s->raw_huffman_values[ht[i].class][ht[i].index],
127  ht[i].values, ht[i].length);
128  }
129  }
130 
131  return 0;
132 }
133 
134 static void parse_avid(MJpegDecodeContext *s, uint8_t *buf, int len)
135 {
136  s->buggy_avid = 1;
137  if (len > 14 && buf[12] == 1) /* 1 - NTSC */
138  s->interlace_polarity = 1;
139  if (len > 14 && buf[12] == 2) /* 2 - PAL */
140  s->interlace_polarity = 0;
141  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
142  av_log(s->avctx, AV_LOG_INFO, "AVID: len:%d %d\n", len, len > 14 ? buf[12] : -1);
143 }
144 
145 static void init_idct(AVCodecContext *avctx)
146 {
147  MJpegDecodeContext *s = avctx->priv_data;
148 
149  ff_idctdsp_init(&s->idsp, avctx);
152 }
153 
155 {
156  MJpegDecodeContext *s = avctx->priv_data;
157  int ret;
158 
159  if (!s->picture_ptr) {
160  s->picture = av_frame_alloc();
161  if (!s->picture)
162  return AVERROR(ENOMEM);
163  s->picture_ptr = s->picture;
164  }
165 
166  s->avctx = avctx;
167  ff_blockdsp_init(&s->bdsp, avctx);
168  ff_hpeldsp_init(&s->hdsp, avctx->flags);
169  init_idct(avctx);
170  s->buffer_size = 0;
171  s->buffer = NULL;
172  s->start_code = -1;
173  s->first_picture = 1;
174  s->got_picture = 0;
175  s->org_height = avctx->coded_height;
177  avctx->colorspace = AVCOL_SPC_BT470BG;
179 
180  if ((ret = init_default_huffman_tables(s)) < 0)
181  return ret;
182 
183  if (s->extern_huff) {
184  av_log(avctx, AV_LOG_INFO, "using external huffman table\n");
185  if ((ret = init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8)) < 0)
186  return ret;
187  if (ff_mjpeg_decode_dht(s)) {
188  av_log(avctx, AV_LOG_ERROR,
189  "error using external huffman table, switching back to internal\n");
191  }
192  }
193  if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
194  s->interlace_polarity = 1; /* bottom field first */
195  av_log(avctx, AV_LOG_DEBUG, "bottom field first\n");
196  } else if (avctx->field_order == AV_FIELD_UNKNOWN) {
197  if (avctx->codec_tag == AV_RL32("MJPG"))
198  s->interlace_polarity = 1;
199  }
200 
201  if ( avctx->extradata_size > 8
202  && AV_RL32(avctx->extradata) == 0x2C
203  && AV_RL32(avctx->extradata+4) == 0x18) {
204  parse_avid(s, avctx->extradata, avctx->extradata_size);
205  }
206 
207  if (avctx->codec->id == AV_CODEC_ID_AMV)
208  s->flipped = 1;
209 
210  return 0;
211 }
212 
213 
214 /* quantize tables */
216 {
217  int len, index, i;
218 
219  len = get_bits(&s->gb, 16) - 2;
220 
221  if (8*len > get_bits_left(&s->gb)) {
222  av_log(s->avctx, AV_LOG_ERROR, "dqt: len %d is too large\n", len);
223  return AVERROR_INVALIDDATA;
224  }
225 
226  while (len >= 65) {
227  int pr = get_bits(&s->gb, 4);
228  if (pr > 1) {
229  av_log(s->avctx, AV_LOG_ERROR, "dqt: invalid precision\n");
230  return AVERROR_INVALIDDATA;
231  }
232  index = get_bits(&s->gb, 4);
233  if (index >= 4)
234  return -1;
235  av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
236  /* read quant table */
237  for (i = 0; i < 64; i++) {
238  s->quant_matrixes[index][i] = get_bits(&s->gb, pr ? 16 : 8);
239  if (s->quant_matrixes[index][i] == 0) {
240  av_log(s->avctx, AV_LOG_ERROR, "dqt: 0 quant value\n");
241  return AVERROR_INVALIDDATA;
242  }
243  }
244 
245  // XXX FIXME fine-tune, and perhaps add dc too
246  s->qscale[index] = FFMAX(s->quant_matrixes[index][1],
247  s->quant_matrixes[index][8]) >> 1;
248  av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n",
249  index, s->qscale[index]);
250  len -= 1 + 64 * (1+pr);
251  }
252  return 0;
253 }
254 
255 /* decode huffman tables and build VLC decoders */
257 {
258  int len, index, i, class, n, v;
259  uint8_t bits_table[17];
260  uint8_t val_table[256];
261  int ret = 0;
262 
263  len = get_bits(&s->gb, 16) - 2;
264 
265  if (8*len > get_bits_left(&s->gb)) {
266  av_log(s->avctx, AV_LOG_ERROR, "dht: len %d is too large\n", len);
267  return AVERROR_INVALIDDATA;
268  }
269 
270  while (len > 0) {
271  if (len < 17)
272  return AVERROR_INVALIDDATA;
273  class = get_bits(&s->gb, 4);
274  if (class >= 2)
275  return AVERROR_INVALIDDATA;
276  index = get_bits(&s->gb, 4);
277  if (index >= 4)
278  return AVERROR_INVALIDDATA;
279  n = 0;
280  for (i = 1; i <= 16; i++) {
281  bits_table[i] = get_bits(&s->gb, 8);
282  n += bits_table[i];
283  }
284  len -= 17;
285  if (len < n || n > 256)
286  return AVERROR_INVALIDDATA;
287 
288  for (i = 0; i < n; i++) {
289  v = get_bits(&s->gb, 8);
290  val_table[i] = v;
291  }
292  len -= n;
293 
294  /* build VLC and flush previous vlc if present */
295  ff_free_vlc(&s->vlcs[class][index]);
296  av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
297  class, index, n);
298  if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table,
299  n, class > 0)) < 0)
300  return ret;
301 
302  if (class > 0) {
303  ff_free_vlc(&s->vlcs[2][index]);
304  if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table,
305  n, 0)) < 0)
306  return ret;
307  }
308 
309  for (i = 0; i < 16; i++)
310  s->raw_huffman_lengths[class][index][i] = bits_table[i + 1];
311  for (i = 0; i < 256; i++)
312  s->raw_huffman_values[class][index][i] = val_table[i];
313  }
314  return 0;
315 }
316 
318 {
319  int len, nb_components, i, width, height, bits, ret, size_change;
320  unsigned pix_fmt_id;
321  int h_count[MAX_COMPONENTS] = { 0 };
322  int v_count[MAX_COMPONENTS] = { 0 };
323 
324  s->cur_scan = 0;
325  memset(s->upscale_h, 0, sizeof(s->upscale_h));
326  memset(s->upscale_v, 0, sizeof(s->upscale_v));
327 
328  len = get_bits(&s->gb, 16);
329  bits = get_bits(&s->gb, 8);
330 
331  if (bits > 16 || bits < 1) {
332  av_log(s->avctx, AV_LOG_ERROR, "bits %d is invalid\n", bits);
333  return AVERROR_INVALIDDATA;
334  }
335 
336  if (s->avctx->bits_per_raw_sample != bits) {
337  av_log(s->avctx, s->avctx->bits_per_raw_sample > 0 ? AV_LOG_INFO : AV_LOG_DEBUG, "Changing bps from %d to %d\n", s->avctx->bits_per_raw_sample, bits);
339  init_idct(s->avctx);
340  }
341  if (s->pegasus_rct)
342  bits = 9;
343  if (bits == 9 && !s->pegasus_rct)
344  s->rct = 1; // FIXME ugly
345 
346  if(s->lossless && s->avctx->lowres){
347  av_log(s->avctx, AV_LOG_ERROR, "lowres is not possible with lossless jpeg\n");
348  return -1;
349  }
350 
351  height = get_bits(&s->gb, 16);
352  width = get_bits(&s->gb, 16);
353 
354  // HACK for odd_height.mov
355  if (s->interlaced && s->width == width && s->height == height + 1)
356  height= s->height;
357 
358  av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
359  if (av_image_check_size(width, height, 0, s->avctx) < 0)
360  return AVERROR_INVALIDDATA;
361  if (s->buf_size && (width + 7) / 8 * ((height + 7) / 8) > s->buf_size * 4LL)
362  return AVERROR_INVALIDDATA;
363 
364  nb_components = get_bits(&s->gb, 8);
365  if (nb_components <= 0 ||
366  nb_components > MAX_COMPONENTS)
367  return -1;
368  if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
369  if (nb_components != s->nb_components) {
371  "nb_components changing in interlaced picture\n");
372  return AVERROR_INVALIDDATA;
373  }
374  }
375  if (s->ls && !(bits <= 8 || nb_components == 1)) {
377  "JPEG-LS that is not <= 8 "
378  "bits/component or 16-bit gray");
379  return AVERROR_PATCHWELCOME;
380  }
381  if (len != 8 + 3 * nb_components) {
382  av_log(s->avctx, AV_LOG_ERROR, "decode_sof0: error, len(%d) mismatch %d components\n", len, nb_components);
383  return AVERROR_INVALIDDATA;
384  }
385 
386  s->nb_components = nb_components;
387  s->h_max = 1;
388  s->v_max = 1;
389  for (i = 0; i < nb_components; i++) {
390  /* component id */
391  s->component_id[i] = get_bits(&s->gb, 8) - 1;
392  h_count[i] = get_bits(&s->gb, 4);
393  v_count[i] = get_bits(&s->gb, 4);
394  /* compute hmax and vmax (only used in interleaved case) */
395  if (h_count[i] > s->h_max)
396  s->h_max = h_count[i];
397  if (v_count[i] > s->v_max)
398  s->v_max = v_count[i];
399  s->quant_index[i] = get_bits(&s->gb, 8);
400  if (s->quant_index[i] >= 4) {
401  av_log(s->avctx, AV_LOG_ERROR, "quant_index is invalid\n");
402  return AVERROR_INVALIDDATA;
403  }
404  if (!h_count[i] || !v_count[i]) {
406  "Invalid sampling factor in component %d %d:%d\n",
407  i, h_count[i], v_count[i]);
408  return AVERROR_INVALIDDATA;
409  }
410 
411  av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
412  i, h_count[i], v_count[i],
413  s->component_id[i], s->quant_index[i]);
414  }
415  if ( nb_components == 4
416  && s->component_id[0] == 'C' - 1
417  && s->component_id[1] == 'M' - 1
418  && s->component_id[2] == 'Y' - 1
419  && s->component_id[3] == 'K' - 1)
420  s->adobe_transform = 0;
421 
422  if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
423  avpriv_report_missing_feature(s->avctx, "Subsampling in JPEG-LS");
424  return AVERROR_PATCHWELCOME;
425  }
426 
427  if (s->bayer) {
428  if (nb_components == 2) {
429  /* Bayer images embedded in DNGs can contain 2 interleaved components and the
430  width stored in their SOF3 markers is the width of each one. We only output
431  a single component, therefore we need to adjust the output image width. We
432  handle the deinterleaving (but not the debayering) in this file. */
433  width *= 2;
434  }
435  /* They can also contain 1 component, which is double the width and half the height
436  of the final image (rows are interleaved). We don't handle the decoding in this
437  file, but leave that to the TIFF/DNG decoder. */
438  }
439 
440  /* if different size, realloc/alloc picture */
441  if (width != s->width || height != s->height || bits != s->bits ||
442  memcmp(s->h_count, h_count, sizeof(h_count)) ||
443  memcmp(s->v_count, v_count, sizeof(v_count))) {
444  size_change = 1;
445 
446  s->width = width;
447  s->height = height;
448  s->bits = bits;
449  memcpy(s->h_count, h_count, sizeof(h_count));
450  memcpy(s->v_count, v_count, sizeof(v_count));
451  s->interlaced = 0;
452  s->got_picture = 0;
453 
454  /* test interlaced mode */
455  if (s->first_picture &&
456  (s->multiscope != 2 || s->avctx->time_base.den >= 25 * s->avctx->time_base.num) &&
457  s->org_height != 0 &&
458  s->height < ((s->org_height * 3) / 4)) {
459  s->interlaced = 1;
463  height *= 2;
464  }
465 
466  ret = ff_set_dimensions(s->avctx, width, height);
467  if (ret < 0)
468  return ret;
469 
470  s->first_picture = 0;
471  } else {
472  size_change = 0;
473  }
474 
475  if (s->got_picture && s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
476  if (s->progressive) {
477  avpriv_request_sample(s->avctx, "progressively coded interlaced picture");
478  return AVERROR_INVALIDDATA;
479  }
480  } else {
481  if (s->v_max == 1 && s->h_max == 1 && s->lossless==1 && (nb_components==3 || nb_components==4))
482  s->rgb = 1;
483  else if (!s->lossless)
484  s->rgb = 0;
485  /* XXX: not complete test ! */
486  pix_fmt_id = ((unsigned)s->h_count[0] << 28) | (s->v_count[0] << 24) |
487  (s->h_count[1] << 20) | (s->v_count[1] << 16) |
488  (s->h_count[2] << 12) | (s->v_count[2] << 8) |
489  (s->h_count[3] << 4) | s->v_count[3];
490  av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
491  /* NOTE we do not allocate pictures large enough for the possible
492  * padding of h/v_count being 4 */
493  if (!(pix_fmt_id & 0xD0D0D0D0))
494  pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
495  if (!(pix_fmt_id & 0x0D0D0D0D))
496  pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
497 
498  for (i = 0; i < 8; i++) {
499  int j = 6 + (i&1) - (i&6);
500  int is = (pix_fmt_id >> (4*i)) & 0xF;
501  int js = (pix_fmt_id >> (4*j)) & 0xF;
502 
503  if (is == 1 && js != 2 && (i < 2 || i > 5))
504  js = (pix_fmt_id >> ( 8 + 4*(i&1))) & 0xF;
505  if (is == 1 && js != 2 && (i < 2 || i > 5))
506  js = (pix_fmt_id >> (16 + 4*(i&1))) & 0xF;
507 
508  if (is == 1 && js == 2) {
509  if (i & 1) s->upscale_h[j/2] = 1;
510  else s->upscale_v[j/2] = 1;
511  }
512  }
513 
514  if (s->bayer) {
515  if (pix_fmt_id != 0x11110000 && pix_fmt_id != 0x11000000)
516  goto unk_pixfmt;
517  }
518 
519  switch (pix_fmt_id) {
520  case 0x11110000: /* for bayer-encoded huffman lossless JPEGs embedded in DNGs */
521  if (!s->bayer)
522  goto unk_pixfmt;
524  break;
525  case 0x11111100:
526  if (s->rgb)
528  else {
529  if ( s->adobe_transform == 0
530  || s->component_id[0] == 'R' - 1 && s->component_id[1] == 'G' - 1 && s->component_id[2] == 'B' - 1) {
532  } else {
536  }
537  }
538  av_assert0(s->nb_components == 3);
539  break;
540  case 0x11111111:
541  if (s->rgb)
543  else {
544  if (s->adobe_transform == 0 && s->bits <= 8) {
546  } else {
549  }
550  }
551  av_assert0(s->nb_components == 4);
552  break;
553  case 0x22111122:
554  case 0x22111111:
555  if (s->adobe_transform == 0 && s->bits <= 8) {
557  s->upscale_v[1] = s->upscale_v[2] = 1;
558  s->upscale_h[1] = s->upscale_h[2] = 1;
559  } else if (s->adobe_transform == 2 && s->bits <= 8) {
561  s->upscale_v[1] = s->upscale_v[2] = 1;
562  s->upscale_h[1] = s->upscale_h[2] = 1;
564  } else {
565  if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
568  }
569  av_assert0(s->nb_components == 4);
570  break;
571  case 0x12121100:
572  case 0x22122100:
573  case 0x21211100:
574  case 0x22211200:
575  case 0x22221100:
576  case 0x22112200:
577  case 0x11222200:
579  else
580  goto unk_pixfmt;
582  break;
583  case 0x11000000:
584  case 0x13000000:
585  case 0x14000000:
586  case 0x31000000:
587  case 0x33000000:
588  case 0x34000000:
589  case 0x41000000:
590  case 0x43000000:
591  case 0x44000000:
592  if(s->bits <= 8)
594  else
596  break;
597  case 0x12111100:
598  case 0x14121200:
599  case 0x14111100:
600  case 0x22211100:
601  case 0x22112100:
602  if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') {
603  if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
604  else
605  goto unk_pixfmt;
606  s->upscale_v[0] = s->upscale_v[1] = 1;
607  } else {
608  if (pix_fmt_id == 0x14111100)
609  s->upscale_v[1] = s->upscale_v[2] = 1;
611  else
612  goto unk_pixfmt;
614  }
615  break;
616  case 0x21111100:
617  if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') {
618  if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
619  else
620  goto unk_pixfmt;
621  s->upscale_h[0] = s->upscale_h[1] = 1;
622  } else {
626  }
627  break;
628  case 0x31111100:
629  if (s->bits > 8)
630  goto unk_pixfmt;
633  s->upscale_h[1] = s->upscale_h[2] = 2;
634  break;
635  case 0x22121100:
636  case 0x22111200:
638  else
639  goto unk_pixfmt;
641  break;
642  case 0x22111100:
643  case 0x23111100:
644  case 0x42111100:
645  case 0x24111100:
649  if (pix_fmt_id == 0x42111100) {
650  if (s->bits > 8)
651  goto unk_pixfmt;
652  s->upscale_h[1] = s->upscale_h[2] = 1;
653  } else if (pix_fmt_id == 0x24111100) {
654  if (s->bits > 8)
655  goto unk_pixfmt;
656  s->upscale_v[1] = s->upscale_v[2] = 1;
657  } else if (pix_fmt_id == 0x23111100) {
658  if (s->bits > 8)
659  goto unk_pixfmt;
660  s->upscale_v[1] = s->upscale_v[2] = 2;
661  }
662  break;
663  case 0x41111100:
665  else
666  goto unk_pixfmt;
668  break;
669  default:
670  unk_pixfmt:
671  avpriv_report_missing_feature(s->avctx, "Pixel format 0x%x bits:%d", pix_fmt_id, s->bits);
672  memset(s->upscale_h, 0, sizeof(s->upscale_h));
673  memset(s->upscale_v, 0, sizeof(s->upscale_v));
674  return AVERROR_PATCHWELCOME;
675  }
676  if ((AV_RB32(s->upscale_h) || AV_RB32(s->upscale_v)) && s->avctx->lowres) {
677  avpriv_report_missing_feature(s->avctx, "Lowres for weird subsampling");
678  return AVERROR_PATCHWELCOME;
679  }
680  if ((AV_RB32(s->upscale_h) || AV_RB32(s->upscale_v)) && s->progressive && s->avctx->pix_fmt == AV_PIX_FMT_GBRP) {
681  avpriv_report_missing_feature(s->avctx, "progressive for weird subsampling");
682  return AVERROR_PATCHWELCOME;
683  }
684  if (s->ls) {
685  memset(s->upscale_h, 0, sizeof(s->upscale_h));
686  memset(s->upscale_v, 0, sizeof(s->upscale_v));
687  if (s->nb_components == 3) {
689  } else if (s->nb_components != 1) {
690  av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of components %d\n", s->nb_components);
691  return AVERROR_PATCHWELCOME;
692  } else if (s->palette_index && s->bits <= 8)
694  else if (s->bits <= 8)
696  else
698  }
699 
701  if (!s->pix_desc) {
702  av_log(s->avctx, AV_LOG_ERROR, "Could not get a pixel format descriptor.\n");
703  return AVERROR_BUG;
704  }
705 
706  if (s->avctx->pix_fmt == s->hwaccel_sw_pix_fmt && !size_change) {
707  s->avctx->pix_fmt = s->hwaccel_pix_fmt;
708  } else {
709  enum AVPixelFormat pix_fmts[] = {
710 #if CONFIG_MJPEG_NVDEC_HWACCEL
712 #endif
713 #if CONFIG_MJPEG_VAAPI_HWACCEL
715 #endif
716  s->avctx->pix_fmt,
718  };
719  s->hwaccel_pix_fmt = ff_get_format(s->avctx, pix_fmts);
720  if (s->hwaccel_pix_fmt < 0)
721  return AVERROR(EINVAL);
722 
724  s->avctx->pix_fmt = s->hwaccel_pix_fmt;
725  }
726 
727  if (s->avctx->skip_frame == AVDISCARD_ALL) {
729  s->picture_ptr->key_frame = 1;
730  s->got_picture = 1;
731  return 0;
732  }
733 
736  return -1;
738  s->picture_ptr->key_frame = 1;
739  s->got_picture = 1;
740 
741  for (i = 0; i < 4; i++)
742  s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
743 
744  ff_dlog(s->avctx, "%d %d %d %d %d %d\n",
745  s->width, s->height, s->linesize[0], s->linesize[1],
746  s->interlaced, s->avctx->height);
747 
748  }
749 
750  if ((s->rgb && !s->lossless && !s->ls) ||
751  (!s->rgb && s->ls && s->nb_components > 1) ||
752  (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 && !s->ls)
753  ) {
754  av_log(s->avctx, AV_LOG_ERROR, "Unsupported coding and pixel format combination\n");
755  return AVERROR_PATCHWELCOME;
756  }
757 
758  /* totally blank picture as progressive JPEG will only add details to it */
759  if (s->progressive) {
760  int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8);
761  int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
762  for (i = 0; i < s->nb_components; i++) {
763  int size = bw * bh * s->h_count[i] * s->v_count[i];
764  av_freep(&s->blocks[i]);
765  av_freep(&s->last_nnz[i]);
766  s->blocks[i] = av_mallocz_array(size, sizeof(**s->blocks));
767  s->last_nnz[i] = av_mallocz_array(size, sizeof(**s->last_nnz));
768  if (!s->blocks[i] || !s->last_nnz[i])
769  return AVERROR(ENOMEM);
770  s->block_stride[i] = bw * s->h_count[i];
771  }
772  memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
773  }
774 
775  if (s->avctx->hwaccel) {
778  if (!s->hwaccel_picture_private)
779  return AVERROR(ENOMEM);
780 
781  ret = s->avctx->hwaccel->start_frame(s->avctx, s->raw_image_buffer,
783  if (ret < 0)
784  return ret;
785  }
786 
787  return 0;
788 }
789 
790 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
791 {
792  int code;
793  code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
794  if (code < 0 || code > 16) {
796  "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
797  0, dc_index, &s->vlcs[0][dc_index]);
798  return 0xfffff;
799  }
800 
801  if (code)
802  return get_xbits(&s->gb, code);
803  else
804  return 0;
805 }
806 
807 /* decode block and dequantize */
808 static int decode_block(MJpegDecodeContext *s, int16_t *block, int component,
809  int dc_index, int ac_index, uint16_t *quant_matrix)
810 {
811  int code, i, j, level, val;
812 
813  /* DC coef */
814  val = mjpeg_decode_dc(s, dc_index);
815  if (val == 0xfffff) {
816  av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
817  return AVERROR_INVALIDDATA;
818  }
819  val = val * (unsigned)quant_matrix[0] + s->last_dc[component];
820  val = av_clip_int16(val);
821  s->last_dc[component] = val;
822  block[0] = val;
823  /* AC coefs */
824  i = 0;
825  {OPEN_READER(re, &s->gb);
826  do {
827  UPDATE_CACHE(re, &s->gb);
828  GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
829 
830  i += ((unsigned)code) >> 4;
831  code &= 0xf;
832  if (code) {
833  if (code > MIN_CACHE_BITS - 16)
834  UPDATE_CACHE(re, &s->gb);
835 
836  {
837  int cache = GET_CACHE(re, &s->gb);
838  int sign = (~cache) >> 31;
839  level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
840  }
841 
842  LAST_SKIP_BITS(re, &s->gb, code);
843 
844  if (i > 63) {
845  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
846  return AVERROR_INVALIDDATA;
847  }
848  j = s->scantable.permutated[i];
849  block[j] = level * quant_matrix[i];
850  }
851  } while (i < 63);
852  CLOSE_READER(re, &s->gb);}
853 
854  return 0;
855 }
856 
858  int component, int dc_index,
859  uint16_t *quant_matrix, int Al)
860 {
861  unsigned val;
862  s->bdsp.clear_block(block);
863  val = mjpeg_decode_dc(s, dc_index);
864  if (val == 0xfffff) {
865  av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
866  return AVERROR_INVALIDDATA;
867  }
868  val = (val * (quant_matrix[0] << Al)) + s->last_dc[component];
869  s->last_dc[component] = val;
870  block[0] = val;
871  return 0;
872 }
873 
874 /* decode block and dequantize - progressive JPEG version */
876  uint8_t *last_nnz, int ac_index,
877  uint16_t *quant_matrix,
878  int ss, int se, int Al, int *EOBRUN)
879 {
880  int code, i, j, val, run;
881  unsigned level;
882 
883  if (*EOBRUN) {
884  (*EOBRUN)--;
885  return 0;
886  }
887 
888  {
889  OPEN_READER(re, &s->gb);
890  for (i = ss; ; i++) {
891  UPDATE_CACHE(re, &s->gb);
892  GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
893 
894  run = ((unsigned) code) >> 4;
895  code &= 0xF;
896  if (code) {
897  i += run;
898  if (code > MIN_CACHE_BITS - 16)
899  UPDATE_CACHE(re, &s->gb);
900 
901  {
902  int cache = GET_CACHE(re, &s->gb);
903  int sign = (~cache) >> 31;
904  level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
905  }
906 
907  LAST_SKIP_BITS(re, &s->gb, code);
908 
909  if (i >= se) {
910  if (i == se) {
911  j = s->scantable.permutated[se];
912  block[j] = level * (quant_matrix[se] << Al);
913  break;
914  }
915  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
916  return AVERROR_INVALIDDATA;
917  }
918  j = s->scantable.permutated[i];
919  block[j] = level * (quant_matrix[i] << Al);
920  } else {
921  if (run == 0xF) {// ZRL - skip 15 coefficients
922  i += 15;
923  if (i >= se) {
924  av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
925  return AVERROR_INVALIDDATA;
926  }
927  } else {
928  val = (1 << run);
929  if (run) {
930  UPDATE_CACHE(re, &s->gb);
931  val += NEG_USR32(GET_CACHE(re, &s->gb), run);
932  LAST_SKIP_BITS(re, &s->gb, run);
933  }
934  *EOBRUN = val - 1;
935  break;
936  }
937  }
938  }
939  CLOSE_READER(re, &s->gb);
940  }
941 
942  if (i > *last_nnz)
943  *last_nnz = i;
944 
945  return 0;
946 }
947 
948 #define REFINE_BIT(j) { \
949  UPDATE_CACHE(re, &s->gb); \
950  sign = block[j] >> 15; \
951  block[j] += SHOW_UBITS(re, &s->gb, 1) * \
952  ((quant_matrix[i] ^ sign) - sign) << Al; \
953  LAST_SKIP_BITS(re, &s->gb, 1); \
954 }
955 
956 #define ZERO_RUN \
957 for (; ; i++) { \
958  if (i > last) { \
959  i += run; \
960  if (i > se) { \
961  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
962  return -1; \
963  } \
964  break; \
965  } \
966  j = s->scantable.permutated[i]; \
967  if (block[j]) \
968  REFINE_BIT(j) \
969  else if (run-- == 0) \
970  break; \
971 }
972 
973 /* decode block and dequantize - progressive JPEG refinement pass */
975  uint8_t *last_nnz,
976  int ac_index, uint16_t *quant_matrix,
977  int ss, int se, int Al, int *EOBRUN)
978 {
979  int code, i = ss, j, sign, val, run;
980  int last = FFMIN(se, *last_nnz);
981 
982  OPEN_READER(re, &s->gb);
983  if (*EOBRUN) {
984  (*EOBRUN)--;
985  } else {
986  for (; ; i++) {
987  UPDATE_CACHE(re, &s->gb);
988  GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
989 
990  if (code & 0xF) {
991  run = ((unsigned) code) >> 4;
992  UPDATE_CACHE(re, &s->gb);
993  val = SHOW_UBITS(re, &s->gb, 1);
994  LAST_SKIP_BITS(re, &s->gb, 1);
995  ZERO_RUN;
996  j = s->scantable.permutated[i];
997  val--;
998  block[j] = ((quant_matrix[i] << Al) ^ val) - val;
999  if (i == se) {
1000  if (i > *last_nnz)
1001  *last_nnz = i;
1002  CLOSE_READER(re, &s->gb);
1003  return 0;
1004  }
1005  } else {
1006  run = ((unsigned) code) >> 4;
1007  if (run == 0xF) {
1008  ZERO_RUN;
1009  } else {
1010  val = run;
1011  run = (1 << run);
1012  if (val) {
1013  UPDATE_CACHE(re, &s->gb);
1014  run += SHOW_UBITS(re, &s->gb, val);
1015  LAST_SKIP_BITS(re, &s->gb, val);
1016  }
1017  *EOBRUN = run - 1;
1018  break;
1019  }
1020  }
1021  }
1022 
1023  if (i > *last_nnz)
1024  *last_nnz = i;
1025  }
1026 
1027  for (; i <= last; i++) {
1028  j = s->scantable.permutated[i];
1029  if (block[j])
1030  REFINE_BIT(j)
1031  }
1032  CLOSE_READER(re, &s->gb);
1033 
1034  return 0;
1035 }
1036 #undef REFINE_BIT
1037 #undef ZERO_RUN
1038 
1039 static int handle_rstn(MJpegDecodeContext *s, int nb_components)
1040 {
1041  int i;
1042  int reset = 0;
1043 
1044  if (s->restart_interval) {
1045  s->restart_count--;
1046  if(s->restart_count == 0 && s->avctx->codec_id == AV_CODEC_ID_THP){
1047  align_get_bits(&s->gb);
1048  for (i = 0; i < nb_components; i++) /* reset dc */
1049  s->last_dc[i] = (4 << s->bits);
1050  }
1051 
1052  i = 8 + ((-get_bits_count(&s->gb)) & 7);
1053  /* skip RSTn */
1054  if (s->restart_count == 0) {
1055  if( show_bits(&s->gb, i) == (1 << i) - 1
1056  || show_bits(&s->gb, i) == 0xFF) {
1057  int pos = get_bits_count(&s->gb);
1058  align_get_bits(&s->gb);
1059  while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
1060  skip_bits(&s->gb, 8);
1061  if (get_bits_left(&s->gb) >= 8 && (get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
1062  for (i = 0; i < nb_components; i++) /* reset dc */
1063  s->last_dc[i] = (4 << s->bits);
1064  reset = 1;
1065  } else
1066  skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
1067  }
1068  }
1069  }
1070  return reset;
1071 }
1072 
1073 /* Handles 1 to 4 components */
1074 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform)
1075 {
1076  int i, mb_x, mb_y;
1077  unsigned width;
1078  uint16_t (*buffer)[4];
1079  int left[4], top[4], topleft[4];
1080  const int linesize = s->linesize[0];
1081  const int mask = ((1 << s->bits) - 1) << point_transform;
1082  int resync_mb_y = 0;
1083  int resync_mb_x = 0;
1084  int vpred[6];
1085 
1086  if (!s->bayer && s->nb_components < 3)
1087  return AVERROR_INVALIDDATA;
1088  if (s->bayer && s->nb_components > 2)
1089  return AVERROR_INVALIDDATA;
1090  if (s->nb_components <= 0 || s->nb_components > 4)
1091  return AVERROR_INVALIDDATA;
1092  if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
1093  return AVERROR_INVALIDDATA;
1094 
1095 
1097 
1098  if (s->restart_interval == 0)
1099  s->restart_interval = INT_MAX;
1100 
1101  if (s->bayer)
1102  width = s->mb_width / nb_components; /* Interleaved, width stored is the total so need to divide */
1103  else
1104  width = s->mb_width;
1105 
1106  av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, width * 4 * sizeof(s->ljpeg_buffer[0][0]));
1107  if (!s->ljpeg_buffer)
1108  return AVERROR(ENOMEM);
1109 
1110  buffer = s->ljpeg_buffer;
1111 
1112  for (i = 0; i < 4; i++)
1113  buffer[0][i] = 1 << (s->bits - 1);
1114 
1115  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1116  uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
1117 
1118  if (s->interlaced && s->bottom_field)
1119  ptr += linesize >> 1;
1120 
1121  for (i = 0; i < 4; i++)
1122  top[i] = left[i] = topleft[i] = buffer[0][i];
1123 
1124  if ((mb_y * s->width) % s->restart_interval == 0) {
1125  for (i = 0; i < 6; i++)
1126  vpred[i] = 1 << (s->bits-1);
1127  }
1128 
1129  for (mb_x = 0; mb_x < width; mb_x++) {
1130  int modified_predictor = predictor;
1131 
1132  if (get_bits_left(&s->gb) < 1) {
1133  av_log(s->avctx, AV_LOG_ERROR, "bitstream end in rgb_scan\n");
1134  return AVERROR_INVALIDDATA;
1135  }
1136 
1137  if (s->restart_interval && !s->restart_count){
1139  resync_mb_x = mb_x;
1140  resync_mb_y = mb_y;
1141  for(i=0; i<4; i++)
1142  top[i] = left[i]= topleft[i]= 1 << (s->bits - 1);
1143  }
1144  if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
1145  modified_predictor = 1;
1146 
1147  for (i=0;i<nb_components;i++) {
1148  int pred, dc;
1149 
1150  topleft[i] = top[i];
1151  top[i] = buffer[mb_x][i];
1152 
1153  dc = mjpeg_decode_dc(s, s->dc_index[i]);
1154  if(dc == 0xFFFFF)
1155  return -1;
1156 
1157  if (!s->bayer || mb_x) {
1158  pred = left[i];
1159  } else { /* This path runs only for the first line in bayer images */
1160  vpred[i] += dc;
1161  pred = vpred[i] - dc;
1162  }
1163 
1164  PREDICT(pred, topleft[i], top[i], pred, modified_predictor);
1165 
1166  left[i] = buffer[mb_x][i] =
1167  mask & (pred + (unsigned)(dc * (1 << point_transform)));
1168  }
1169 
1170  if (s->restart_interval && !--s->restart_count) {
1171  align_get_bits(&s->gb);
1172  skip_bits(&s->gb, 16); /* skip RSTn */
1173  }
1174  }
1175  if (s->rct && s->nb_components == 4) {
1176  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1177  ptr[4*mb_x + 2] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
1178  ptr[4*mb_x + 1] = buffer[mb_x][1] + ptr[4*mb_x + 2];
1179  ptr[4*mb_x + 3] = buffer[mb_x][2] + ptr[4*mb_x + 2];
1180  ptr[4*mb_x + 0] = buffer[mb_x][3];
1181  }
1182  } else if (s->nb_components == 4) {
1183  for(i=0; i<nb_components; i++) {
1184  int c= s->comp_index[i];
1185  if (s->bits <= 8) {
1186  for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1187  ptr[4*mb_x+3-c] = buffer[mb_x][i];
1188  }
1189  } else if(s->bits == 9) {
1190  return AVERROR_PATCHWELCOME;
1191  } else {
1192  for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1193  ((uint16_t*)ptr)[4*mb_x+c] = buffer[mb_x][i];
1194  }
1195  }
1196  }
1197  } else if (s->rct) {
1198  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1199  ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
1200  ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
1201  ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
1202  }
1203  } else if (s->pegasus_rct) {
1204  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1205  ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
1206  ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
1207  ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
1208  }
1209  } else if (s->bayer) {
1210  if (nb_components == 1) {
1211  /* Leave decoding to the TIFF/DNG decoder (see comment in ff_mjpeg_decode_sof) */
1212  for (mb_x = 0; mb_x < width; mb_x++)
1213  ((uint16_t*)ptr)[mb_x] = buffer[mb_x][0];
1214  } else if (nb_components == 2) {
1215  for (mb_x = 0; mb_x < width; mb_x++) {
1216  ((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
1217  ((uint16_t*)ptr)[2*mb_x + 1] = buffer[mb_x][1];
1218  }
1219  }
1220  } else {
1221  for(i=0; i<nb_components; i++) {
1222  int c= s->comp_index[i];
1223  if (s->bits <= 8) {
1224  for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1225  ptr[3*mb_x+2-c] = buffer[mb_x][i];
1226  }
1227  } else if(s->bits == 9) {
1228  return AVERROR_PATCHWELCOME;
1229  } else {
1230  for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1231  ((uint16_t*)ptr)[3*mb_x+2-c] = buffer[mb_x][i];
1232  }
1233  }
1234  }
1235  }
1236  }
1237  return 0;
1238 }
1239 
1240 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor,
1241  int point_transform, int nb_components)
1242 {
1243  int i, mb_x, mb_y, mask;
1244  int bits= (s->bits+7)&~7;
1245  int resync_mb_y = 0;
1246  int resync_mb_x = 0;
1247 
1248  point_transform += bits - s->bits;
1249  mask = ((1 << s->bits) - 1) << point_transform;
1250 
1251  av_assert0(nb_components>=1 && nb_components<=4);
1252 
1253  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1254  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1255  if (get_bits_left(&s->gb) < 1) {
1256  av_log(s->avctx, AV_LOG_ERROR, "bitstream end in yuv_scan\n");
1257  return AVERROR_INVALIDDATA;
1258  }
1259  if (s->restart_interval && !s->restart_count){
1261  resync_mb_x = mb_x;
1262  resync_mb_y = mb_y;
1263  }
1264 
1265  if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){
1266  int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
1267  int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
1268  for (i = 0; i < nb_components; i++) {
1269  uint8_t *ptr;
1270  uint16_t *ptr16;
1271  int n, h, v, x, y, c, j, linesize;
1272  n = s->nb_blocks[i];
1273  c = s->comp_index[i];
1274  h = s->h_scount[i];
1275  v = s->v_scount[i];
1276  x = 0;
1277  y = 0;
1278  linesize= s->linesize[c];
1279 
1280  if(bits>8) linesize /= 2;
1281 
1282  for(j=0; j<n; j++) {
1283  int pred, dc;
1284 
1285  dc = mjpeg_decode_dc(s, s->dc_index[i]);
1286  if(dc == 0xFFFFF)
1287  return -1;
1288  if ( h * mb_x + x >= s->width
1289  || v * mb_y + y >= s->height) {
1290  // Nothing to do
1291  } else if (bits<=8) {
1292  ptr = s->picture_ptr->data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
1293  if(y==0 && toprow){
1294  if(x==0 && leftcol){
1295  pred= 1 << (bits - 1);
1296  }else{
1297  pred= ptr[-1];
1298  }
1299  }else{
1300  if(x==0 && leftcol){
1301  pred= ptr[-linesize];
1302  }else{
1303  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1304  }
1305  }
1306 
1307  if (s->interlaced && s->bottom_field)
1308  ptr += linesize >> 1;
1309  pred &= mask;
1310  *ptr= pred + ((unsigned)dc << point_transform);
1311  }else{
1312  ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
1313  if(y==0 && toprow){
1314  if(x==0 && leftcol){
1315  pred= 1 << (bits - 1);
1316  }else{
1317  pred= ptr16[-1];
1318  }
1319  }else{
1320  if(x==0 && leftcol){
1321  pred= ptr16[-linesize];
1322  }else{
1323  PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1324  }
1325  }
1326 
1327  if (s->interlaced && s->bottom_field)
1328  ptr16 += linesize >> 1;
1329  pred &= mask;
1330  *ptr16= pred + ((unsigned)dc << point_transform);
1331  }
1332  if (++x == h) {
1333  x = 0;
1334  y++;
1335  }
1336  }
1337  }
1338  } else {
1339  for (i = 0; i < nb_components; i++) {
1340  uint8_t *ptr;
1341  uint16_t *ptr16;
1342  int n, h, v, x, y, c, j, linesize, dc;
1343  n = s->nb_blocks[i];
1344  c = s->comp_index[i];
1345  h = s->h_scount[i];
1346  v = s->v_scount[i];
1347  x = 0;
1348  y = 0;
1349  linesize = s->linesize[c];
1350 
1351  if(bits>8) linesize /= 2;
1352 
1353  for (j = 0; j < n; j++) {
1354  int pred;
1355 
1356  dc = mjpeg_decode_dc(s, s->dc_index[i]);
1357  if(dc == 0xFFFFF)
1358  return -1;
1359  if ( h * mb_x + x >= s->width
1360  || v * mb_y + y >= s->height) {
1361  // Nothing to do
1362  } else if (bits<=8) {
1363  ptr = s->picture_ptr->data[c] +
1364  (linesize * (v * mb_y + y)) +
1365  (h * mb_x + x); //FIXME optimize this crap
1366  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1367 
1368  pred &= mask;
1369  *ptr = pred + ((unsigned)dc << point_transform);
1370  }else{
1371  ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
1372  PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1373 
1374  pred &= mask;
1375  *ptr16= pred + ((unsigned)dc << point_transform);
1376  }
1377 
1378  if (++x == h) {
1379  x = 0;
1380  y++;
1381  }
1382  }
1383  }
1384  }
1385  if (s->restart_interval && !--s->restart_count) {
1386  align_get_bits(&s->gb);
1387  skip_bits(&s->gb, 16); /* skip RSTn */
1388  }
1389  }
1390  }
1391  return 0;
1392 }
1393 
1395  uint8_t *dst, const uint8_t *src,
1396  int linesize, int lowres)
1397 {
1398  switch (lowres) {
1399  case 0: s->hdsp.put_pixels_tab[1][0](dst, src, linesize, 8);
1400  break;
1401  case 1: copy_block4(dst, src, linesize, linesize, 4);
1402  break;
1403  case 2: copy_block2(dst, src, linesize, linesize, 2);
1404  break;
1405  case 3: *dst = *src;
1406  break;
1407  }
1408 }
1409 
1410 static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
1411 {
1412  int block_x, block_y;
1413  int size = 8 >> s->avctx->lowres;
1414  if (s->bits > 8) {
1415  for (block_y=0; block_y<size; block_y++)
1416  for (block_x=0; block_x<size; block_x++)
1417  *(uint16_t*)(ptr + 2*block_x + block_y*linesize) <<= 16 - s->bits;
1418  } else {
1419  for (block_y=0; block_y<size; block_y++)
1420  for (block_x=0; block_x<size; block_x++)
1421  *(ptr + block_x + block_y*linesize) <<= 8 - s->bits;
1422  }
1423 }
1424 
1425 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
1426  int Al, const uint8_t *mb_bitmask,
1427  int mb_bitmask_size,
1428  const AVFrame *reference)
1429 {
1430  int i, mb_x, mb_y, chroma_h_shift, chroma_v_shift, chroma_width, chroma_height;
1432  const uint8_t *reference_data[MAX_COMPONENTS];
1433  int linesize[MAX_COMPONENTS];
1434  GetBitContext mb_bitmask_gb = {0}; // initialize to silence gcc warning
1435  int bytes_per_pixel = 1 + (s->bits > 8);
1436 
1437  if (mb_bitmask) {
1438  if (mb_bitmask_size != (s->mb_width * s->mb_height + 7)>>3) {
1439  av_log(s->avctx, AV_LOG_ERROR, "mb_bitmask_size mismatches\n");
1440  return AVERROR_INVALIDDATA;
1441  }
1442  init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
1443  }
1444 
1445  s->restart_count = 0;
1446 
1447  av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &chroma_h_shift,
1448  &chroma_v_shift);
1449  chroma_width = AV_CEIL_RSHIFT(s->width, chroma_h_shift);
1450  chroma_height = AV_CEIL_RSHIFT(s->height, chroma_v_shift);
1451 
1452  for (i = 0; i < nb_components; i++) {
1453  int c = s->comp_index[i];
1454  data[c] = s->picture_ptr->data[c];
1455  reference_data[c] = reference ? reference->data[c] : NULL;
1456  linesize[c] = s->linesize[c];
1457  s->coefs_finished[c] |= 1;
1458  }
1459 
1460  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1461  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1462  const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
1463 
1464  if (s->restart_interval && !s->restart_count)
1466 
1467  if (get_bits_left(&s->gb) < 0) {
1468  av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
1469  -get_bits_left(&s->gb));
1470  return AVERROR_INVALIDDATA;
1471  }
1472  for (i = 0; i < nb_components; i++) {
1473  uint8_t *ptr;
1474  int n, h, v, x, y, c, j;
1475  int block_offset;
1476  n = s->nb_blocks[i];
1477  c = s->comp_index[i];
1478  h = s->h_scount[i];
1479  v = s->v_scount[i];
1480  x = 0;
1481  y = 0;
1482  for (j = 0; j < n; j++) {
1483  block_offset = (((linesize[c] * (v * mb_y + y) * 8) +
1484  (h * mb_x + x) * 8 * bytes_per_pixel) >> s->avctx->lowres);
1485 
1486  if (s->interlaced && s->bottom_field)
1487  block_offset += linesize[c] >> 1;
1488  if ( 8*(h * mb_x + x) < ((c == 1) || (c == 2) ? chroma_width : s->width)
1489  && 8*(v * mb_y + y) < ((c == 1) || (c == 2) ? chroma_height : s->height)) {
1490  ptr = data[c] + block_offset;
1491  } else
1492  ptr = NULL;
1493  if (!s->progressive) {
1494  if (copy_mb) {
1495  if (ptr)
1496  mjpeg_copy_block(s, ptr, reference_data[c] + block_offset,
1497  linesize[c], s->avctx->lowres);
1498 
1499  } else {
1500  s->bdsp.clear_block(s->block);
1501  if (decode_block(s, s->block, i,
1502  s->dc_index[i], s->ac_index[i],
1503  s->quant_matrixes[s->quant_sindex[i]]) < 0) {
1505  "error y=%d x=%d\n", mb_y, mb_x);
1506  return AVERROR_INVALIDDATA;
1507  }
1508  if (ptr) {
1509  s->idsp.idct_put(ptr, linesize[c], s->block);
1510  if (s->bits & 7)
1511  shift_output(s, ptr, linesize[c]);
1512  }
1513  }
1514  } else {
1515  int block_idx = s->block_stride[c] * (v * mb_y + y) +
1516  (h * mb_x + x);
1517  int16_t *block = s->blocks[c][block_idx];
1518  if (Ah)
1519  block[0] += get_bits1(&s->gb) *
1520  s->quant_matrixes[s->quant_sindex[i]][0] << Al;
1521  else if (decode_dc_progressive(s, block, i, s->dc_index[i],
1522  s->quant_matrixes[s->quant_sindex[i]],
1523  Al) < 0) {
1525  "error y=%d x=%d\n", mb_y, mb_x);
1526  return AVERROR_INVALIDDATA;
1527  }
1528  }
1529  ff_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
1530  ff_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
1531  mb_x, mb_y, x, y, c, s->bottom_field,
1532  (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1533  if (++x == h) {
1534  x = 0;
1535  y++;
1536  }
1537  }
1538  }
1539 
1540  handle_rstn(s, nb_components);
1541  }
1542  }
1543  return 0;
1544 }
1545 
1547  int se, int Ah, int Al)
1548 {
1549  int mb_x, mb_y;
1550  int EOBRUN = 0;
1551  int c = s->comp_index[0];
1552  uint16_t *quant_matrix = s->quant_matrixes[s->quant_sindex[0]];
1553 
1554  av_assert0(ss>=0 && Ah>=0 && Al>=0);
1555  if (se < ss || se > 63) {
1556  av_log(s->avctx, AV_LOG_ERROR, "SS/SE %d/%d is invalid\n", ss, se);
1557  return AVERROR_INVALIDDATA;
1558  }
1559 
1560  // s->coefs_finished is a bitmask for coefficients coded
1561  // ss and se are parameters telling start and end coefficients
1562  s->coefs_finished[c] |= (2ULL << se) - (1ULL << ss);
1563 
1564  s->restart_count = 0;
1565 
1566  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1567  int block_idx = mb_y * s->block_stride[c];
1568  int16_t (*block)[64] = &s->blocks[c][block_idx];
1569  uint8_t *last_nnz = &s->last_nnz[c][block_idx];
1570  if (get_bits_left(&s->gb) <= 0) {
1571  av_log(s->avctx, AV_LOG_ERROR, "bitstream truncated in mjpeg_decode_scan_progressive_ac\n");
1572  return AVERROR_INVALIDDATA;
1573  }
1574  for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
1575  int ret;
1576  if (s->restart_interval && !s->restart_count)
1578 
1579  if (Ah)
1580  ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
1581  quant_matrix, ss, se, Al, &EOBRUN);
1582  else
1583  ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
1584  quant_matrix, ss, se, Al, &EOBRUN);
1585  if (ret < 0) {
1587  "error y=%d x=%d\n", mb_y, mb_x);
1588  return AVERROR_INVALIDDATA;
1589  }
1590 
1591  if (handle_rstn(s, 0))
1592  EOBRUN = 0;
1593  }
1594  }
1595  return 0;
1596 }
1597 
1599 {
1600  int mb_x, mb_y;
1601  int c;
1602  const int bytes_per_pixel = 1 + (s->bits > 8);
1603  const int block_size = s->lossless ? 1 : 8;
1604 
1605  for (c = 0; c < s->nb_components; c++) {
1606  uint8_t *data = s->picture_ptr->data[c];
1607  int linesize = s->linesize[c];
1608  int h = s->h_max / s->h_count[c];
1609  int v = s->v_max / s->v_count[c];
1610  int mb_width = (s->width + h * block_size - 1) / (h * block_size);
1611  int mb_height = (s->height + v * block_size - 1) / (v * block_size);
1612 
1613  if (~s->coefs_finished[c])
1614  av_log(s->avctx, AV_LOG_WARNING, "component %d is incomplete\n", c);
1615 
1616  if (s->interlaced && s->bottom_field)
1617  data += linesize >> 1;
1618 
1619  for (mb_y = 0; mb_y < mb_height; mb_y++) {
1620  uint8_t *ptr = data + (mb_y * linesize * 8 >> s->avctx->lowres);
1621  int block_idx = mb_y * s->block_stride[c];
1622  int16_t (*block)[64] = &s->blocks[c][block_idx];
1623  for (mb_x = 0; mb_x < mb_width; mb_x++, block++) {
1624  s->idsp.idct_put(ptr, linesize, *block);
1625  if (s->bits & 7)
1626  shift_output(s, ptr, linesize);
1627  ptr += bytes_per_pixel*8 >> s->avctx->lowres;
1628  }
1629  }
1630  }
1631 }
1632 
1634  int mb_bitmask_size, const AVFrame *reference)
1635 {
1636  int len, nb_components, i, h, v, predictor, point_transform;
1637  int index, id, ret;
1638  const int block_size = s->lossless ? 1 : 8;
1639  int ilv, prev_shift;
1640 
1641  if (!s->got_picture) {
1643  "Can not process SOS before SOF, skipping\n");
1644  return -1;
1645  }
1646 
1647  if (reference) {
1648  if (reference->width != s->picture_ptr->width ||
1649  reference->height != s->picture_ptr->height ||
1650  reference->format != s->picture_ptr->format) {
1651  av_log(s->avctx, AV_LOG_ERROR, "Reference mismatching\n");
1652  return AVERROR_INVALIDDATA;
1653  }
1654  }
1655 
1656  /* XXX: verify len field validity */
1657  len = get_bits(&s->gb, 16);
1658  nb_components = get_bits(&s->gb, 8);
1659  if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
1661  "decode_sos: nb_components (%d)",
1662  nb_components);
1663  return AVERROR_PATCHWELCOME;
1664  }
1665  if (len != 6 + 2 * nb_components) {
1666  av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
1667  return AVERROR_INVALIDDATA;
1668  }
1669  for (i = 0; i < nb_components; i++) {
1670  id = get_bits(&s->gb, 8) - 1;
1671  av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1672  /* find component index */
1673  for (index = 0; index < s->nb_components; index++)
1674  if (id == s->component_id[index])
1675  break;
1676  if (index == s->nb_components) {
1678  "decode_sos: index(%d) out of components\n", index);
1679  return AVERROR_INVALIDDATA;
1680  }
1681  /* Metasoft MJPEG codec has Cb and Cr swapped */
1682  if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1683  && nb_components == 3 && s->nb_components == 3 && i)
1684  index = 3 - i;
1685 
1686  s->quant_sindex[i] = s->quant_index[index];
1687  s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1688  s->h_scount[i] = s->h_count[index];
1689  s->v_scount[i] = s->v_count[index];
1690 
1691  if((nb_components == 1 || nb_components == 3) && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P)
1692  index = (index+2)%3;
1693 
1694  s->comp_index[i] = index;
1695 
1696  s->dc_index[i] = get_bits(&s->gb, 4);
1697  s->ac_index[i] = get_bits(&s->gb, 4);
1698 
1699  if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1700  s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1701  goto out_of_range;
1702  if (!s->vlcs[0][s->dc_index[i]].table || !(s->progressive ? s->vlcs[2][s->ac_index[0]].table : s->vlcs[1][s->ac_index[i]].table))
1703  goto out_of_range;
1704  }
1705 
1706  predictor = get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1707  ilv = get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1708  if(s->avctx->codec_tag != AV_RL32("CJPG")){
1709  prev_shift = get_bits(&s->gb, 4); /* Ah */
1710  point_transform = get_bits(&s->gb, 4); /* Al */
1711  }else
1712  prev_shift = point_transform = 0;
1713 
1714  if (nb_components > 1) {
1715  /* interleaved stream */
1716  s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1717  s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1718  } else if (!s->ls) { /* skip this for JPEG-LS */
1719  h = s->h_max / s->h_scount[0];
1720  v = s->v_max / s->v_scount[0];
1721  s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1722  s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1723  s->nb_blocks[0] = 1;
1724  s->h_scount[0] = 1;
1725  s->v_scount[0] = 1;
1726  }
1727 
1728  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1729  av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d skip:%d %s comp:%d\n",
1730  s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1731  predictor, point_transform, ilv, s->bits, s->mjpb_skiptosod,
1732  s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""), nb_components);
1733 
1734 
1735  /* mjpeg-b can have padding bytes between sos and image data, skip them */
1736  for (i = s->mjpb_skiptosod; i > 0; i--)
1737  skip_bits(&s->gb, 8);
1738 
1739 next_field:
1740  for (i = 0; i < nb_components; i++)
1741  s->last_dc[i] = (4 << s->bits);
1742 
1743  if (s->avctx->hwaccel) {
1744  int bytes_to_start = get_bits_count(&s->gb) / 8;
1745  av_assert0(bytes_to_start >= 0 &&
1746  s->raw_scan_buffer_size >= bytes_to_start);
1747 
1748  ret = s->avctx->hwaccel->decode_slice(s->avctx,
1749  s->raw_scan_buffer + bytes_to_start,
1750  s->raw_scan_buffer_size - bytes_to_start);
1751  if (ret < 0)
1752  return ret;
1753 
1754  } else if (s->lossless) {
1755  av_assert0(s->picture_ptr == s->picture);
1756  if (CONFIG_JPEGLS_DECODER && s->ls) {
1757 // for () {
1758 // reset_ls_coding_parameters(s, 0);
1759 
1760  if ((ret = ff_jpegls_decode_picture(s, predictor,
1761  point_transform, ilv)) < 0)
1762  return ret;
1763  } else {
1764  if (s->rgb || s->bayer) {
1765  if ((ret = ljpeg_decode_rgb_scan(s, nb_components, predictor, point_transform)) < 0)
1766  return ret;
1767  } else {
1768  if ((ret = ljpeg_decode_yuv_scan(s, predictor,
1769  point_transform,
1770  nb_components)) < 0)
1771  return ret;
1772  }
1773  }
1774  } else {
1775  if (s->progressive && predictor) {
1776  av_assert0(s->picture_ptr == s->picture);
1777  if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor,
1778  ilv, prev_shift,
1779  point_transform)) < 0)
1780  return ret;
1781  } else {
1782  if ((ret = mjpeg_decode_scan(s, nb_components,
1783  prev_shift, point_transform,
1784  mb_bitmask, mb_bitmask_size, reference)) < 0)
1785  return ret;
1786  }
1787  }
1788 
1789  if (s->interlaced &&
1790  get_bits_left(&s->gb) > 32 &&
1791  show_bits(&s->gb, 8) == 0xFF) {
1792  GetBitContext bak = s->gb;
1793  align_get_bits(&bak);
1794  if (show_bits(&bak, 16) == 0xFFD1) {
1795  av_log(s->avctx, AV_LOG_DEBUG, "AVRn interlaced picture marker found\n");
1796  s->gb = bak;
1797  skip_bits(&s->gb, 16);
1798  s->bottom_field ^= 1;
1799 
1800  goto next_field;
1801  }
1802  }
1803 
1804  emms_c();
1805  return 0;
1806  out_of_range:
1807  av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1808  return AVERROR_INVALIDDATA;
1809 }
1810 
1812 {
1813  if (get_bits(&s->gb, 16) != 4)
1814  return AVERROR_INVALIDDATA;
1815  s->restart_interval = get_bits(&s->gb, 16);
1816  s->restart_count = 0;
1817  av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1818  s->restart_interval);
1819 
1820  return 0;
1821 }
1822 
1824 {
1825  int len, id, i;
1826 
1827  len = get_bits(&s->gb, 16);
1828  if (len < 6) {
1829  if (s->bayer) {
1830  // Pentax K-1 (digital camera) JPEG images embedded in DNG images contain unknown APP0 markers
1831  av_log(s->avctx, AV_LOG_WARNING, "skipping APPx (len=%"PRId32") for bayer-encoded image\n", len);
1832  skip_bits(&s->gb, len);
1833  return 0;
1834  } else
1835  return AVERROR_INVALIDDATA;
1836  }
1837  if (8 * len > get_bits_left(&s->gb))
1838  return AVERROR_INVALIDDATA;
1839 
1840  id = get_bits_long(&s->gb, 32);
1841  len -= 6;
1842 
1843  if (s->avctx->debug & FF_DEBUG_STARTCODE)
1844  av_log(s->avctx, AV_LOG_DEBUG, "APPx (%s / %8X) len=%d\n",
1845  av_fourcc2str(av_bswap32(id)), id, len);
1846 
1847  /* Buggy AVID, it puts EOI only at every 10th frame. */
1848  /* Also, this fourcc is used by non-avid files too, it holds some
1849  information, but it's always present in AVID-created files. */
1850  if (id == AV_RB32("AVI1")) {
1851  /* structure:
1852  4bytes AVI1
1853  1bytes polarity
1854  1bytes always zero
1855  4bytes field_size
1856  4bytes field_size_less_padding
1857  */
1858  s->buggy_avid = 1;
1859  i = get_bits(&s->gb, 8); len--;
1860  av_log(s->avctx, AV_LOG_DEBUG, "polarity %d\n", i);
1861  goto out;
1862  }
1863 
1864  if (id == AV_RB32("JFIF")) {
1865  int t_w, t_h, v1, v2;
1866  if (len < 8)
1867  goto out;
1868  skip_bits(&s->gb, 8); /* the trailing zero-byte */
1869  v1 = get_bits(&s->gb, 8);
1870  v2 = get_bits(&s->gb, 8);
1871  skip_bits(&s->gb, 8);
1872 
1873  s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1874  s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1875  if ( s->avctx->sample_aspect_ratio.num <= 0
1876  || s->avctx->sample_aspect_ratio.den <= 0) {
1877  s->avctx->sample_aspect_ratio.num = 0;
1878  s->avctx->sample_aspect_ratio.den = 1;
1879  }
1880 
1881  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1882  av_log(s->avctx, AV_LOG_INFO,
1883  "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1884  v1, v2,
1887 
1888  len -= 8;
1889  if (len >= 2) {
1890  t_w = get_bits(&s->gb, 8);
1891  t_h = get_bits(&s->gb, 8);
1892  if (t_w && t_h) {
1893  /* skip thumbnail */
1894  if (len -10 - (t_w * t_h * 3) > 0)
1895  len -= t_w * t_h * 3;
1896  }
1897  len -= 2;
1898  }
1899  goto out;
1900  }
1901 
1902  if ( id == AV_RB32("Adob")
1903  && len >= 7
1904  && show_bits(&s->gb, 8) == 'e'
1905  && show_bits_long(&s->gb, 32) != AV_RB32("e_CM")) {
1906  skip_bits(&s->gb, 8); /* 'e' */
1907  skip_bits(&s->gb, 16); /* version */
1908  skip_bits(&s->gb, 16); /* flags0 */
1909  skip_bits(&s->gb, 16); /* flags1 */
1910  s->adobe_transform = get_bits(&s->gb, 8);
1911  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1912  av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found, transform=%d\n", s->adobe_transform);
1913  len -= 7;
1914  goto out;
1915  }
1916 
1917  if (id == AV_RB32("LJIF")) {
1918  int rgb = s->rgb;
1919  int pegasus_rct = s->pegasus_rct;
1920  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1921  av_log(s->avctx, AV_LOG_INFO,
1922  "Pegasus lossless jpeg header found\n");
1923  skip_bits(&s->gb, 16); /* version ? */
1924  skip_bits(&s->gb, 16); /* unknown always 0? */
1925  skip_bits(&s->gb, 16); /* unknown always 0? */
1926  skip_bits(&s->gb, 16); /* unknown always 0? */
1927  switch (i=get_bits(&s->gb, 8)) {
1928  case 1:
1929  rgb = 1;
1930  pegasus_rct = 0;
1931  break;
1932  case 2:
1933  rgb = 1;
1934  pegasus_rct = 1;
1935  break;
1936  default:
1937  av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace %d\n", i);
1938  }
1939 
1940  len -= 9;
1941  if (s->got_picture)
1942  if (rgb != s->rgb || pegasus_rct != s->pegasus_rct) {
1943  av_log(s->avctx, AV_LOG_WARNING, "Mismatching LJIF tag\n");
1944  goto out;
1945  }
1946 
1947  s->rgb = rgb;
1948  s->pegasus_rct = pegasus_rct;
1949 
1950  goto out;
1951  }
1952  if (id == AV_RL32("colr") && len > 0) {
1953  s->colr = get_bits(&s->gb, 8);
1954  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1955  av_log(s->avctx, AV_LOG_INFO, "COLR %d\n", s->colr);
1956  len --;
1957  goto out;
1958  }
1959  if (id == AV_RL32("xfrm") && len > 0) {
1960  s->xfrm = get_bits(&s->gb, 8);
1961  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1962  av_log(s->avctx, AV_LOG_INFO, "XFRM %d\n", s->xfrm);
1963  len --;
1964  goto out;
1965  }
1966 
1967  /* JPS extension by VRex */
1968  if (s->start_code == APP3 && id == AV_RB32("_JPS") && len >= 10) {
1969  int flags, layout, type;
1970  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1971  av_log(s->avctx, AV_LOG_INFO, "_JPSJPS_\n");
1972 
1973  skip_bits(&s->gb, 32); len -= 4; /* JPS_ */
1974  skip_bits(&s->gb, 16); len -= 2; /* block length */
1975  skip_bits(&s->gb, 8); /* reserved */
1976  flags = get_bits(&s->gb, 8);
1977  layout = get_bits(&s->gb, 8);
1978  type = get_bits(&s->gb, 8);
1979  len -= 4;
1980 
1981  av_freep(&s->stereo3d);
1982  s->stereo3d = av_stereo3d_alloc();
1983  if (!s->stereo3d) {
1984  goto out;
1985  }
1986  if (type == 0) {
1988  } else if (type == 1) {
1989  switch (layout) {
1990  case 0x01:
1992  break;
1993  case 0x02:
1995  break;
1996  case 0x03:
1998  break;
1999  }
2000  if (!(flags & 0x04)) {
2002  }
2003  }
2004  goto out;
2005  }
2006 
2007  /* EXIF metadata */
2008  if (s->start_code == APP1 && id == AV_RB32("Exif") && len >= 2) {
2009  GetByteContext gbytes;
2010  int ret, le, ifd_offset, bytes_read;
2011  const uint8_t *aligned;
2012 
2013  skip_bits(&s->gb, 16); // skip padding
2014  len -= 2;
2015 
2016  // init byte wise reading
2017  aligned = align_get_bits(&s->gb);
2018  bytestream2_init(&gbytes, aligned, len);
2019 
2020  // read TIFF header
2021  ret = ff_tdecode_header(&gbytes, &le, &ifd_offset);
2022  if (ret) {
2023  av_log(s->avctx, AV_LOG_ERROR, "mjpeg: invalid TIFF header in EXIF data\n");
2024  } else {
2025  bytestream2_seek(&gbytes, ifd_offset, SEEK_SET);
2026 
2027  // read 0th IFD and store the metadata
2028  // (return values > 0 indicate the presence of subimage metadata)
2029  ret = ff_exif_decode_ifd(s->avctx, &gbytes, le, 0, &s->exif_metadata);
2030  if (ret < 0) {
2031  av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error decoding EXIF data\n");
2032  }
2033  }
2034 
2035  bytes_read = bytestream2_tell(&gbytes);
2036  skip_bits(&s->gb, bytes_read << 3);
2037  len -= bytes_read;
2038 
2039  goto out;
2040  }
2041 
2042  /* Apple MJPEG-A */
2043  if ((s->start_code == APP1) && (len > (0x28 - 8))) {
2044  id = get_bits_long(&s->gb, 32);
2045  len -= 4;
2046  /* Apple MJPEG-A */
2047  if (id == AV_RB32("mjpg")) {
2048  /* structure:
2049  4bytes field size
2050  4bytes pad field size
2051  4bytes next off
2052  4bytes quant off
2053  4bytes huff off
2054  4bytes image off
2055  4bytes scan off
2056  4bytes data off
2057  */
2058  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2059  av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
2060  }
2061  }
2062 
2063  if (s->start_code == APP2 && id == AV_RB32("ICC_") && len >= 10) {
2064  int id2;
2065  unsigned seqno;
2066  unsigned nummarkers;
2067 
2068  id = get_bits_long(&s->gb, 32);
2069  id2 = get_bits(&s->gb, 24);
2070  len -= 7;
2071  if (id != AV_RB32("PROF") || id2 != AV_RB24("ILE")) {
2072  av_log(s->avctx, AV_LOG_WARNING, "Invalid ICC_PROFILE header in APP2\n");
2073  goto out;
2074  }
2075 
2076  skip_bits(&s->gb, 8);
2077  seqno = get_bits(&s->gb, 8);
2078  len -= 2;
2079  if (seqno == 0) {
2080  av_log(s->avctx, AV_LOG_WARNING, "Invalid sequence number in APP2\n");
2081  goto out;
2082  }
2083 
2084  nummarkers = get_bits(&s->gb, 8);
2085  len -= 1;
2086  if (nummarkers == 0) {
2087  av_log(s->avctx, AV_LOG_WARNING, "Invalid number of markers coded in APP2\n");
2088  goto out;
2089  } else if (s->iccnum != 0 && nummarkers != s->iccnum) {
2090  av_log(s->avctx, AV_LOG_WARNING, "Mistmatch in coded number of ICC markers between markers\n");
2091  goto out;
2092  } else if (seqno > nummarkers) {
2093  av_log(s->avctx, AV_LOG_WARNING, "Mismatching sequence number and coded number of ICC markers\n");
2094  goto out;
2095  }
2096 
2097  /* Allocate if this is the first APP2 we've seen. */
2098  if (s->iccnum == 0) {
2099  s->iccdata = av_mallocz(nummarkers * sizeof(*(s->iccdata)));
2100  s->iccdatalens = av_mallocz(nummarkers * sizeof(*(s->iccdatalens)));
2101  if (!s->iccdata || !s->iccdatalens) {
2102  av_log(s->avctx, AV_LOG_ERROR, "Could not allocate ICC data arrays\n");
2103  return AVERROR(ENOMEM);
2104  }
2105  s->iccnum = nummarkers;
2106  }
2107 
2108  if (s->iccdata[seqno - 1]) {
2109  av_log(s->avctx, AV_LOG_WARNING, "Duplicate ICC sequence number\n");
2110  goto out;
2111  }
2112 
2113  s->iccdatalens[seqno - 1] = len;
2114  s->iccdata[seqno - 1] = av_malloc(len);
2115  if (!s->iccdata[seqno - 1]) {
2116  av_log(s->avctx, AV_LOG_ERROR, "Could not allocate ICC data buffer\n");
2117  return AVERROR(ENOMEM);
2118  }
2119 
2120  memcpy(s->iccdata[seqno - 1], align_get_bits(&s->gb), len);
2121  skip_bits(&s->gb, len << 3);
2122  len = 0;
2123  s->iccread++;
2124 
2125  if (s->iccread > s->iccnum)
2126  av_log(s->avctx, AV_LOG_WARNING, "Read more ICC markers than are supposed to be coded\n");
2127  }
2128 
2129 out:
2130  /* slow but needed for extreme adobe jpegs */
2131  if (len < 0)
2133  "mjpeg: error, decode_app parser read over the end\n");
2134  while (--len > 0)
2135  skip_bits(&s->gb, 8);
2136 
2137  return 0;
2138 }
2139 
2141 {
2142  int len = get_bits(&s->gb, 16);
2143  if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
2144  int i;
2145  char *cbuf = av_malloc(len - 1);
2146  if (!cbuf)
2147  return AVERROR(ENOMEM);
2148 
2149  for (i = 0; i < len - 2; i++)
2150  cbuf[i] = get_bits(&s->gb, 8);
2151  if (i > 0 && cbuf[i - 1] == '\n')
2152  cbuf[i - 1] = 0;
2153  else
2154  cbuf[i] = 0;
2155 
2156  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2157  av_log(s->avctx, AV_LOG_INFO, "comment: '%s'\n", cbuf);
2158 
2159  /* buggy avid, it puts EOI only at every 10th frame */
2160  if (!strncmp(cbuf, "AVID", 4)) {
2161  parse_avid(s, cbuf, len);
2162  } else if (!strcmp(cbuf, "CS=ITU601"))
2163  s->cs_itu601 = 1;
2164  else if ((!strncmp(cbuf, "Intel(R) JPEG Library, version 1", 32) && s->avctx->codec_tag) ||
2165  (!strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
2166  s->flipped = 1;
2167  else if (!strcmp(cbuf, "MULTISCOPE II")) {
2168  s->avctx->sample_aspect_ratio = (AVRational) { 1, 2 };
2169  s->multiscope = 2;
2170  }
2171 
2172  av_free(cbuf);
2173  }
2174 
2175  return 0;
2176 }
2177 
2178 /* return the 8 bit start code value and update the search
2179  state. Return -1 if no start code found */
2180 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
2181 {
2182  const uint8_t *buf_ptr;
2183  unsigned int v, v2;
2184  int val;
2185  int skipped = 0;
2186 
2187  buf_ptr = *pbuf_ptr;
2188  while (buf_end - buf_ptr > 1) {
2189  v = *buf_ptr++;
2190  v2 = *buf_ptr;
2191  if ((v == 0xff) && (v2 >= SOF0) && (v2 <= COM) && buf_ptr < buf_end) {
2192  val = *buf_ptr++;
2193  goto found;
2194  }
2195  skipped++;
2196  }
2197  buf_ptr = buf_end;
2198  val = -1;
2199 found:
2200  ff_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
2201  *pbuf_ptr = buf_ptr;
2202  return val;
2203 }
2204 
2206  const uint8_t **buf_ptr, const uint8_t *buf_end,
2207  const uint8_t **unescaped_buf_ptr,
2208  int *unescaped_buf_size)
2209 {
2210  int start_code;
2211  start_code = find_marker(buf_ptr, buf_end);
2212 
2213  av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
2214  if (!s->buffer)
2215  return AVERROR(ENOMEM);
2216 
2217  /* unescape buffer of SOS, use special treatment for JPEG-LS */
2218  if (start_code == SOS && !s->ls) {
2219  const uint8_t *src = *buf_ptr;
2220  const uint8_t *ptr = src;
2221  uint8_t *dst = s->buffer;
2222 
2223  #define copy_data_segment(skip) do { \
2224  ptrdiff_t length = (ptr - src) - (skip); \
2225  if (length > 0) { \
2226  memcpy(dst, src, length); \
2227  dst += length; \
2228  src = ptr; \
2229  } \
2230  } while (0)
2231 
2232  if (s->avctx->codec_id == AV_CODEC_ID_THP) {
2233  ptr = buf_end;
2234  copy_data_segment(0);
2235  } else {
2236  while (ptr < buf_end) {
2237  uint8_t x = *(ptr++);
2238 
2239  if (x == 0xff) {
2240  ptrdiff_t skip = 0;
2241  while (ptr < buf_end && x == 0xff) {
2242  x = *(ptr++);
2243  skip++;
2244  }
2245 
2246  /* 0xFF, 0xFF, ... */
2247  if (skip > 1) {
2248  copy_data_segment(skip);
2249 
2250  /* decrement src as it is equal to ptr after the
2251  * copy_data_segment macro and we might want to
2252  * copy the current value of x later on */
2253  src--;
2254  }
2255 
2256  if (x < RST0 || x > RST7) {
2257  copy_data_segment(1);
2258  if (x)
2259  break;
2260  }
2261  }
2262  }
2263  if (src < ptr)
2264  copy_data_segment(0);
2265  }
2266  #undef copy_data_segment
2267 
2268  *unescaped_buf_ptr = s->buffer;
2269  *unescaped_buf_size = dst - s->buffer;
2270  memset(s->buffer + *unescaped_buf_size, 0,
2272 
2273  av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %"PTRDIFF_SPECIFIER" bytes\n",
2274  (buf_end - *buf_ptr) - (dst - s->buffer));
2275  } else if (start_code == SOS && s->ls) {
2276  const uint8_t *src = *buf_ptr;
2277  uint8_t *dst = s->buffer;
2278  int bit_count = 0;
2279  int t = 0, b = 0;
2280  PutBitContext pb;
2281 
2282  /* find marker */
2283  while (src + t < buf_end) {
2284  uint8_t x = src[t++];
2285  if (x == 0xff) {
2286  while ((src + t < buf_end) && x == 0xff)
2287  x = src[t++];
2288  if (x & 0x80) {
2289  t -= FFMIN(2, t);
2290  break;
2291  }
2292  }
2293  }
2294  bit_count = t * 8;
2295  init_put_bits(&pb, dst, t);
2296 
2297  /* unescape bitstream */
2298  while (b < t) {
2299  uint8_t x = src[b++];
2300  put_bits(&pb, 8, x);
2301  if (x == 0xFF && b < t) {
2302  x = src[b++];
2303  if (x & 0x80) {
2304  av_log(s->avctx, AV_LOG_WARNING, "Invalid escape sequence\n");
2305  x &= 0x7f;
2306  }
2307  put_bits(&pb, 7, x);
2308  bit_count--;
2309  }
2310  }
2311  flush_put_bits(&pb);
2312 
2313  *unescaped_buf_ptr = dst;
2314  *unescaped_buf_size = (bit_count + 7) >> 3;
2315  memset(s->buffer + *unescaped_buf_size, 0,
2317  } else {
2318  *unescaped_buf_ptr = *buf_ptr;
2319  *unescaped_buf_size = buf_end - *buf_ptr;
2320  }
2321 
2322  return start_code;
2323 }
2324 
2326 {
2327  int i;
2328 
2329  if (s->iccdata)
2330  for (i = 0; i < s->iccnum; i++)
2331  av_freep(&s->iccdata[i]);
2332  av_freep(&s->iccdata);
2333  av_freep(&s->iccdatalens);
2334 
2335  s->iccread = 0;
2336  s->iccnum = 0;
2337 }
2338 
2339 int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
2340  AVPacket *avpkt)
2341 {
2342  AVFrame *frame = data;
2343  const uint8_t *buf = avpkt->data;
2344  int buf_size = avpkt->size;
2345  MJpegDecodeContext *s = avctx->priv_data;
2346  const uint8_t *buf_end, *buf_ptr;
2347  const uint8_t *unescaped_buf_ptr;
2348  int hshift, vshift;
2349  int unescaped_buf_size;
2350  int start_code;
2351  int i, index;
2352  int ret = 0;
2353  int is16bit;
2354 
2355  s->buf_size = buf_size;
2356 
2358  av_freep(&s->stereo3d);
2359  s->adobe_transform = -1;
2360 
2361  if (s->iccnum != 0)
2362  reset_icc_profile(s);
2363 
2364  buf_ptr = buf;
2365  buf_end = buf + buf_size;
2366  while (buf_ptr < buf_end) {
2367  /* find start next marker */
2368  start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
2369  &unescaped_buf_ptr,
2370  &unescaped_buf_size);
2371  /* EOF */
2372  if (start_code < 0) {
2373  break;
2374  } else if (unescaped_buf_size > INT_MAX / 8) {
2375  av_log(avctx, AV_LOG_ERROR,
2376  "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
2377  start_code, unescaped_buf_size, buf_size);
2378  return AVERROR_INVALIDDATA;
2379  }
2380  av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%"PTRDIFF_SPECIFIER"\n",
2381  start_code, buf_end - buf_ptr);
2382 
2383  ret = init_get_bits8(&s->gb, unescaped_buf_ptr, unescaped_buf_size);
2384 
2385  if (ret < 0) {
2386  av_log(avctx, AV_LOG_ERROR, "invalid buffer\n");
2387  goto fail;
2388  }
2389 
2390  s->start_code = start_code;
2391  if (s->avctx->debug & FF_DEBUG_STARTCODE)
2392  av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
2393 
2394  /* process markers */
2395  if (start_code >= RST0 && start_code <= RST7) {
2396  av_log(avctx, AV_LOG_DEBUG,
2397  "restart marker: %d\n", start_code & 0x0f);
2398  /* APP fields */
2399  } else if (start_code >= APP0 && start_code <= APP15) {
2400  if ((ret = mjpeg_decode_app(s)) < 0)
2401  av_log(avctx, AV_LOG_ERROR, "unable to decode APP fields: %s\n",
2402  av_err2str(ret));
2403  /* Comment */
2404  } else if (start_code == COM) {
2405  ret = mjpeg_decode_com(s);
2406  if (ret < 0)
2407  return ret;
2408  } else if (start_code == DQT) {
2409  ret = ff_mjpeg_decode_dqt(s);
2410  if (ret < 0)
2411  return ret;
2412  }
2413 
2414  ret = -1;
2415 
2416  if (!CONFIG_JPEGLS_DECODER &&
2417  (start_code == SOF48 || start_code == LSE)) {
2418  av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n");
2419  return AVERROR(ENOSYS);
2420  }
2421 
2422  if (avctx->skip_frame == AVDISCARD_ALL) {
2423  switch(start_code) {
2424  case SOF0:
2425  case SOF1:
2426  case SOF2:
2427  case SOF3:
2428  case SOF48:
2429  case SOI:
2430  case SOS:
2431  case EOI:
2432  break;
2433  default:
2434  goto skip;
2435  }
2436  }
2437 
2438  switch (start_code) {
2439  case SOI:
2440  s->restart_interval = 0;
2441  s->restart_count = 0;
2442  s->raw_image_buffer = buf_ptr;
2443  s->raw_image_buffer_size = buf_end - buf_ptr;
2444  /* nothing to do on SOI */
2445  break;
2446  case DHT:
2447  if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
2448  av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
2449  goto fail;
2450  }
2451  break;
2452  case SOF0:
2453  case SOF1:
2454  if (start_code == SOF0)
2456  else
2458  s->lossless = 0;
2459  s->ls = 0;
2460  s->progressive = 0;
2461  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2462  goto fail;
2463  break;
2464  case SOF2:
2466  s->lossless = 0;
2467  s->ls = 0;
2468  s->progressive = 1;
2469  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2470  goto fail;
2471  break;
2472  case SOF3:
2475  s->lossless = 1;
2476  s->ls = 0;
2477  s->progressive = 0;
2478  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2479  goto fail;
2480  break;
2481  case SOF48:
2484  s->lossless = 1;
2485  s->ls = 1;
2486  s->progressive = 0;
2487  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2488  goto fail;
2489  break;
2490  case LSE:
2491  if (!CONFIG_JPEGLS_DECODER ||
2492  (ret = ff_jpegls_decode_lse(s)) < 0)
2493  goto fail;
2494  break;
2495  case EOI:
2496 eoi_parser:
2497  if (!avctx->hwaccel && avctx->skip_frame != AVDISCARD_ALL &&
2498  s->progressive && s->cur_scan && s->got_picture)
2500  s->cur_scan = 0;
2501  if (!s->got_picture) {
2502  av_log(avctx, AV_LOG_WARNING,
2503  "Found EOI before any SOF, ignoring\n");
2504  break;
2505  }
2506  if (s->interlaced) {
2507  s->bottom_field ^= 1;
2508  /* if not bottom field, do not output image yet */
2509  if (s->bottom_field == !s->interlace_polarity)
2510  break;
2511  }
2512  if (avctx->skip_frame == AVDISCARD_ALL) {
2513  s->got_picture = 0;
2514  goto the_end_no_picture;
2515  }
2516  if (s->avctx->hwaccel) {
2517  ret = s->avctx->hwaccel->end_frame(s->avctx);
2518  if (ret < 0)
2519  return ret;
2520 
2522  }
2523  if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0)
2524  return ret;
2525  *got_frame = 1;
2526  s->got_picture = 0;
2527 
2528  if (!s->lossless) {
2529  int qp = FFMAX3(s->qscale[0],
2530  s->qscale[1],
2531  s->qscale[2]);
2532  int qpw = (s->width + 15) / 16;
2533  AVBufferRef *qp_table_buf = av_buffer_alloc(qpw);
2534  if (qp_table_buf) {
2535  memset(qp_table_buf->data, qp, qpw);
2536  av_frame_set_qp_table(data, qp_table_buf, 0, FF_QSCALE_TYPE_MPEG1);
2537  }
2538 
2539  if(avctx->debug & FF_DEBUG_QP)
2540  av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", qp);
2541  }
2542 
2543  goto the_end;
2544  case SOS:
2545  s->raw_scan_buffer = buf_ptr;
2546  s->raw_scan_buffer_size = buf_end - buf_ptr;
2547 
2548  s->cur_scan++;
2549  if (avctx->skip_frame == AVDISCARD_ALL) {
2550  skip_bits(&s->gb, get_bits_left(&s->gb));
2551  break;
2552  }
2553 
2554  if ((ret = ff_mjpeg_decode_sos(s, NULL, 0, NULL)) < 0 &&
2555  (avctx->err_recognition & AV_EF_EXPLODE))
2556  goto fail;
2557  break;
2558  case DRI:
2559  if ((ret = mjpeg_decode_dri(s)) < 0)
2560  return ret;
2561  break;
2562  case SOF5:
2563  case SOF6:
2564  case SOF7:
2565  case SOF9:
2566  case SOF10:
2567  case SOF11:
2568  case SOF13:
2569  case SOF14:
2570  case SOF15:
2571  case JPG:
2572  av_log(avctx, AV_LOG_ERROR,
2573  "mjpeg: unsupported coding type (%x)\n", start_code);
2574  break;
2575  }
2576 
2577 skip:
2578  /* eof process start code */
2579  buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
2580  av_log(avctx, AV_LOG_DEBUG,
2581  "marker parser used %d bytes (%d bits)\n",
2582  (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
2583  }
2584  if (s->got_picture && s->cur_scan) {
2585  av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
2586  goto eoi_parser;
2587  }
2588  av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
2589  return AVERROR_INVALIDDATA;
2590 fail:
2591  s->got_picture = 0;
2592  return ret;
2593 the_end:
2594 
2595  is16bit = av_pix_fmt_desc_get(s->avctx->pix_fmt)->comp[0].step > 1;
2596 
2597  if (AV_RB32(s->upscale_h)) {
2598  int p;
2600  avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2601  avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2602  avctx->pix_fmt == AV_PIX_FMT_YUV440P ||
2603  avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2604  avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2605  avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
2606  avctx->pix_fmt == AV_PIX_FMT_YUV420P16||
2607  avctx->pix_fmt == AV_PIX_FMT_YUVA420P ||
2608  avctx->pix_fmt == AV_PIX_FMT_YUVA420P16||
2609  avctx->pix_fmt == AV_PIX_FMT_GBRP ||
2610  avctx->pix_fmt == AV_PIX_FMT_GBRAP
2611  );
2612  ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2613  if (ret)
2614  return ret;
2615 
2617  for (p = 0; p<s->nb_components; p++) {
2618  uint8_t *line = s->picture_ptr->data[p];
2619  int w = s->width;
2620  int h = s->height;
2621  if (!s->upscale_h[p])
2622  continue;
2623  if (p==1 || p==2) {
2624  w = AV_CEIL_RSHIFT(w, hshift);
2625  h = AV_CEIL_RSHIFT(h, vshift);
2626  }
2627  if (s->upscale_v[p] == 1)
2628  h = (h+1)>>1;
2629  av_assert0(w > 0);
2630  for (i = 0; i < h; i++) {
2631  if (s->upscale_h[p] == 1) {
2632  if (is16bit) ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 2];
2633  else line[w - 1] = line[(w - 1) / 2];
2634  for (index = w - 2; index > 0; index--) {
2635  if (is16bit)
2636  ((uint16_t*)line)[index] = (((uint16_t*)line)[index / 2] + ((uint16_t*)line)[(index + 1) / 2]) >> 1;
2637  else
2638  line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
2639  }
2640  } else if (s->upscale_h[p] == 2) {
2641  if (is16bit) {
2642  ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 3];
2643  if (w > 1)
2644  ((uint16_t*)line)[w - 2] = ((uint16_t*)line)[w - 1];
2645  } else {
2646  line[w - 1] = line[(w - 1) / 3];
2647  if (w > 1)
2648  line[w - 2] = line[w - 1];
2649  }
2650  for (index = w - 3; index > 0; index--) {
2651  line[index] = (line[index / 3] + line[(index + 1) / 3] + line[(index + 2) / 3] + 1) / 3;
2652  }
2653  }
2654  line += s->linesize[p];
2655  }
2656  }
2657  }
2658  if (AV_RB32(s->upscale_v)) {
2659  int p;
2661  avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2662  avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
2663  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
2664  avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2665  avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
2666  avctx->pix_fmt == AV_PIX_FMT_YUV440P ||
2667  avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2668  avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2669  avctx->pix_fmt == AV_PIX_FMT_YUVA420P ||
2670  avctx->pix_fmt == AV_PIX_FMT_YUVA420P16||
2671  avctx->pix_fmt == AV_PIX_FMT_GBRP ||
2672  avctx->pix_fmt == AV_PIX_FMT_GBRAP
2673  );
2674  ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2675  if (ret)
2676  return ret;
2677 
2679  for (p = 0; p < s->nb_components; p++) {
2680  uint8_t *dst;
2681  int w = s->width;
2682  int h = s->height;
2683  if (!s->upscale_v[p])
2684  continue;
2685  if (p==1 || p==2) {
2686  w = AV_CEIL_RSHIFT(w, hshift);
2687  h = AV_CEIL_RSHIFT(h, vshift);
2688  }
2689  dst = &((uint8_t *)s->picture_ptr->data[p])[(h - 1) * s->linesize[p]];
2690  for (i = h - 1; i; i--) {
2691  uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[p])[i * s->upscale_v[p] / (s->upscale_v[p] + 1) * s->linesize[p]];
2692  uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[p])[(i + 1) * s->upscale_v[p] / (s->upscale_v[p] + 1) * s->linesize[p]];
2693  if (s->upscale_v[p] != 2 && (src1 == src2 || i == h - 1)) {
2694  memcpy(dst, src1, w);
2695  } else {
2696  for (index = 0; index < w; index++)
2697  dst[index] = (src1[index] + src2[index]) >> 1;
2698  }
2699  dst -= s->linesize[p];
2700  }
2701  }
2702  }
2703  if (s->flipped && !s->rgb) {
2704  int j;
2705  ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2706  if (ret)
2707  return ret;
2708 
2710  for (index=0; index<s->nb_components; index++) {
2711  uint8_t *dst = s->picture_ptr->data[index];
2712  int w = s->picture_ptr->width;
2713  int h = s->picture_ptr->height;
2714  if(index && index<3){
2715  w = AV_CEIL_RSHIFT(w, hshift);
2716  h = AV_CEIL_RSHIFT(h, vshift);
2717  }
2718  if(dst){
2719  uint8_t *dst2 = dst + s->picture_ptr->linesize[index]*(h-1);
2720  for (i=0; i<h/2; i++) {
2721  for (j=0; j<w; j++)
2722  FFSWAP(int, dst[j], dst2[j]);
2723  dst += s->picture_ptr->linesize[index];
2724  dst2 -= s->picture_ptr->linesize[index];
2725  }
2726  }
2727  }
2728  }
2729  if (s->adobe_transform == 0 && s->avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
2730  int w = s->picture_ptr->width;
2731  int h = s->picture_ptr->height;
2732  av_assert0(s->nb_components == 4);
2733  for (i=0; i<h; i++) {
2734  int j;
2735  uint8_t *dst[4];
2736  for (index=0; index<4; index++) {
2737  dst[index] = s->picture_ptr->data[index]
2738  + s->picture_ptr->linesize[index]*i;
2739  }
2740  for (j=0; j<w; j++) {
2741  int k = dst[3][j];
2742  int r = dst[0][j] * k;
2743  int g = dst[1][j] * k;
2744  int b = dst[2][j] * k;
2745  dst[0][j] = g*257 >> 16;
2746  dst[1][j] = b*257 >> 16;
2747  dst[2][j] = r*257 >> 16;
2748  dst[3][j] = 255;
2749  }
2750  }
2751  }
2752  if (s->adobe_transform == 2 && s->avctx->pix_fmt == AV_PIX_FMT_YUVA444P) {
2753  int w = s->picture_ptr->width;
2754  int h = s->picture_ptr->height;
2755  av_assert0(s->nb_components == 4);
2756  for (i=0; i<h; i++) {
2757  int j;
2758  uint8_t *dst[4];
2759  for (index=0; index<4; index++) {
2760  dst[index] = s->picture_ptr->data[index]
2761  + s->picture_ptr->linesize[index]*i;
2762  }
2763  for (j=0; j<w; j++) {
2764  int k = dst[3][j];
2765  int r = (255 - dst[0][j]) * k;
2766  int g = (128 - dst[1][j]) * k;
2767  int b = (128 - dst[2][j]) * k;
2768  dst[0][j] = r*257 >> 16;
2769  dst[1][j] = (g*257 >> 16) + 128;
2770  dst[2][j] = (b*257 >> 16) + 128;
2771  dst[3][j] = 255;
2772  }
2773  }
2774  }
2775 
2776  if (s->stereo3d) {
2777  AVStereo3D *stereo = av_stereo3d_create_side_data(data);
2778  if (stereo) {
2779  stereo->type = s->stereo3d->type;
2780  stereo->flags = s->stereo3d->flags;
2781  }
2782  av_freep(&s->stereo3d);
2783  }
2784 
2785  if (s->iccnum != 0 && s->iccnum == s->iccread) {
2786  AVFrameSideData *sd;
2787  size_t offset = 0;
2788  int total_size = 0;
2789  int i;
2790 
2791  /* Sum size of all parts. */
2792  for (i = 0; i < s->iccnum; i++)
2793  total_size += s->iccdatalens[i];
2794 
2795  sd = av_frame_new_side_data(data, AV_FRAME_DATA_ICC_PROFILE, total_size);
2796  if (!sd) {
2797  av_log(s->avctx, AV_LOG_ERROR, "Could not allocate frame side data\n");
2798  return AVERROR(ENOMEM);
2799  }
2800 
2801  /* Reassemble the parts, which are now in-order. */
2802  for (i = 0; i < s->iccnum; i++) {
2803  memcpy(sd->data + offset, s->iccdata[i], s->iccdatalens[i]);
2804  offset += s->iccdatalens[i];
2805  }
2806  }
2807 
2808  av_dict_copy(&((AVFrame *) data)->metadata, s->exif_metadata, 0);
2810 
2811 the_end_no_picture:
2812  av_log(avctx, AV_LOG_DEBUG, "decode frame unused %"PTRDIFF_SPECIFIER" bytes\n",
2813  buf_end - buf_ptr);
2814 // return buf_end - buf_ptr;
2815  return buf_ptr - buf;
2816 }
2817 
2818 /* mxpeg may call the following function (with a blank MJpegDecodeContext)
2819  * even without having called ff_mjpeg_decode_init(). */
2821 {
2822  MJpegDecodeContext *s = avctx->priv_data;
2823  int i, j;
2824 
2825  if (s->interlaced && s->bottom_field == !s->interlace_polarity && s->got_picture && !avctx->frame_number) {
2826  av_log(avctx, AV_LOG_INFO, "Single field\n");
2827  }
2828 
2829  if (s->picture) {
2830  av_frame_free(&s->picture);
2831  s->picture_ptr = NULL;
2832  } else if (s->picture_ptr)
2834 
2835  av_freep(&s->buffer);
2836  av_freep(&s->stereo3d);
2837  av_freep(&s->ljpeg_buffer);
2838  s->ljpeg_buffer_size = 0;
2839 
2840  for (i = 0; i < 3; i++) {
2841  for (j = 0; j < 4; j++)
2842  ff_free_vlc(&s->vlcs[i][j]);
2843  }
2844  for (i = 0; i < MAX_COMPONENTS; i++) {
2845  av_freep(&s->blocks[i]);
2846  av_freep(&s->last_nnz[i]);
2847  }
2849 
2850  reset_icc_profile(s);
2851 
2853 
2854  return 0;
2855 }
2856 
2857 static void decode_flush(AVCodecContext *avctx)
2858 {
2859  MJpegDecodeContext *s = avctx->priv_data;
2860  s->got_picture = 0;
2861 }
2862 
2863 #if CONFIG_MJPEG_DECODER
2864 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
2865 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2866 static const AVOption options[] = {
2867  { "extern_huff", "Use external huffman table.",
2868  OFFSET(extern_huff), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
2869  { NULL },
2870 };
2871 
2872 static const AVClass mjpegdec_class = {
2873  .class_name = "MJPEG decoder",
2874  .item_name = av_default_item_name,
2875  .option = options,
2876  .version = LIBAVUTIL_VERSION_INT,
2877 };
2878 
2880  .name = "mjpeg",
2881  .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
2882  .type = AVMEDIA_TYPE_VIDEO,
2883  .id = AV_CODEC_ID_MJPEG,
2884  .priv_data_size = sizeof(MJpegDecodeContext),
2886  .close = ff_mjpeg_decode_end,
2888  .flush = decode_flush,
2889  .capabilities = AV_CODEC_CAP_DR1,
2890  .max_lowres = 3,
2891  .priv_class = &mjpegdec_class,
2895  .hw_configs = (const AVCodecHWConfigInternal*[]) {
2896 #if CONFIG_MJPEG_NVDEC_HWACCEL
2897  HWACCEL_NVDEC(mjpeg),
2898 #endif
2899 #if CONFIG_MJPEG_VAAPI_HWACCEL
2900  HWACCEL_VAAPI(mjpeg),
2901 #endif
2902  NULL
2903  },
2904 };
2905 #endif
2906 #if CONFIG_THP_DECODER
2908  .name = "thp",
2909  .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
2910  .type = AVMEDIA_TYPE_VIDEO,
2911  .id = AV_CODEC_ID_THP,
2912  .priv_data_size = sizeof(MJpegDecodeContext),
2914  .close = ff_mjpeg_decode_end,
2916  .flush = decode_flush,
2917  .capabilities = AV_CODEC_CAP_DR1,
2918  .max_lowres = 3,
2920 };
2921 #endif
int block_stride[MAX_COMPONENTS]
Definition: mjpegdec.h:86
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:602
#define AV_STEREO3D_FLAG_INVERT
Inverted views, Right/Bottom represents the left view.
Definition: stereo3d.h:167
int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
Definition: frame.c:55
#define NULL
Definition: coverity.c:32
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: decode.c:1299
const struct AVCodec * codec
Definition: avcodec.h:540
const AVPixFmtDescriptor * pix_desc
!< stereoscopic information (cached, since it is read before frame allocation)
Definition: mjpegdec.h:136
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Definition: mjpeg.h:81
int v_count[MAX_COMPONENTS]
Definition: mjpegdec.h:89
#define se(name, range_min, range_max)
Definition: cbs_h2645.c:273
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
int ff_exif_decode_ifd(void *logctx, GetByteContext *gbytes, int le, int depth, AVDictionary **metadata)
Definition: exif.c:122
AVOption.
Definition: opt.h:248
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static void flush(AVCodecContext *avctx)
enum AVPixelFormat hwaccel_sw_pix_fmt
Definition: mjpegdec.h:152
Definition: mjpeg.h:71
Definition: mjpeg.h:111
Definition: mjpeg.h:73
float re
Definition: fft.c:82
Definition: mjpeg.h:40
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
misc image utilities
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
Definition: mjpeg.h:42
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
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:106
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
const char * g
Definition: vf_curves.c:115
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:389
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:518
size_t raw_image_buffer_size
Definition: mjpegdec.h:145
void(* clear_block)(int16_t *block)
Definition: blockdsp.h:36
#define avpriv_request_sample(...)
int h_scount[MAX_COMPONENTS]
Definition: mjpegdec.h:94
BlockDSPContext bdsp
Definition: mjpegdec.h:111
int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
Definition: mjpegdec.c:215
static int mjpeg_decode_com(MJpegDecodeContext *s)
Definition: mjpegdec.c:2140
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1166
TIFF constants & data structures.
int ff_init_vlc_sparse(VLC *vlc_arg, 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:275
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
int num
Numerator.
Definition: rational.h:59
#define HWACCEL_NVDEC(codec)
Definition: hwconfig.h:71
int qscale[4]
quantizer scale calculated from quant_matrixes
Definition: mjpegdec.h:58
int size
Definition: packet.h:364
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
Definition: blockdsp.c:60
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
uint8_t * buffer
Definition: mjpegdec.h:54
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:910
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:741
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
#define copy_data_segment(skip)
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
GLint GLenum type
Definition: opengl_enc.c:104
Definition: mjpeg.h:68
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:72
int dc_index[MAX_COMPONENTS]
Definition: mjpegdec.h:91
Definition: mjpeg.h:75
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
Definition: mjpeg.h:53
int linesize[MAX_COMPONENTS]
linesize << interlaced
Definition: mjpegdec.h:103
discard all
Definition: avcodec.h:236
uint8_t permutated[64]
Definition: idctdsp.h:33
Views are next to each other.
Definition: stereo3d.h:67
uint8_t upscale_v[4]
Definition: mjpegdec.h:70
uint8_t run
Definition: svq3.c:204
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1762
static int decode_block(MJpegDecodeContext *s, int16_t *block, int component, int dc_index, int ac_index, uint16_t *quant_matrix)
Definition: mjpegdec.c:808
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1695
int profile
profile
Definition: avcodec.h:1871
int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
Definition: mjpegdec.c:256
AVCodec.
Definition: codec.h:190
EXIF metadata parser.
JPEG-LS decoder.
MJPEG encoder and decoder.
#define FF_PROFILE_MJPEG_HUFFMAN_PROGRESSIVE_DCT
Definition: avcodec.h:1970
AVStereo3D * av_stereo3d_alloc(void)
Allocate an AVStereo3D structure and set its fields to default values.
Definition: stereo3d.c:28
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
int comp_index[MAX_COMPONENTS]
Definition: mjpegdec.h:90
static void reset_icc_profile(MJpegDecodeContext *s)
Definition: mjpegdec.c:2325
static void mjpeg_idct_scan_progressive_ac(MJpegDecodeContext *s)
Definition: mjpegdec.c:1598
HpelDSPContext hdsp
Definition: mjpegdec.h:112
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:654
#define FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT
Definition: avcodec.h:1968
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:2016
static void build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, const uint8_t *bits_table)
Definition: mjpegdec.c:53
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:72
const uint8_t * raw_image_buffer
Definition: mjpegdec.h:144
int16_t block[64]
Definition: mjpegdec.h:105
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, int nb_codes, int is_ac)
Definition: mjpegdec.c:68
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
Definition: mjpeg.h:72
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
The exact code depends on how similar the blocks are and how related they are to the block
uint8_t
#define av_cold
Definition: attributes.h:88
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
static int mjpeg_decode_dri(MJpegDecodeContext *s)
Definition: mjpegdec.c:1811
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
AVOptions.
Stereo 3D type: this structure describes how two videos are packed within a single video surface...
Definition: stereo3d.h:176
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:1617
uint16_t(* ljpeg_buffer)[4]
Definition: mjpegdec.h:128
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
Definition: mjpeg.h:46
unsigned int ljpeg_buffer_size
Definition: mjpegdec.h:129
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:443
#define FF_CODEC_PROPERTY_LOSSLESS
Definition: avcodec.h:2204
#define FF_PROFILE_MJPEG_JPEG_LS
Definition: avcodec.h:1972
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:632
Definition: mjpeg.h:54
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:91
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: jpegtables.c:65
uint8_t * last_nnz[MAX_COMPONENTS]
Definition: mjpegdec.h:107
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
AVFrame * picture_ptr
Definition: mjpegdec.h:101
Structure to hold side data for an AVFrame.
Definition: frame.h:220
#define height
uint8_t * data
Definition: packet.h:363
int quant_sindex[MAX_COMPONENTS]
Definition: mjpegdec.h:96
#define MAX_COMPONENTS
Definition: mjpegdec.h:44
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
Definition: pixfmt.h:100
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
#define FF_PROFILE_MJPEG_HUFFMAN_LOSSLESS
Definition: avcodec.h:1971
int h_count[MAX_COMPONENTS]
Definition: mjpegdec.h:88
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
#define ff_dlog(a,...)
#define AV_PIX_FMT_BGR48
Definition: pixfmt.h:390
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:412
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:461
int lowres
low resolution decoding, 1-> 1/2 size, 2->1/4 size
Definition: avcodec.h:1770
Video is not stereoscopic (and metadata has to be there).
Definition: stereo3d.h:55
ptrdiff_t size
Definition: opengl_enc.c:100
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:441
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1173
#define av_log(a,...)
#define PREDICT(ret, topleft, top, left, predictor)
Definition: mjpeg.h:118
static int aligned(int val)
Definition: dashdec.c:167
#define src
Definition: vp8dsp.c:254
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mjpegdec.c:2339
enum AVCodecID id
Definition: codec.h:204
AVDictionary * exif_metadata
Definition: mjpegdec.h:132
static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block, uint8_t *last_nnz, int ac_index, uint16_t *quant_matrix, int ss, int se, int Al, int *EOBRUN)
Definition: mjpegdec.c:875
uint8_t ** iccdata
Definition: mjpegdec.h:138
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:178
int width
Definition: frame.h:372
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int flags
Additional information about the frame packing.
Definition: stereo3d.h:185
static int handle_rstn(MJpegDecodeContext *s, int nb_components)
Definition: mjpegdec.c:1039
static const uint16_t mask[17]
Definition: lzw.c:38
static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block, uint8_t *last_nnz, int ac_index, uint16_t *quant_matrix, int ss, int se, int Al, int *EOBRUN)
Definition: mjpegdec.c:974
#define PTRDIFF_SPECIFIER
Definition: internal.h:228
int nb_blocks[MAX_COMPONENTS]
Definition: mjpegdec.h:93
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: jpegtables.c:70
static void copy_mb(CinepakEncContext *s, uint8_t *a_data[4], int a_linesize[4], uint8_t *b_data[4], int b_linesize[4])
Definition: cinepakenc.c:506
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:2820
VLC vlcs[3][4]
Definition: mjpegdec.h:57
static void parse_avid(MJpegDecodeContext *s, uint8_t *buf, int len)
Definition: mjpegdec.c:134
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2601
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
Views are packed per line, as if interlaced.
Definition: stereo3d.h:129
const char * r
Definition: vf_curves.c:114
unsigned int pos
Definition: spdifenc.c:410
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:443
#define av_fourcc2str(fourcc)
Definition: avutil.h:348
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your local see the OFFSET() macro
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:611
Definition: graph2dot.c:48
simple assert() macros that are a bit more flexible than ISO C assert().
GLsizei GLsizei * length
Definition: opengl_enc.c:114
const char * name
Name of the codec implementation.
Definition: codec.h:197
uint8_t bits
Definition: vp3data.h:202
static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al, const uint8_t *mb_bitmask, int mb_bitmask_size, const AVFrame *reference)
Definition: mjpegdec.c:1425
int ff_jpegls_decode_lse(MJpegDecodeContext *s)
Decode LSE block with initialization parameters.
Definition: jpeglsdec.c:51
static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
Definition: mjpegdec.c:2180
#define CLOSE_READER(name, gb)
Definition: get_bits.h:149
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:123
Definition: mjpeg.h:39
Definition: mjpeg.h:70
Definition: vlc.h:26
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition: hpeldsp.c:338
AVStereo3D * av_stereo3d_create_side_data(AVFrame *frame)
Allocate a complete AVFrameSideData and add it to the frame.
Definition: stereo3d.c:33
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:502
JPEG-LS.
Definition: mjpeg.h:103
Definition: mjpeg.h:79
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:317
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:261
ScanTable scantable
Definition: mjpegdec.h:110
Definition: mjpeg.h:80
#define b
Definition: input.c:41
static av_always_inline void mjpeg_copy_block(MJpegDecodeContext *s, uint8_t *dst, const uint8_t *src, int linesize, int lowres)
Definition: mjpegdec.c:1394
Definition: mjpeg.h:56
int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
Definition: mjpegdec.c:317
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:397
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:418
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1660
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:383
#define FFMIN(a, b)
Definition: common.h:96
Definition: mjpeg.h:44
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
uint8_t interlaced
Definition: mxfenc.c:2168
#define width
int component_id[MAX_COMPONENTS]
Definition: mjpegdec.h:87
static int mjpeg_decode_app(MJpegDecodeContext *s)
Definition: mjpegdec.c:1823
#define NEG_USR32(a, s)
Definition: mathops.h:166
uint8_t w
Definition: llviddspenc.c:38
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return values
uint8_t raw_huffman_lengths[2][4][16]
Definition: mjpegdec.h:149
Definition: mjpeg.h:41
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
#define FF_PROFILE_MJPEG_HUFFMAN_EXTENDED_SEQUENTIAL_DCT
Definition: avcodec.h:1969
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:446
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
Definition: frame.h:143
int quant_index[4]
Definition: mjpegdec.h:98
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:199
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:797
int v_scount[MAX_COMPONENTS]
Definition: mjpegdec.h:95
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:1671
#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:706
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:96
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
GetBitContext gb
Definition: mjpegdec.h:49
void(* idct_put)(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
block -> idct -> clip to unsigned 8 bit -> dest.
Definition: idctdsp.h:72
HW acceleration through CUDA.
Definition: pixfmt.h:235
#define ZERO_RUN
Definition: mjpegdec.c:956
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:211
#define FF_ARRAY_ELEMS(a)
Full range content.
Definition: pixfmt.h:586
int bits
Definition: vlc.h:27
if(ret)
static const float pred[4]
Definition: siprdata.h:259
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:410
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:387
AVCodec ff_mjpeg_decoder
IDCTDSPContext idsp
Definition: mjpegdec.h:113
#define src1
Definition: h264pred.c:139
enum AVStereo3DType type
How views are packed within the video.
Definition: stereo3d.h:180
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define av_bswap32
Definition: bswap.h:33
Libavcodec external API header.
Views are on top of each other.
Definition: stereo3d.h:79
Definition: mjpeg.h:52
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:91
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
Definition: avcodec.h:2523
enum AVCodecID codec_id
Definition: avcodec.h:541
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:345
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
static void copy_block4(uint8_t *dst, const uint8_t *src, ptrdiff_t dstStride, ptrdiff_t srcStride, int h)
Definition: copy_block.h:37
int debug
debug
Definition: avcodec.h:1616
AVStereo3D * stereo3d
Definition: mjpegdec.h:134
main external API structure.
Definition: avcodec.h:531
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2]...the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so...,+,-,+,-,+,+,-,+,-,+,...hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32-hcoeff[1]-hcoeff[2]-...a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2}an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||.........intra?||||:Block01:yes no||||:Block02:.................||||:Block03::y DC::ref index:||||:Block04::cb DC::motion x:||||.........:cr DC::motion y:||||.................|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------------------------------|||Y subbands||Cb subbands||Cr subbands||||------||------||------|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||------||------||------||||------||------||------|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||------||------||------||||------||------||------|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||------||------||------||||------||------||------|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------------------------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction------------|\Dequantization-------------------\||Reference frames|\IDWT|--------------|Motion\|||Frame 0||Frame 1||Compensation.OBMC v-------|--------------|--------------.\------> Frame n output Frame Frame<----------------------------------/|...|-------------------Range Coder:============Binary Range Coder:-------------------The implemented range coder is an adapted version based upon"Range encoding: an algorithm for removing redundancy from a digitised message."by G.N.N.Martin.The symbols encoded by the Snow range coder are bits(0|1).The associated probabilities are not fix but change depending on the symbol mix seen so far.bit seen|new state---------+-----------------------------------------------0|256-state_transition_table[256-old_state];1|state_transition_table[old_state];state_transition_table={0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:-------------------------FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1.the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff)*mv_scale Intra DC Prediction block[y][x] dc[1]
Definition: snow.txt:400
uint8_t * data
The data buffer.
Definition: buffer.h:89
static int get_xbits(GetBitContext *s, int n)
Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
Definition: get_bits.h:321
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
Definition: avcodec.h:556
#define OPEN_READER(name, gb)
Definition: get_bits.h:138
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1879
uint8_t * data
Definition: frame.h:222
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2]...the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so...,+,-,+,-,+,+,-,+,-,+,...hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32-hcoeff[1]-hcoeff[2]-...a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2}an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||.........intra?||||:Block01:yes no||||:Block02:.................||||:Block03::y DC::ref index:||||:Block04::cb DC::motion x:||||.........:cr DC::motion y:||||.................|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------------------------------|||Y subbands||Cb subbands||Cr subbands||||------||------||------|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||------||------||------||||------||------||------|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||------||------||------||||------||------||------|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||------||------||------||||------||------||------|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------------------------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction------------|\Dequantization-------------------\||Reference frames|\IDWT|--------------|Motion\|||Frame 0||Frame 1||Compensation.OBMC v-------|--------------|--------------.\------> Frame n output Frame Frame<----------------------------------/|...|-------------------Range Coder:============Binary Range Coder:-------------------The implemented range coder is an adapted version based upon"Range encoding: an algorithm for removing redundancy from a digitised message."by G.N.N.Martin.The symbols encoded by the Snow range coder are bits(0|1).The associated probabilities are not fix but change depending on the symbol mix seen so far.bit seen|new state---------+-----------------------------------------------0|256-state_transition_table[256-old_state];1|state_transition_table[old_state];state_transition_table={0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:-------------------------FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1.the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:206
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:633
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: jpegtables.c:67
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void init_idct(AVCodecContext *avctx)
Definition: mjpegdec.c:145
int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv)
Definition: jpeglsdec.c:348
int coded_height
Definition: avcodec.h:719
Describe the class of an AVClass context structure.
Definition: log.h:67
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
static const AVProfile profiles[]
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, int size)
Add a new side data to a frame.
Definition: frame.c:726
int index
Definition: gxfenc.c:89
static int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
Definition: mjpegdec.c:790
int ac_index[MAX_COMPONENTS]
Definition: mjpegdec.h:92
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1159
Rational number (pair of numerator and denominator).
Definition: rational.h:58
static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform)
Definition: mjpegdec.c:1074
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
#define GET_CACHE(name, gb)
Definition: get_bits.h:215
static const SheerTable rgb[2]
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
Definition: mjpeg.h:45
uint64_t coefs_finished[MAX_COMPONENTS]
bitmask of which coefs have been completely decoded (progressive mode)
Definition: mjpegdec.h:108
Definition: mjpeg.h:48
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: jpegtables.c:99
enum AVPixelFormat hwaccel_pix_fmt
Definition: mjpegdec.h:153
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:300
uint8_t raw_huffman_values[2][4][256]
Definition: mjpegdec.h:150
static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al)
Definition: mjpegdec.c:1546
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: jpegtables.c:102
#define MIN_CACHE_BITS
Definition: get_bits.h:128
Definition: mjpeg.h:47
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
JPEG-LS extension parameters.
Definition: mjpeg.h:104
#define flags(name, subs,...)
Definition: cbs_av1.c:560
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
size_t raw_scan_buffer_size
Definition: mjpegdec.h:147
int(* decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Callback for each slice.
Definition: avcodec.h:2512
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:328
uint8_t level
Definition: svq3.c:205
int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask, int mb_bitmask_size, const AVFrame *reference)
Definition: mjpegdec.c:1633
Narrow or limited range content.
Definition: pixfmt.h:569
int(* start_frame)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Called at the beginning of each frame or field picture.
Definition: avcodec.h:2484
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:154
static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block, int component, int dc_index, uint16_t *quant_matrix, int Al)
Definition: mjpegdec.c:857
Definition: mjpeg.h:94
const AVProfile ff_mjpeg_profiles[]
Definition: profiles.c:163
static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform, int nb_components)
Definition: mjpegdec.c:1240
A reference to a data buffer.
Definition: buffer.h:81
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Y , 8bpp.
Definition: pixfmt.h:74
const OptionDef options[]
Definition: ffmpeg_opt.c:3400
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:117
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
Definition: internal.h:60
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
#define FF_DEBUG_QP
Definition: avcodec.h:1621
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
unsigned properties
Properties of the stream that gets decoded.
Definition: avcodec.h:2203
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:64
int den
Denominator.
Definition: rational.h:60
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:215
static int lowres
Definition: ffplay.c:336
const uint8_t * raw_scan_buffer
Definition: mjpegdec.h:146
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: jpegtables.c:73
AVCodecContext * avctx
Definition: mjpegdec.h:48
void * priv_data
Definition: avcodec.h:558
static void copy_block2(uint8_t *dst, const uint8_t *src, ptrdiff_t dstStride, ptrdiff_t srcStride, int h)
Definition: copy_block.h:27
#define av_free(p)
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: idctdsp.c:29
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:1630
static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
Definition: mjpegdec.c:1410
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: idctdsp.c:238
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:466
int got_picture
we found a SOF and picture is valid, too.
Definition: mjpegdec.h:102
int len
#define HWACCEL_VAAPI(codec)
Definition: hwconfig.h:73
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: jpegtables.c:75
int frame_priv_data_size
Size of per-frame hardware accelerator private data.
Definition: avcodec.h:2532
int16_t(*[MAX_COMPONENTS] blocks)[64]
intermediate sums (progressive mode)
Definition: mjpegdec.h:106
AVFrame * picture
Definition: mjpegdec.h:100
void * hwaccel_picture_private
Definition: mjpegdec.h:154
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
The official guide to swscale for confused that is
Definition: swscale.txt:2
Definition: mjpeg.h:50
Y , 16bpp, little-endian.
Definition: pixfmt.h:98
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:392
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:212
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:258
int last_dc[MAX_COMPONENTS]
Definition: mjpegdec.h:99
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:693
static int init_default_huffman_tables(MJpegDecodeContext *s)
Definition: mjpegdec.c:92
#define REFINE_BIT(j)
Definition: mjpegdec.c:948
uint8_t upscale_h[4]
Definition: mjpegdec.h:69
static void decode_flush(AVCodecContext *avctx)
Definition: mjpegdec.c:2857
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1222
int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
Decodes a TIFF header from the input bytestream and sets the endianness in *le and the offset to the ...
Definition: tiff_common.c:261
int height
Definition: frame.h:372
FILE * out
Definition: movenc.c:54
#define av_freep(p)
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:1188
#define av_always_inline
Definition: attributes.h:45
static const uint8_t start_code[]
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:188
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
Definition: pixfmt.h:608
Definition: mjpeg.h:82
#define VD
Definition: cuviddec.c:1096
#define FFSWAP(type, a, b)
Definition: common.h:99
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:2205
#define FF_QSCALE_TYPE_MPEG1
Definition: internal.h:92
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
MJPEG decoder.
#define MKTAG(a, b, c, d)
Definition: common.h:405
AVCodec ff_thp_decoder
Definition: mjpeg.h:61
enum AVCodecID id
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:91
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
static double val(void *priv, double ch)
Definition: aeval.c:76
This structure stores compressed data.
Definition: packet.h:340
Definition: rpzaenc.c:58
uint16_t quant_matrixes[4][64]
Definition: mjpegdec.h:56
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:365
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:514
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:411
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
for(j=16;j >0;--j)
int i
Definition: input.c:407
#define FFMAX3(a, b, c)
Definition: common.h:95
GLuint buffer
Definition: opengl_enc.c:101
int step
Number of elements between 2 horizontally consecutive pixels.
Definition: pixdesc.h:41
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:190
Definition: mjpeg.h:49
bitstream writer API