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