4 * Copyright (c) 2020 Paul B Mahol
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Cintel RAW image decoder
28 #define BITSTREAM_READER_LE
30 #include "libavutil/intfloat.h"
31 #include "libavutil/display.h"
33 #include "bytestream.h"
38 typedef struct CRIContext {
39 AVCodecContext *jpeg_avctx; // wrapper context for MJPEG
40 AVFrame *jpgframe; // decoded JPEG tile
46 uint64_t tile_size[4];
49 static av_cold int cri_decode_init(AVCodecContext *avctx)
51 CRIContext *s = avctx->priv_data;
55 s->jpgframe = av_frame_alloc();
57 return AVERROR(ENOMEM);
59 codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
62 s->jpeg_avctx = avcodec_alloc_context3(codec);
64 return AVERROR(ENOMEM);
65 s->jpeg_avctx->flags = avctx->flags;
66 s->jpeg_avctx->flags2 = avctx->flags2;
67 s->jpeg_avctx->dct_algo = avctx->dct_algo;
68 s->jpeg_avctx->idct_algo = avctx->idct_algo;
69 ret = avcodec_open2(s->jpeg_avctx, codec, NULL);
76 static void unpack_10bit(GetByteContext *gb, uint16_t *dst, int shift,
77 int w, int h, ptrdiff_t stride)
83 uint32_t a0 = bytestream2_get_le32(gb);
84 uint32_t a1 = bytestream2_get_le32(gb);
85 uint32_t a2 = bytestream2_get_le32(gb);
86 uint32_t a3 = bytestream2_get_le32(gb);
87 dst[pos] = (((a0 >> 1) & 0xE00) | (a0 & 0x1FF)) << shift;
95 dst[pos] = (((a0 >> 13) & 0x3F) | ((a0 >> 14) & 0xFC0)) << shift;
103 dst[pos] = (((a0 >> 26) & 7) | ((a1 & 0x1FF) << 3)) << shift;
111 dst[pos] = (((a1 >> 10) & 0x1FF) | ((a1 >> 11) & 0xE00)) << shift;
119 dst[pos] = (((a1 >> 23) & 0x3F) | ((a2 & 0x3F) << 6)) << shift;
127 dst[pos] = (((a2 >> 7) & 0xFF8) | ((a2 >> 6) & 7)) << shift;
135 dst[pos] = (((a3 & 7) << 9) | ((a2 >> 20) & 0x1FF)) << shift;
143 dst[pos] = (((a3 >> 4) & 0xFC0) | ((a3 >> 3) & 0x3F)) << shift;
151 dst[pos] = (((a3 >> 16) & 7) | ((a3 >> 17) & 0xFF8)) << shift;
164 static int cri_decode_frame(AVCodecContext *avctx, void *data,
165 int *got_frame, AVPacket *avpkt)
167 CRIContext *s = avctx->priv_data;
168 GetByteContext *gb = &s->gb;
169 ThreadFrame frame = { .f = data };
170 int ret, bps, hflip = 0, vflip = 0;
171 AVFrameSideData *rotation;
178 bytestream2_init(gb, avpkt->data, avpkt->size);
180 while (bytestream2_get_bytes_left(gb) > 8) {
181 char codec_name[1024];
182 uint32_t key, length;
185 key = bytestream2_get_le32(gb);
186 length = bytestream2_get_le32(gb);
191 return AVERROR_INVALIDDATA;
193 if (bytestream2_get_le32(gb) != MKTAG('D', 'V', 'C', 'C'))
194 return AVERROR_INVALIDDATA;
198 return AVERROR_INVALIDDATA;
199 avctx->width = bytestream2_get_le32(gb);
200 avctx->height = bytestream2_get_le32(gb);
201 s->color_model = bytestream2_get_le32(gb);
202 if (bytestream2_get_le32(gb) != 1)
203 return AVERROR_INVALIDDATA;
208 return AVERROR_INVALIDDATA;
210 if (bytestream2_get_le32(gb) != 0)
211 return AVERROR_INVALIDDATA;
214 bytestream2_get_buffer(gb, codec_name, FFMIN(length, sizeof(codec_name) - 1));
215 length -= FFMIN(length, sizeof(codec_name) - 1);
216 if (strncmp(codec_name, "cintel_craw", FFMIN(length, sizeof(codec_name) - 1)))
217 return AVERROR_INVALIDDATA;
221 if (bytestream2_get_bytes_left(gb) < length)
222 return AVERROR_INVALIDDATA;
223 s->data = gb->buffer;
224 s->data_size = length;
227 hflip = bytestream2_get_byte(gb) != 0;
231 vflip = bytestream2_get_byte(gb) != 0;
236 return AVERROR_INVALIDDATA;
237 framerate = av_int2float(bytestream2_get_le32(gb));
238 avctx->framerate.num = framerate * 1000;
239 avctx->framerate.den = 1000;
243 return AVERROR_INVALIDDATA;
245 for (int i = 0; i < 4; i++)
246 s->tile_size[i] = bytestream2_get_le64(gb);
249 av_log(avctx, AV_LOG_DEBUG, "skipping unknown key %u of length %u\n", key, length);
251 bytestream2_skip(gb, length);
255 switch (s->color_model) {
258 avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR16;
262 avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG16;
266 avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB16;
271 avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG16;
275 switch (s->color_model) {
292 return AVERROR_INVALIDDATA;
296 for (int i = 0; i < 4; i++) {
297 if (s->tile_size[i] >= s->data_size)
298 return AVERROR_INVALIDDATA;
301 if (s->tile_size[0] + s->tile_size[1] + s->tile_size[2] + s->tile_size[3] !=
303 return AVERROR_INVALIDDATA;
306 if (!s->data || !s->data_size)
307 return AVERROR_INVALIDDATA;
309 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
312 avctx->bits_per_raw_sample = bps;
314 if (!compressed && s->color_model == 45) {
315 uint16_t *dst = (uint16_t *)p->data[0];
318 bytestream2_init(&gb, s->data, s->data_size);
319 unpack_10bit(&gb, dst, 4, avctx->width, avctx->height, p->linesize[0] / 2);
320 } else if (!compressed) {
322 const int shift = 16 - bps;
324 ret = init_get_bits8(&gbit, s->data, s->data_size);
328 for (int y = 0; y < avctx->height; y++) {
329 uint16_t *dst = (uint16_t *)(p->data[0] + y * p->linesize[0]);
331 for (int x = 0; x < avctx->width; x++)
332 dst[x] = get_bits(&gbit, bps) << shift;
337 for (int tile = 0; tile < 4; tile++) {
340 av_init_packet(&jpkt);
341 jpkt.data = (uint8_t *)s->data + offset;
342 jpkt.size = s->tile_size[tile];
344 ret = avcodec_send_packet(s->jpeg_avctx, &jpkt);
346 av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
350 ret = avcodec_receive_frame(s->jpeg_avctx, s->jpgframe);
351 if (ret < 0 || s->jpgframe->format != AV_PIX_FMT_GRAY16 ||
352 s->jpeg_avctx->width * 2 != avctx->width ||
353 s->jpeg_avctx->height * 2 != avctx->height) {
355 av_log(avctx, AV_LOG_ERROR,
356 "JPEG decoding error (%d).\n", ret);
358 av_log(avctx, AV_LOG_ERROR,
359 "JPEG invalid format.\n");
360 ret = AVERROR_INVALIDDATA;
363 /* Normally skip, if error explode */
364 if (avctx->err_recognition & AV_EF_EXPLODE)
370 for (int y = 0; y < s->jpeg_avctx->height; y++) {
371 const int hw = s->jpgframe->width / 2;
372 uint16_t *dst = (uint16_t *)(p->data[0] + (y * 2) * p->linesize[0] + tile * hw * 2);
373 const uint16_t *src = (const uint16_t *)(s->jpgframe->data[0] + y * s->jpgframe->linesize[0]);
375 memcpy(dst, src, hw * 2);
377 dst += p->linesize[0] / 2;
378 memcpy(dst, src, hw * 2);
381 av_frame_unref(s->jpgframe);
382 offset += s->tile_size[tile];
386 if (hflip || vflip) {
387 rotation = av_frame_new_side_data(p, AV_FRAME_DATA_DISPLAYMATRIX,
388 sizeof(int32_t) * 9);
390 av_display_rotation_set((int32_t *)rotation->data, 0.f);
391 av_display_matrix_flip((int32_t *)rotation->data, hflip, vflip);
395 p->pict_type = AV_PICTURE_TYPE_I;
403 static av_cold int cri_decode_close(AVCodecContext *avctx)
405 CRIContext *s = avctx->priv_data;
407 av_frame_free(&s->jpgframe);
408 avcodec_free_context(&s->jpeg_avctx);
413 AVCodec ff_cri_decoder = {
415 .type = AVMEDIA_TYPE_VIDEO,
416 .id = AV_CODEC_ID_CRI,
417 .priv_data_size = sizeof(CRIContext),
418 .init = cri_decode_init,
419 .decode = cri_decode_frame,
420 .close = cri_decode_close,
421 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
422 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
423 .long_name = NULL_IF_CONFIG_SMALL("Cintel RAW"),