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