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