Skip to content

Commit 6a461ef

Browse files
committed
lavc/mjpegdec: Enable decoding of single-component bayer images
Also, ensure no false positives when determining DNG bayer images, by setting them in tiff.c instead of relying on a heuristic. There's no way to determine this just from the JPEG data, so we have to pass this information from outside the MJPEG decoder. Signed-off-by: Nick Renieris <velocityra@gmail.com>
1 parent 24fffd9 commit 6a461ef

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

libavcodec/mjpegdec.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,17 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
412412
return AVERROR_PATCHWELCOME;
413413
}
414414

415-
/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. They contain 2
416-
interleaved components and the width stored in their SOF3 markers is the
417-
width of each one. We only output a single component, therefore we need
418-
to adjust the output image width. */
419-
if (s->lossless == 1 && nb_components == 2) {
420-
s->bayer = 1;
421-
width *= 2;
415+
if (s->bayer) {
416+
if (nb_components == 2) {
417+
/* Bayer images embedded in DNGs can contain 2 interleaved components and the
418+
width stored in their SOF3 markers is the width of each one. We only output
419+
a single component, therefore we need to adjust the output image width. We
420+
handle the deinterleaving (but not the debayering) in this file. */
421+
width *= 2;
422+
}
423+
/* They can also contain 1 component, which is double the width and half the height
424+
of the final image (rows are interleaved). We don't handle the decoding in this
425+
file, but leave that to the TIFF/DNG decoder. */
422426
}
423427

424428
/* if different size, realloc/alloc picture */
@@ -1184,10 +1188,16 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int p
11841188
ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
11851189
ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
11861190
}
1187-
} else if (s->bayer && nb_components == 2) {
1188-
for (mb_x = 0; mb_x < width; mb_x++) {
1189-
((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
1190-
((uint16_t*)ptr)[2*mb_x + 1] = buffer[mb_x][1];
1191+
} else if (s->bayer) {
1192+
if (nb_components == 1) {
1193+
/* Leave decoding to the TIFF/DNG decoder (see comment in ff_mjpeg_decode_sof) */
1194+
for (mb_x = 0; mb_x < width; mb_x++)
1195+
((uint16_t*)ptr)[mb_x] = buffer[mb_x][0];
1196+
} else if (nb_components == 2) {
1197+
for (mb_x = 0; mb_x < width; mb_x++) {
1198+
((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
1199+
((uint16_t*)ptr)[2*mb_x + 1] = buffer[mb_x][1];
1200+
}
11911201
}
11921202
} else {
11931203
for(i=0; i<nb_components; i++) {

libavcodec/tiff.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,13 @@ static int dng_decode_jpeg_tile(AVCodecContext *avctx, AVFrame *frame,
839839
jpkt.data = (uint8_t*)s->gb.buffer;
840840
jpkt.size = tile_byte_count;
841841

842+
if (s->is_bayer) {
843+
MJpegDecodeContext *mjpegdecctx = s->avctx_mjpeg->priv_data;
844+
/* We have to set this information here, there is no way to know if a given JPEG is a DNG-embedded
845+
image or not from its own data (and we need that information when decoding it). */
846+
mjpegdecctx->bayer = 1;
847+
}
848+
842849
ret = avcodec_send_packet(s->avctx_mjpeg, &jpkt);
843850
if (ret < 0) {
844851
av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");

0 commit comments

Comments
 (0)