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