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