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