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