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 AVPacket *jpkt; // encoded JPEG tile
41 AVFrame *jpgframe; // decoded JPEG tile
47 uint64_t tile_size[4];
50 static av_cold int cri_decode_init(AVCodecContext *avctx)
52 CRIContext *s = avctx->priv_data;
56 s->jpgframe = av_frame_alloc();
58 return AVERROR(ENOMEM);
60 s->jpkt = av_packet_alloc();
62 return AVERROR(ENOMEM);
64 codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
67 s->jpeg_avctx = avcodec_alloc_context3(codec);
69 return AVERROR(ENOMEM);
70 s->jpeg_avctx->flags = avctx->flags;
71 s->jpeg_avctx->flags2 = avctx->flags2;
72 s->jpeg_avctx->dct_algo = avctx->dct_algo;
73 s->jpeg_avctx->idct_algo = avctx->idct_algo;
74 ret = avcodec_open2(s->jpeg_avctx, codec, NULL);
81 static void unpack_10bit(GetByteContext *gb, uint16_t *dst, int shift,
82 int w, int h, ptrdiff_t stride)
88 uint32_t a0, a1, a2, a3;
89 if (bytestream2_get_bytes_left(gb) < 4)
91 a0 = bytestream2_get_le32(gb);
92 a1 = bytestream2_get_le32(gb);
93 a2 = bytestream2_get_le32(gb);
94 a3 = bytestream2_get_le32(gb);
95 dst[pos] = (((a0 >> 1) & 0xE00) | (a0 & 0x1FF)) << shift;
103 dst[pos] = (((a0 >> 13) & 0x3F) | ((a0 >> 14) & 0xFC0)) << shift;
111 dst[pos] = (((a0 >> 26) & 7) | ((a1 & 0x1FF) << 3)) << shift;
119 dst[pos] = (((a1 >> 10) & 0x1FF) | ((a1 >> 11) & 0xE00)) << shift;
127 dst[pos] = (((a1 >> 23) & 0x3F) | ((a2 & 0x3F) << 6)) << shift;
135 dst[pos] = (((a2 >> 7) & 0xFF8) | ((a2 >> 6) & 7)) << shift;
143 dst[pos] = (((a3 & 7) << 9) | ((a2 >> 20) & 0x1FF)) << shift;
151 dst[pos] = (((a3 >> 4) & 0xFC0) | ((a3 >> 3) & 0x3F)) << shift;
159 dst[pos] = (((a3 >> 16) & 7) | ((a3 >> 17) & 0xFF8)) << shift;
172 static int cri_decode_frame(AVCodecContext *avctx, void *data,
173 int *got_frame, AVPacket *avpkt)
175 CRIContext *s = avctx->priv_data;
176 GetByteContext *gb = &s->gb;
177 ThreadFrame frame = { .f = data };
178 int ret, bps, hflip = 0, vflip = 0;
179 AVFrameSideData *rotation;
186 bytestream2_init(gb, avpkt->data, avpkt->size);
188 while (bytestream2_get_bytes_left(gb) > 8) {
189 char codec_name[1024];
190 uint32_t key, length;
194 key = bytestream2_get_le32(gb);
195 length = bytestream2_get_le32(gb);
200 return AVERROR_INVALIDDATA;
202 if (bytestream2_get_le32(gb) != MKTAG('D', 'V', 'C', 'C'))
203 return AVERROR_INVALIDDATA;
207 return AVERROR_INVALIDDATA;
208 width = bytestream2_get_le32(gb);
209 height = bytestream2_get_le32(gb);
210 s->color_model = bytestream2_get_le32(gb);
211 if (bytestream2_get_le32(gb) != 1)
212 return AVERROR_INVALIDDATA;
213 ret = ff_set_dimensions(avctx, width, height);
220 return AVERROR_INVALIDDATA;
222 if (bytestream2_get_le32(gb) != 0)
223 return AVERROR_INVALIDDATA;
226 bytestream2_get_buffer(gb, codec_name, FFMIN(length, sizeof(codec_name) - 1));
227 length -= FFMIN(length, sizeof(codec_name) - 1);
228 if (strncmp(codec_name, "cintel_craw", FFMIN(length, sizeof(codec_name) - 1)))
229 return AVERROR_INVALIDDATA;
233 if (bytestream2_get_bytes_left(gb) < length)
234 return AVERROR_INVALIDDATA;
235 s->data = gb->buffer;
236 s->data_size = length;
239 hflip = bytestream2_get_byte(gb) != 0;
243 vflip = bytestream2_get_byte(gb) != 0;
248 return AVERROR_INVALIDDATA;
249 framerate = av_int2float(bytestream2_get_le32(gb));
250 avctx->framerate.num = framerate * 1000;
251 avctx->framerate.den = 1000;
255 return AVERROR_INVALIDDATA;
257 for (int i = 0; i < 4; i++)
258 s->tile_size[i] = bytestream2_get_le64(gb);
261 av_log(avctx, AV_LOG_DEBUG, "skipping unknown key %u of length %u\n", key, length);
263 bytestream2_skip(gb, length);
267 switch (s->color_model) {
270 avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR16;
274 avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG16;
278 avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB16;
283 avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG16;
287 switch (s->color_model) {
304 return AVERROR_INVALIDDATA;
308 for (int i = 0; i < 4; i++) {
309 if (s->tile_size[i] >= s->data_size)
310 return AVERROR_INVALIDDATA;
313 if (s->tile_size[0] + s->tile_size[1] + s->tile_size[2] + s->tile_size[3] !=
315 return AVERROR_INVALIDDATA;
318 if (!s->data || !s->data_size)
319 return AVERROR_INVALIDDATA;
321 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
324 avctx->bits_per_raw_sample = bps;
326 if (!compressed && s->color_model == 45) {
327 uint16_t *dst = (uint16_t *)p->data[0];
330 bytestream2_init(&gb, s->data, s->data_size);
331 unpack_10bit(&gb, dst, 4, avctx->width, avctx->height, p->linesize[0] / 2);
332 } else if (!compressed) {
334 const int shift = 16 - bps;
336 ret = init_get_bits8(&gbit, s->data, s->data_size);
340 for (int y = 0; y < avctx->height; y++) {
341 uint16_t *dst = (uint16_t *)(p->data[0] + y * p->linesize[0]);
343 if (get_bits_left(&gbit) < avctx->width * bps)
346 for (int x = 0; x < avctx->width; x++)
347 dst[x] = get_bits(&gbit, bps) << shift;
352 for (int tile = 0; tile < 4; tile++) {
353 av_packet_unref(s->jpkt);
354 s->jpkt->data = (uint8_t *)s->data + offset;
355 s->jpkt->size = s->tile_size[tile];
357 ret = avcodec_send_packet(s->jpeg_avctx, s->jpkt);
359 av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
363 ret = avcodec_receive_frame(s->jpeg_avctx, s->jpgframe);
364 if (ret < 0 || s->jpgframe->format != AV_PIX_FMT_GRAY16 ||
365 s->jpeg_avctx->width * 2 != avctx->width ||
366 s->jpeg_avctx->height * 2 != avctx->height) {
368 av_log(avctx, AV_LOG_ERROR,
369 "JPEG decoding error (%d).\n", ret);
371 av_log(avctx, AV_LOG_ERROR,
372 "JPEG invalid format.\n");
373 ret = AVERROR_INVALIDDATA;
376 /* Normally skip, if error explode */
377 if (avctx->err_recognition & AV_EF_EXPLODE)
383 for (int y = 0; y < s->jpeg_avctx->height; y++) {
384 const int hw = s->jpgframe->width / 2;
385 uint16_t *dst = (uint16_t *)(p->data[0] + (y * 2) * p->linesize[0] + tile * hw * 2);
386 const uint16_t *src = (const uint16_t *)(s->jpgframe->data[0] + y * s->jpgframe->linesize[0]);
388 memcpy(dst, src, hw * 2);
390 dst += p->linesize[0] / 2;
391 memcpy(dst, src, hw * 2);
394 av_frame_unref(s->jpgframe);
395 offset += s->tile_size[tile];
399 if (hflip || vflip) {
400 rotation = av_frame_new_side_data(p, AV_FRAME_DATA_DISPLAYMATRIX,
401 sizeof(int32_t) * 9);
403 av_display_rotation_set((int32_t *)rotation->data, 0.f);
404 av_display_matrix_flip((int32_t *)rotation->data, hflip, vflip);
408 p->pict_type = AV_PICTURE_TYPE_I;
416 static av_cold int cri_decode_close(AVCodecContext *avctx)
418 CRIContext *s = avctx->priv_data;
420 av_frame_free(&s->jpgframe);
421 av_packet_free(&s->jpkt);
422 avcodec_free_context(&s->jpeg_avctx);
427 AVCodec ff_cri_decoder = {
429 .type = AVMEDIA_TYPE_VIDEO,
430 .id = AV_CODEC_ID_CRI,
431 .priv_data_size = sizeof(CRIContext),
432 .init = cri_decode_init,
433 .decode = cri_decode_frame,
434 .close = cri_decode_close,
435 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
436 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
437 .long_name = NULL_IF_CONFIG_SMALL("Cintel RAW"),