@@ -426,19 +426,6 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
426426 return AVERROR_PATCHWELCOME ;
427427 }
428428
429- if (s -> bayer ) {
430- if (nb_components == 2 ) {
431- /* Bayer images embedded in DNGs can contain 2 interleaved components and the
432- width stored in their SOF3 markers is the width of each one. We only output
433- a single component, therefore we need to adjust the output image width. We
434- handle the deinterleaving (but not the debayering) in this file. */
435- width *= 2 ;
436- }
437- /* They can also contain 1 component, which is double the width and half the height
438- of the final image (rows are interleaved). We don't handle the decoding in this
439- file, but leave that to the TIFF/DNG decoder. */
440- }
441-
442429 /* if different size, realloc/alloc picture */
443430 if (width != s -> width || height != s -> height || bits != s -> bits ||
444431 memcmp (s -> h_count , h_count , sizeof (h_count )) ||
@@ -475,6 +462,19 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
475462 s -> orig_height < height )
476463 s -> avctx -> height = AV_CEIL_RSHIFT (s -> orig_height , s -> avctx -> lowres );
477464
465+ if (s -> bayer ) {
466+ if (nb_components == 2 ) {
467+ /* Bayer images embedded in DNGs can contain 2 interleaved components and the
468+ * width stored in their SOF3 markers is the width of each one. We only output
469+ * a single component, therefore we need to adjust the output image width. We
470+ * handle the deinterleaving (but not the debayering) in this file. */
471+ s -> avctx -> width *= 2 ;
472+ }
473+ /* They can also contain 1 component, which is double the width and half the height
474+ * of the final image (rows are interleaved). We don't handle the decoding in this
475+ * file, but leave that to the TIFF/DNG decoder. */
476+ }
477+
478478 s -> first_picture = 0 ;
479479 } else {
480480 size_change = 0 ;
@@ -1086,14 +1086,12 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s)
10861086 int predictor = s -> Ss ;
10871087 int point_transform = s -> Al ;
10881088 int i , mb_x , mb_y ;
1089- unsigned width ;
10901089 uint16_t (* buffer )[4 ];
10911090 int left [4 ], top [4 ], topleft [4 ];
10921091 const int linesize = s -> linesize [0 ];
10931092 const int mask = ((1 << s -> bits ) - 1 ) << point_transform ;
10941093 int resync_mb_y = 0 ;
10951094 int resync_mb_x = 0 ;
1096- int vpred [6 ];
10971095 int ret ;
10981096
10991097 if (!s -> bayer && s -> nb_components < 3 )
@@ -1109,16 +1107,8 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s)
11091107 return AVERROR_INVALIDDATA ;
11101108 }
11111109
1112-
1113- for (i = 0 ; i < 6 ; i ++ )
1114- vpred [i ] = 1 << (s -> bits - 1 );
1115-
1116- if (s -> bayer )
1117- width = s -> mb_width / nb_components ; /* Interleaved, width stored is the total so need to divide */
1118- else
1119- width = s -> mb_width ;
1120-
1121- av_fast_malloc (& s -> ljpeg_buffer , & s -> ljpeg_buffer_size , width * 4 * sizeof (s -> ljpeg_buffer [0 ][0 ]));
1110+ av_fast_malloc (& s -> ljpeg_buffer , & s -> ljpeg_buffer_size ,
1111+ (unsigned )s -> mb_width * 4 * sizeof (s -> ljpeg_buffer [0 ][0 ]));
11221112 if (!s -> ljpeg_buffer )
11231113 return AVERROR (ENOMEM );
11241114
@@ -1138,7 +1128,7 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s)
11381128 for (i = 0 ; i < 4 ; i ++ )
11391129 top [i ] = left [i ] = topleft [i ] = buffer [0 ][i ];
11401130
1141- for (mb_x = 0 ; mb_x < width ; mb_x ++ ) {
1131+ for (mb_x = 0 ; mb_x < s -> mb_width ; mb_x ++ ) {
11421132 int modified_predictor = predictor ;
11431133 int restart ;
11441134
@@ -1166,19 +1156,12 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s)
11661156 topleft [i ] = top [i ];
11671157 top [i ] = buffer [mb_x ][i ];
11681158
1159+ PREDICT (pred , topleft [i ], top [i ], left [i ], modified_predictor );
1160+
11691161 ret = mjpeg_decode_dc (s , s -> dc_index [i ], & dc );
11701162 if (ret < 0 )
11711163 return ret ;
11721164
1173- if (!s -> bayer || mb_x ) {
1174- pred = left [i ];
1175- } else { /* This path runs only for the first line in bayer images */
1176- vpred [i ] += dc ;
1177- pred = vpred [i ] - dc ;
1178- }
1179-
1180- PREDICT (pred , topleft [i ], top [i ], pred , modified_predictor );
1181-
11821165 left [i ] = buffer [mb_x ][i ] =
11831166 mask & (pred + (unsigned )(dc * (1 << point_transform )));
11841167 }
@@ -1222,10 +1205,10 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s)
12221205 return AVERROR_PATCHWELCOME ;
12231206 if (nb_components == 1 ) {
12241207 /* Leave decoding to the TIFF/DNG decoder (see comment in ff_mjpeg_decode_sof) */
1225- for (mb_x = 0 ; mb_x < width ; mb_x ++ )
1208+ for (mb_x = 0 ; mb_x < s -> mb_width ; mb_x ++ )
12261209 ((uint16_t * )ptr )[mb_x ] = buffer [mb_x ][0 ];
12271210 } else if (nb_components == 2 ) {
1228- for (mb_x = 0 ; mb_x < width ; mb_x ++ ) {
1211+ for (mb_x = 0 ; mb_x < s -> mb_width ; mb_x ++ ) {
12291212 ((uint16_t * )ptr )[2 * mb_x + 0 ] = buffer [mb_x ][0 ];
12301213 ((uint16_t * )ptr )[2 * mb_x + 1 ] = buffer [mb_x ][1 ];
12311214 }
0 commit comments