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