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