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