+/**
+ * Map stored raw sensor values into linear reference values (see: DNG Specification - Chapter 5)
+ */
+static uint16_t av_always_inline dng_process_color16(uint16_t value,
+ const uint16_t *lut,
+ uint16_t black_level,
+ float scale_factor) {
+ float value_norm;
+
+ // Lookup table lookup
+ if (lut)
+ value = lut[value];
+
+ // Black level subtraction
+ value = av_clip_uint16_c((unsigned)value - black_level);
+
+ // Color scaling
+ value_norm = (float)value * scale_factor;
+
+ value = av_clip_uint16_c(value_norm * 65535);
+
+ return value;
+}
+
+static uint16_t av_always_inline dng_process_color8(uint16_t value,
+ const uint16_t *lut,
+ uint16_t black_level,
+ float scale_factor) {
+ return dng_process_color16(value, lut, black_level, scale_factor) >> 8;
+}
+
+static void dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
+ const uint8_t *src, int src_stride,
+ int width, int height, int is_single_comp, int is_u16)
+{
+ int line, col;
+ float scale_factor;
+
+ scale_factor = 1.0f / (s->white_level - s->black_level);
+
+ if (is_single_comp) {
+ if (!is_u16)
+ return; /* <= 8bpp unsupported */
+
+ /* Image is double the width and half the height we need, each row comprises 2 rows of the output
+ (split vertically in the middle). */
+ for (line = 0; line < height / 2; line++) {
+ uint16_t *dst_u16 = (uint16_t *)dst;
+ uint16_t *src_u16 = (uint16_t *)src;
+
+ /* Blit first half of input row row to initial row of output */
+ for (col = 0; col < width; col++)
+ *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level, scale_factor);
+
+ /* Advance the destination pointer by a row (source pointer remains in the same place) */
+ dst += dst_stride * sizeof(uint16_t);
+ dst_u16 = (uint16_t *)dst;
+
+ /* Blit second half of input row row to next row of output */
+ for (col = 0; col < width; col++)
+ *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level, scale_factor);
+
+ dst += dst_stride * sizeof(uint16_t);
+ src += src_stride * sizeof(uint16_t);
+ }
+ } else {
+ /* Input and output image are the same size and the MJpeg decoder has done per-component
+ deinterleaving, so blitting here is straightforward. */
+ if (is_u16) {
+ for (line = 0; line < height; line++) {
+ uint16_t *dst_u16 = (uint16_t *)dst;
+ uint16_t *src_u16 = (uint16_t *)src;
+
+ for (col = 0; col < width; col++)
+ *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level, scale_factor);
+
+ dst += dst_stride * sizeof(uint16_t);
+ src += src_stride * sizeof(uint16_t);
+ }
+ } else {
+ for (line = 0; line < height; line++) {
+ for (col = 0; col < width; col++)
+ *dst++ = dng_process_color8(*src++, s->dng_lut, s->black_level, scale_factor);
+
+ dst += dst_stride;
+ src += src_stride;
+ }
+ }
+ }
+}
+
+static int dng_decode_jpeg(AVCodecContext *avctx, AVFrame *frame,
+ int tile_byte_count, int dst_x, int dst_y, int w, int h)
+{
+ TiffContext *s = avctx->priv_data;
+ AVPacket jpkt;
+ uint8_t *dst_data, *src_data;
+ uint32_t dst_offset; /* offset from dst buffer in pixels */
+ int is_single_comp, is_u16, pixel_size;
+ int ret;
+
+ /* Prepare a packet and send to the MJPEG decoder */
+ av_init_packet(&jpkt);
+ jpkt.data = (uint8_t*)s->gb.buffer;
+ jpkt.size = tile_byte_count;
+
+ if (s->is_bayer) {
+ MJpegDecodeContext *mjpegdecctx = s->avctx_mjpeg->priv_data;
+ /* We have to set this information here, there is no way to know if a given JPEG is a DNG-embedded
+ image or not from its own data (and we need that information when decoding it). */
+ mjpegdecctx->bayer = 1;
+ }
+
+ ret = avcodec_send_packet(s->avctx_mjpeg, &jpkt);
+ if (ret < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
+ return ret;
+ }
+
+ ret = avcodec_receive_frame(s->avctx_mjpeg, s->jpgframe);
+ if (ret < 0) {
+ av_log(avctx, AV_LOG_ERROR, "JPEG decoding error: %s.\n", av_err2str(ret));
+
+ /* Normally skip, error if explode */
+ if (avctx->err_recognition & AV_EF_EXPLODE)
+ return AVERROR_INVALIDDATA;
+ else
+ return 0;
+ }
+
+ /* Copy the outputted tile's pixels from 'jpgframe' to 'frame' (final buffer) */
+
+ /* See dng_blit for explanation */
+ is_single_comp = (s->avctx_mjpeg->width == w * 2 && s->avctx_mjpeg->height == h / 2);
+
+ is_u16 = (s->bpp > 8);
+ pixel_size = (is_u16 ? sizeof(uint16_t) : sizeof(uint8_t));
+
+ if (is_single_comp && !is_u16) {
+ av_log(s->avctx, AV_LOG_ERROR, "DNGs with bpp <= 8 and 1 component are unsupported\n");
+ av_frame_unref(s->jpgframe);
+ return AVERROR_PATCHWELCOME;
+ }
+
+ dst_offset = dst_x + frame->linesize[0] * dst_y / pixel_size;
+ dst_data = frame->data[0] + dst_offset * pixel_size;
+ src_data = s->jpgframe->data[0];
+
+ dng_blit(s,
+ dst_data,
+ frame->linesize[0] / pixel_size,
+ src_data,
+ s->jpgframe->linesize[0] / pixel_size,
+ w,
+ h,
+ is_single_comp,
+ is_u16);
+
+ av_frame_unref(s->jpgframe);
+
+ return 0;
+}
+
+static int dng_decode_tiles(AVCodecContext *avctx, AVFrame *frame, AVPacket *avpkt)
+{
+ TiffContext *s = avctx->priv_data;
+ int tile_idx;
+ int tile_offset_offset, tile_offset;
+ int tile_byte_count_offset, tile_byte_count;
+ int tile_count_x, tile_count_y;
+ int tile_width, tile_length;
+ int has_width_leftover, has_height_leftover;
+ int tile_x = 0, tile_y = 0;
+ int pos_x = 0, pos_y = 0;
+ int ret;
+
+ s->jpgframe->width = s->tile_width;
+ s->jpgframe->height = s->tile_length;
+
+ s->avctx_mjpeg->width = s->tile_width;
+ s->avctx_mjpeg->height = s->tile_length;
+
+ has_width_leftover = (s->width % s->tile_width != 0);
+ has_height_leftover = (s->height % s->tile_length != 0);
+
+ /* Calculate tile counts (round up) */
+ tile_count_x = (s->width + s->tile_width - 1) / s->tile_width;
+ tile_count_y = (s->height + s->tile_length - 1) / s->tile_length;
+
+ /* Iterate over the number of tiles */
+ for (tile_idx = 0; tile_idx < s->tile_count; tile_idx++) {
+ tile_x = tile_idx % tile_count_x;
+ tile_y = tile_idx / tile_count_x;
+
+ if (has_width_leftover && tile_x == tile_count_x - 1) // If on the right-most tile
+ tile_width = s->width % s->tile_width;
+ else
+ tile_width = s->tile_width;
+
+ if (has_height_leftover && tile_y == tile_count_y - 1) // If on the bottom-most tile
+ tile_length = s->height % s->tile_length;
+ else
+ tile_length = s->tile_length;
+
+ /* Read tile offset */
+ tile_offset_offset = s->tile_offsets_offset + tile_idx * sizeof(int);
+ bytestream2_seek(&s->gb, tile_offset_offset, SEEK_SET);
+ tile_offset = ff_tget_long(&s->gb, s->le);
+
+ /* Read tile byte size */
+ tile_byte_count_offset = s->tile_byte_counts_offset + tile_idx * sizeof(int);
+ bytestream2_seek(&s->gb, tile_byte_count_offset, SEEK_SET);
+ tile_byte_count = ff_tget_long(&s->gb, s->le);
+
+ /* Seek to tile data */
+ bytestream2_seek(&s->gb, tile_offset, SEEK_SET);
+
+ /* Decode JPEG tile and copy it in the reference frame */
+ ret = dng_decode_jpeg(avctx, frame, tile_byte_count, pos_x, pos_y, tile_width, tile_length);
+
+ if (ret < 0)
+ return ret;
+
+ /* Advance current positions */
+ pos_x += tile_width;
+ if (tile_x == tile_count_x - 1) { // If on the right edge
+ pos_x = 0;
+ pos_y += tile_length;
+ }
+ }
+
+ /* Frame is ready to be output */
+ frame->pict_type = AV_PICTURE_TYPE_I;
+ frame->key_frame = 1;
+
+ return avpkt->size;
+}
+
+static int dng_decode_strip(AVCodecContext *avctx, AVFrame *frame)
+{
+ TiffContext *s = avctx->priv_data;
+
+ s->jpgframe->width = s->width;
+ s->jpgframe->height = s->height;
+
+ s->avctx_mjpeg->width = s->width;
+ s->avctx_mjpeg->height = s->height;
+
+ return dng_decode_jpeg(avctx, frame, s->stripsize, 0, 0, s->width, s->height);
+}
+