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 "copy_block.h"
38 #include "internal.h"
39 #include "mjpeg.h"
40 #include "mjpegdec.h"
41 #include "jpeglsdec.h"
42 
43 
44 static int build_vlc(VLC *vlc, const uint8_t *bits_table,
45  const uint8_t *val_table, int nb_codes,
46  int use_static, int is_ac)
47 {
48  uint8_t huff_size[256] = { 0 };
49  uint16_t huff_code[256];
50  uint16_t huff_sym[256];
51  int i;
52 
53  av_assert0(nb_codes <= 256);
54 
55  ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
56 
57  for (i = 0; i < 256; i++)
58  huff_sym[i] = i + 16 * is_ac;
59 
60  if (is_ac)
61  huff_sym[0] = 16 * 256;
62 
63  return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
64  huff_code, 2, 2, huff_sym, 2, 2, use_static);
65 }
66 
68 {
70  avpriv_mjpeg_val_dc, 12, 0, 0);
72  avpriv_mjpeg_val_dc, 12, 0, 0);
81 }
82 
84 {
85  MJpegDecodeContext *s = avctx->priv_data;
86 
87  if (!s->picture_ptr)
88  s->picture_ptr = &s->picture;
90 
91  s->avctx = avctx;
92  ff_hpeldsp_init(&s->hdsp, avctx->flags);
93  ff_dsputil_init(&s->dsp, avctx);
95  s->buffer_size = 0;
96  s->buffer = NULL;
97  s->start_code = -1;
98  s->first_picture = 1;
99  s->got_picture = 0;
100  s->org_height = avctx->coded_height;
102 
104 
105  if (s->extern_huff) {
106  av_log(avctx, AV_LOG_INFO, "using external huffman table\n");
107  init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
108  if (ff_mjpeg_decode_dht(s)) {
109  av_log(avctx, AV_LOG_ERROR,
110  "error using external huffman table, switching back to internal\n");
112  }
113  }
114  if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
115  s->interlace_polarity = 1; /* bottom field first */
116  av_log(avctx, AV_LOG_DEBUG, "bottom field first\n");
117  }
118  if (avctx->codec->id == AV_CODEC_ID_AMV)
119  s->flipped = 1;
120 
121  return 0;
122 }
123 
124 
125 /* quantize tables */
127 {
128  int len, index, i, j;
129 
130  len = get_bits(&s->gb, 16) - 2;
131 
132  while (len >= 65) {
133  int pr = get_bits(&s->gb, 4);
134  if (pr > 1) {
135  av_log(s->avctx, AV_LOG_ERROR, "dqt: invalid precision\n");
136  return AVERROR_INVALIDDATA;
137  }
138  index = get_bits(&s->gb, 4);
139  if (index >= 4)
140  return -1;
141  av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
142  /* read quant table */
143  for (i = 0; i < 64; i++) {
144  j = s->scantable.permutated[i];
145  s->quant_matrixes[index][j] = get_bits(&s->gb, pr ? 16 : 8);
146  }
147 
148  // XXX FIXME finetune, and perhaps add dc too
149  s->qscale[index] = FFMAX(s->quant_matrixes[index][s->scantable.permutated[1]],
150  s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
151  av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n",
152  index, s->qscale[index]);
153  len -= 65;
154  }
155  return 0;
156 }
157 
158 /* decode huffman tables and build VLC decoders */
160 {
161  int len, index, i, class, n, v, code_max;
162  uint8_t bits_table[17];
163  uint8_t val_table[256];
164  int ret = 0;
165 
166  len = get_bits(&s->gb, 16) - 2;
167 
168  while (len > 0) {
169  if (len < 17)
170  return AVERROR_INVALIDDATA;
171  class = get_bits(&s->gb, 4);
172  if (class >= 2)
173  return AVERROR_INVALIDDATA;
174  index = get_bits(&s->gb, 4);
175  if (index >= 4)
176  return AVERROR_INVALIDDATA;
177  n = 0;
178  for (i = 1; i <= 16; i++) {
179  bits_table[i] = get_bits(&s->gb, 8);
180  n += bits_table[i];
181  }
182  len -= 17;
183  if (len < n || n > 256)
184  return AVERROR_INVALIDDATA;
185 
186  code_max = 0;
187  for (i = 0; i < n; i++) {
188  v = get_bits(&s->gb, 8);
189  if (v > code_max)
190  code_max = v;
191  val_table[i] = v;
192  }
193  len -= n;
194 
195  /* build VLC and flush previous vlc if present */
196  ff_free_vlc(&s->vlcs[class][index]);
197  av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
198  class, index, code_max + 1);
199  if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table,
200  code_max + 1, 0, class > 0)) < 0)
201  return ret;
202 
203  if (class > 0) {
204  ff_free_vlc(&s->vlcs[2][index]);
205  if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table,
206  code_max + 1, 0, 0)) < 0)
207  return ret;
208  }
209  }
210  return 0;
211 }
212 
214 {
215  int len, nb_components, i, width, height, pix_fmt_id;
216  int h_count[MAX_COMPONENTS];
217  int v_count[MAX_COMPONENTS];
218 
219  s->cur_scan = 0;
220  s->upscale_h = s->upscale_v = 0;
221 
222  /* XXX: verify len field validity */
223  len = get_bits(&s->gb, 16);
224  s->bits = get_bits(&s->gb, 8);
225 
226  if (s->pegasus_rct)
227  s->bits = 9;
228  if (s->bits == 9 && !s->pegasus_rct)
229  s->rct = 1; // FIXME ugly
230 
231  if (s->bits != 8 && !s->lossless) {
232  av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
233  return -1;
234  }
235 
236  if(s->lossless && s->avctx->lowres){
237  av_log(s->avctx, AV_LOG_ERROR, "lowres is not possible with lossless jpeg\n");
238  return -1;
239  }
240 
241  height = get_bits(&s->gb, 16);
242  width = get_bits(&s->gb, 16);
243 
244  // HACK for odd_height.mov
245  if (s->interlaced && s->width == width && s->height == height + 1)
246  height= s->height;
247 
248  av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
249  if (av_image_check_size(width, height, 0, s->avctx))
250  return AVERROR_INVALIDDATA;
251 
252  nb_components = get_bits(&s->gb, 8);
253  if (nb_components <= 0 ||
254  nb_components > MAX_COMPONENTS)
255  return -1;
256  if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
257  if (nb_components != s->nb_components) {
258  av_log(s->avctx, AV_LOG_ERROR, "nb_components changing in interlaced picture\n");
259  return AVERROR_INVALIDDATA;
260  }
261  }
262  if (s->ls && !(s->bits <= 8 || nb_components == 1)) {
264  "JPEG-LS that is not <= 8 "
265  "bits/component or 16-bit gray");
266  return AVERROR_PATCHWELCOME;
267  }
268  s->nb_components = nb_components;
269  s->h_max = 1;
270  s->v_max = 1;
271  memset(h_count, 0, sizeof(h_count));
272  memset(v_count, 0, sizeof(v_count));
273  for (i = 0; i < nb_components; i++) {
274  /* component id */
275  s->component_id[i] = get_bits(&s->gb, 8) - 1;
276  h_count[i] = get_bits(&s->gb, 4);
277  v_count[i] = get_bits(&s->gb, 4);
278  /* compute hmax and vmax (only used in interleaved case) */
279  if (h_count[i] > s->h_max)
280  s->h_max = h_count[i];
281  if (v_count[i] > s->v_max)
282  s->v_max = v_count[i];
283  if (!h_count[i] || !v_count[i]) {
284  av_log(s->avctx, AV_LOG_ERROR, "h/v_count is 0\n");
285  return -1;
286  }
287  s->quant_index[i] = get_bits(&s->gb, 8);
288  if (s->quant_index[i] >= 4) {
289  av_log(s->avctx, AV_LOG_ERROR, "quant_index is invalid\n");
290  return AVERROR_INVALIDDATA;
291  }
292  av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
293  i, h_count[i], v_count[i],
294  s->component_id[i], s->quant_index[i]);
295  }
296 
297  if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
298  avpriv_report_missing_feature(s->avctx, "Subsampling in JPEG-LS");
299  return AVERROR_PATCHWELCOME;
300  }
301 
302 
303  /* if different size, realloc/alloc picture */
304  if ( width != s->width || height != s->height
305  || memcmp(s->h_count, h_count, sizeof(h_count))
306  || memcmp(s->v_count, v_count, sizeof(v_count))) {
307 
308  s->width = width;
309  s->height = height;
310  memcpy(s->h_count, h_count, sizeof(h_count));
311  memcpy(s->v_count, v_count, sizeof(v_count));
312  s->interlaced = 0;
313  s->got_picture = 0;
314 
315  /* test interlaced mode */
316  if (s->first_picture &&
317  s->org_height != 0 &&
318  s->height < ((s->org_height * 3) / 4)) {
319  s->interlaced = 1;
323  height *= 2;
324  }
325 
326  avcodec_set_dimensions(s->avctx, width, height);
327 
328  s->first_picture = 0;
329  }
330 
331  if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
332  if (s->progressive) {
333  avpriv_request_sample(s->avctx, "progressively coded interlaced picture");
334  return AVERROR_INVALIDDATA;
335  }
336  } else{
337  if (s->v_max == 1 && s->h_max == 1 && s->lossless==1 && nb_components==3)
338  s->rgb = 1;
339  else if (!s->lossless)
340  s->rgb = 0;
341  /* XXX: not complete test ! */
342  pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
343  (s->h_count[1] << 20) | (s->v_count[1] << 16) |
344  (s->h_count[2] << 12) | (s->v_count[2] << 8) |
345  (s->h_count[3] << 4) | s->v_count[3];
346  av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
347  /* NOTE we do not allocate pictures large enough for the possible
348  * padding of h/v_count being 4 */
349  if (!(pix_fmt_id & 0xD0D0D0D0))
350  pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
351  if (!(pix_fmt_id & 0x0D0D0D0D))
352  pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
353 
354  switch (pix_fmt_id) {
355  case 0x11111100:
356  if (s->rgb)
358  else {
359  if (s->component_id[0] == 'Q' && s->component_id[1] == 'F' && s->component_id[2] == 'A') {
361  } else {
364  }
365  }
366  av_assert0(s->nb_components == 3);
367  break;
368  case 0x12121100:
369  case 0x22122100:
372  s->upscale_v = 2;
373  s->upscale_h = (pix_fmt_id == 0x22122100);
374  s->chroma_height = s->height;
375  break;
376  case 0x21211100:
377  case 0x22211200:
380  s->upscale_v = (pix_fmt_id == 0x22211200);
381  s->upscale_h = 2;
382  s->chroma_height = s->height;
383  break;
384  case 0x22221100:
387  s->upscale_v = 2;
388  s->upscale_h = 2;
389  s->chroma_height = s->height / 2;
390  break;
391  case 0x11000000:
392  case 0x13000000:
393  case 0x14000000:
394  case 0x31000000:
395  case 0x33000000:
396  case 0x34000000:
397  case 0x41000000:
398  case 0x43000000:
399  case 0x44000000:
400  if(s->bits <= 8)
402  else
404  break;
405  case 0x12111100:
406  case 0x22211100:
407  case 0x22112100:
410  s->upscale_h = (pix_fmt_id == 0x22211100) * 2 + (pix_fmt_id == 0x22112100);
411  s->chroma_height = s->height / 2;
412  break;
413  case 0x21111100:
416  break;
417  case 0x22121100:
418  case 0x22111200:
421  s->upscale_v = (pix_fmt_id == 0x22121100) + 1;
422  break;
423  case 0x22111100:
426  break;
427  default:
428  av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
429  return AVERROR_PATCHWELCOME;
430  }
431  if ((s->upscale_h || s->upscale_v) && s->avctx->lowres) {
432  av_log(s->avctx, AV_LOG_ERROR, "lowres not supported for weird subsampling\n");
433  return AVERROR_PATCHWELCOME;
434  }
435  if (s->ls) {
436  s->upscale_h = s->upscale_v = 0;
437  if (s->nb_components > 1)
439  else if (s->bits <= 8)
441  else
443  }
444 
447  return -1;
449  s->picture_ptr->key_frame = 1;
450  s->got_picture = 1;
451 
452  for (i = 0; i < 3; i++)
453  s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
454 
455  av_dlog(s->avctx, "%d %d %d %d %d %d\n",
456  s->width, s->height, s->linesize[0], s->linesize[1],
457  s->interlaced, s->avctx->height);
458 
459  if (len != (8 + (3 * nb_components)))
460  av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
461  }
462 
463  if (s->rgb && !s->lossless && !s->ls) {
464  av_log(s->avctx, AV_LOG_ERROR, "Unsupported coding and pixel format combination\n");
465  return AVERROR_PATCHWELCOME;
466  }
467 
468  /* totally blank picture as progressive JPEG will only add details to it */
469  if (s->progressive) {
470  int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8);
471  int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
472  for (i = 0; i < s->nb_components; i++) {
473  int size = bw * bh * s->h_count[i] * s->v_count[i];
474  av_freep(&s->blocks[i]);
475  av_freep(&s->last_nnz[i]);
476  s->blocks[i] = av_malloc(size * sizeof(**s->blocks));
477  s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz));
478  s->block_stride[i] = bw * s->h_count[i];
479  }
480  memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
481  }
482  return 0;
483 }
484 
485 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
486 {
487  int code;
488  code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
489  if (code < 0) {
491  "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
492  0, dc_index, &s->vlcs[0][dc_index]);
493  return 0xffff;
494  }
495 
496  if (code)
497  return get_xbits(&s->gb, code);
498  else
499  return 0;
500 }
501 
502 /* decode block and dequantize */
503 static int decode_block(MJpegDecodeContext *s, int16_t *block, int component,
504  int dc_index, int ac_index, int16_t *quant_matrix)
505 {
506  int code, i, j, level, val;
507 
508  /* DC coef */
509  val = mjpeg_decode_dc(s, dc_index);
510  if (val == 0xffff) {
511  av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
512  return AVERROR_INVALIDDATA;
513  }
514  val = val * quant_matrix[0] + s->last_dc[component];
515  s->last_dc[component] = val;
516  block[0] = val;
517  /* AC coefs */
518  i = 0;
519  {OPEN_READER(re, &s->gb);
520  do {
521  UPDATE_CACHE(re, &s->gb);
522  GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
523 
524  i += ((unsigned)code) >> 4;
525  code &= 0xf;
526  if (code) {
527  if (code > MIN_CACHE_BITS - 16)
528  UPDATE_CACHE(re, &s->gb);
529 
530  {
531  int cache = GET_CACHE(re, &s->gb);
532  int sign = (~cache) >> 31;
533  level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
534  }
535 
536  LAST_SKIP_BITS(re, &s->gb, code);
537 
538  if (i > 63) {
539  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
540  return AVERROR_INVALIDDATA;
541  }
542  j = s->scantable.permutated[i];
543  block[j] = level * quant_matrix[j];
544  }
545  } while (i < 63);
546  CLOSE_READER(re, &s->gb);}
547 
548  return 0;
549 }
550 
551 static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block,
552  int component, int dc_index,
553  int16_t *quant_matrix, int Al)
554 {
555  int val;
556  s->dsp.clear_block(block);
557  val = mjpeg_decode_dc(s, dc_index);
558  if (val == 0xffff) {
559  av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
560  return AVERROR_INVALIDDATA;
561  }
562  val = (val * quant_matrix[0] << Al) + s->last_dc[component];
563  s->last_dc[component] = val;
564  block[0] = val;
565  return 0;
566 }
567 
568 /* decode block and dequantize - progressive JPEG version */
569 static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
570  uint8_t *last_nnz, int ac_index,
571  int16_t *quant_matrix,
572  int ss, int se, int Al, int *EOBRUN)
573 {
574  int code, i, j, level, val, run;
575 
576  if (*EOBRUN) {
577  (*EOBRUN)--;
578  return 0;
579  }
580 
581  {
582  OPEN_READER(re, &s->gb);
583  for (i = ss; ; i++) {
584  UPDATE_CACHE(re, &s->gb);
585  GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
586 
587  run = ((unsigned) code) >> 4;
588  code &= 0xF;
589  if (code) {
590  i += run;
591  if (code > MIN_CACHE_BITS - 16)
592  UPDATE_CACHE(re, &s->gb);
593 
594  {
595  int cache = GET_CACHE(re, &s->gb);
596  int sign = (~cache) >> 31;
597  level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
598  }
599 
600  LAST_SKIP_BITS(re, &s->gb, code);
601 
602  if (i >= se) {
603  if (i == se) {
604  j = s->scantable.permutated[se];
605  block[j] = level * quant_matrix[j] << Al;
606  break;
607  }
608  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
609  return AVERROR_INVALIDDATA;
610  }
611  j = s->scantable.permutated[i];
612  block[j] = level * quant_matrix[j] << Al;
613  } else {
614  if (run == 0xF) {// ZRL - skip 15 coefficients
615  i += 15;
616  if (i >= se) {
617  av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
618  return AVERROR_INVALIDDATA;
619  }
620  } else {
621  val = (1 << run);
622  if (run) {
623  UPDATE_CACHE(re, &s->gb);
624  val += NEG_USR32(GET_CACHE(re, &s->gb), run);
625  LAST_SKIP_BITS(re, &s->gb, run);
626  }
627  *EOBRUN = val - 1;
628  break;
629  }
630  }
631  }
632  CLOSE_READER(re, &s->gb);
633  }
634 
635  if (i > *last_nnz)
636  *last_nnz = i;
637 
638  return 0;
639 }
640 
641 #define REFINE_BIT(j) { \
642  UPDATE_CACHE(re, &s->gb); \
643  sign = block[j] >> 15; \
644  block[j] += SHOW_UBITS(re, &s->gb, 1) * \
645  ((quant_matrix[j] ^ sign) - sign) << Al; \
646  LAST_SKIP_BITS(re, &s->gb, 1); \
647 }
648 
649 #define ZERO_RUN \
650 for (; ; i++) { \
651  if (i > last) { \
652  i += run; \
653  if (i > se) { \
654  av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
655  return -1; \
656  } \
657  break; \
658  } \
659  j = s->scantable.permutated[i]; \
660  if (block[j]) \
661  REFINE_BIT(j) \
662  else if (run-- == 0) \
663  break; \
664 }
665 
666 /* decode block and dequantize - progressive JPEG refinement pass */
667 static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block,
668  uint8_t *last_nnz,
669  int ac_index, int16_t *quant_matrix,
670  int ss, int se, int Al, int *EOBRUN)
671 {
672  int code, i = ss, j, sign, val, run;
673  int last = FFMIN(se, *last_nnz);
674 
675  OPEN_READER(re, &s->gb);
676  if (*EOBRUN) {
677  (*EOBRUN)--;
678  } else {
679  for (; ; i++) {
680  UPDATE_CACHE(re, &s->gb);
681  GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
682 
683  if (code & 0xF) {
684  run = ((unsigned) code) >> 4;
685  UPDATE_CACHE(re, &s->gb);
686  val = SHOW_UBITS(re, &s->gb, 1);
687  LAST_SKIP_BITS(re, &s->gb, 1);
688  ZERO_RUN;
689  j = s->scantable.permutated[i];
690  val--;
691  block[j] = ((quant_matrix[j]^val) - val) << Al;
692  if (i == se) {
693  if (i > *last_nnz)
694  *last_nnz = i;
695  CLOSE_READER(re, &s->gb);
696  return 0;
697  }
698  } else {
699  run = ((unsigned) code) >> 4;
700  if (run == 0xF) {
701  ZERO_RUN;
702  } else {
703  val = run;
704  run = (1 << run);
705  if (val) {
706  UPDATE_CACHE(re, &s->gb);
707  run += SHOW_UBITS(re, &s->gb, val);
708  LAST_SKIP_BITS(re, &s->gb, val);
709  }
710  *EOBRUN = run - 1;
711  break;
712  }
713  }
714  }
715 
716  if (i > *last_nnz)
717  *last_nnz = i;
718  }
719 
720  for (; i <= last; i++) {
721  j = s->scantable.permutated[i];
722  if (block[j])
723  REFINE_BIT(j)
724  }
725  CLOSE_READER(re, &s->gb);
726 
727  return 0;
728 }
729 #undef REFINE_BIT
730 #undef ZERO_RUN
731 
732 static void handle_rstn(MJpegDecodeContext *s, int nb_components)
733 {
734  int i;
735  if (s->restart_interval) {
736  s->restart_count--;
737  if(s->restart_count == 0 && s->avctx->codec_id == AV_CODEC_ID_THP){
738  align_get_bits(&s->gb);
739  for (i = 0; i < nb_components; i++) /* reset dc */
740  s->last_dc[i] = 1024;
741  }
742 
743  i = 8 + ((-get_bits_count(&s->gb)) & 7);
744  /* skip RSTn */
745  if (s->restart_count == 0) {
746  if( show_bits(&s->gb, i) == (1 << i) - 1
747  || show_bits(&s->gb, i) == 0xFF) {
748  int pos = get_bits_count(&s->gb);
749  align_get_bits(&s->gb);
750  while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
751  skip_bits(&s->gb, 8);
752  if (get_bits_left(&s->gb) >= 8 && (get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
753  for (i = 0; i < nb_components; i++) /* reset dc */
754  s->last_dc[i] = 1024;
755  } else
756  skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
757  }
758  }
759  }
760 }
761 
762 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform)
763 {
764  int i, mb_x, mb_y;
765  uint16_t (*buffer)[4];
766  int left[3], top[3], topleft[3];
767  const int linesize = s->linesize[0];
768  const int mask = (1 << s->bits) - 1;
769  int resync_mb_y = 0;
770  int resync_mb_x = 0;
771 
773 
775  (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
776  buffer = s->ljpeg_buffer;
777 
778  for (i = 0; i < 3; i++)
779  buffer[0][i] = 1 << (s->bits - 1);
780 
781  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
782  uint8_t *ptr = s->picture.data[0] + (linesize * mb_y);
783 
784  if (s->interlaced && s->bottom_field)
785  ptr += linesize >> 1;
786 
787  for (i = 0; i < 3; i++)
788  top[i] = left[i] = topleft[i] = buffer[0][i];
789 
790  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
791  int modified_predictor = predictor;
792 
793  if (s->restart_interval && !s->restart_count){
795  resync_mb_x = mb_x;
796  resync_mb_y = mb_y;
797  for(i=0; i<3; i++)
798  top[i] = left[i]= topleft[i]= 1 << (s->bits - 1);
799  }
800  if (mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || !mb_x)
801  modified_predictor = 1;
802 
803  for (i=0;i<nb_components;i++) {
804  int pred, dc;
805 
806  topleft[i] = top[i];
807  top[i] = buffer[mb_x][i];
808 
809  PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
810 
811  dc = mjpeg_decode_dc(s, s->dc_index[i]);
812  if(dc == 0xFFFF)
813  return -1;
814 
815  left[i] = buffer[mb_x][i] =
816  mask & (pred + (dc << point_transform));
817  }
818 
819  if (s->restart_interval && !--s->restart_count) {
820  align_get_bits(&s->gb);
821  skip_bits(&s->gb, 16); /* skip RSTn */
822  }
823  }
824 
825  if (s->rct) {
826  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
827  ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
828  ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
829  ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
830  }
831  } else if (s->pegasus_rct) {
832  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
833  ptr[3*mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
834  ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
835  ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
836  }
837  } else {
838  for(i=0; i<nb_components; i++) {
839  int c= s->comp_index[i];
840  for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
841  ptr[3*mb_x+2-c] = buffer[mb_x][i];
842  }
843  }
844  }
845  }
846  return 0;
847 }
848 
849 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int nb_components, int predictor,
850  int point_transform)
851 {
852  int i, mb_x, mb_y;
853  int bits= (s->bits+7)&~7;
854  int resync_mb_y = 0;
855  int resync_mb_x = 0;
856 
857  point_transform += bits - s->bits;
858 
859  av_assert0(nb_components>=1 && nb_components<=3);
860 
861  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
862  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
863  if (s->restart_interval && !s->restart_count){
865  resync_mb_x = mb_x;
866  resync_mb_y = mb_y;
867  }
868 
869  if(!mb_x || mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x || s->interlaced){
870  int toprow = mb_y == resync_mb_y || mb_y == resync_mb_y+1 && mb_x < resync_mb_x;
871  int leftcol = !mb_x || mb_y == resync_mb_y && mb_x == resync_mb_x;
872  for (i = 0; i < nb_components; i++) {
873  uint8_t *ptr;
874  uint16_t *ptr16;
875  int n, h, v, x, y, c, j, linesize;
876  n = s->nb_blocks[i];
877  c = s->comp_index[i];
878  h = s->h_scount[i];
879  v = s->v_scount[i];
880  x = 0;
881  y = 0;
882  linesize= s->linesize[c];
883 
884  if(bits>8) linesize /= 2;
885 
886  for(j=0; j<n; j++) {
887  int pred, dc;
888 
889  dc = mjpeg_decode_dc(s, s->dc_index[i]);
890  if(dc == 0xFFFF)
891  return -1;
892  if(bits<=8){
893  ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
894  if(y==0 && toprow){
895  if(x==0 && leftcol){
896  pred= 1 << (bits - 1);
897  }else{
898  pred= ptr[-1];
899  }
900  }else{
901  if(x==0 && leftcol){
902  pred= ptr[-linesize];
903  }else{
904  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
905  }
906  }
907 
908  if (s->interlaced && s->bottom_field)
909  ptr += linesize >> 1;
910  pred &= (-1)<<(8-s->bits);
911  *ptr= pred + (dc << point_transform);
912  }else{
913  ptr16 = (uint16_t*)(s->picture.data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
914  if(y==0 && toprow){
915  if(x==0 && leftcol){
916  pred= 1 << (bits - 1);
917  }else{
918  pred= ptr16[-1];
919  }
920  }else{
921  if(x==0 && leftcol){
922  pred= ptr16[-linesize];
923  }else{
924  PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
925  }
926  }
927 
928  if (s->interlaced && s->bottom_field)
929  ptr16 += linesize >> 1;
930  pred &= (-1)<<(16-s->bits);
931  *ptr16= pred + (dc << point_transform);
932  }
933  if (++x == h) {
934  x = 0;
935  y++;
936  }
937  }
938  }
939  } else {
940  for (i = 0; i < nb_components; i++) {
941  uint8_t *ptr;
942  uint16_t *ptr16;
943  int n, h, v, x, y, c, j, linesize, dc;
944  n = s->nb_blocks[i];
945  c = s->comp_index[i];
946  h = s->h_scount[i];
947  v = s->v_scount[i];
948  x = 0;
949  y = 0;
950  linesize = s->linesize[c];
951 
952  if(bits>8) linesize /= 2;
953 
954  for (j = 0; j < n; j++) {
955  int pred;
956 
957  dc = mjpeg_decode_dc(s, s->dc_index[i]);
958  if(dc == 0xFFFF)
959  return -1;
960  if(bits<=8){
961  ptr = s->picture.data[c] +
962  (linesize * (v * mb_y + y)) +
963  (h * mb_x + x); //FIXME optimize this crap
964  PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
965 
966  pred &= (-1)<<(8-s->bits);
967  *ptr = pred + (dc << point_transform);
968  }else{
969  ptr16 = (uint16_t*)(s->picture.data[c] + 2*(linesize * (v * mb_y + y)) + 2*(h * mb_x + x)); //FIXME optimize this crap
970  PREDICT(pred, ptr16[-linesize-1], ptr16[-linesize], ptr16[-1], predictor);
971 
972  pred &= (-1)<<(16-s->bits);
973  *ptr16= pred + (dc << point_transform);
974  }
975 
976  if (++x == h) {
977  x = 0;
978  y++;
979  }
980  }
981  }
982  }
983  if (s->restart_interval && !--s->restart_count) {
984  align_get_bits(&s->gb);
985  skip_bits(&s->gb, 16); /* skip RSTn */
986  }
987  }
988  }
989  return 0;
990 }
991 
993  uint8_t *dst, const uint8_t *src,
994  int linesize, int lowres)
995 {
996  switch (lowres) {
997  case 0: s->hdsp.put_pixels_tab[1][0](dst, src, linesize, 8);
998  break;
999  case 1: copy_block4(dst, src, linesize, linesize, 4);
1000  break;
1001  case 2: copy_block2(dst, src, linesize, linesize, 2);
1002  break;
1003  case 3: *dst = *src;
1004  break;
1005  }
1006 }
1007 
1008 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
1009  int Al, const uint8_t *mb_bitmask,
1010  const AVFrame *reference)
1011 {
1012  int i, mb_x, mb_y;
1014  const uint8_t *reference_data[MAX_COMPONENTS];
1015  int linesize[MAX_COMPONENTS];
1016  GetBitContext mb_bitmask_gb;
1017 
1018  if (mb_bitmask)
1019  init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
1020 
1021  if (s->flipped && s->avctx->lowres) {
1022  av_log(s->avctx, AV_LOG_ERROR, "Can not flip image with lowres\n");
1023  s->flipped = 0;
1024  }
1025 
1026  for (i = 0; i < nb_components; i++) {
1027  int c = s->comp_index[i];
1028  data[c] = s->picture_ptr->data[c];
1029  reference_data[c] = reference ? reference->data[c] : NULL;
1030  linesize[c] = s->linesize[c];
1031  s->coefs_finished[c] |= 1;
1032  if (s->flipped && !(s->avctx->flags & CODEC_FLAG_EMU_EDGE)) {
1033  // picture should be flipped upside-down for this codec
1034  int offset = (linesize[c] * (s->v_scount[i] *
1035  (8 * s->mb_height - ((s->height / s->v_max) & 7)) - 1));
1036  data[c] += offset;
1037  reference_data[c] += offset;
1038  linesize[c] *= -1;
1039  }
1040  }
1041 
1042  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1043  for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
1044  const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
1045 
1046  if (s->restart_interval && !s->restart_count)
1048 
1049  if (get_bits_left(&s->gb) < 0) {
1050  av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
1051  -get_bits_left(&s->gb));
1052  return AVERROR_INVALIDDATA;
1053  }
1054  for (i = 0; i < nb_components; i++) {
1055  uint8_t *ptr;
1056  int n, h, v, x, y, c, j;
1057  int block_offset;
1058  n = s->nb_blocks[i];
1059  c = s->comp_index[i];
1060  h = s->h_scount[i];
1061  v = s->v_scount[i];
1062  x = 0;
1063  y = 0;
1064  for (j = 0; j < n; j++) {
1065  block_offset = (((linesize[c] * (v * mb_y + y) * 8) +
1066  (h * mb_x + x) * 8) >> s->avctx->lowres);
1067 
1068  if (s->interlaced && s->bottom_field)
1069  block_offset += linesize[c] >> 1;
1070  ptr = data[c] + block_offset;
1071  if (!s->progressive) {
1072  if (copy_mb)
1073  mjpeg_copy_block(s, ptr, reference_data[c] + block_offset,
1074  linesize[c], s->avctx->lowres);
1075 
1076  else {
1077  s->dsp.clear_block(s->block);
1078  if (decode_block(s, s->block, i,
1079  s->dc_index[i], s->ac_index[i],
1080  s->quant_matrixes[s->quant_index[c]]) < 0) {
1082  "error y=%d x=%d\n", mb_y, mb_x);
1083  return AVERROR_INVALIDDATA;
1084  }
1085  s->dsp.idct_put(ptr, linesize[c], s->block);
1086  }
1087  } else {
1088  int block_idx = s->block_stride[c] * (v * mb_y + y) +
1089  (h * mb_x + x);
1090  int16_t *block = s->blocks[c][block_idx];
1091  if (Ah)
1092  block[0] += get_bits1(&s->gb) *
1093  s->quant_matrixes[s->quant_index[c]][0] << Al;
1094  else if (decode_dc_progressive(s, block, i, s->dc_index[i],
1095  s->quant_matrixes[s->quant_index[c]],
1096  Al) < 0) {
1098  "error y=%d x=%d\n", mb_y, mb_x);
1099  return AVERROR_INVALIDDATA;
1100  }
1101  }
1102  av_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
1103  av_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
1104  mb_x, mb_y, x, y, c, s->bottom_field,
1105  (v * mb_y + y) * 8, (h * mb_x + x) * 8);
1106  if (++x == h) {
1107  x = 0;
1108  y++;
1109  }
1110  }
1111  }
1112 
1113  handle_rstn(s, nb_components);
1114  }
1115  }
1116  return 0;
1117 }
1118 
1120  int se, int Ah, int Al)
1121 {
1122  int mb_x, mb_y;
1123  int EOBRUN = 0;
1124  int c = s->comp_index[0];
1125  uint8_t *data = s->picture.data[c];
1126  int linesize = s->linesize[c];
1127  int last_scan = 0;
1128  int16_t *quant_matrix = s->quant_matrixes[s->quant_index[c]];
1129 
1130  if (se > 63) {
1131  av_log(s->avctx, AV_LOG_ERROR, "SE %d is too large\n", se);
1132  return AVERROR_INVALIDDATA;
1133  }
1134 
1135  if (!Al) {
1136  s->coefs_finished[c] |= (1LL << (se + 1)) - (1LL << ss);
1137  last_scan = !~s->coefs_finished[c];
1138  }
1139 
1140  if (s->interlaced && s->bottom_field)
1141  data += linesize >> 1;
1142 
1143  s->restart_count = 0;
1144 
1145  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1146  uint8_t *ptr = data + (mb_y * linesize * 8 >> s->avctx->lowres);
1147  int block_idx = mb_y * s->block_stride[c];
1148  int16_t (*block)[64] = &s->blocks[c][block_idx];
1149  uint8_t *last_nnz = &s->last_nnz[c][block_idx];
1150  for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
1151  int ret;
1152  if (s->restart_interval && !s->restart_count)
1154 
1155  if (Ah)
1156  ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
1157  quant_matrix, ss, se, Al, &EOBRUN);
1158  else
1159  ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
1160  quant_matrix, ss, se, Al, &EOBRUN);
1161  if (ret < 0) {
1163  "error y=%d x=%d\n", mb_y, mb_x);
1164  return AVERROR_INVALIDDATA;
1165  }
1166 
1167  if (last_scan) {
1168  s->dsp.idct_put(ptr, linesize, *block);
1169  ptr += 8 >> s->avctx->lowres;
1170  }
1171  handle_rstn(s, 0);
1172  }
1173  }
1174  return 0;
1175 }
1176 
1178  const AVFrame *reference)
1179 {
1180  int len, nb_components, i, h, v, predictor, point_transform;
1181  int index, id, ret;
1182  const int block_size = s->lossless ? 1 : 8;
1183  int ilv, prev_shift;
1184 
1185  if (!s->got_picture) {
1187  "Can not process SOS before SOF, skipping\n");
1188  return -1;
1189  }
1190 
1191  av_assert0(s->picture_ptr->data[0]);
1192  /* XXX: verify len field validity */
1193  len = get_bits(&s->gb, 16);
1194  nb_components = get_bits(&s->gb, 8);
1195  if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
1197  "decode_sos: nb_components (%d) unsupported\n", nb_components);
1198  return AVERROR_PATCHWELCOME;
1199  }
1200  if (len != 6 + 2 * nb_components) {
1201  av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
1202  return AVERROR_INVALIDDATA;
1203  }
1204  for (i = 0; i < nb_components; i++) {
1205  id = get_bits(&s->gb, 8) - 1;
1206  av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1207  /* find component index */
1208  for (index = 0; index < s->nb_components; index++)
1209  if (id == s->component_id[index])
1210  break;
1211  if (index == s->nb_components) {
1213  "decode_sos: index(%d) out of components\n", index);
1214  return AVERROR_INVALIDDATA;
1215  }
1216  /* Metasoft MJPEG codec has Cb and Cr swapped */
1217  if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1218  && nb_components == 3 && s->nb_components == 3 && i)
1219  index = 3 - i;
1220 
1221  if(nb_components == 3 && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P)
1222  index = (i+2)%3;
1223  if(nb_components == 1 && s->nb_components == 3 && s->avctx->pix_fmt == AV_PIX_FMT_GBR24P)
1224  index = (index+2)%3;
1225 
1226  s->comp_index[i] = index;
1227 
1228  s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1229  s->h_scount[i] = s->h_count[index];
1230  s->v_scount[i] = s->v_count[index];
1231 
1232  s->dc_index[i] = get_bits(&s->gb, 4);
1233  s->ac_index[i] = get_bits(&s->gb, 4);
1234 
1235  if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1236  s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1237  goto out_of_range;
1238  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))
1239  goto out_of_range;
1240  }
1241 
1242  predictor = get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1243  ilv = get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1244  if(s->avctx->codec_tag != AV_RL32("CJPG")){
1245  prev_shift = get_bits(&s->gb, 4); /* Ah */
1246  point_transform = get_bits(&s->gb, 4); /* Al */
1247  }else
1248  prev_shift = point_transform = 0;
1249 
1250  if (nb_components > 1) {
1251  /* interleaved stream */
1252  s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1253  s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1254  } else if (!s->ls) { /* skip this for JPEG-LS */
1255  h = s->h_max / s->h_scount[0];
1256  v = s->v_max / s->v_scount[0];
1257  s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1258  s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1259  s->nb_blocks[0] = 1;
1260  s->h_scount[0] = 1;
1261  s->v_scount[0] = 1;
1262  }
1263 
1264  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1265  av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d skip:%d %s comp:%d\n",
1266  s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1267  predictor, point_transform, ilv, s->bits, s->mjpb_skiptosod,
1268  s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""), nb_components);
1269 
1270 
1271  /* mjpeg-b can have padding bytes between sos and image data, skip them */
1272  for (i = s->mjpb_skiptosod; i > 0; i--)
1273  skip_bits(&s->gb, 8);
1274 
1275 next_field:
1276  for (i = 0; i < nb_components; i++)
1277  s->last_dc[i] = 1024;
1278 
1279  if (s->lossless) {
1280  av_assert0(s->picture_ptr == &s->picture);
1281  if (CONFIG_JPEGLS_DECODER && s->ls) {
1282 // for () {
1283 // reset_ls_coding_parameters(s, 0);
1284 
1285  if ((ret = ff_jpegls_decode_picture(s, predictor,
1286  point_transform, ilv)) < 0)
1287  return ret;
1288  } else {
1289  if (s->rgb) {
1290  if ((ret = ljpeg_decode_rgb_scan(s, nb_components, predictor, point_transform)) < 0)
1291  return ret;
1292  } else {
1293  if ((ret = ljpeg_decode_yuv_scan(s, nb_components, predictor, point_transform)) < 0)
1294  return ret;
1295  }
1296  }
1297  } else {
1298  if (s->progressive && predictor) {
1299  av_assert0(s->picture_ptr == &s->picture);
1300  if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor,
1301  ilv, prev_shift,
1302  point_transform)) < 0)
1303  return ret;
1304  } else {
1305  if ((ret = mjpeg_decode_scan(s, nb_components,
1306  prev_shift, point_transform,
1307  mb_bitmask, reference)) < 0)
1308  return ret;
1309  }
1310  }
1311 
1312  if (s->interlaced &&
1313  get_bits_left(&s->gb) > 32 &&
1314  show_bits(&s->gb, 8) == 0xFF) {
1315  GetBitContext bak = s->gb;
1316  align_get_bits(&bak);
1317  if (show_bits(&bak, 16) == 0xFFD1) {
1318  av_log(s->avctx, AV_LOG_DEBUG, "AVRn interlaced picture marker found\n");
1319  s->gb = bak;
1320  skip_bits(&s->gb, 16);
1321  s->bottom_field ^= 1;
1322 
1323  goto next_field;
1324  }
1325  }
1326 
1327  emms_c();
1328  return 0;
1329  out_of_range:
1330  av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1331  return AVERROR_INVALIDDATA;
1332 }
1333 
1335 {
1336  if (get_bits(&s->gb, 16) != 4)
1337  return AVERROR_INVALIDDATA;
1338  s->restart_interval = get_bits(&s->gb, 16);
1339  s->restart_count = 0;
1340  av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1341  s->restart_interval);
1342 
1343  return 0;
1344 }
1345 
1347 {
1348  int len, id, i;
1349 
1350  len = get_bits(&s->gb, 16);
1351  if (len < 5)
1352  return AVERROR_INVALIDDATA;
1353  if (8 * len > get_bits_left(&s->gb))
1354  return AVERROR_INVALIDDATA;
1355 
1356  id = get_bits_long(&s->gb, 32);
1357  len -= 6;
1358 
1359  if (s->avctx->debug & FF_DEBUG_STARTCODE)
1360  av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
1361 
1362  /* Buggy AVID, it puts EOI only at every 10th frame. */
1363  /* Also, this fourcc is used by non-avid files too, it holds some
1364  information, but it's always present in AVID-created files. */
1365  if (id == AV_RB32("AVI1")) {
1366  /* structure:
1367  4bytes AVI1
1368  1bytes polarity
1369  1bytes always zero
1370  4bytes field_size
1371  4bytes field_size_less_padding
1372  */
1373  s->buggy_avid = 1;
1374  i = get_bits(&s->gb, 8); len--;
1375  av_log(s->avctx, AV_LOG_DEBUG, "polarity %d\n", i);
1376 #if 0
1377  skip_bits(&s->gb, 8);
1378  skip_bits(&s->gb, 32);
1379  skip_bits(&s->gb, 32);
1380  len -= 10;
1381 #endif
1382  goto out;
1383  }
1384 
1385 // len -= 2;
1386 
1387  if (id == AV_RB32("JFIF")) {
1388  int t_w, t_h, v1, v2;
1389  skip_bits(&s->gb, 8); /* the trailing zero-byte */
1390  v1 = get_bits(&s->gb, 8);
1391  v2 = get_bits(&s->gb, 8);
1392  skip_bits(&s->gb, 8);
1393 
1394  s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1395  s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1396 
1397  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1398  av_log(s->avctx, AV_LOG_INFO,
1399  "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1400  v1, v2,
1403 
1404  t_w = get_bits(&s->gb, 8);
1405  t_h = get_bits(&s->gb, 8);
1406  if (t_w && t_h) {
1407  /* skip thumbnail */
1408  if (len -10 - (t_w * t_h * 3) > 0)
1409  len -= t_w * t_h * 3;
1410  }
1411  len -= 10;
1412  goto out;
1413  }
1414 
1415  if (id == AV_RB32("Adob") && (get_bits(&s->gb, 8) == 'e')) {
1416  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1417  av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
1418  skip_bits(&s->gb, 16); /* version */
1419  skip_bits(&s->gb, 16); /* flags0 */
1420  skip_bits(&s->gb, 16); /* flags1 */
1421  skip_bits(&s->gb, 8); /* transform */
1422  len -= 7;
1423  goto out;
1424  }
1425 
1426  if (id == AV_RB32("LJIF")) {
1427  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1428  av_log(s->avctx, AV_LOG_INFO,
1429  "Pegasus lossless jpeg header found\n");
1430  skip_bits(&s->gb, 16); /* version ? */
1431  skip_bits(&s->gb, 16); /* unknown always 0? */
1432  skip_bits(&s->gb, 16); /* unknown always 0? */
1433  skip_bits(&s->gb, 16); /* unknown always 0? */
1434  switch (get_bits(&s->gb, 8)) {
1435  case 1:
1436  s->rgb = 1;
1437  s->pegasus_rct = 0;
1438  break;
1439  case 2:
1440  s->rgb = 1;
1441  s->pegasus_rct = 1;
1442  break;
1443  default:
1444  av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
1445  }
1446  len -= 9;
1447  goto out;
1448  }
1449 
1450  /* Apple MJPEG-A */
1451  if ((s->start_code == APP1) && (len > (0x28 - 8))) {
1452  id = get_bits_long(&s->gb, 32);
1453  len -= 4;
1454  /* Apple MJPEG-A */
1455  if (id == AV_RB32("mjpg")) {
1456 #if 0
1457  skip_bits(&s->gb, 32); /* field size */
1458  skip_bits(&s->gb, 32); /* pad field size */
1459  skip_bits(&s->gb, 32); /* next off */
1460  skip_bits(&s->gb, 32); /* quant off */
1461  skip_bits(&s->gb, 32); /* huff off */
1462  skip_bits(&s->gb, 32); /* image off */
1463  skip_bits(&s->gb, 32); /* scan off */
1464  skip_bits(&s->gb, 32); /* data off */
1465 #endif
1466  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1467  av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1468  }
1469  }
1470 
1471 out:
1472  /* slow but needed for extreme adobe jpegs */
1473  if (len < 0)
1475  "mjpeg: error, decode_app parser read over the end\n");
1476  while (--len > 0)
1477  skip_bits(&s->gb, 8);
1478 
1479  return 0;
1480 }
1481 
1483 {
1484  int len = get_bits(&s->gb, 16);
1485  if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
1486  char *cbuf = av_malloc(len - 1);
1487  if (cbuf) {
1488  int i;
1489  for (i = 0; i < len - 2; i++)
1490  cbuf[i] = get_bits(&s->gb, 8);
1491  if (i > 0 && cbuf[i - 1] == '\n')
1492  cbuf[i - 1] = 0;
1493  else
1494  cbuf[i] = 0;
1495 
1496  if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1497  av_log(s->avctx, AV_LOG_INFO, "comment: '%s'\n", cbuf);
1498 
1499  /* buggy avid, it puts EOI only at every 10th frame */
1500  if (!strcmp(cbuf, "AVID")) {
1501  s->buggy_avid = 1;
1502  } else if (!strcmp(cbuf, "CS=ITU601"))
1503  s->cs_itu601 = 1;
1504  else if ((len > 31 && !strncmp(cbuf, "Intel(R) JPEG Library, version 1", 32)) ||
1505  (len > 19 && !strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
1506  s->flipped = 1;
1507 
1508  av_free(cbuf);
1509  }
1510  }
1511 
1512  return 0;
1513 }
1514 
1515 /* return the 8 bit start code value and update the search
1516  state. Return -1 if no start code found */
1517 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
1518 {
1519  const uint8_t *buf_ptr;
1520  unsigned int v, v2;
1521  int val;
1522  int skipped = 0;
1523 
1524  buf_ptr = *pbuf_ptr;
1525  while (buf_ptr < buf_end) {
1526  v = *buf_ptr++;
1527  v2 = *buf_ptr;
1528  if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1529  val = *buf_ptr++;
1530  goto found;
1531  }
1532  skipped++;
1533  }
1534  val = -1;
1535 found:
1536  av_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
1537  *pbuf_ptr = buf_ptr;
1538  return val;
1539 }
1540 
1542  const uint8_t **buf_ptr, const uint8_t *buf_end,
1543  const uint8_t **unescaped_buf_ptr,
1544  int *unescaped_buf_size)
1545 {
1546  int start_code;
1547  start_code = find_marker(buf_ptr, buf_end);
1548 
1549  av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
1550  if (!s->buffer)
1551  return AVERROR(ENOMEM);
1552 
1553  /* unescape buffer of SOS, use special treatment for JPEG-LS */
1554  if (start_code == SOS && !s->ls) {
1555  const uint8_t *src = *buf_ptr;
1556  uint8_t *dst = s->buffer;
1557 
1558  while (src < buf_end) {
1559  uint8_t x = *(src++);
1560 
1561  *(dst++) = x;
1562  if (s->avctx->codec_id != AV_CODEC_ID_THP) {
1563  if (x == 0xff) {
1564  while (src < buf_end && x == 0xff)
1565  x = *(src++);
1566 
1567  if (x >= 0xd0 && x <= 0xd7)
1568  *(dst++) = x;
1569  else if (x)
1570  break;
1571  }
1572  }
1573  }
1574  *unescaped_buf_ptr = s->buffer;
1575  *unescaped_buf_size = dst - s->buffer;
1576  memset(s->buffer + *unescaped_buf_size, 0,
1578 
1579  av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
1580  (buf_end - *buf_ptr) - (dst - s->buffer));
1581  } else if (start_code == SOS && s->ls) {
1582  const uint8_t *src = *buf_ptr;
1583  uint8_t *dst = s->buffer;
1584  int bit_count = 0;
1585  int t = 0, b = 0;
1586  PutBitContext pb;
1587 
1588  s->cur_scan++;
1589 
1590  /* find marker */
1591  while (src + t < buf_end) {
1592  uint8_t x = src[t++];
1593  if (x == 0xff) {
1594  while ((src + t < buf_end) && x == 0xff)
1595  x = src[t++];
1596  if (x & 0x80) {
1597  t -= FFMIN(2, t);
1598  break;
1599  }
1600  }
1601  }
1602  bit_count = t * 8;
1603  init_put_bits(&pb, dst, t);
1604 
1605  /* unescape bitstream */
1606  while (b < t) {
1607  uint8_t x = src[b++];
1608  put_bits(&pb, 8, x);
1609  if (x == 0xFF) {
1610  x = src[b++];
1611  put_bits(&pb, 7, x);
1612  bit_count--;
1613  }
1614  }
1615  flush_put_bits(&pb);
1616 
1617  *unescaped_buf_ptr = dst;
1618  *unescaped_buf_size = (bit_count + 7) >> 3;
1619  memset(s->buffer + *unescaped_buf_size, 0,
1621  } else {
1622  *unescaped_buf_ptr = *buf_ptr;
1623  *unescaped_buf_size = buf_end - *buf_ptr;
1624  }
1625 
1626  return start_code;
1627 }
1628 
1629 int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1630  AVPacket *avpkt)
1631 {
1632  const uint8_t *buf = avpkt->data;
1633  int buf_size = avpkt->size;
1634  MJpegDecodeContext *s = avctx->priv_data;
1635  const uint8_t *buf_end, *buf_ptr;
1636  const uint8_t *unescaped_buf_ptr;
1637  int hshift, vshift;
1638  int unescaped_buf_size;
1639  int start_code;
1640  int i, index;
1641  int ret = 0;
1642 
1643  buf_ptr = buf;
1644  buf_end = buf + buf_size;
1645  while (buf_ptr < buf_end) {
1646  /* find start next marker */
1647  start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
1648  &unescaped_buf_ptr,
1649  &unescaped_buf_size);
1650  /* EOF */
1651  if (start_code < 0) {
1652  goto the_end;
1653  } else if (unescaped_buf_size > (1U<<28)) {
1654  av_log(avctx, AV_LOG_ERROR, "MJPEG packet 0x%x too big (0x%x/0x%x), corrupt data?\n",
1655  start_code, unescaped_buf_size, buf_size);
1656  return AVERROR_INVALIDDATA;
1657  } else {
1658  av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n",
1659  start_code, buf_end - buf_ptr);
1660  if ((ret = init_get_bits8(&s->gb, unescaped_buf_ptr, unescaped_buf_size)) < 0) {
1661  av_log(avctx, AV_LOG_ERROR, "invalid buffer\n");
1662  goto fail;
1663  }
1664 
1665  s->start_code = start_code;
1666  if (s->avctx->debug & FF_DEBUG_STARTCODE)
1667  av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1668 
1669  /* process markers */
1670  if (start_code >= 0xd0 && start_code <= 0xd7)
1671  av_log(avctx, AV_LOG_DEBUG,
1672  "restart marker: %d\n", start_code & 0x0f);
1673  /* APP fields */
1674  else if (start_code >= APP0 && start_code <= APP15)
1675  mjpeg_decode_app(s);
1676  /* Comment */
1677  else if (start_code == COM)
1678  mjpeg_decode_com(s);
1679 
1680  ret = -1;
1681  switch (start_code) {
1682  case SOI:
1683  s->restart_interval = 0;
1684  s->restart_count = 0;
1685  /* nothing to do on SOI */
1686  break;
1687  case DQT:
1689  break;
1690  case DHT:
1691  if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
1692  av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1693  goto fail;
1694  }
1695  break;
1696  case SOF0:
1697  case SOF1:
1698  s->lossless = 0;
1699  s->ls = 0;
1700  s->progressive = 0;
1701  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1702  goto fail;
1703  break;
1704  case SOF2:
1705  s->lossless = 0;
1706  s->ls = 0;
1707  s->progressive = 1;
1708  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1709  goto fail;
1710  break;
1711  case SOF3:
1712  s->lossless = 1;
1713  s->ls = 0;
1714  s->progressive = 0;
1715  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1716  goto fail;
1717  break;
1718  case SOF48:
1719  s->lossless = 1;
1720  s->ls = 1;
1721  s->progressive = 0;
1722  if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1723  goto fail;
1724  break;
1725  case LSE:
1726  if (!CONFIG_JPEGLS_DECODER ||
1727  (ret = ff_jpegls_decode_lse(s)) < 0)
1728  goto fail;
1729  break;
1730  case EOI:
1731 eoi_parser:
1732  s->cur_scan = 0;
1733  if (!s->got_picture) {
1734  av_log(avctx, AV_LOG_WARNING,
1735  "Found EOI before any SOF, ignoring\n");
1736  break;
1737  }
1738  if (s->interlaced) {
1739  s->bottom_field ^= 1;
1740  /* if not bottom field, do not output image yet */
1741  if (s->bottom_field == !s->interlace_polarity)
1742  break;
1743  }
1744  if ((ret = av_frame_ref(data, s->picture_ptr)) < 0)
1745  return ret;
1746  *got_frame = 1;
1747  s->got_picture = 0;
1748 
1749  if (!s->lossless) {
1750  int qp = FFMAX3(s->qscale[0],
1751  s->qscale[1],
1752  s->qscale[2]);
1753  int qpw = (s->width + 15) / 16;
1754  AVBufferRef *qp_table_buf = av_buffer_alloc(qpw);
1755  if (qp_table_buf) {
1756  memset(qp_table_buf->data, qp, qpw);
1757  av_frame_set_qp_table(data, qp_table_buf, 0, FF_QSCALE_TYPE_MPEG1);
1758  }
1759 
1760  if(avctx->debug & FF_DEBUG_QP)
1761  av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", qp);
1762  }
1763 
1764  goto the_end;
1765  case SOS:
1766  if ((ret = ff_mjpeg_decode_sos(s, NULL, NULL)) < 0 &&
1767  (avctx->err_recognition & AV_EF_EXPLODE))
1768  goto fail;
1769  break;
1770  case DRI:
1771  mjpeg_decode_dri(s);
1772  break;
1773  case SOF5:
1774  case SOF6:
1775  case SOF7:
1776  case SOF9:
1777  case SOF10:
1778  case SOF11:
1779  case SOF13:
1780  case SOF14:
1781  case SOF15:
1782  case JPG:
1783  av_log(avctx, AV_LOG_ERROR,
1784  "mjpeg: unsupported coding type (%x)\n", start_code);
1785  break;
1786  }
1787 
1788  /* eof process start code */
1789  buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
1790  av_log(avctx, AV_LOG_DEBUG,
1791  "marker parser used %d bytes (%d bits)\n",
1792  (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
1793  }
1794  }
1795  if (s->got_picture) {
1796  av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
1797  goto eoi_parser;
1798  }
1799  av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
1800  return AVERROR_INVALIDDATA;
1801 fail:
1802  s->got_picture = 0;
1803  return ret;
1804 the_end:
1805  if (s->upscale_h) {
1806  uint8_t *line = s->picture_ptr->data[s->upscale_h];
1808  avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
1809  avctx->pix_fmt == AV_PIX_FMT_YUVJ440P ||
1810  avctx->pix_fmt == AV_PIX_FMT_YUV440P);
1811  for (i = 0; i < s->chroma_height; i++) {
1812  for (index = s->width - 1; index; index--)
1813  line[index] = (line[index / 2] + line[(index + 1) / 2]) >> 1;
1814  line += s->linesize[s->upscale_h];
1815  }
1816  }
1817  if (s->upscale_v) {
1818  uint8_t *dst = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[(s->height - 1) * s->linesize[s->upscale_v]];
1819  int w;
1820  avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
1821  w = s->width >> hshift;
1823  avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
1824  avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
1825  avctx->pix_fmt == AV_PIX_FMT_YUV422P);
1826  for (i = s->height - 1; i; i--) {
1827  uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[i / 2 * s->linesize[s->upscale_v]];
1828  uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[(i + 1) / 2 * s->linesize[s->upscale_v]];
1829  if (src1 == src2) {
1830  memcpy(dst, src1, w);
1831  } else {
1832  for (index = 0; index < w; index++)
1833  dst[index] = (src1[index] + src2[index]) >> 1;
1834  }
1835  dst -= s->linesize[s->upscale_v];
1836  }
1837  }
1838  if (s->flipped && (s->avctx->flags & CODEC_FLAG_EMU_EDGE)) {
1839  int j;
1840  avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
1841  for (index=0; index<4; index++) {
1842  uint8_t *dst = s->picture_ptr->data[index];
1843  int w = s->width;
1844  int h = s->height;
1845  if(index && index<3){
1846  w = -((-w) >> hshift);
1847  h = -((-h) >> vshift);
1848  }
1849  if(dst){
1850  uint8_t *dst2 = dst + s->linesize[index]*(h-1);
1851  for (i=0; i<h/2; i++) {
1852  for (j=0; j<w; j++)
1853  FFSWAP(int, dst[j], dst2[j]);
1854  dst += s->linesize[index];
1855  dst2 -= s->linesize[index];
1856  }
1857  }
1858  }
1859  }
1860 
1861  av_log(avctx, AV_LOG_DEBUG, "decode frame unused %td bytes\n",
1862  buf_end - buf_ptr);
1863 // return buf_end - buf_ptr;
1864  return buf_ptr - buf;
1865 }
1866 
1868 {
1869  MJpegDecodeContext *s = avctx->priv_data;
1870  int i, j;
1871 
1872  if (s->picture_ptr)
1874 
1875  av_free(s->buffer);
1876  av_freep(&s->ljpeg_buffer);
1877  s->ljpeg_buffer_size = 0;
1878 
1879  for (i = 0; i < 3; i++) {
1880  for (j = 0; j < 4; j++)
1881  ff_free_vlc(&s->vlcs[i][j]);
1882  }
1883  for (i = 0; i < MAX_COMPONENTS; i++) {
1884  av_freep(&s->blocks[i]);
1885  av_freep(&s->last_nnz[i]);
1886  }
1887  return 0;
1888 }
1889 
1890 static void decode_flush(AVCodecContext *avctx)
1891 {
1892  MJpegDecodeContext *s = avctx->priv_data;
1893  s->got_picture = 0;
1894 }
1895 
1896 #if CONFIG_MJPEG_DECODER
1897 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
1898 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1899 static const AVOption options[] = {
1900  { "extern_huff", "Use external huffman table.",
1901  OFFSET(extern_huff), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
1902  { NULL },
1903 };
1904 
1905 static const AVClass mjpegdec_class = {
1906  .class_name = "MJPEG decoder",
1907  .item_name = av_default_item_name,
1908  .option = options,
1909  .version = LIBAVUTIL_VERSION_INT,
1910 };
1911 
1912 AVCodec ff_mjpeg_decoder = {
1913  .name = "mjpeg",
1914  .type = AVMEDIA_TYPE_VIDEO,
1915  .id = AV_CODEC_ID_MJPEG,
1916  .priv_data_size = sizeof(MJpegDecodeContext),
1920  .flush = decode_flush,
1921  .capabilities = CODEC_CAP_DR1,
1922  .max_lowres = 3,
1923  .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
1924  .priv_class = &mjpegdec_class,
1925 };
1926 #endif
1927 #if CONFIG_THP_DECODER
1928 AVCodec ff_thp_decoder = {
1929  .name = "thp",
1930  .type = AVMEDIA_TYPE_VIDEO,
1931  .id = AV_CODEC_ID_THP,
1932  .priv_data_size = sizeof(MJpegDecodeContext),
1936  .flush = decode_flush,
1937  .capabilities = CODEC_CAP_DR1,
1938  .max_lowres = 3,
1939  .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
1940 };
1941 #endif
int block_stride[MAX_COMPONENTS]
Definition: mjpegdec.h:77
Definition: mjpeg.h:115
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
const struct AVCodec * codec
float v
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2675
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int v_count[MAX_COMPONENTS]
Definition: mjpegdec.h:80
Definition: mjpeg.h:57
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
enum AVCodecID id
Definition: mxfenc.c:89
av_default_item_name
JPEG-LS.
Definition: mjpeg.h:107
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:198
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: mjpeg.c:73
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
int h_scount[MAX_COMPONENTS]
Definition: mjpegdec.h:85
Definition: mjpeg.h:53
AVFrame picture
Definition: mjpegdec.h:90
int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
Definition: mjpegdec.c:126
static int mjpeg_decode_com(MJpegDecodeContext *s)
Definition: mjpegdec.c:1482
enum AVColorRange color_range
MPEG vs JPEG YUV range.
int num
numerator
Definition: rational.h:44
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
int qscale[4]
quantizer scale calculated from quant_matrixes
Definition: mjpegdec.h:52
uint8_t * buffer
Definition: mjpegdec.h:48
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: mjpeg.h:74
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...
int dc_index[MAX_COMPONENTS]
Definition: mjpegdec.h:82
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: mjpeg.c:70
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: mjpeg.c:99
int linesize[MAX_COMPONENTS]
linesize << interlaced
Definition: mjpegdec.h:93
uint8_t permutated[64]
Definition: dsputil.h:116
uint8_t run
Definition: svq3.c:136
static void handle_rstn(MJpegDecodeContext *s, int nb_components)
Definition: mjpegdec.c:732
void(* clear_block)(int16_t *block)
Definition: dsputil.h:145
int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
Definition: mjpegdec.c:159
Definition: mjpeg.h:56
JPEG-LS decoder.
MJPEG encoder and decoder.
int comp_index[MAX_COMPONENTS]
Definition: mjpegdec.h:81
output residual component w
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:101
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
#define FF_DEBUG_QP
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
#define FF_QSCALE_TYPE_MPEG1
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:55
Definition: mjpeg.h:45
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
#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.
int ff_init_vlc_sparse(VLC *vlc, 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:262
uint8_t bits
Definition: crc.c:216
uint8_t
#define av_cold
Definition: attributes.h:78
static int mjpeg_decode_dri(MJpegDecodeContext *s)
Definition: mjpegdec.c:1334
AVOptions.
uint16_t(* ljpeg_buffer)[4]
Definition: mjpegdec.h:115
#define AV_RB32
Definition: mjpeg.h:50
#define b
Definition: input.c:42
unsigned int ljpeg_buffer_size
Definition: mjpegdec.h:116
int16_t quant_matrixes[4][64]
Definition: mjpegdec.h:50
#define emms_c()
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
uint8_t * last_nnz[MAX_COMPONENTS]
Definition: mjpegdec.h:97
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
AVFrame * picture_ptr
Definition: mjpegdec.h:91
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:142
mpeg1, jpeg, h263
uint8_t * data
#define MAX_COMPONENTS
Definition: mjpegdec.h:39
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:104
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:193
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: mjpeg.c:127
int h_count[MAX_COMPONENTS]
Definition: mjpegdec.h:79
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:81
uint8_t idct_permutation[64]
idct input permutation.
Definition: dsputil.h:249
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:270
Definition: mjpeg.h:49
int lowres
low resolution decoding, 1-> 1/2 size, 2->1/4 size
static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al, const uint8_t *mb_bitmask, const AVFrame *reference)
Definition: mjpegdec.c:1008
Definition: mjpeg.h:77
Definition: mjpeg.h:83
Definition: mjpeg.h:48
const OptionDef options[]
Definition: ffserver.c:4697
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
#define VD
Definition: dvdsubdec.c:590
#define PREDICT(ret, topleft, top, left, predictor)
Definition: mjpeg.h:128
static void predictor(uint8_t *src, int size)
Definition: exr.c:188
Discrete Time axis x
#define U(x)
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:557
int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mjpegdec.c:1629
enum AVCodecID id
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:160
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
static const uint16_t mask[17]
Definition: lzw.c:37
#define AV_EF_EXPLODE
int nb_blocks[MAX_COMPONENTS]
Definition: mjpegdec.h:84
Definition: mjpeg.h:43
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:1867
VLC vlcs[3][4]
Definition: mjpegdec.h:51
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
Definition: mjpeg.h:60
int flags
CODEC_FLAG_*.
Definition: graph2dot.c:48
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
Definition: mjpeg.h:44
const char * name
Name of the codec implementation.
int ff_jpegls_decode_lse(MJpegDecodeContext *s)
Decode LSE block with initialization parameters.
Definition: jpeglsdec.c:52
static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
Definition: mjpegdec.c:1517
static void put_bits(J2kEncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
static const uint8_t offset[127][2]
Definition: vf_spp.c:70
#define CLOSE_READER(name, gb)
Definition: get_bits.h:140
#define FFMAX(a, b)
Definition: common.h:56
external API header
int size
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:44
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:72
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition: hpeldsp.c:37
the normal 2^n-1 "JPEG" YUV ranges
static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block, int component, int dc_index, int16_t *quant_matrix, int Al)
Definition: mjpegdec.c:551
static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform)
Definition: mjpegdec.c:849
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
#define FF_DEBUG_STARTCODE
the normal 219*2^(n-8) "MPEG" YUV ranges
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:231
ScanTable scantable
Definition: mjpegdec.h:99
static av_always_inline void mjpeg_copy_block(MJpegDecodeContext *s, uint8_t *dst, const uint8_t *src, int linesize, int lowres)
Definition: mjpegdec.c:992
int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
Definition: mjpegdec.c:213
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:266
#define FFMIN(a, b)
Definition: common.h:58
Definition: mjpeg.h:76
static int decode_block(MJpegDecodeContext *s, int16_t *block, int component, int dc_index, int ac_index, int16_t *quant_matrix)
Definition: mjpegdec.c:503
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:80
int component_id[MAX_COMPONENTS]
Definition: mjpegdec.h:78
static int mjpeg_decode_app(MJpegDecodeContext *s)
Definition: mjpegdec.c:1346
ret
Definition: avfilter.c:821
#define NEG_USR32(a, s)
Definition: mathops.h:159
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: mjpeg.c:65
t
Definition: genspecsines3.m:6
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: mjpeg.c:67
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:255
int quant_index[4]
Definition: mjpegdec.h:88
#define LAST_SKIP_BITS(name, gb, num)
Definition: get_bits.h:181
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:524
#define AV_RL32
int v_scount[MAX_COMPONENTS]
Definition: mjpegdec.h:86
#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:458
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
Definition: mjpeg.h:46
FIXME Range Coding of cr are level
Definition: snow.txt:367
DSPContext dsp
Definition: mjpegdec.h:100
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:65
GetBitContext gb
Definition: mjpegdec.h:44
#define ZERO_RUN
Definition: mjpegdec.c:649
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:187
Definition: mjpeg.h:52
static void flush(AVCodecContext *avctx)
Definition: mjpeg.h:75
static const float pred[4]
Definition: siprdata.h:259
Definition: mjpeg.h:54
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
NULL
Definition: eval.c:55
or the Software in violation of any applicable export control laws in any jurisdiction Except as provided by mandatorily applicable UPF has no obligation to provide you with source code to the Software In the event Software contains any source code
static int width
Definition: tests/utils.c:158
AVS_Value src
Definition: avisynth_c.h:523
enum AVCodecID codec_id
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:410
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
FIXME Range Coding of cr are mx and my are Motion Vector top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff)*mv_scale Intra DC Predicton block[y][x] dc[1]
Definition: snow.txt:392
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 + mantisse with no MSB).
Definition: get_bits.h:212
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
#define OPEN_READER(name, gb)
Definition: get_bits.h:126
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:594
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv)
Definition: jpeglsdec.c:261
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Describe the class of an AVClass context structure.
Definition: log.h:50
struct MJpegDecodeContext MJpegDecodeContext
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
int index
Definition: gxfenc.c:89
static int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
Definition: mjpegdec.c:485
int ac_index[MAX_COMPONENTS]
Definition: mjpegdec.h:83
synthesis window for stochastic i
static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int predictor, int point_transform)
Definition: mjpegdec.c:762
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
#define GET_CACHE(name, gb)
Definition: get_bits.h:191
uint64_t coefs_finished[MAX_COMPONENTS]
bitmask of which coefs have been completely decoded (progressive mode)
Definition: mjpegdec.h:98
Definition: mjpeg.h:58
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:330
#define AV_PIX_FMT_GBR24P
Definition: pixfmt.h:251
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:306
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
#define CONFIG_JPEGLS_DECODER
Definition: config.h:515
static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al)
Definition: mjpegdec.c:1119
#define MIN_CACHE_BITS
Definition: get_bits.h:122
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by an given frame.
Definition: frame.c:228
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
#define FF_DEBUG_PICT_INFO
#define OFFSET(x)
Definition: ffmpeg_opt.c:2553
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:83
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: mjpeg.c:75
int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask, const AVFrame *reference)
Definition: mjpegdec.c:1177
A reference to a data buffer.
Definition: buffer.h:81
#define CODEC_FLAG_EMU_EDGE
Don&#39;t draw edges.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
Y , 8bpp.
Definition: pixfmt.h:76
common internal api header.
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:115
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:81
static void build_basic_mjpeg_vlc(MJpegDecodeContext *s)
Definition: mjpegdec.c:67
Definition: mjpeg.h:84
static double c[64]
the buffer and buffer reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFilterBuffer structures They must not be accessed but through references stored in AVFilterBufferRef structures Several references can point to the same buffer
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:82
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:667
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:54
int den
denominator
Definition: rational.h:45
function y
Definition: D.m:1
static int lowres
Definition: ffplay.c:298
void(* idct_put)(uint8_t *dest, int line_size, int16_t *block)
block -> idct -> clip to unsigned 8 bit -> dest.
Definition: dsputil.h:229
AVCodecContext * avctx
Definition: mjpegdec.h:43
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: mjpeg.c:102
float re
Definition: fft-test.c:64
Definition: mjpeg.h:79
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:275
int got_picture
we found a SOF and picture is valid, too.
Definition: mjpegdec.h:92
int len
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: dsputil.c:110
int16_t(*[MAX_COMPONENTS] blocks)[64]
intermediate sums (progressive mode)
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
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
JPEG-LS extension parameters.
Definition: mjpeg.h:108
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
int last_dc[MAX_COMPONENTS]
Definition: mjpegdec.h:89
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:418
#define REFINE_BIT(j)
Definition: mjpegdec.c:641
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:569
static void decode_flush(AVCodecContext *avctx)
Definition: mjpegdec.c:1890
#define AV_LOG_INFO
Definition: log.h:156
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:103
enum AVFieldOrder field_order
Field order.
#define av_always_inline
Definition: attributes.h:41
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
#define FFSWAP(type, a, b)
Definition: common.h:61
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:1541
MJPEG decoder.
#define MKTAG(a, b, c, d)
Definition: common.h:282
This structure stores compressed data.
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:344
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
Definition: frame.c:51
#define FFMAX3(a, b, c)
Definition: common.h:57
Definition: mjpeg.h:51
Definition: mjpeg.h:98