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 "decode.h"
40 #include "hwconfig.h"
41 #include "idctdsp.h"
42 #include "internal.h"
43 #include "jpegtables.h"
44 #include "mjpeg.h"
45 #include "mjpegdec.h"
46 #include "jpeglsdec.h"
47 #include "profiles.h"
48 #include "put_bits.h"
49 #include "tiff.h"
50 #include "exif.h"
51 #include "bytestream.h"
52 
53 
54 static void build_huffman_codes(uint8_t *huff_size, const uint8_t *bits_table)
55 {
56  for (int i = 1, 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  k++;
61  }
62  }
63 }
64 
65 static int build_vlc(VLC *vlc, const uint8_t *bits_table,
66  const uint8_t *val_table, int nb_codes,
67  int is_ac, void *logctx)
68 {
69  uint8_t huff_size[256];
70  uint16_t huff_sym[256];
71  int i;
72 
73  av_assert0(nb_codes <= 256);
74 
75  build_huffman_codes(huff_size, bits_table);
76 
77  for (i = 0; i < nb_codes; i++) {
78  huff_sym[i] = val_table[i] + 16 * is_ac;
79 
80  if (is_ac && !val_table[i])
81  huff_sym[i] = 16 * 256;
82  }
83 
84  return ff_init_vlc_from_lengths(vlc, 9, nb_codes, huff_size, 1,
85  huff_sym, 2, 2, 0, 0, logctx);
86 }
87 
89 {
90  static const struct {
91  int class;
92  int index;
93  const uint8_t *bits;
94  const uint8_t *values;
95  int length;
96  } ht[] = {
98  avpriv_mjpeg_val_dc, 12 },
100  avpriv_mjpeg_val_dc, 12 },
109  };
110  int i, ret;
111 
112  for (i = 0; i < FF_ARRAY_ELEMS(ht); i++) {
113  ret = build_vlc(&s->vlcs[ht[i].class][ht[i].index],
114  ht[i].bits, ht[i].values, ht[i].length,
115  ht[i].class == 1, s->avctx);
116  if (ret < 0)
117  return ret;
118 
119  if (ht[i].class < 2) {
120  memcpy(s->raw_huffman_lengths[ht[i].class][ht[i].index],
121  ht[i].bits + 1, 16);
122  memcpy(s->raw_huffman_values[ht[i].class][ht[i].index],
123  ht[i].values, ht[i].length);
124  }
125  }
126 
127  return 0;
128 }
129 
130 static void parse_avid(MJpegDecodeContext *s, uint8_t *buf, int len)
131 {
132  s->buggy_avid = 1;
133  if (len > 14 && buf[12] == 1) /* 1 - NTSC */
134  s->interlace_polarity = 1;
135  if (len > 14 && buf[12] == 2) /* 2 - PAL */
136  s->interlace_polarity = 0;
137  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
138  av_log(s->avctx, AV_LOG_INFO, "AVID: len:%d %d\n", len, len > 14 ? buf[12] : -1);
139 }
140 
141 static void init_idct(AVCodecContext *avctx)
142 {
143  MJpegDecodeContext *s = avctx->priv_data;
144 
145  ff_idctdsp_init(&s->idsp, avctx);
148 }
149 
151 {
152  MJpegDecodeContext *s = avctx->priv_data;
153  int ret;
154 
155  if (!s->picture_ptr) {
156  s->picture = av_frame_alloc();
157  if (!s->picture)
158  return AVERROR(ENOMEM);
159  s->picture_ptr = s->picture;
160  }
161 
162  s->pkt = av_packet_alloc();
163  if (!s->pkt)
164  return AVERROR(ENOMEM);
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->orig_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->codec_id == AV_CODEC_ID_SMVJPEG) {
202  if (avctx->extradata_size >= 4)
204 
205  if (s->smv_frames_per_jpeg <= 0) {
206  av_log(avctx, AV_LOG_ERROR, "Invalid number of frames per jpeg.\n");
207  return AVERROR_INVALIDDATA;
208  }
209 
210  s->smv_frame = av_frame_alloc();
211  if (!s->smv_frame)
212  return AVERROR(ENOMEM);
213  } else if (avctx->extradata_size > 8
214  && AV_RL32(avctx->extradata) == 0x2C
215  && AV_RL32(avctx->extradata+4) == 0x18) {
216  parse_avid(s, avctx->extradata, avctx->extradata_size);
217  }
218 
219  if (avctx->codec->id == AV_CODEC_ID_AMV)
220  s->flipped = 1;
221 
222  return 0;
223 }
224 
225 
226 /* quantize tables */
228 {
229  int len, index, i;
230 
231  len = get_bits(&s->gb, 16) - 2;
232 
233  if (8*len > get_bits_left(&s->gb)) {
234  av_log(s->avctx, AV_LOG_ERROR, "dqt: len %d is too large\n", len);
235  return AVERROR_INVALIDDATA;
236  }
237 
238  while (len >= 65) {
239  int pr = get_bits(&s->gb, 4);
240  if (pr > 1) {
241  av_log(s->avctx, AV_LOG_ERROR, "dqt: invalid precision\n");
242  return AVERROR_INVALIDDATA;
243  }
244  index = get_bits(&s->gb, 4);
245  if (index >= 4)
246  return -1;
247  av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
248  /* read quant table */
249  for (i = 0; i < 64; i++) {
250  s->quant_matrixes[index][i] = get_bits(&s->gb, pr ? 16 : 8);
251  if (s->quant_matrixes[index][i] == 0) {
252  av_log(s->avctx, AV_LOG_ERROR, "dqt: 0 quant value\n");
253  return AVERROR_INVALIDDATA;
254  }
255  }
256 
257  // XXX FIXME fine-tune, and perhaps add dc too
258  s->qscale[index] = FFMAX(s->quant_matrixes[index][1],
259  s->quant_matrixes[index][8]) >> 1;
260  av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n",
261  index, s->qscale[index]);
262  len -= 1 + 64 * (1+pr);
263  }
264  return 0;
265 }
266 
267 /* decode huffman tables and build VLC decoders */
269 {
270  int len, index, i, class, n, v;
271  uint8_t bits_table[17];
272  uint8_t val_table[256];
273  int ret = 0;
274 
275  len = get_bits(&s->gb, 16) - 2;
276 
277  if (8*len > get_bits_left(&s->gb)) {
278  av_log(s->avctx, AV_LOG_ERROR, "dht: len %d is too large\n", len);
279  return AVERROR_INVALIDDATA;
280  }
281 
282  while (len > 0) {
283  if (len < 17)
284  return AVERROR_INVALIDDATA;
285  class = get_bits(&s->gb, 4);
286  if (class >= 2)
287  return AVERROR_INVALIDDATA;
288  index = get_bits(&s->gb, 4);
289  if (index >= 4)
290  return AVERROR_INVALIDDATA;
291  n = 0;
292  for (i = 1; i <= 16; i++) {
293  bits_table[i] = get_bits(&s->gb, 8);
294  n += bits_table[i];
295  }
296  len -= 17;
297  if (len < n || n > 256)
298  return AVERROR_INVALIDDATA;
299 
300  for (i = 0; i < n; i++) {
301  v = get_bits(&s->gb, 8);
302  val_table[i] = v;
303  }
304  len -= n;
305 
306  /* build VLC and flush previous vlc if present */
307  ff_free_vlc(&s->vlcs[class][index]);
308  av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
309  class, index, n);
310  if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table,
311  n, class > 0, s->avctx)) < 0)
312  return ret;
313 
314  if (class > 0) {
315  ff_free_vlc(&s->vlcs[2][index]);
316  if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table,
317  n, 0, s->avctx)) < 0)
318  return ret;
319  }
320 
321  for (i = 0; i < 16; i++)
322  s->raw_huffman_lengths[class][index][i] = bits_table[i + 1];
323  for (i = 0; i < 256; i++)
324  s->raw_huffman_values[class][index][i] = val_table[i];
325  }
326  return 0;
327 }
328 
330 {
331  int len, nb_components, i, width, height, bits, ret, size_change;
332  unsigned pix_fmt_id;
333  int h_count[MAX_COMPONENTS] = { 0 };
334  int v_count[MAX_COMPONENTS] = { 0 };
335 
336  s->cur_scan = 0;
337  memset(s->upscale_h, 0, sizeof(s->upscale_h));
338  memset(s->upscale_v, 0, sizeof(s->upscale_v));
339 
340  len = get_bits(&s->gb, 16);
341  bits = get_bits(&s->gb, 8);
342 
343  if (bits > 16 || bits < 1) {
344  av_log(s->avctx, AV_LOG_ERROR, "bits %d is invalid\n", bits);
345  return AVERROR_INVALIDDATA;
346  }
347 
348  if (s->avctx->bits_per_raw_sample != bits) {
349  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);
351  init_idct(s->avctx);
352  }
353  if (s->pegasus_rct)
354  bits = 9;
355  if (bits == 9 && !s->pegasus_rct)
356  s->rct = 1; // FIXME ugly
357 
358  if(s->lossless && s->avctx->lowres){
359  av_log(s->avctx, AV_LOG_ERROR, "lowres is not possible with lossless jpeg\n");
360  return -1;
361  }
362 
363  height = get_bits(&s->gb, 16);
364  width = get_bits(&s->gb, 16);
365 
366  // HACK for odd_height.mov
367  if (s->interlaced && s->width == width && s->height == height + 1)
368  height= s->height;
369 
370  av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
371  if (av_image_check_size(width, height, 0, s->avctx) < 0)
372  return AVERROR_INVALIDDATA;
373  if (s->buf_size && (width + 7) / 8 * ((height + 7) / 8) > s->buf_size * 4LL)
374  return AVERROR_INVALIDDATA;
375 
376  nb_components = get_bits(&s->gb, 8);
377  if (nb_components <= 0 ||
378  nb_components > MAX_COMPONENTS)
379  return -1;
380  if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
381  if (nb_components != s->nb_components) {
383  "nb_components changing in interlaced picture\n");
384  return AVERROR_INVALIDDATA;
385  }
386  }
387  if (s->ls && !(bits <= 8 || nb_components == 1)) {
389  "JPEG-LS that is not <= 8 "
390  "bits/component or 16-bit gray");
391  return AVERROR_PATCHWELCOME;
392  }
393  if (len != 8 + 3 * nb_components) {
394  av_log(s->avctx, AV_LOG_ERROR, "decode_sof0: error, len(%d) mismatch %d components\n", len, nb_components);
395  return AVERROR_INVALIDDATA;
396  }
397 
398  s->nb_components = nb_components;
399  s->h_max = 1;
400  s->v_max = 1;
401  for (i = 0; i < nb_components; i++) {
402  /* component id */
403  s->component_id[i] = get_bits(&s->gb, 8) - 1;
404  h_count[i] = get_bits(&s->gb, 4);
405  v_count[i] = get_bits(&s->gb, 4);
406  /* compute hmax and vmax (only used in interleaved case) */
407  if (h_count[i] > s->h_max)
408  s->h_max = h_count[i];
409  if (v_count[i] > s->v_max)
410  s->v_max = v_count[i];
411  s->quant_index[i] = get_bits(&s->gb, 8);
412  if (s->quant_index[i] >= 4) {
413  av_log(s->avctx, AV_LOG_ERROR, "quant_index is invalid\n");
414  return AVERROR_INVALIDDATA;
415  }
416  if (!h_count[i] || !v_count[i]) {
418  "Invalid sampling factor in component %d %d:%d\n",
419  i, h_count[i], v_count[i]);
420  return AVERROR_INVALIDDATA;
421  }
422 
423  av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
424  i, h_count[i], v_count[i],
425  s->component_id[i], s->quant_index[i]);
426  }
427  if ( nb_components == 4
428  && s->component_id[0] == 'C' - 1
429  && s->component_id[1] == 'M' - 1
430  && s->component_id[2] == 'Y' - 1
431  && s->component_id[3] == 'K' - 1)
432  s->adobe_transform = 0;
433 
434  if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
435  avpriv_report_missing_feature(s->avctx, "Subsampling in JPEG-LS");
436  return AVERROR_PATCHWELCOME;
437  }
438 
439  if (s->bayer) {
440  if (nb_components == 2) {
441  /* Bayer images embedded in DNGs can contain 2 interleaved components and the
442  width stored in their SOF3 markers is the width of each one. We only output
443  a single component, therefore we need to adjust the output image width. We
444  handle the deinterleaving (but not the debayering) in this file. */
445  width *= 2;
446  }
447  /* They can also contain 1 component, which is double the width and half the height
448  of the final image (rows are interleaved). We don't handle the decoding in this
449  file, but leave that to the TIFF/DNG decoder. */
450  }
451 
452  /* if different size, realloc/alloc picture */
453  if (width != s->width || height != s->height || bits != s->bits ||
454  memcmp(s->h_count, h_count, sizeof(h_count)) ||
455  memcmp(s->v_count, v_count, sizeof(v_count))) {
456  size_change = 1;
457 
458  s->width = width;
459  s->height = height;
460  s->bits = bits;
461  memcpy(s->h_count, h_count, sizeof(h_count));
462  memcpy(s->v_count, v_count, sizeof(v_count));
463  s->interlaced = 0;
464  s->got_picture = 0;
465 
466  /* test interlaced mode */
467  if (s->first_picture &&
468  (s->multiscope != 2 || s->avctx->time_base.den >= 25 * s->avctx->time_base.num) &&
469  s->orig_height != 0 &&
470  s->height < ((s->orig_height * 3) / 4)) {
471  s->interlaced = 1;
475  height *= 2;
476  }
477 
478  ret = ff_set_dimensions(s->avctx, width, height);
479  if (ret < 0)
480  return ret;
481 
482  s->first_picture = 0;
483  } else {
484  size_change = 0;
485  }
486 
487  if (s->avctx->codec_id == AV_CODEC_ID_SMVJPEG) {
489  if (s->avctx->height <= 0)
490  return AVERROR_INVALIDDATA;
491  }
492 
493  if (s->got_picture && s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
494  if (s->progressive) {
495  avpriv_request_sample(s->avctx, "progressively coded interlaced picture");
496  return AVERROR_INVALIDDATA;
497  }
498  } else {
499  if (s->v_max == 1 && s->h_max == 1 && s->lossless==1 && (nb_components==3 || nb_components==4))
500  s->rgb = 1;
501  else if (!s->lossless)
502  s->rgb = 0;
503  /* XXX: not complete test ! */
504  pix_fmt_id = ((unsigned)s->h_count[0] << 28) | (s->v_count[0] << 24) |
505  (s->h_count[1] << 20) | (s->v_count[1] << 16) |
506  (s->h_count[2] << 12) | (s->v_count[2] << 8) |
507  (s->h_count[3] << 4) | s->v_count[3];
508  av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
509  /* NOTE we do not allocate pictures large enough for the possible
510  * padding of h/v_count being 4 */
511  if (!(pix_fmt_id & 0xD0D0D0D0))
512  pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
513  if (!(pix_fmt_id & 0x0D0D0D0D))
514  pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
515 
516  for (i = 0; i < 8; i++) {
517  int j = 6 + (i&1) - (i&6);
518  int is = (pix_fmt_id >> (4*i)) & 0xF;
519  int js = (pix_fmt_id >> (4*j)) & 0xF;
520 
521  if (is == 1 && js != 2 && (i < 2 || i > 5))
522  js = (pix_fmt_id >> ( 8 + 4*(i&1))) & 0xF;
523  if (is == 1 && js != 2 && (i < 2 || i > 5))
524  js = (pix_fmt_id >> (16 + 4*(i&1))) & 0xF;
525 
526  if (is == 1 && js == 2) {
527  if (i & 1) s->upscale_h[j/2] = 1;
528  else s->upscale_v[j/2] = 1;
529  }
530  }
531 
532  if (s->bayer) {
533  if (pix_fmt_id != 0x11110000 && pix_fmt_id != 0x11000000)
534  goto unk_pixfmt;
535  }
536 
537  switch (pix_fmt_id) {
538  case 0x11110000: /* for bayer-encoded huffman lossless JPEGs embedded in DNGs */
539  if (!s->bayer)
540  goto unk_pixfmt;
542  break;
543  case 0x11111100:
544  if (s->rgb)
546  else {
547  if ( s->adobe_transform == 0
548  || s->component_id[0] == 'R' - 1 && s->component_id[1] == 'G' - 1 && s->component_id[2] == 'B' - 1) {
550  } else {
554  }
555  }
556  av_assert0(s->nb_components == 3);
557  break;
558  case 0x11111111:
559  if (s->rgb)
561  else {
562  if (s->adobe_transform == 0 && s->bits <= 8) {
564  } else {
567  }
568  }
569  av_assert0(s->nb_components == 4);
570  break;
571  case 0x22111122:
572  case 0x22111111:
573  if (s->adobe_transform == 0 && s->bits <= 8) {
575  s->upscale_v[1] = s->upscale_v[2] = 1;
576  s->upscale_h[1] = s->upscale_h[2] = 1;
577  } else if (s->adobe_transform == 2 && s->bits <= 8) {
579  s->upscale_v[1] = s->upscale_v[2] = 1;
580  s->upscale_h[1] = s->upscale_h[2] = 1;
582  } else {
583  if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
586  }
587  av_assert0(s->nb_components == 4);
588  break;
589  case 0x12121100:
590  case 0x22122100:
591  case 0x21211100:
592  case 0x22211200:
593  case 0x22221100:
594  case 0x22112200:
595  case 0x11222200:
597  else
598  goto unk_pixfmt;
600  break;
601  case 0x11000000:
602  case 0x13000000:
603  case 0x14000000:
604  case 0x31000000:
605  case 0x33000000:
606  case 0x34000000:
607  case 0x41000000:
608  case 0x43000000:
609  case 0x44000000:
610  if(s->bits <= 8)
612  else
614  break;
615  case 0x12111100:
616  case 0x14121200:
617  case 0x14111100:
618  case 0x22211100:
619  case 0x22112100:
620  if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') {
621  if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
622  else
623  goto unk_pixfmt;
624  s->upscale_v[0] = s->upscale_v[1] = 1;
625  } else {
626  if (pix_fmt_id == 0x14111100)
627  s->upscale_v[1] = s->upscale_v[2] = 1;
629  else
630  goto unk_pixfmt;
632  }
633  break;
634  case 0x21111100:
635  if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') {
636  if (s->bits <= 8) s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
637  else
638  goto unk_pixfmt;
639  s->upscale_h[0] = s->upscale_h[1] = 1;
640  } else {
644  }
645  break;
646  case 0x31111100:
647  if (s->bits > 8)
648  goto unk_pixfmt;
651  s->upscale_h[1] = s->upscale_h[2] = 2;
652  break;
653  case 0x22121100:
654  case 0x22111200:
656  else
657  goto unk_pixfmt;
659  break;
660  case 0x22111100:
661  case 0x23111100:
662  case 0x42111100:
663  case 0x24111100:
667  if (pix_fmt_id == 0x42111100) {
668  if (s->bits > 8)
669  goto unk_pixfmt;
670  s->upscale_h[1] = s->upscale_h[2] = 1;
671  } else if (pix_fmt_id == 0x24111100) {
672  if (s->bits > 8)
673  goto unk_pixfmt;
674  s->upscale_v[1] = s->upscale_v[2] = 1;
675  } else if (pix_fmt_id == 0x23111100) {
676  if (s->bits > 8)
677  goto unk_pixfmt;
678  s->upscale_v[1] = s->upscale_v[2] = 2;
679  }
680  break;
681  case 0x41111100:
683  else
684  goto unk_pixfmt;
686  break;
687  default:
688  unk_pixfmt:
689  avpriv_report_missing_feature(s->avctx, "Pixel format 0x%x bits:%d", pix_fmt_id, s->bits);
690  memset(s->upscale_h, 0, sizeof(s->upscale_h));
691  memset(s->upscale_v, 0, sizeof(s->upscale_v));
692  return AVERROR_PATCHWELCOME;
693  }
694  if ((AV_RB32(s->upscale_h) || AV_RB32(s->upscale_v)) && s->avctx->lowres) {
695  avpriv_report_missing_feature(s->avctx, "Lowres for weird subsampling");
696  return AVERROR_PATCHWELCOME;
697  }
698  if ((AV_RB32(s->upscale_h) || AV_RB32(s->upscale_v)) && s->progressive && s->avctx->pix_fmt == AV_PIX_FMT_GBRP) {
699  avpriv_report_missing_feature(s->avctx, "progressive for weird subsampling");
700  return AVERROR_PATCHWELCOME;
701  }
702  if (s->ls) {
703  memset(s->upscale_h, 0, sizeof(s->upscale_h));
704  memset(s->upscale_v, 0, sizeof(s->upscale_v));
705  if (s->nb_components == 3) {
707  } else if (s->nb_components != 1) {
708  av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of components %d\n", s->nb_components);
709  return AVERROR_PATCHWELCOME;
710  } else if (s->palette_index && s->bits <= 8)
712  else if (s->bits <= 8)
714  else
716  }
717 
719  if (!s->pix_desc) {
720  av_log(s->avctx, AV_LOG_ERROR, "Could not get a pixel format descriptor.\n");
721  return AVERROR_BUG;
722  }
723 
724  if (s->avctx->pix_fmt == s->hwaccel_sw_pix_fmt && !size_change) {
725  s->avctx->pix_fmt = s->hwaccel_pix_fmt;
726  } else {
727  enum AVPixelFormat pix_fmts[] = {
728 #if CONFIG_MJPEG_NVDEC_HWACCEL
730 #endif
731 #if CONFIG_MJPEG_VAAPI_HWACCEL
733 #endif
734  s->avctx->pix_fmt,
736  };
737  s->hwaccel_pix_fmt = ff_get_format(s->avctx, pix_fmts);
738  if (s->hwaccel_pix_fmt < 0)
739  return AVERROR(EINVAL);
740 
742  s->avctx->pix_fmt = s->hwaccel_pix_fmt;
743  }
744 
745  if (s->avctx->skip_frame == AVDISCARD_ALL) {
747  s->picture_ptr->key_frame = 1;
748  s->got_picture = 1;
749  return 0;
750  }
751 
754  return -1;
756  s->picture_ptr->key_frame = 1;
757  s->got_picture = 1;
758 
759  for (i = 0; i < 4; i++)
760  s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
761 
762  ff_dlog(s->avctx, "%d %d %d %d %d %d\n",
763  s->width, s->height, s->linesize[0], s->linesize[1],
764  s->interlaced, s->avctx->height);
765 
766  }
767 
768  if ((s->rgb && !s->lossless && !s->ls) ||
769  (!s->rgb && s->ls && s->nb_components > 1) ||
770  (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 && !s->ls)
771  ) {
772  av_log(s->avctx, AV_LOG_ERROR, "Unsupported coding and pixel format combination\n");
773  return AVERROR_PATCHWELCOME;
774  }
775 
776  /* totally blank picture as progressive JPEG will only add details to it */
777  if (s->progressive) {
778  int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8);
779  int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
780  for (i = 0; i < s->nb_components; i++) {
781  int size = bw * bh * s->h_count[i] * s->v_count[i];
782  av_freep(&s->blocks[i]);
783  av_freep(&s->last_nnz[i]);
784  s->blocks[i] = av_mallocz_array(size, sizeof(**s->blocks));
785  s->last_nnz[i] = av_mallocz_array(size, sizeof(**s->last_nnz));
786  if (!s->blocks[i] || !s->last_nnz[i])
787  return AVERROR(ENOMEM);
788  s->block_stride[i] = bw * s->h_count[i];
789  }
790  memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
791  }
792 
793  if (s->avctx->hwaccel) {
796  if (!s->hwaccel_picture_private)
797  return AVERROR(ENOMEM);
798 
799  ret = s->avctx->hwaccel->start_frame(s->avctx, s->raw_image_buffer,
801  if (ret < 0)
802  return ret;
803  }
804 
805  return 0;
806 }
807 
808 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
809 {
810  int code;
811  code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
812  if (code < 0 || code > 16) {
814  "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
815  0, dc_index, &s->vlcs[0][dc_index]);
816  return 0xfffff;
817  }
818 
819  if (code)
820  return get_xbits(&s->gb, code);
821  else
822  return 0;
823 }
824 
825 /* decode block and dequantize */
826 static int decode_block(MJpegDecodeContext *s, int16_t *block, int component,
827  int dc_index, int ac_index, uint16_t *quant_matrix)
828 {
829  int code, i, j, level, val;
830 
831  /* DC coef */
832  val = mjpeg_decode_dc(s, dc_index);
833  if (val == 0xfffff) {
834  av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
835  return AVERROR_INVALIDDATA;
836  }
837  val = val * (unsigned)quant_matrix[0] + s->last_dc[component];
838  val = av_clip_int16(val);
839  s->last_dc[component] = val;
840  block[0] = val;
841  /* AC coefs */
842  i = 0;
843  {OPEN_READER(re, &s->gb);
844  do {
845  UPDATE_CACHE(re, &s->gb);
846  GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
847 
848  i += ((unsigned)code) >> 4;
849  code &= 0xf;
850  if (code) {
851  if (code > MIN_CACHE_BITS - 16)
852  UPDATE_CACHE(re, &s->gb);
853 
854  {
855  int cache = GET_CACHE(re, &s->gb);
856  int sign = (~cache) >> 31;
857  level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
858  }
859 
860  LAST_SKIP_BITS(re, &s->gb, code);
861 
862  if (i > 63) {
863  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
864  return AVERROR_INVALIDDATA;
865  }
866  j = s->scantable.permutated[i];
867  block[j] = level * quant_matrix[i];
868  }
869  } while (i < 63);
870  CLOSE_READER(re, &s->gb);}
871 
872  return 0;
873 }
874 
876  int component, int dc_index,
877  uint16_t *quant_matrix, int Al)
878 {
879  unsigned val;
880  s->bdsp.clear_block(block);
881  val = mjpeg_decode_dc(s, dc_index);
882  if (val == 0xfffff) {
883  av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
884  return AVERROR_INVALIDDATA;
885  }
886  val = (val * (quant_matrix[0] << Al)) + s->last_dc[component];
887  s->last_dc[component] = val;
888  block[0] = val;
889  return 0;
890 }
891 
892 /* decode block and dequantize - progressive JPEG version */
894  uint8_t *last_nnz, int ac_index,
895  uint16_t *quant_matrix,
896  int ss, int se, int Al, int *EOBRUN)
897 {
898  int code, i, j, val, run;
899  unsigned level;
900 
901  if (*EOBRUN) {
902  (*EOBRUN)--;
903  return 0;
904  }
905 
906  {
907  OPEN_READER(re, &s->gb);
908  for (i = ss; ; i++) {
909  UPDATE_CACHE(re, &s->gb);
910  GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
911 
912  run = ((unsigned) code) >> 4;
913  code &= 0xF;
914  if (code) {
915  i += run;
916  if (code > MIN_CACHE_BITS - 16)
917  UPDATE_CACHE(re, &s->gb);
918 
919  {
920  int cache = GET_CACHE(re, &s->gb);
921  int sign = (~cache) >> 31;
922  level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
923  }
924 
925  LAST_SKIP_BITS(re, &s->gb, code);
926 
927  if (i >= se) {
928  if (i == se) {
929  j = s->scantable.permutated[se];
930  block[j] = level * (quant_matrix[se] << Al);
931  break;
932  }
933  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
934  return AVERROR_INVALIDDATA;
935  }
936  j = s->scantable.permutated[i];
937  block[j] = level * (quant_matrix[i] << Al);
938  } else {
939  if (run == 0xF) {// ZRL - skip 15 coefficients
940  i += 15;
941  if (i >= se) {
942  av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
943  return AVERROR_INVALIDDATA;
944  }
945  } else {
946  val = (1 << run);
947  if (run) {
948  UPDATE_CACHE(re, &s->gb);
949  val += NEG_USR32(GET_CACHE(re, &s->gb), run);
950  LAST_SKIP_BITS(re, &s->gb, run);
951  }
952  *EOBRUN = val - 1;
953  break;
954  }
955  }
956  }
957  CLOSE_READER(re, &s->gb);
958  }
959 
960  if (i > *last_nnz)
961  *last_nnz = i;
962 
963  return 0;
964 }
965 
966 #define REFINE_BIT(j) { \
967  UPDATE_CACHE(re, &s->gb); \
968  sign = block[j] >> 15; \
969  block[j] += SHOW_UBITS(re, &s->gb, 1) * \
970  ((quant_matrix[i] ^ sign) - sign) << Al; \
971  LAST_SKIP_BITS(re, &s->gb, 1); \
972 }
973 
974 #define ZERO_RUN \
975 for (; ; i++) { \
976  if (i > last) { \
977  i += run; \
978  if (i > se) { \
979  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
980  return -1; \
981  } \
982  break; \
983  } \
984  j = s->scantable.permutated[i]; \
985  if (block[j]) \
986  REFINE_BIT(j) \
987  else if (run-- == 0) \
988  break; \
989 }
990 
991 /* decode block and dequantize - progressive JPEG refinement pass */
993  uint8_t *last_nnz,
994  int ac_index, uint16_t *quant_matrix,
995  int ss, int se, int Al, int *EOBRUN)
996 {
997  int code, i = ss, j, sign, val, run;
998  int last = FFMIN(se, *last_nnz);
999 
1000  OPEN_READER(re, &s->gb);
1001  if (*EOBRUN) {
1002  (*EOBRUN)--;
1003  } else {
1004  for (; ; i++) {
1005  UPDATE_CACHE(re, &s->gb);
1006  GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
1007 
1008  if (code & 0xF) {
1009  run = ((unsigned) code) >> 4;
1010  UPDATE_CACHE(re, &s->gb);
1011  val = SHOW_UBITS(re, &s->gb, 1);
1012  LAST_SKIP_BITS(re, &s->gb, 1);
1013  ZERO_RUN;
1014  j = s->scantable.permutated[i];
1015  val--;
1016  block[j] = ((quant_matrix[i] << Al) ^ val) - val;
1017  if (i == se) {
1018  if (i > *last_nnz)
1019  *last_nnz = i;
1020  CLOSE_READER(re, &s->gb);
1021  return 0;
1022  }
1023  } else {
1024  run = ((unsigned) code) >> 4;
1025  if (run == 0xF) {
1026  ZERO_RUN;
1027  } else {
1028  val = run;
1029  run = (1 << run);
1030  if (val) {
1031  UPDATE_CACHE(re, &s->gb);
1032  run += SHOW_UBITS(re, &s->gb, val);
1033  LAST_SKIP_BITS(re, &s->gb, val);
1034  }
1035  *EOBRUN = run - 1;
1036  break;
1037  }
1038  }
1039  }
1040 
1041  if (i > *last_nnz)
1042  *last_nnz = i;
1043  }
1044 
1045  for (; i <= last; i++) {
1046  j = s->scantable.permutated[i];
1047  if (block[j])
1048  REFINE_BIT(j)
1049  }
1050  CLOSE_READER(re, &s->gb);
1051 
1052  return 0;
1053 }
1054 #undef REFINE_BIT
1055 #undef ZERO_RUN
1056 
1057 static int handle_rstn(MJpegDecodeContext *s, int nb_components)
1058 {
1059  int i;
1060  int reset = 0;
1061 
1062  if (s->restart_interval) {
1063  s->restart_count--;
1064  if(s->restart_count == 0 && s->avctx->codec_id == AV_CODEC_ID_THP){
1065  align_get_bits(&s->gb);
1066  for (i = 0; i < nb_components; i++) /* reset dc */
1067  s->last_dc[i] = (4 << s->bits);
1068  }
1069 
1070  i = 8 + ((-get_bits_count(&s->gb)) & 7);
1071  /* skip RSTn */
1072  if (s->restart_count == 0) {
1073  if( show_bits(&s->gb, i) == (1 << i) - 1
1074  || show_bits(&s->gb, i) == 0xFF) {
1075  int pos = get_bits_count(&s->gb);
1076  align_get_bits(&s->gb);
1077  while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
1078  skip_bits(&s->gb, 8);
1079  if (get_bits_left(&s->gb) >= 8 && (get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
1080  for (i = 0; i < nb_components; i++) /* reset dc */
1081  s->last_dc[i] = (4 << s->bits);
1082  reset = 1;
1083  } else
1084  skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
1085  }
1086  }
1087  }
1088  return reset;
1089 }
1090 
1091 /* Handles 1 to 4 components */
1092 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform)
1093 {
1094  int i, mb_x, mb_y;
1095  unsigned width;
1096  uint16_t (*buffer)[4];
1097  int left[4], top[4], topleft[4];
1098  const int linesize = s->linesize[0];
1099  const int mask = ((1 << s->bits) - 1) << point_transform;
1100  int resync_mb_y = 0;
1101  int resync_mb_x = 0;
1102  int vpred[6];
1103 
1104  if (!s->bayer && s->nb_components < 3)
1105  return AVERROR_INVALIDDATA;
1106  if (s->bayer && s->nb_components > 2)
1107  return AVERROR_INVALIDDATA;
1108  if (s->nb_components <= 0 || s->nb_components > 4)
1109  return AVERROR_INVALIDDATA;
1110  if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
1111  return AVERROR_INVALIDDATA;
1112 
1113 
1115 
1116  if (s->restart_interval == 0)
1117  s->restart_interval = INT_MAX;
1118 
1119  if (s->bayer)
1120  width = s->mb_width / nb_components; /* Interleaved, width stored is the total so need to divide */
1121  else
1122  width = s->mb_width;
1123 
1124  av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, width * 4 * sizeof(s->ljpeg_buffer[0][0]));
1125  if (!s->ljpeg_buffer)
1126  return AVERROR(ENOMEM);
1127 
1128  buffer = s->ljpeg_buffer;
1129 
1130  for (i = 0; i < 4; i++)
1131  buffer[0][i] = 1 << (s->bits - 1);
1132 
1133  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1134  uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
1135 
1136  if (s->interlaced && s->bottom_field)
1137  ptr += linesize >> 1;
1138 
1139  for (i = 0; i < 4; i++)
1140  top[i] = left[i] = topleft[i] = buffer[0][i];
1141 
1142  if ((mb_y * s->width) % s->restart_interval == 0) {
1143  for (i = 0; i < 6; i++)
1144  vpred[i] = 1 << (s->bits-1);
1145  }
1146 
1147  for (mb_x = 0; mb_x < width; mb_x++) {
1148  int modified_predictor = predictor;
1149 
1150  if (get_bits_left(&s->gb) < 1) {
1151  av_log(s->avctx, AV_LOG_ERROR, "bitstream end in rgb_scan\n");
1152  return AVERROR_INVALIDDATA;
1153  }
1154 
1155  if (s->restart_interval && !s->restart_count){
1157  resync_mb_x = mb_x;
1158  resync_mb_y = mb_y;
1159  for(i=0; i<4; i++)
1160  top[i] = left[i]= topleft[i]= 1 << (s->bits - 1);
1161  }
1162  if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
1163  modified_predictor = 1;
1164 
1165  for (i=0;i<nb_components;i++) {
1166  int pred, dc;
1167 
1168  topleft[i] = top[i];
1169  top[i] = buffer[mb_x][i];
1170 
1171  dc = mjpeg_decode_dc(s, s->dc_index[i]);
1172  if(dc == 0xFFFFF)
1173  return -1;
1174 
1175  if (!s->bayer || mb_x) {
1176  pred = left[i];
1177  } else { /* This path runs only for the first line in bayer images */
1178  vpred[i] += dc;
1179  pred = vpred[i] - dc;
1180  }
1181 
1182  PREDICT(pred, topleft[i], top[i], pred, modified_predictor);
1183 
1184  left[i] = buffer[mb_x][i] =
1185  mask & (pred + (unsigned)(dc * (1 << point_transform)));
1186  }
1187 
1188  if (s->restart_interval && !--s->restart_count) {
1189  align_get_bits(&s->gb);
1190  skip_bits(&s->gb, 16); /* skip RSTn */
1191  }
1192  }
1193  if (s->rct && s->nb_components == 4) {
1194  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1195  ptr[4*mb_x + 2] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
1196  ptr[4*mb_x + 1] = buffer[mb_x][1] + ptr[4*mb_x + 2];
1197  ptr[4*mb_x + 3] = buffer[mb_x][2] + ptr[4*mb_x + 2];
1198  ptr[4*mb_x + 0] = buffer[mb_x][3];
1199  }
1200  } else if (s->nb_components == 4) {
1201  for(i=0; i<nb_components; i++) {
1202  int c= s->comp_index[i];
1203  if (s->bits <= 8) {
1204  for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1205  ptr[4*mb_x+3-c] = buffer[mb_x][i];
1206  }
1207  } else if(s->bits == 9) {
1208  return AVERROR_PATCHWELCOME;
1209  } else {
1210  for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1211  ((uint16_t*)ptr)[4*mb_x+c] = buffer[mb_x][i];
1212  }
1213  }
1214  }
1215  } else if (s->rct) {
1216  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1217  ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
1218  ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
1219  ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
1220  }
1221  } else if (s->pegasus_rct) {
1222  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1223  ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
1224  ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
1225  ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
1226  }
1227  } else if (s->bayer) {
1228  if (nb_components == 1) {
1229  /* Leave decoding to the TIFF/DNG decoder (see comment in ff_mjpeg_decode_sof) */
1230  for (mb_x = 0; mb_x < width; mb_x++)
1231  ((uint16_t*)ptr)[mb_x] = buffer[mb_x][0];
1232  } else if (nb_components == 2) {
1233  for (mb_x = 0; mb_x < width; mb_x++) {
1234  ((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
1235  ((uint16_t*)ptr)[2*mb_x + 1] = buffer[mb_x][1];
1236  }
1237  }
1238  } else {
1239  for(i=0; i<nb_components; i++) {
1240  int c= s->comp_index[i];
1241  if (s->bits <= 8) {
1242  for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1243  ptr[3*mb_x+2-c] = buffer[mb_x][i];
1244  }
1245  } else if(s->bits == 9) {
1246  return AVERROR_PATCHWELCOME;
1247  } else {
1248  for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
1249  ((uint16_t*)ptr)[3*mb_x+2-c] = buffer[mb_x][i];
1250  }
1251  }
1252  }
1253  }
1254  }
1255  return 0;
1256 }
1257 
1259  int point_transform, int nb_components)
1260 {
1261  int i, mb_x, mb_y, mask;
1262  int bits= (s->bits+7)&~7;
1263  int resync_mb_y = 0;
1264  int resync_mb_x = 0;
1265 
1266  point_transform += bits - s->bits;
1267  mask = ((1 << s->bits) - 1) << point_transform;
1268 
1269  av_assert0(nb_components>=1 && nb_components<=4);
1270 
1271  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1272  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1273  if (get_bits_left(&s->gb) < 1) {
1274  av_log(s->avctx, AV_LOG_ERROR, "bitstream end in yuv_scan\n");
1275  return AVERROR_INVALIDDATA;
1276  }
1277  if (s->restart_interval && !s->restart_count){
1279  resync_mb_x = mb_x;
1280  resync_mb_y = mb_y;
1281  }
1282 
1283  if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){
1284  int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
1285  int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
1286  for (i = 0; i < nb_components; i++) {
1287  uint8_t *ptr;
1288  uint16_t *ptr16;
1289  int n, h, v, x, y, c, j, linesize;
1290  n = s->nb_blocks[i];
1291  c = s->comp_index[i];
1292  h = s->h_scount[i];
1293  v = s->v_scount[i];
1294  x = 0;
1295  y = 0;
1296  linesize= s->linesize[c];
1297 
1298  if(bits>8) linesize /= 2;
1299 
1300  for(j=0; j<n; j++) {
1301  int pred, dc;
1302 
1303  dc = mjpeg_decode_dc(s, s->dc_index[i]);
1304  if(dc == 0xFFFFF)
1305  return -1;
1306  if ( h * mb_x + x >= s->width
1307  || v * mb_y + y >= s->height) {
1308  // Nothing to do
1309  } else if (bits<=8) {
1310  ptr = s->picture_ptr->data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
1311  if(y==0 && toprow){
1312  if(x==0 && leftcol){
1313  pred= 1 << (bits - 1);
1314  }else{
1315  pred= ptr[-1];
1316  }
1317  }else{
1318  if(x==0 && leftcol){
1319  pred= ptr[-linesize];
1320  }else{
1321  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1322  }
1323  }
1324 
1325  if (s->interlaced && s->bottom_field)
1326  ptr += linesize >> 1;
1327  pred &= mask;
1328  *ptr= pred + ((unsigned)dc << point_transform);
1329  }else{
1330  ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
1331  if(y==0 && toprow){
1332  if(x==0 && leftcol){
1333  pred= 1 << (bits - 1);
1334  }else{
1335  pred= ptr16[-1];
1336  }
1337  }else{
1338  if(x==0 && leftcol){
1339  pred= ptr16[-linesize];
1340  }else{
1341  PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1342  }
1343  }
1344 
1345  if (s->interlaced && s->bottom_field)
1346  ptr16 += linesize >> 1;
1347  pred &= mask;
1348  *ptr16= pred + ((unsigned)dc << point_transform);
1349  }
1350  if (++x == h) {
1351  x = 0;
1352  y++;
1353  }
1354  }
1355  }
1356  } else {
1357  for (i = 0; i < nb_components; i++) {
1358  uint8_t *ptr;
1359  uint16_t *ptr16;
1360  int n, h, v, x, y, c, j, linesize, dc;
1361  n = s->nb_blocks[i];
1362  c = s->comp_index[i];
1363  h = s->h_scount[i];
1364  v = s->v_scount[i];
1365  x = 0;
1366  y = 0;
1367  linesize = s->linesize[c];
1368 
1369  if(bits>8) linesize /= 2;
1370 
1371  for (j = 0; j < n; j++) {
1372  int pred;
1373 
1374  dc = mjpeg_decode_dc(s, s->dc_index[i]);
1375  if(dc == 0xFFFFF)
1376  return -1;
1377  if ( h * mb_x + x >= s->width
1378  || v * mb_y + y >= s->height) {
1379  // Nothing to do
1380  } else if (bits<=8) {
1381  ptr = s->picture_ptr->data[c] +
1382  (linesize * (v * mb_y + y)) +
1383  (h * mb_x + x); //FIXME optimize this crap
1384  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
1385 
1386  pred &= mask;
1387  *ptr = pred + ((unsigned)dc << point_transform);
1388  }else{
1389  ptr16 = (uint16_t*)(s->picture_ptr->data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
1390  PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
1391 
1392  pred &= mask;
1393  *ptr16= pred + ((unsigned)dc << point_transform);
1394  }
1395 
1396  if (++x == h) {
1397  x = 0;
1398  y++;
1399  }
1400  }
1401  }
1402  }
1403  if (s->restart_interval && !--s->restart_count) {
1404  align_get_bits(&s->gb);
1405  skip_bits(&s->gb, 16); /* skip RSTn */
1406  }
1407  }
1408  }
1409  return 0;
1410 }
1411 
1413  uint8_t *dst, const uint8_t *src,
1414  int linesize, int lowres)
1415 {
1416  switch (lowres) {
1417  case 0: s->hdsp.put_pixels_tab[1][0](dst, src, linesize, 8);
1418  break;
1419  case 1: copy_block4(dst, src, linesize, linesize, 4);
1420  break;
1421  case 2: copy_block2(dst, src, linesize, linesize, 2);
1422  break;
1423  case 3: *dst = *src;
1424  break;
1425  }
1426 }
1427 
1428 static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
1429 {
1430  int block_x, block_y;
1431  int size = 8 >> s->avctx->lowres;
1432  if (s->bits > 8) {
1433  for (block_y=0; block_y<size; block_y++)
1434  for (block_x=0; block_x<size; block_x++)
1435  *(uint16_t*)(ptr + 2*block_x + block_y*linesize) <<= 16 - s->bits;
1436  } else {
1437  for (block_y=0; block_y<size; block_y++)
1438  for (block_x=0; block_x<size; block_x++)
1439  *(ptr + block_x + block_y*linesize) <<= 8 - s->bits;
1440  }
1441 }
1442 
1443 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
1444  int Al, const uint8_t *mb_bitmask,
1445  int mb_bitmask_size,
1446  const AVFrame *reference)
1447 {
1448  int i, mb_x, mb_y, chroma_h_shift, chroma_v_shift, chroma_width, chroma_height;
1450  const uint8_t *reference_data[MAX_COMPONENTS];
1451  int linesize[MAX_COMPONENTS];
1452  GetBitContext mb_bitmask_gb = {0}; // initialize to silence gcc warning
1453  int bytes_per_pixel = 1 + (s->bits > 8);
1454 
1455  if (mb_bitmask) {
1456  if (mb_bitmask_size != (s->mb_width * s->mb_height + 7)>>3) {
1457  av_log(s->avctx, AV_LOG_ERROR, "mb_bitmask_size mismatches\n");
1458  return AVERROR_INVALIDDATA;
1459  }
1460  init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
1461  }
1462 
1463  s->restart_count = 0;
1464 
1465  av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &chroma_h_shift,
1466  &chroma_v_shift);
1467  chroma_width = AV_CEIL_RSHIFT(s->width, chroma_h_shift);
1468  chroma_height = AV_CEIL_RSHIFT(s->height, chroma_v_shift);
1469 
1470  for (i = 0; i < nb_components; i++) {
1471  int c = s->comp_index[i];
1472  data[c] = s->picture_ptr->data[c];
1473  reference_data[c] = reference ? reference->data[c] : NULL;
1474  linesize[c] = s->linesize[c];
1475  s->coefs_finished[c] |= 1;
1476  }
1477 
1478  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1479  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1480  const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
1481 
1482  if (s->restart_interval && !s->restart_count)
1484 
1485  if (get_bits_left(&s->gb) < 0) {
1486  av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
1487  -get_bits_left(&s->gb));
1488  return AVERROR_INVALIDDATA;
1489  }
1490  for (i = 0; i < nb_components; i++) {
1491  uint8_t *ptr;
1492  int n, h, v, x, y, c, j;
1493  int block_offset;
1494  n = s->nb_blocks[i];
1495  c = s->comp_index[i];
1496  h = s->h_scount[i];
1497  v = s->v_scount[i];
1498  x = 0;
1499  y = 0;
1500  for (j = 0; j < n; j++) {
1501  block_offset = (((linesize[c] * (v * mb_y + y) * 8) +
1502  (h * mb_x + x) * 8 * bytes_per_pixel) >> s->avctx->lowres);
1503 
1504  if (s->interlaced && s->bottom_field)
1505  block_offset += linesize[c] >> 1;
1506  if ( 8*(h * mb_x + x) < ((c == 1) || (c == 2) ? chroma_width : s->width)
1507  && 8*(v * mb_y + y) < ((c == 1) || (c == 2) ? chroma_height : s->height)) {
1508  ptr = data[c] + block_offset;
1509  } else
1510  ptr = NULL;
1511  if (!s->progressive) {
1512  if (copy_mb) {
1513  if (ptr)
1514  mjpeg_copy_block(s, ptr, reference_data[c] + block_offset,
1515  linesize[c], s->avctx->lowres);
1516 
1517  } else {
1518  s->bdsp.clear_block(s->block);
1519  if (decode_block(s, s->block, i,
1520  s->dc_index[i], s->ac_index[i],
1521  s->quant_matrixes[s->quant_sindex[i]]) < 0) {
1523  "error y=%d x=%d\n", mb_y, mb_x);
1524  return AVERROR_INVALIDDATA;
1525  }
1526  if (ptr) {
1527  s->idsp.idct_put(ptr, linesize[c], s->block);
1528  if (s->bits & 7)
1529  shift_output(s, ptr, linesize[c]);
1530  }
1531  }
1532  } else {
1533  int block_idx = s->block_stride[c] * (v * mb_y + y) +
1534  (h * mb_x + x);
1535  int16_t *block = s->blocks[c][block_idx];
1536  if (Ah)
1537  block[0] += get_bits1(&s->gb) *
1538  s->quant_matrixes[s->quant_sindex[i]][0] << Al;
1539  else if (decode_dc_progressive(s, block, i, s->dc_index[i],
1540  s->quant_matrixes[s->quant_sindex[i]],
1541  Al) < 0) {
1543  "error y=%d x=%d\n", mb_y, mb_x);
1544  return AVERROR_INVALIDDATA;
1545  }
1546  }
1547  ff_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
1548  ff_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
1549  mb_x, mb_y, x, y, c, s->bottom_field,
1550  (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1551  if (++x == h) {
1552  x = 0;
1553  y++;
1554  }
1555  }
1556  }
1557 
1558  handle_rstn(s, nb_components);
1559  }
1560  }
1561  return 0;
1562 }
1563 
1565  int se, int Ah, int Al)
1566 {
1567  int mb_x, mb_y;
1568  int EOBRUN = 0;
1569  int c = s->comp_index[0];
1570  uint16_t *quant_matrix = s->quant_matrixes[s->quant_sindex[0]];
1571 
1572  av_assert0(ss>=0 && Ah>=0 && Al>=0);
1573  if (se < ss || se > 63) {
1574  av_log(s->avctx, AV_LOG_ERROR, "SS/SE %d/%d is invalid\n", ss, se);
1575  return AVERROR_INVALIDDATA;
1576  }
1577 
1578  // s->coefs_finished is a bitmask for coefficients coded
1579  // ss and se are parameters telling start and end coefficients
1580  s->coefs_finished[c] |= (2ULL << se) - (1ULL << ss);
1581 
1582  s->restart_count = 0;
1583 
1584  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1585  int block_idx = mb_y * s->block_stride[c];
1586  int16_t (*block)[64] = &s->blocks[c][block_idx];
1587  uint8_t *last_nnz = &s->last_nnz[c][block_idx];
1588  if (get_bits_left(&s->gb) <= 0) {
1589  av_log(s->avctx, AV_LOG_ERROR, "bitstream truncated in mjpeg_decode_scan_progressive_ac\n");
1590  return AVERROR_INVALIDDATA;
1591  }
1592  for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
1593  int ret;
1594  if (s->restart_interval && !s->restart_count)
1596 
1597  if (Ah)
1598  ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
1599  quant_matrix, ss, se, Al, &EOBRUN);
1600  else
1601  ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
1602  quant_matrix, ss, se, Al, &EOBRUN);
1603  if (ret < 0) {
1605  "error y=%d x=%d\n", mb_y, mb_x);
1606  return AVERROR_INVALIDDATA;
1607  }
1608 
1609  if (handle_rstn(s, 0))
1610  EOBRUN = 0;
1611  }
1612  }
1613  return 0;
1614 }
1615 
1617 {
1618  int mb_x, mb_y;
1619  int c;
1620  const int bytes_per_pixel = 1 + (s->bits > 8);
1621  const int block_size = s->lossless ? 1 : 8;
1622 
1623  for (c = 0; c < s->nb_components; c++) {
1624  uint8_t *data = s->picture_ptr->data[c];
1625  int linesize = s->linesize[c];
1626  int h = s->h_max / s->h_count[c];
1627  int v = s->v_max / s->v_count[c];
1628  int mb_width = (s->width + h * block_size - 1) / (h * block_size);
1629  int mb_height = (s->height + v * block_size - 1) / (v * block_size);
1630 
1631  if (~s->coefs_finished[c])
1632  av_log(s->avctx, AV_LOG_WARNING, "component %d is incomplete\n", c);
1633 
1634  if (s->interlaced && s->bottom_field)
1635  data += linesize >> 1;
1636 
1637  for (mb_y = 0; mb_y < mb_height; mb_y++) {
1638  uint8_t *ptr = data + (mb_y * linesize * 8 >> s->avctx->lowres);
1639  int block_idx = mb_y * s->block_stride[c];
1640  int16_t (*block)[64] = &s->blocks[c][block_idx];
1641  for (mb_x = 0; mb_x < mb_width; mb_x++, block++) {
1642  s->idsp.idct_put(ptr, linesize, *block);
1643  if (s->bits & 7)
1644  shift_output(s, ptr, linesize);
1645  ptr += bytes_per_pixel*8 >> s->avctx->lowres;
1646  }
1647  }
1648  }
1649 }
1650 
1652  int mb_bitmask_size, const AVFrame *reference)
1653 {
1654  int len, nb_components, i, h, v, predictor, point_transform;
1655  int index, id, ret;
1656  const int block_size = s->lossless ? 1 : 8;
1657  int ilv, prev_shift;
1658 
1659  if (!s->got_picture) {
1661  "Can not process SOS before SOF, skipping\n");
1662  return -1;
1663  }
1664 
1665  if (reference) {
1666  if (reference->width != s->picture_ptr->width ||
1667  reference->height != s->picture_ptr->height ||
1668  reference->format != s->picture_ptr->format) {
1669  av_log(s->avctx, AV_LOG_ERROR, "Reference mismatching\n");
1670  return AVERROR_INVALIDDATA;
1671  }
1672  }
1673 
1674  /* XXX: verify len field validity */
1675  len = get_bits(&s->gb, 16);
1676  nb_components = get_bits(&s->gb, 8);
1677  if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
1679  "decode_sos: nb_components (%d)",
1680  nb_components);
1681  return AVERROR_PATCHWELCOME;
1682  }
1683  if (len != 6 + 2 * nb_components) {
1684  av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
1685  return AVERROR_INVALIDDATA;
1686  }
1687  for (i = 0; i < nb_components; i++) {
1688  id = get_bits(&s->gb, 8) - 1;
1689  av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1690  /* find component index */
1691  for (index = 0; index < s->nb_components; index++)
1692  if (id == s->component_id[index])
1693  break;
1694  if (index == s->nb_components) {
1696  "decode_sos: index(%d) out of components\n", index);
1697  return AVERROR_INVALIDDATA;
1698  }
1699  /* Metasoft MJPEG codec has Cb and Cr swapped */
1700  if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1701  && nb_components == 3 && s->nb_components == 3 && i)
1702  index = 3 - i;
1703 
1704  s->quant_sindex[i] = s->quant_index[index];
1705  s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1706  s->h_scount[i] = s->h_count[index];
1707  s->v_scount[i] = s->v_count[index];
1708 
1709  if((nb_components == 1 || nb_components == 3) && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P)
1710  index = (index+2)%3;
1711 
1712  s->comp_index[i] = index;
1713 
1714  s->dc_index[i] = get_bits(&s->gb, 4);
1715  s->ac_index[i] = get_bits(&s->gb, 4);
1716 
1717  if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1718  s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1719  goto out_of_range;
1720  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))
1721  goto out_of_range;
1722  }
1723 
1724  predictor = get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1725  ilv = get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1726  if(s->avctx->codec_tag != AV_RL32("CJPG")){
1727  prev_shift = get_bits(&s->gb, 4); /* Ah */
1728  point_transform = get_bits(&s->gb, 4); /* Al */
1729  }else
1730  prev_shift = point_transform = 0;
1731 
1732  if (nb_components > 1) {
1733  /* interleaved stream */
1734  s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1735  s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1736  } else if (!s->ls) { /* skip this for JPEG-LS */
1737  h = s->h_max / s->h_scount[0];
1738  v = s->v_max / s->v_scount[0];
1739  s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1740  s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1741  s->nb_blocks[0] = 1;
1742  s->h_scount[0] = 1;
1743  s->v_scount[0] = 1;
1744  }
1745 
1746  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1747  av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d skip:%d %s comp:%d\n",
1748  s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1749  predictor, point_transform, ilv, s->bits, s->mjpb_skiptosod,
1750  s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""), nb_components);
1751 
1752 
1753  /* mjpeg-b can have padding bytes between sos and image data, skip them */
1754  for (i = s->mjpb_skiptosod; i > 0; i--)
1755  skip_bits(&s->gb, 8);
1756 
1757 next_field:
1758  for (i = 0; i < nb_components; i++)
1759  s->last_dc[i] = (4 << s->bits);
1760 
1761  if (s->avctx->hwaccel) {
1762  int bytes_to_start = get_bits_count(&s->gb) / 8;
1763  av_assert0(bytes_to_start >= 0 &&
1764  s->raw_scan_buffer_size >= bytes_to_start);
1765 
1766  ret = s->avctx->hwaccel->decode_slice(s->avctx,
1767  s->raw_scan_buffer + bytes_to_start,
1768  s->raw_scan_buffer_size - bytes_to_start);
1769  if (ret < 0)
1770  return ret;
1771 
1772  } else if (s->lossless) {
1773  av_assert0(s->picture_ptr == s->picture);
1774  if (CONFIG_JPEGLS_DECODER && s->ls) {
1775 // for () {
1776 // reset_ls_coding_parameters(s, 0);
1777 
1778  if ((ret = ff_jpegls_decode_picture(s, predictor,
1779  point_transform, ilv)) < 0)
1780  return ret;
1781  } else {
1782  if (s->rgb || s->bayer) {
1783  if ((ret = ljpeg_decode_rgb_scan(s, nb_components, predictor, point_transform)) < 0)
1784  return ret;
1785  } else {
1786  if ((ret = ljpeg_decode_yuv_scan(s, predictor,
1787  point_transform,
1788  nb_components)) < 0)
1789  return ret;
1790  }
1791  }
1792  } else {
1793  if (s->progressive && predictor) {
1794  av_assert0(s->picture_ptr == s->picture);
1795  if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor,
1796  ilv, prev_shift,
1797  point_transform)) < 0)
1798  return ret;
1799  } else {
1800  if ((ret = mjpeg_decode_scan(s, nb_components,
1801  prev_shift, point_transform,
1802  mb_bitmask, mb_bitmask_size, reference)) < 0)
1803  return ret;
1804  }
1805  }
1806 
1807  if (s->interlaced &&
1808  get_bits_left(&s->gb) > 32 &&
1809  show_bits(&s->gb, 8) == 0xFF) {
1810  GetBitContext bak = s->gb;
1811  align_get_bits(&bak);
1812  if (show_bits(&bak, 16) == 0xFFD1) {
1813  av_log(s->avctx, AV_LOG_DEBUG, "AVRn interlaced picture marker found\n");
1814  s->gb = bak;
1815  skip_bits(&s->gb, 16);
1816  s->bottom_field ^= 1;
1817 
1818  goto next_field;
1819  }
1820  }
1821 
1822  emms_c();
1823  return 0;
1824  out_of_range:
1825  av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1826  return AVERROR_INVALIDDATA;
1827 }
1828 
1830 {
1831  if (get_bits(&s->gb, 16) != 4)
1832  return AVERROR_INVALIDDATA;
1833  s->restart_interval = get_bits(&s->gb, 16);
1834  s->restart_count = 0;
1835  av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1836  s->restart_interval);
1837 
1838  return 0;
1839 }
1840 
1842 {
1843  int len, id, i;
1844 
1845  len = get_bits(&s->gb, 16);
1846  if (len < 6) {
1847  if (s->bayer) {
1848  // Pentax K-1 (digital camera) JPEG images embedded in DNG images contain unknown APP0 markers
1849  av_log(s->avctx, AV_LOG_WARNING, "skipping APPx (len=%"PRId32") for bayer-encoded image\n", len);
1850  skip_bits(&s->gb, len);
1851  return 0;
1852  } else
1853  return AVERROR_INVALIDDATA;
1854  }
1855  if (8 * len > get_bits_left(&s->gb))
1856  return AVERROR_INVALIDDATA;
1857 
1858  id = get_bits_long(&s->gb, 32);
1859  len -= 6;
1860 
1861  if (s->avctx->debug & FF_DEBUG_STARTCODE)
1862  av_log(s->avctx, AV_LOG_DEBUG, "APPx (%s / %8X) len=%d\n",
1863  av_fourcc2str(av_bswap32(id)), id, len);
1864 
1865  /* Buggy AVID, it puts EOI only at every 10th frame. */
1866  /* Also, this fourcc is used by non-avid files too, it holds some
1867  information, but it's always present in AVID-created files. */
1868  if (id == AV_RB32("AVI1")) {
1869  /* structure:
1870  4bytes AVI1
1871  1bytes polarity
1872  1bytes always zero
1873  4bytes field_size
1874  4bytes field_size_less_padding
1875  */
1876  s->buggy_avid = 1;
1877  i = get_bits(&s->gb, 8); len--;
1878  av_log(s->avctx, AV_LOG_DEBUG, "polarity %d\n", i);
1879  goto out;
1880  }
1881 
1882  if (id == AV_RB32("JFIF")) {
1883  int t_w, t_h, v1, v2;
1884  if (len < 8)
1885  goto out;
1886  skip_bits(&s->gb, 8); /* the trailing zero-byte */
1887  v1 = get_bits(&s->gb, 8);
1888  v2 = get_bits(&s->gb, 8);
1889  skip_bits(&s->gb, 8);
1890 
1891  s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1892  s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1893  if ( s->avctx->sample_aspect_ratio.num <= 0
1894  || s->avctx->sample_aspect_ratio.den <= 0) {
1895  s->avctx->sample_aspect_ratio.num = 0;
1896  s->avctx->sample_aspect_ratio.den = 1;
1897  }
1898 
1899  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1900  av_log(s->avctx, AV_LOG_INFO,
1901  "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1902  v1, v2,
1905 
1906  len -= 8;
1907  if (len >= 2) {
1908  t_w = get_bits(&s->gb, 8);
1909  t_h = get_bits(&s->gb, 8);
1910  if (t_w && t_h) {
1911  /* skip thumbnail */
1912  if (len -10 - (t_w * t_h * 3) > 0)
1913  len -= t_w * t_h * 3;
1914  }
1915  len -= 2;
1916  }
1917  goto out;
1918  }
1919 
1920  if ( id == AV_RB32("Adob")
1921  && len >= 7
1922  && show_bits(&s->gb, 8) == 'e'
1923  && show_bits_long(&s->gb, 32) != AV_RB32("e_CM")) {
1924  skip_bits(&s->gb, 8); /* 'e' */
1925  skip_bits(&s->gb, 16); /* version */
1926  skip_bits(&s->gb, 16); /* flags0 */
1927  skip_bits(&s->gb, 16); /* flags1 */
1928  s->adobe_transform = get_bits(&s->gb, 8);
1929  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1930  av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found, transform=%d\n", s->adobe_transform);
1931  len -= 7;
1932  goto out;
1933  }
1934 
1935  if (id == AV_RB32("LJIF")) {
1936  int rgb = s->rgb;
1937  int pegasus_rct = s->pegasus_rct;
1938  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1939  av_log(s->avctx, AV_LOG_INFO,
1940  "Pegasus lossless jpeg header found\n");
1941  skip_bits(&s->gb, 16); /* version ? */
1942  skip_bits(&s->gb, 16); /* unknown always 0? */
1943  skip_bits(&s->gb, 16); /* unknown always 0? */
1944  skip_bits(&s->gb, 16); /* unknown always 0? */
1945  switch (i=get_bits(&s->gb, 8)) {
1946  case 1:
1947  rgb = 1;
1948  pegasus_rct = 0;
1949  break;
1950  case 2:
1951  rgb = 1;
1952  pegasus_rct = 1;
1953  break;
1954  default:
1955  av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace %d\n", i);
1956  }
1957 
1958  len -= 9;
1959  if (s->got_picture)
1960  if (rgb != s->rgb || pegasus_rct != s->pegasus_rct) {
1961  av_log(s->avctx, AV_LOG_WARNING, "Mismatching LJIF tag\n");
1962  goto out;
1963  }
1964 
1965  s->rgb = rgb;
1966  s->pegasus_rct = pegasus_rct;
1967 
1968  goto out;
1969  }
1970  if (id == AV_RL32("colr") && len > 0) {
1971  s->colr = get_bits(&s->gb, 8);
1972  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1973  av_log(s->avctx, AV_LOG_INFO, "COLR %d\n", s->colr);
1974  len --;
1975  goto out;
1976  }
1977  if (id == AV_RL32("xfrm") && len > 0) {
1978  s->xfrm = get_bits(&s->gb, 8);
1979  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1980  av_log(s->avctx, AV_LOG_INFO, "XFRM %d\n", s->xfrm);
1981  len --;
1982  goto out;
1983  }
1984 
1985  /* JPS extension by VRex */
1986  if (s->start_code == APP3 && id == AV_RB32("_JPS") && len >= 10) {
1987  int flags, layout, type;
1988  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1989  av_log(s->avctx, AV_LOG_INFO, "_JPSJPS_\n");
1990 
1991  skip_bits(&s->gb, 32); len -= 4; /* JPS_ */
1992  skip_bits(&s->gb, 16); len -= 2; /* block length */
1993  skip_bits(&s->gb, 8); /* reserved */
1994  flags = get_bits(&s->gb, 8);
1995  layout = get_bits(&s->gb, 8);
1996  type = get_bits(&s->gb, 8);
1997  len -= 4;
1998 
1999  av_freep(&s->stereo3d);
2000  s->stereo3d = av_stereo3d_alloc();
2001  if (!s->stereo3d) {
2002  goto out;
2003  }
2004  if (type == 0) {
2006  } else if (type == 1) {
2007  switch (layout) {
2008  case 0x01:
2010  break;
2011  case 0x02:
2013  break;
2014  case 0x03:
2016  break;
2017  }
2018  if (!(flags & 0x04)) {
2020  }
2021  }
2022  goto out;
2023  }
2024 
2025  /* EXIF metadata */
2026  if (s->start_code == APP1 && id == AV_RB32("Exif") && len >= 2) {
2027  GetByteContext gbytes;
2028  int ret, le, ifd_offset, bytes_read;
2029  const uint8_t *aligned;
2030 
2031  skip_bits(&s->gb, 16); // skip padding
2032  len -= 2;
2033 
2034  // init byte wise reading
2035  aligned = align_get_bits(&s->gb);
2036  bytestream2_init(&gbytes, aligned, len);
2037 
2038  // read TIFF header
2039  ret = ff_tdecode_header(&gbytes, &le, &ifd_offset);
2040  if (ret) {
2041  av_log(s->avctx, AV_LOG_ERROR, "mjpeg: invalid TIFF header in EXIF data\n");
2042  } else {
2043  bytestream2_seek(&gbytes, ifd_offset, SEEK_SET);
2044 
2045  // read 0th IFD and store the metadata
2046  // (return values > 0 indicate the presence of subimage metadata)
2047  ret = ff_exif_decode_ifd(s->avctx, &gbytes, le, 0, &s->exif_metadata);
2048  if (ret < 0) {
2049  av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error decoding EXIF data\n");
2050  }
2051  }
2052 
2053  bytes_read = bytestream2_tell(&gbytes);
2054  skip_bits(&s->gb, bytes_read << 3);
2055  len -= bytes_read;
2056 
2057  goto out;
2058  }
2059 
2060  /* Apple MJPEG-A */
2061  if ((s->start_code == APP1) && (len > (0x28 - 8))) {
2062  id = get_bits_long(&s->gb, 32);
2063  len -= 4;
2064  /* Apple MJPEG-A */
2065  if (id == AV_RB32("mjpg")) {
2066  /* structure:
2067  4bytes field size
2068  4bytes pad field size
2069  4bytes next off
2070  4bytes quant off
2071  4bytes huff off
2072  4bytes image off
2073  4bytes scan off
2074  4bytes data off
2075  */
2076  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2077  av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
2078  }
2079  }
2080 
2081  if (s->start_code == APP2 && id == AV_RB32("ICC_") && len >= 10) {
2082  int id2;
2083  unsigned seqno;
2084  unsigned nummarkers;
2085 
2086  id = get_bits_long(&s->gb, 32);
2087  id2 = get_bits(&s->gb, 24);
2088  len -= 7;
2089  if (id != AV_RB32("PROF") || id2 != AV_RB24("ILE")) {
2090  av_log(s->avctx, AV_LOG_WARNING, "Invalid ICC_PROFILE header in APP2\n");
2091  goto out;
2092  }
2093 
2094  skip_bits(&s->gb, 8);
2095  seqno = get_bits(&s->gb, 8);
2096  len -= 2;
2097  if (seqno == 0) {
2098  av_log(s->avctx, AV_LOG_WARNING, "Invalid sequence number in APP2\n");
2099  goto out;
2100  }
2101 
2102  nummarkers = get_bits(&s->gb, 8);
2103  len -= 1;
2104  if (nummarkers == 0) {
2105  av_log(s->avctx, AV_LOG_WARNING, "Invalid number of markers coded in APP2\n");
2106  goto out;
2107  } else if (s->iccnum != 0 && nummarkers != s->iccnum) {
2108  av_log(s->avctx, AV_LOG_WARNING, "Mistmatch in coded number of ICC markers between markers\n");
2109  goto out;
2110  } else if (seqno > nummarkers) {
2111  av_log(s->avctx, AV_LOG_WARNING, "Mismatching sequence number and coded number of ICC markers\n");
2112  goto out;
2113  }
2114 
2115  /* Allocate if this is the first APP2 we've seen. */
2116  if (s->iccnum == 0) {
2117  s->iccdata = av_mallocz(nummarkers * sizeof(*(s->iccdata)));
2118  s->iccdatalens = av_mallocz(nummarkers * sizeof(*(s->iccdatalens)));
2119  if (!s->iccdata || !s->iccdatalens) {
2120  av_log(s->avctx, AV_LOG_ERROR, "Could not allocate ICC data arrays\n");
2121  return AVERROR(ENOMEM);
2122  }
2123  s->iccnum = nummarkers;
2124  }
2125 
2126  if (s->iccdata[seqno - 1]) {
2127  av_log(s->avctx, AV_LOG_WARNING, "Duplicate ICC sequence number\n");
2128  goto out;
2129  }
2130 
2131  s->iccdatalens[seqno - 1] = len;
2132  s->iccdata[seqno - 1] = av_malloc(len);
2133  if (!s->iccdata[seqno - 1]) {
2134  av_log(s->avctx, AV_LOG_ERROR, "Could not allocate ICC data buffer\n");
2135  return AVERROR(ENOMEM);
2136  }
2137 
2138  memcpy(s->iccdata[seqno - 1], align_get_bits(&s->gb), len);
2139  skip_bits(&s->gb, len << 3);
2140  len = 0;
2141  s->iccread++;
2142 
2143  if (s->iccread > s->iccnum)
2144  av_log(s->avctx, AV_LOG_WARNING, "Read more ICC markers than are supposed to be coded\n");
2145  }
2146 
2147 out:
2148  /* slow but needed for extreme adobe jpegs */
2149  if (len < 0)
2151  "mjpeg: error, decode_app parser read over the end\n");
2152  while (--len > 0)
2153  skip_bits(&s->gb, 8);
2154 
2155  return 0;
2156 }
2157 
2159 {
2160  int len = get_bits(&s->gb, 16);
2161  if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
2162  int i;
2163  char *cbuf = av_malloc(len - 1);
2164  if (!cbuf)
2165  return AVERROR(ENOMEM);
2166 
2167  for (i = 0; i < len - 2; i++)
2168  cbuf[i] = get_bits(&s->gb, 8);
2169  if (i > 0 && cbuf[i - 1] == '\n')
2170  cbuf[i - 1] = 0;
2171  else
2172  cbuf[i] = 0;
2173 
2174  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2175  av_log(s->avctx, AV_LOG_INFO, "comment: '%s'\n", cbuf);
2176 
2177  /* buggy avid, it puts EOI only at every 10th frame */
2178  if (!strncmp(cbuf, "AVID", 4)) {
2179  parse_avid(s, cbuf, len);
2180  } else if (!strcmp(cbuf, "CS=ITU601"))
2181  s->cs_itu601 = 1;
2182  else if ((!strncmp(cbuf, "Intel(R) JPEG Library, version 1", 32) && s->avctx->codec_tag) ||
2183  (!strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
2184  s->flipped = 1;
2185  else if (!strcmp(cbuf, "MULTISCOPE II")) {
2186  s->avctx->sample_aspect_ratio = (AVRational) { 1, 2 };
2187  s->multiscope = 2;
2188  }
2189 
2190  av_free(cbuf);
2191  }
2192 
2193  return 0;
2194 }
2195 
2196 /* return the 8 bit start code value and update the search
2197  state. Return -1 if no start code found */
2198 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
2199 {
2200  const uint8_t *buf_ptr;
2201  unsigned int v, v2;
2202  int val;
2203  int skipped = 0;
2204 
2205  buf_ptr = *pbuf_ptr;
2206  while (buf_end - buf_ptr > 1) {
2207  v = *buf_ptr++;
2208  v2 = *buf_ptr;
2209  if ((v == 0xff) && (v2 >= SOF0) && (v2 <= COM) && buf_ptr < buf_end) {
2210  val = *buf_ptr++;
2211  goto found;
2212  }
2213  skipped++;
2214  }
2215  buf_ptr = buf_end;
2216  val = -1;
2217 found:
2218  ff_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
2219  *pbuf_ptr = buf_ptr;
2220  return val;
2221 }
2222 
2224  const uint8_t **buf_ptr, const uint8_t *buf_end,
2225  const uint8_t **unescaped_buf_ptr,
2226  int *unescaped_buf_size)
2227 {
2228  int start_code;
2229  start_code = find_marker(buf_ptr, buf_end);
2230 
2231  av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
2232  if (!s->buffer)
2233  return AVERROR(ENOMEM);
2234 
2235  /* unescape buffer of SOS, use special treatment for JPEG-LS */
2236  if (start_code == SOS && !s->ls) {
2237  const uint8_t *src = *buf_ptr;
2238  const uint8_t *ptr = src;
2239  uint8_t *dst = s->buffer;
2240 
2241  #define copy_data_segment(skip) do { \
2242  ptrdiff_t length = (ptr - src) - (skip); \
2243  if (length > 0) { \
2244  memcpy(dst, src, length); \
2245  dst += length; \
2246  src = ptr; \
2247  } \
2248  } while (0)
2249 
2250  if (s->avctx->codec_id == AV_CODEC_ID_THP) {
2251  ptr = buf_end;
2252  copy_data_segment(0);
2253  } else {
2254  while (ptr < buf_end) {
2255  uint8_t x = *(ptr++);
2256 
2257  if (x == 0xff) {
2258  ptrdiff_t skip = 0;
2259  while (ptr < buf_end && x == 0xff) {
2260  x = *(ptr++);
2261  skip++;
2262  }
2263 
2264  /* 0xFF, 0xFF, ... */
2265  if (skip > 1) {
2266  copy_data_segment(skip);
2267 
2268  /* decrement src as it is equal to ptr after the
2269  * copy_data_segment macro and we might want to
2270  * copy the current value of x later on */
2271  src--;
2272  }
2273 
2274  if (x < RST0 || x > RST7) {
2275  copy_data_segment(1);
2276  if (x)
2277  break;
2278  }
2279  }
2280  }
2281  if (src < ptr)
2282  copy_data_segment(0);
2283  }
2284  #undef copy_data_segment
2285 
2286  *unescaped_buf_ptr = s->buffer;
2287  *unescaped_buf_size = dst - s->buffer;
2288  memset(s->buffer + *unescaped_buf_size, 0,
2290 
2291  av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %"PTRDIFF_SPECIFIER" bytes\n",
2292  (buf_end - *buf_ptr) - (dst - s->buffer));
2293  } else if (start_code == SOS && s->ls) {
2294  const uint8_t *src = *buf_ptr;
2295  uint8_t *dst = s->buffer;
2296  int bit_count = 0;
2297  int t = 0, b = 0;
2298  PutBitContext pb;
2299 
2300  /* find marker */
2301  while (src + t < buf_end) {
2302  uint8_t x = src[t++];
2303  if (x == 0xff) {
2304  while ((src + t < buf_end) && x == 0xff)
2305  x = src[t++];
2306  if (x & 0x80) {
2307  t -= FFMIN(2, t);
2308  break;
2309  }
2310  }
2311  }
2312  bit_count = t * 8;
2313  init_put_bits(&pb, dst, t);
2314 
2315  /* unescape bitstream */
2316  while (b < t) {
2317  uint8_t x = src[b++];
2318  put_bits(&pb, 8, x);
2319  if (x == 0xFF && b < t) {
2320  x = src[b++];
2321  if (x & 0x80) {
2322  av_log(s->avctx, AV_LOG_WARNING, "Invalid escape sequence\n");
2323  x &= 0x7f;
2324  }
2325  put_bits(&pb, 7, x);
2326  bit_count--;
2327  }
2328  }
2329  flush_put_bits(&pb);
2330 
2331  *unescaped_buf_ptr = dst;
2332  *unescaped_buf_size = (bit_count + 7) >> 3;
2333  memset(s->buffer + *unescaped_buf_size, 0,
2335  } else {
2336  *unescaped_buf_ptr = *buf_ptr;
2337  *unescaped_buf_size = buf_end - *buf_ptr;
2338  }
2339 
2340  return start_code;
2341 }
2342 
2344 {
2345  int i;
2346 
2347  if (s->iccdata)
2348  for (i = 0; i < s->iccnum; i++)
2349  av_freep(&s->iccdata[i]);
2350  av_freep(&s->iccdata);
2351  av_freep(&s->iccdatalens);
2352 
2353  s->iccread = 0;
2354  s->iccnum = 0;
2355 }
2356 
2357 // SMV JPEG just stacks several output frames into one JPEG picture
2358 // we handle that by setting up the cropping parameters appropriately
2360 {
2361  MJpegDecodeContext *s = avctx->priv_data;
2362  int ret;
2363 
2364  if (s->smv_next_frame > 0) {
2365  av_assert0(s->smv_frame->buf[0]);
2366  av_frame_unref(frame);
2367  ret = av_frame_ref(frame, s->smv_frame);
2368  if (ret < 0)
2369  return ret;
2370  } else {
2371  av_assert0(frame->buf[0]);
2373  ret = av_frame_ref(s->smv_frame, frame);
2374  if (ret < 0)
2375  return ret;
2376  }
2377 
2378  av_assert0((s->smv_next_frame + 1) * avctx->height <= avctx->coded_height);
2379 
2380  frame->width = avctx->coded_width;
2381  frame->height = avctx->coded_height;
2382  frame->crop_top = FFMIN(s->smv_next_frame * avctx->height, frame->height);
2383  frame->crop_bottom = frame->height - (s->smv_next_frame + 1) * avctx->height;
2384 
2386 
2387  if (s->smv_next_frame == 0)
2389 
2390  return 0;
2391 }
2392 
2394 {
2395  MJpegDecodeContext *s = avctx->priv_data;
2396  int ret;
2397 
2398  av_packet_unref(s->pkt);
2399  ret = ff_decode_get_packet(avctx, s->pkt);
2400  if (ret < 0)
2401  return ret;
2402 
2403 #if CONFIG_SP5X_DECODER || CONFIG_AMV_DECODER
2404  if (avctx->codec_id == AV_CODEC_ID_SP5X ||
2405  avctx->codec_id == AV_CODEC_ID_AMV) {
2406  ret = ff_sp5x_process_packet(avctx, s->pkt);
2407  if (ret < 0)
2408  return ret;
2409  }
2410 #endif
2411 
2412  s->buf_size = s->pkt->size;
2413 
2414  return 0;
2415 }
2416 
2418 {
2419  MJpegDecodeContext *s = avctx->priv_data;
2420  const uint8_t *buf_end, *buf_ptr;
2421  const uint8_t *unescaped_buf_ptr;
2422  int hshift, vshift;
2423  int unescaped_buf_size;
2424  int start_code;
2425  int i, index;
2426  int ret = 0;
2427  int is16bit;
2428 
2429  if (avctx->codec_id == AV_CODEC_ID_SMVJPEG && s->smv_next_frame > 0)
2430  return smv_process_frame(avctx, frame);
2431 
2433  av_freep(&s->stereo3d);
2434  s->adobe_transform = -1;
2435 
2436  if (s->iccnum != 0)
2437  reset_icc_profile(s);
2438 
2439  ret = mjpeg_get_packet(avctx);
2440  if (ret < 0)
2441  return ret;
2442 
2443  buf_ptr = s->pkt->data;
2444  buf_end = s->pkt->data + s->pkt->size;
2445  while (buf_ptr < buf_end) {
2446  /* find start next marker */
2447  start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
2448  &unescaped_buf_ptr,
2449  &unescaped_buf_size);
2450  /* EOF */
2451  if (start_code < 0) {
2452  break;
2453  } else if (unescaped_buf_size > INT_MAX / 8) {
2454  av_log(avctx, AV_LOG_ERROR,
2455  "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
2456  start_code, unescaped_buf_size, s->pkt->size);
2457  return AVERROR_INVALIDDATA;
2458  }
2459  av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%"PTRDIFF_SPECIFIER"\n",
2460  start_code, buf_end - buf_ptr);
2461 
2462  ret = init_get_bits8(&s->gb, unescaped_buf_ptr, unescaped_buf_size);
2463 
2464  if (ret < 0) {
2465  av_log(avctx, AV_LOG_ERROR, "invalid buffer\n");
2466  goto fail;
2467  }
2468 
2469  s->start_code = start_code;
2470  if (s->avctx->debug & FF_DEBUG_STARTCODE)
2471  av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
2472 
2473  /* process markers */
2474  if (start_code >= RST0 && start_code <= RST7) {
2475  av_log(avctx, AV_LOG_DEBUG,
2476  "restart marker: %d\n", start_code & 0x0f);
2477  /* APP fields */
2478  } else if (start_code >= APP0 && start_code <= APP15) {
2479  if ((ret = mjpeg_decode_app(s)) < 0)
2480  av_log(avctx, AV_LOG_ERROR, "unable to decode APP fields: %s\n",
2481  av_err2str(ret));
2482  /* Comment */
2483  } else if (start_code == COM) {
2484  ret = mjpeg_decode_com(s);
2485  if (ret < 0)
2486  return ret;
2487  } else if (start_code == DQT) {
2488  ret = ff_mjpeg_decode_dqt(s);
2489  if (ret < 0)
2490  return ret;
2491  }
2492 
2493  ret = -1;
2494 
2495  if (!CONFIG_JPEGLS_DECODER &&
2496  (start_code == SOF48 || start_code == LSE)) {
2497  av_log(avctx, AV_LOG_ERROR, "JPEG-LS support not enabled.\n");
2498  return AVERROR(ENOSYS);
2499  }
2500 
2501  if (avctx->skip_frame == AVDISCARD_ALL) {
2502  switch(start_code) {
2503  case SOF0:
2504  case SOF1:
2505  case SOF2:
2506  case SOF3:
2507  case SOF48:
2508  case SOI:
2509  case SOS:
2510  case EOI:
2511  break;
2512  default:
2513  goto skip;
2514  }
2515  }
2516 
2517  switch (start_code) {
2518  case SOI:
2519  s->restart_interval = 0;
2520  s->restart_count = 0;
2521  s->raw_image_buffer = buf_ptr;
2522  s->raw_image_buffer_size = buf_end - buf_ptr;
2523  /* nothing to do on SOI */
2524  break;
2525  case DHT:
2526  if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
2527  av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
2528  goto fail;
2529  }
2530  break;
2531  case SOF0:
2532  case SOF1:
2533  if (start_code == SOF0)
2535  else
2537  s->lossless = 0;
2538  s->ls = 0;
2539  s->progressive = 0;
2540  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2541  goto fail;
2542  break;
2543  case SOF2:
2545  s->lossless = 0;
2546  s->ls = 0;
2547  s->progressive = 1;
2548  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2549  goto fail;
2550  break;
2551  case SOF3:
2554  s->lossless = 1;
2555  s->ls = 0;
2556  s->progressive = 0;
2557  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2558  goto fail;
2559  break;
2560  case SOF48:
2563  s->lossless = 1;
2564  s->ls = 1;
2565  s->progressive = 0;
2566  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
2567  goto fail;
2568  break;
2569  case LSE:
2570  if (!CONFIG_JPEGLS_DECODER ||
2571  (ret = ff_jpegls_decode_lse(s)) < 0)
2572  goto fail;
2573  break;
2574  case EOI:
2575 eoi_parser:
2576  if (!avctx->hwaccel && avctx->skip_frame != AVDISCARD_ALL &&
2577  s->progressive && s->cur_scan && s->got_picture)
2579  s->cur_scan = 0;
2580  if (!s->got_picture) {
2581  av_log(avctx, AV_LOG_WARNING,
2582  "Found EOI before any SOF, ignoring\n");
2583  break;
2584  }
2585  if (s->interlaced) {
2586  s->bottom_field ^= 1;
2587  /* if not bottom field, do not output image yet */
2588  if (s->bottom_field == !s->interlace_polarity)
2589  break;
2590  }
2591  if (avctx->skip_frame == AVDISCARD_ALL) {
2592  s->got_picture = 0;
2593  ret = AVERROR(EAGAIN);
2594  goto the_end_no_picture;
2595  }
2596  if (s->avctx->hwaccel) {
2597  ret = s->avctx->hwaccel->end_frame(s->avctx);
2598  if (ret < 0)
2599  return ret;
2600 
2602  }
2603  if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0)
2604  return ret;
2605  s->got_picture = 0;
2606 
2607  frame->pkt_dts = s->pkt->dts;
2608 
2609  if (!s->lossless) {
2610  int qp = FFMAX3(s->qscale[0],
2611  s->qscale[1],
2612  s->qscale[2]);
2613  int qpw = (s->width + 15) / 16;
2614  AVBufferRef *qp_table_buf = av_buffer_alloc(qpw);
2615  if (qp_table_buf) {
2616  memset(qp_table_buf->data, qp, qpw);
2617  av_frame_set_qp_table(frame, qp_table_buf, 0, FF_QSCALE_TYPE_MPEG1);
2618  }
2619 
2620  if(avctx->debug & FF_DEBUG_QP)
2621  av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", qp);
2622  }
2623 
2624  goto the_end;
2625  case SOS:
2626  s->raw_scan_buffer = buf_ptr;
2627  s->raw_scan_buffer_size = buf_end - buf_ptr;
2628 
2629  s->cur_scan++;
2630  if (avctx->skip_frame == AVDISCARD_ALL) {
2631  skip_bits(&s->gb, get_bits_left(&s->gb));
2632  break;
2633  }
2634 
2635  if ((ret = ff_mjpeg_decode_sos(s, NULL, 0, NULL)) < 0 &&
2636  (avctx->err_recognition & AV_EF_EXPLODE))
2637  goto fail;
2638  break;
2639  case DRI:
2640  if ((ret = mjpeg_decode_dri(s)) < 0)
2641  return ret;
2642  break;
2643  case SOF5:
2644  case SOF6:
2645  case SOF7:
2646  case SOF9:
2647  case SOF10:
2648  case SOF11:
2649  case SOF13:
2650  case SOF14:
2651  case SOF15:
2652  case JPG:
2653  av_log(avctx, AV_LOG_ERROR,
2654  "mjpeg: unsupported coding type (%x)\n", start_code);
2655  break;
2656  }
2657 
2658 skip:
2659  /* eof process start code */
2660  buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
2661  av_log(avctx, AV_LOG_DEBUG,
2662  "marker parser used %d bytes (%d bits)\n",
2663  (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
2664  }
2665  if (s->got_picture && s->cur_scan) {
2666  av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
2667  goto eoi_parser;
2668  }
2669  av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
2670  return AVERROR_INVALIDDATA;
2671 fail:
2672  s->got_picture = 0;
2673  return ret;
2674 the_end:
2675 
2676  is16bit = av_pix_fmt_desc_get(s->avctx->pix_fmt)->comp[0].step > 1;
2677 
2678  if (AV_RB32(s->upscale_h)) {
2679  int p;
2681  avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2682  avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2683  avctx->pix_fmt == AV_PIX_FMT_YUV440P ||
2684  avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2685  avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2686  avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
2687  avctx->pix_fmt == AV_PIX_FMT_YUV420P16||
2688  avctx->pix_fmt == AV_PIX_FMT_YUVA420P ||
2689  avctx->pix_fmt == AV_PIX_FMT_YUVA420P16||
2690  avctx->pix_fmt == AV_PIX_FMT_GBRP ||
2691  avctx->pix_fmt == AV_PIX_FMT_GBRAP
2692  );
2693  ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2694  if (ret)
2695  return ret;
2696 
2698  for (p = 0; p<s->nb_components; p++) {
2699  uint8_t *line = s->picture_ptr->data[p];
2700  int w = s->width;
2701  int h = s->height;
2702  if (!s->upscale_h[p])
2703  continue;
2704  if (p==1 || p==2) {
2705  w = AV_CEIL_RSHIFT(w, hshift);
2706  h = AV_CEIL_RSHIFT(h, vshift);
2707  }
2708  if (s->upscale_v[p] == 1)
2709  h = (h+1)>>1;
2710  av_assert0(w > 0);
2711  for (i = 0; i < h; i++) {
2712  if (s->upscale_h[p] == 1) {
2713  if (is16bit) ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 2];
2714  else line[w - 1] = line[(w - 1) / 2];
2715  for (index = w - 2; index > 0; index--) {
2716  if (is16bit)
2717  ((uint16_t*)line)[index] = (((uint16_t*)line)[index / 2] + ((uint16_t*)line)[(index + 1) / 2]) >> 1;
2718  else
2719  line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
2720  }
2721  } else if (s->upscale_h[p] == 2) {
2722  if (is16bit) {
2723  ((uint16_t*)line)[w - 1] = ((uint16_t*)line)[(w - 1) / 3];
2724  if (w > 1)
2725  ((uint16_t*)line)[w - 2] = ((uint16_t*)line)[w - 1];
2726  } else {
2727  line[w - 1] = line[(w - 1) / 3];
2728  if (w > 1)
2729  line[w - 2] = line[w - 1];
2730  }
2731  for (index = w - 3; index > 0; index--) {
2732  line[index] = (line[index / 3] + line[(index + 1) / 3] + line[(index + 2) / 3] + 1) / 3;
2733  }
2734  }
2735  line += s->linesize[p];
2736  }
2737  }
2738  }
2739  if (AV_RB32(s->upscale_v)) {
2740  int p;
2742  avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
2743  avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
2744  avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
2745  avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
2746  avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
2747  avctx->pix_fmt == AV_PIX_FMT_YUV440P ||
2748  avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
2749  avctx->pix_fmt == AV_PIX_FMT_YUVA444P ||
2750  avctx->pix_fmt == AV_PIX_FMT_YUVA420P ||
2751  avctx->pix_fmt == AV_PIX_FMT_YUVA420P16||
2752  avctx->pix_fmt == AV_PIX_FMT_GBRP ||
2753  avctx->pix_fmt == AV_PIX_FMT_GBRAP
2754  );
2755  ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2756  if (ret)
2757  return ret;
2758 
2760  for (p = 0; p < s->nb_components; p++) {
2761  uint8_t *dst;
2762  int w = s->width;
2763  int h = s->height;
2764  if (!s->upscale_v[p])
2765  continue;
2766  if (p==1 || p==2) {
2767  w = AV_CEIL_RSHIFT(w, hshift);
2768  h = AV_CEIL_RSHIFT(h, vshift);
2769  }
2770  dst = &((uint8_t *)s->picture_ptr->data[p])[(h - 1) * s->linesize[p]];
2771  for (i = h - 1; i; i--) {
2772  uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[p])[i * s->upscale_v[p] / (s->upscale_v[p] + 1) * s->linesize[p]];
2773  uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[p])[(i + 1) * s->upscale_v[p] / (s->upscale_v[p] + 1) * s->linesize[p]];
2774  if (s->upscale_v[p] != 2 && (src1 == src2 || i == h - 1)) {
2775  memcpy(dst, src1, w);
2776  } else {
2777  for (index = 0; index < w; index++)
2778  dst[index] = (src1[index] + src2[index]) >> 1;
2779  }
2780  dst -= s->linesize[p];
2781  }
2782  }
2783  }
2784  if (s->flipped && !s->rgb) {
2785  int j;
2786  ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
2787  if (ret)
2788  return ret;
2789 
2791  for (index=0; index<s->nb_components; index++) {
2792  uint8_t *dst = s->picture_ptr->data[index];
2793  int w = s->picture_ptr->width;
2794  int h = s->picture_ptr->height;
2795  if(index && index<3){
2796  w = AV_CEIL_RSHIFT(w, hshift);
2797  h = AV_CEIL_RSHIFT(h, vshift);
2798  }
2799  if(dst){
2800  uint8_t *dst2 = dst + s->picture_ptr->linesize[index]*(h-1);
2801  for (i=0; i<h/2; i++) {
2802  for (j=0; j<w; j++)
2803  FFSWAP(int, dst[j], dst2[j]);
2804  dst += s->picture_ptr->linesize[index];
2805  dst2 -= s->picture_ptr->linesize[index];
2806  }
2807  }
2808  }
2809  }
2810  if (s->adobe_transform == 0 && s->avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
2811  int w = s->picture_ptr->width;
2812  int h = s->picture_ptr->height;
2813  av_assert0(s->nb_components == 4);
2814  for (i=0; i<h; i++) {
2815  int j;
2816  uint8_t *dst[4];
2817  for (index=0; index<4; index++) {
2818  dst[index] = s->picture_ptr->data[index]
2819  + s->picture_ptr->linesize[index]*i;
2820  }
2821  for (j=0; j<w; j++) {
2822  int k = dst[3][j];
2823  int r = dst[0][j] * k;
2824  int g = dst[1][j] * k;
2825  int b = dst[2][j] * k;
2826  dst[0][j] = g*257 >> 16;
2827  dst[1][j] = b*257 >> 16;
2828  dst[2][j] = r*257 >> 16;
2829  dst[3][j] = 255;
2830  }
2831  }
2832  }
2833  if (s->adobe_transform == 2 && s->avctx->pix_fmt == AV_PIX_FMT_YUVA444P) {
2834  int w = s->picture_ptr->width;
2835  int h = s->picture_ptr->height;
2836  av_assert0(s->nb_components == 4);
2837  for (i=0; i<h; i++) {
2838  int j;
2839  uint8_t *dst[4];
2840  for (index=0; index<4; index++) {
2841  dst[index] = s->picture_ptr->data[index]
2842  + s->picture_ptr->linesize[index]*i;
2843  }
2844  for (j=0; j<w; j++) {
2845  int k = dst[3][j];
2846  int r = (255 - dst[0][j]) * k;
2847  int g = (128 - dst[1][j]) * k;
2848  int b = (128 - dst[2][j]) * k;
2849  dst[0][j] = r*257 >> 16;
2850  dst[1][j] = (g*257 >> 16) + 128;
2851  dst[2][j] = (b*257 >> 16) + 128;
2852  dst[3][j] = 255;
2853  }
2854  }
2855  }
2856 
2857  if (s->stereo3d) {
2858  AVStereo3D *stereo = av_stereo3d_create_side_data(frame);
2859  if (stereo) {
2860  stereo->type = s->stereo3d->type;
2861  stereo->flags = s->stereo3d->flags;
2862  }
2863  av_freep(&s->stereo3d);
2864  }
2865 
2866  if (s->iccnum != 0 && s->iccnum == s->iccread) {
2867  AVFrameSideData *sd;
2868  size_t offset = 0;
2869  int total_size = 0;
2870  int i;
2871 
2872  /* Sum size of all parts. */
2873  for (i = 0; i < s->iccnum; i++)
2874  total_size += s->iccdatalens[i];
2875 
2876  sd = av_frame_new_side_data(frame, AV_FRAME_DATA_ICC_PROFILE, total_size);
2877  if (!sd) {
2878  av_log(s->avctx, AV_LOG_ERROR, "Could not allocate frame side data\n");
2879  return AVERROR(ENOMEM);
2880  }
2881 
2882  /* Reassemble the parts, which are now in-order. */
2883  for (i = 0; i < s->iccnum; i++) {
2884  memcpy(sd->data + offset, s->iccdata[i], s->iccdatalens[i]);
2885  offset += s->iccdatalens[i];
2886  }
2887  }
2888 
2889  av_dict_copy(&frame->metadata, s->exif_metadata, 0);
2891 
2892  if (avctx->codec_id == AV_CODEC_ID_SMVJPEG) {
2893  ret = smv_process_frame(avctx, frame);
2894  if (ret < 0) {
2895  av_frame_unref(frame);
2896  return ret;
2897  }
2898  }
2899 
2900  ret = 0;
2901 
2902 the_end_no_picture:
2903  av_log(avctx, AV_LOG_DEBUG, "decode frame unused %"PTRDIFF_SPECIFIER" bytes\n",
2904  buf_end - buf_ptr);
2905 
2906  return ret;
2907 }
2908 
2909 /* mxpeg may call the following function (with a blank MJpegDecodeContext)
2910  * even without having called ff_mjpeg_decode_init(). */
2912 {
2913  MJpegDecodeContext *s = avctx->priv_data;
2914  int i, j;
2915 
2916  if (s->interlaced && s->bottom_field == !s->interlace_polarity && s->got_picture && !avctx->frame_number) {
2917  av_log(avctx, AV_LOG_INFO, "Single field\n");
2918  }
2919 
2920  if (s->picture) {
2921  av_frame_free(&s->picture);
2922  s->picture_ptr = NULL;
2923  } else if (s->picture_ptr)
2925 
2926  av_packet_free(&s->pkt);
2927 
2928  av_frame_free(&s->smv_frame);
2929 
2930  av_freep(&s->buffer);
2931  av_freep(&s->stereo3d);
2932  av_freep(&s->ljpeg_buffer);
2933  s->ljpeg_buffer_size = 0;
2934 
2935  for (i = 0; i < 3; i++) {
2936  for (j = 0; j < 4; j++)
2937  ff_free_vlc(&s->vlcs[i][j]);
2938  }
2939  for (i = 0; i < MAX_COMPONENTS; i++) {
2940  av_freep(&s->blocks[i]);
2941  av_freep(&s->last_nnz[i]);
2942  }
2944 
2945  reset_icc_profile(s);
2946 
2948 
2949  return 0;
2950 }
2951 
2952 static void decode_flush(AVCodecContext *avctx)
2953 {
2954  MJpegDecodeContext *s = avctx->priv_data;
2955  s->got_picture = 0;
2956 
2957  s->smv_next_frame = 0;
2959 }
2960 
2961 #if CONFIG_MJPEG_DECODER
2962 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
2963 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2964 static const AVOption options[] = {
2965  { "extern_huff", "Use external huffman table.",
2966  OFFSET(extern_huff), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
2967  { NULL },
2968 };
2969 
2970 static const AVClass mjpegdec_class = {
2971  .class_name = "MJPEG decoder",
2972  .item_name = av_default_item_name,
2973  .option = options,
2974  .version = LIBAVUTIL_VERSION_INT,
2975 };
2976 
2978  .name = "mjpeg",
2979  .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
2980  .type = AVMEDIA_TYPE_VIDEO,
2981  .id = AV_CODEC_ID_MJPEG,
2982  .priv_data_size = sizeof(MJpegDecodeContext),
2984  .close = ff_mjpeg_decode_end,
2986  .flush = decode_flush,
2987  .capabilities = AV_CODEC_CAP_DR1,
2988  .max_lowres = 3,
2989  .priv_class = &mjpegdec_class,
2993  .hw_configs = (const AVCodecHWConfigInternal *const []) {
2994 #if CONFIG_MJPEG_NVDEC_HWACCEL
2995  HWACCEL_NVDEC(mjpeg),
2996 #endif
2997 #if CONFIG_MJPEG_VAAPI_HWACCEL
2998  HWACCEL_VAAPI(mjpeg),
2999 #endif
3000  NULL
3001  },
3002 };
3003 #endif
3004 #if CONFIG_THP_DECODER
3006  .name = "thp",
3007  .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
3008  .type = AVMEDIA_TYPE_VIDEO,
3009  .id = AV_CODEC_ID_THP,
3010  .priv_data_size = sizeof(MJpegDecodeContext),
3012  .close = ff_mjpeg_decode_end,
3014  .flush = decode_flush,
3015  .capabilities = AV_CODEC_CAP_DR1,
3016  .max_lowres = 3,
3019 };
3020 #endif
3021 
3022 #if CONFIG_SMVJPEG_DECODER
3024  .name = "smvjpeg",
3025  .long_name = NULL_IF_CONFIG_SMALL("SMV JPEG"),
3026  .type = AVMEDIA_TYPE_VIDEO,
3027  .id = AV_CODEC_ID_SMVJPEG,
3028  .priv_data_size = sizeof(MJpegDecodeContext),
3030  .close = ff_mjpeg_decode_end,
3032  .flush = decode_flush,
3033  .capabilities = AV_CODEC_CAP_DR1,
3036 };
3037 #endif
int block_stride[MAX_COMPONENTS]
Definition: mjpegdec.h:89
#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:1304
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:139
#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:92
#define se(name, range_min, range_max)
Definition: cbs_h2645.c:275
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:115
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:159
Definition: mjpeg.h:71
Definition: mjpeg.h:111
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:719
Definition: mjpeg.h:73
float re
Definition: fft.c:82
Definition: mjpeg.h:40
#define VD
Definition: av1dec.c:1110
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
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:505
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:152
void(* clear_block)(int16_t *block)
Definition: blockdsp.h:36
#define avpriv_request_sample(...)
static int mjpeg_get_packet(AVCodecContext *avctx)
Definition: mjpegdec.c:2393
int h_scount[MAX_COMPONENTS]
Definition: mjpegdec.h:97
BlockDSPContext bdsp
Definition: mjpegdec.h:114
int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
Definition: mjpegdec.c:227
static int mjpeg_decode_com(MJpegDecodeContext *s)
Definition: mjpegdec.c:2158
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1166
TIFF constants & data structures.
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:61
int size
Definition: packet.h:364
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
Definition: blockdsp.c:60
int ff_sp5x_process_packet(AVCodecContext *avctx, AVPacket *avpkt)
Definition: sp5xdec.c:33
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
uint8_t * buffer
Definition: mjpegdec.h:57
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
AVPacket * pkt
Definition: mjpegdec.h:53
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:94
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:106
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:73
uint8_t run
Definition: svq3.c:205
size_t crop_bottom
Definition: frame.h:675
static CopyRet receive_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
Definition: crystalhd.c:560
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1742
static int decode_block(MJpegDecodeContext *s, int16_t *block, int component, int dc_index, int ac_index, uint16_t *quant_matrix)
Definition: mjpegdec.c:826
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1675
int profile
profile
Definition: avcodec.h:1851
int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
Definition: mjpegdec.c:268
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:1953
AVStereo3D * av_stereo3d_alloc(void)
Allocate an AVStereo3D structure and set its fields to default values.
Definition: stereo3d.c:28
int comp_index[MAX_COMPONENTS]
Definition: mjpegdec.h:93
static void reset_icc_profile(MJpegDecodeContext *s)
Definition: mjpegdec.c:2343
static void mjpeg_idct_scan_progressive_ac(MJpegDecodeContext *s)
Definition: mjpegdec.c:1616
HpelDSPContext hdsp
Definition: mjpegdec.h:115
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:1951
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1999
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:151
int16_t block[64]
Definition: mjpegdec.h:108
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#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
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:64
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 build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, int nb_codes, int is_ac, void *logctx)
Definition: mjpegdec.c:65
static int mjpeg_decode_dri(MJpegDecodeContext *s)
Definition: mjpegdec.c:1829
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:1619
uint16_t(* ljpeg_buffer)[4]
Definition: mjpegdec.h:131
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:132
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
Definition: decode.c:202
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:2186
#define FF_PROFILE_MJPEG_JPEG_LS
Definition: avcodec.h:1955
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:110
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
AVFrame * picture_ptr
Definition: mjpegdec.h:104
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:99
#define MAX_COMPONENTS
Definition: mjpegdec.h:45
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:1954
int h_count[MAX_COMPONENTS]
Definition: mjpegdec.h:91
static void build_huffman_codes(uint8_t *huff_size, const uint8_t *bits_table)
Definition: mjpegdec.c:54
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
AVDictionary * metadata
metadata.
Definition: frame.h:600
#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:1750
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:255
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
enum AVCodecID id
Definition: codec.h:204
AVDictionary * exif_metadata
Definition: mjpegdec.h:135
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:893
uint8_t ** iccdata
Definition: mjpegdec.h:141
#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:1057
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:992
#define PTRDIFF_SPECIFIER
Definition: internal.h:190
int nb_blocks[MAX_COMPONENTS]
Definition: mjpegdec.h:96
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:2911
VLC vlcs[3][4]
Definition: mjpegdec.h:60
static void parse_avid(MJpegDecodeContext *s, uint8_t *buf, int len)
Definition: mjpegdec.c:130
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:115
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:141
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:1443
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:2198
#define CLOSE_READER(name, gb)
Definition: get_bits.h:149
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:133
Definition: mjpeg.h:39
#define FF_CODEC_CAP_EXPORTS_CROPPING
The decoder sets the cropping fields in the output frames manually.
Definition: internal.h:66
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
size_t crop_top
Definition: frame.h:674
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:113
Definition: mjpeg.h:80
#define b
Definition: input.c:41
AVFrame * smv_frame
Definition: mjpegdec.h:146
static av_always_inline void mjpeg_copy_block(MJpegDecodeContext *s, uint8_t *dst, const uint8_t *src, int linesize, int lowres)
Definition: mjpegdec.c:1412
Definition: mjpeg.h:56
int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
Definition: mjpegdec.c:329
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:1640
#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:2167
#define width
int component_id[MAX_COMPONENTS]
Definition: mjpegdec.h:90
static int mjpeg_decode_app(MJpegDecodeContext *s)
Definition: mjpegdec.c:1841
#define NEG_USR32(a, s)
Definition: mathops.h:166
uint8_t w
Definition: llviddspenc.c:39
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:156
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:1952
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:101
#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:98
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:1651
#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
AVCodec ff_smvjpeg_decoder
GetBitContext gb
Definition: mjpegdec.h:50
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:974
#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:116
#define src1
Definition: h264pred.c:140
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
int ff_mjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: mjpegdec.c:2417
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:2505
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:1618
AVStereo3D * stereo3d
Definition: mjpegdec.h:137
main external API structure.
Definition: avcodec.h:531
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:606
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:1884
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:141
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:808
int ac_index[MAX_COMPONENTS]
Definition: mjpegdec.h:95
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1159
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int ff_init_vlc_from_lengths(VLC *vlc_arg, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
Definition: bitstream.c:381
static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform)
Definition: mjpegdec.c:1092
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:111
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:160
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:303
uint8_t raw_huffman_values[2][4][256]
Definition: mjpegdec.h:157
static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al)
Definition: mjpegdec.c:1564
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:561
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:154
#define FF_CODEC_CAP_SETS_PKT_DTS
Decoders marked with FF_CODEC_CAP_SETS_PKT_DTS want to set AVFrame.pkt_dts manually.
Definition: internal.h:55
int(* decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Callback for each slice.
Definition: avcodec.h:2494
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:328
uint8_t level
Definition: svq3.c:206
int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask, int mb_bitmask_size, const AVFrame *reference)
Definition: mjpegdec.c:1651
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:2466
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:150
static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block, int component, int dc_index, uint16_t *quant_matrix, int Al)
Definition: mjpegdec.c:875
Definition: mjpeg.h:94
const AVProfile ff_mjpeg_profiles[]
Definition: profiles.c:169
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:423
static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform, int nb_components)
Definition: mjpegdec.c:1258
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:3407
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:1623
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
static void predictor(AVFilterContext *ctx, const void *src, ptrdiff_t src_stride, void *dst, const uint8_t *prescreen, int N, const PredictorCoefficients *const model, int use_q2)
Definition: vf_nnedi.c:397
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:2185
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:153
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: jpegtables.c:73
AVCodecContext * avctx
Definition: mjpegdec.h:49
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:1626
static void shift_output(MJpegDecodeContext *s, uint8_t *ptr, int linesize)
Definition: mjpegdec.c:1428
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:105
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:2514
int16_t(*[MAX_COMPONENTS] blocks)[64]
intermediate sums (progressive mode)
Definition: mjpegdec.h:109
AVFrame * picture
Definition: mjpegdec.h:103
void * hwaccel_picture_private
Definition: mjpegdec.h:161
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
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:53
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:102
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:88
#define REFINE_BIT(j)
Definition: mjpegdec.c:966
uint8_t upscale_h[4]
Definition: mjpegdec.h:72
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: packet.h:362
static int smv_process_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: mjpegdec.c:2359
static void decode_flush(AVCodecContext *avctx)
Definition: mjpegdec.c:2952
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 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:2223
#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
Definition: rpzaenc.c:58
uint16_t quant_matrixes[4][64]
Definition: mjpegdec.h:59
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:431
#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