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