| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * CRI image decoder | ||
| 3 | * | ||
| 4 | * Copyright (c) 2020 Paul B Mahol | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 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. | ||
| 12 | * | ||
| 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. | ||
| 17 | * | ||
| 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 | ||
| 21 | */ | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @file | ||
| 25 | * Cintel RAW image decoder | ||
| 26 | */ | ||
| 27 | |||
| 28 | #define BITSTREAM_READER_LE | ||
| 29 | |||
| 30 | #include "libavutil/attributes_internal.h" | ||
| 31 | #include "libavutil/intfloat.h" | ||
| 32 | #include "libavutil/display.h" | ||
| 33 | #include "avcodec.h" | ||
| 34 | #include "bytestream.h" | ||
| 35 | #include "codec_internal.h" | ||
| 36 | #include "decode.h" | ||
| 37 | #include "get_bits.h" | ||
| 38 | #include "thread.h" | ||
| 39 | |||
| 40 | typedef struct CRIContext { | ||
| 41 | AVCodecContext *jpeg_avctx; // wrapper context for MJPEG | ||
| 42 | AVPacket *jpkt; // encoded JPEG tile | ||
| 43 | AVFrame *jpgframe; // decoded JPEG tile | ||
| 44 | |||
| 45 | GetByteContext gb; | ||
| 46 | int color_model; | ||
| 47 | const uint8_t *data; | ||
| 48 | unsigned data_size; | ||
| 49 | uint64_t tile_size[4]; | ||
| 50 | } CRIContext; | ||
| 51 | |||
| 52 | ✗ | static av_cold int cri_decode_init(AVCodecContext *avctx) | |
| 53 | { | ||
| 54 | ✗ | CRIContext *s = avctx->priv_data; | |
| 55 | int ret; | ||
| 56 | |||
| 57 | ✗ | s->jpgframe = av_frame_alloc(); | |
| 58 | ✗ | if (!s->jpgframe) | |
| 59 | ✗ | return AVERROR(ENOMEM); | |
| 60 | |||
| 61 | ✗ | s->jpkt = av_packet_alloc(); | |
| 62 | ✗ | if (!s->jpkt) | |
| 63 | ✗ | return AVERROR(ENOMEM); | |
| 64 | |||
| 65 | EXTERN const FFCodec ff_mjpeg_decoder; | ||
| 66 | ✗ | s->jpeg_avctx = avcodec_alloc_context3(&ff_mjpeg_decoder.p); | |
| 67 | ✗ | if (!s->jpeg_avctx) | |
| 68 | ✗ | return AVERROR(ENOMEM); | |
| 69 | ✗ | s->jpeg_avctx->flags = avctx->flags; | |
| 70 | ✗ | s->jpeg_avctx->flags2 = avctx->flags2; | |
| 71 | ✗ | s->jpeg_avctx->idct_algo = avctx->idct_algo; | |
| 72 | ✗ | ret = avcodec_open2(s->jpeg_avctx, NULL, NULL); | |
| 73 | ✗ | if (ret < 0) | |
| 74 | ✗ | return ret; | |
| 75 | |||
| 76 | ✗ | return 0; | |
| 77 | } | ||
| 78 | |||
| 79 | ✗ | static void unpack_10bit(GetByteContext *gb, uint16_t *dst, int shift, | |
| 80 | int w, int h, ptrdiff_t stride) | ||
| 81 | { | ||
| 82 | ✗ | int count = w * h; | |
| 83 | ✗ | int pos = 0; | |
| 84 | |||
| 85 | ✗ | while (count > 0) { | |
| 86 | uint32_t a0, a1, a2, a3; | ||
| 87 | ✗ | if (bytestream2_get_bytes_left(gb) < 4) | |
| 88 | ✗ | break; | |
| 89 | ✗ | a0 = bytestream2_get_le32(gb); | |
| 90 | ✗ | a1 = bytestream2_get_le32(gb); | |
| 91 | ✗ | a2 = bytestream2_get_le32(gb); | |
| 92 | ✗ | a3 = bytestream2_get_le32(gb); | |
| 93 | ✗ | dst[pos] = (((a0 >> 1) & 0xE00) | (a0 & 0x1FF)) << shift; | |
| 94 | ✗ | pos++; | |
| 95 | ✗ | if (pos >= w) { | |
| 96 | ✗ | if (count == 1) | |
| 97 | ✗ | break; | |
| 98 | ✗ | dst += stride; | |
| 99 | ✗ | pos = 0; | |
| 100 | } | ||
| 101 | ✗ | dst[pos] = (((a0 >> 13) & 0x3F) | ((a0 >> 14) & 0xFC0)) << shift; | |
| 102 | ✗ | pos++; | |
| 103 | ✗ | if (pos >= w) { | |
| 104 | ✗ | if (count == 2) | |
| 105 | ✗ | break; | |
| 106 | ✗ | dst += stride; | |
| 107 | ✗ | pos = 0; | |
| 108 | } | ||
| 109 | ✗ | dst[pos] = (((a0 >> 26) & 7) | ((a1 & 0x1FF) << 3)) << shift; | |
| 110 | ✗ | pos++; | |
| 111 | ✗ | if (pos >= w) { | |
| 112 | ✗ | if (count == 3) | |
| 113 | ✗ | break; | |
| 114 | ✗ | dst += stride; | |
| 115 | ✗ | pos = 0; | |
| 116 | } | ||
| 117 | ✗ | dst[pos] = (((a1 >> 10) & 0x1FF) | ((a1 >> 11) & 0xE00)) << shift; | |
| 118 | ✗ | pos++; | |
| 119 | ✗ | if (pos >= w) { | |
| 120 | ✗ | if (count == 4) | |
| 121 | ✗ | break; | |
| 122 | ✗ | dst += stride; | |
| 123 | ✗ | pos = 0; | |
| 124 | } | ||
| 125 | ✗ | dst[pos] = (((a1 >> 23) & 0x3F) | ((a2 & 0x3F) << 6)) << shift; | |
| 126 | ✗ | pos++; | |
| 127 | ✗ | if (pos >= w) { | |
| 128 | ✗ | if (count == 5) | |
| 129 | ✗ | break; | |
| 130 | ✗ | dst += stride; | |
| 131 | ✗ | pos = 0; | |
| 132 | } | ||
| 133 | ✗ | dst[pos] = (((a2 >> 7) & 0xFF8) | ((a2 >> 6) & 7)) << shift; | |
| 134 | ✗ | pos++; | |
| 135 | ✗ | if (pos >= w) { | |
| 136 | ✗ | if (count == 6) | |
| 137 | ✗ | break; | |
| 138 | ✗ | dst += stride; | |
| 139 | ✗ | pos = 0; | |
| 140 | } | ||
| 141 | ✗ | dst[pos] = (((a3 & 7) << 9) | ((a2 >> 20) & 0x1FF)) << shift; | |
| 142 | ✗ | pos++; | |
| 143 | ✗ | if (pos >= w) { | |
| 144 | ✗ | if (count == 7) | |
| 145 | ✗ | break; | |
| 146 | ✗ | dst += stride; | |
| 147 | ✗ | pos = 0; | |
| 148 | } | ||
| 149 | ✗ | dst[pos] = (((a3 >> 4) & 0xFC0) | ((a3 >> 3) & 0x3F)) << shift; | |
| 150 | ✗ | pos++; | |
| 151 | ✗ | if (pos >= w) { | |
| 152 | ✗ | if (count == 8) | |
| 153 | ✗ | break; | |
| 154 | ✗ | dst += stride; | |
| 155 | ✗ | pos = 0; | |
| 156 | } | ||
| 157 | ✗ | dst[pos] = (((a3 >> 16) & 7) | ((a3 >> 17) & 0xFF8)) << shift; | |
| 158 | ✗ | pos++; | |
| 159 | ✗ | if (pos >= w) { | |
| 160 | ✗ | if (count == 9) | |
| 161 | ✗ | break; | |
| 162 | ✗ | dst += stride; | |
| 163 | ✗ | pos = 0; | |
| 164 | } | ||
| 165 | |||
| 166 | ✗ | count -= 9; | |
| 167 | } | ||
| 168 | ✗ | } | |
| 169 | |||
| 170 | ✗ | static int cri_decode_frame(AVCodecContext *avctx, AVFrame *p, | |
| 171 | int *got_frame, AVPacket *avpkt) | ||
| 172 | { | ||
| 173 | ✗ | CRIContext *s = avctx->priv_data; | |
| 174 | ✗ | GetByteContext *gb = &s->gb; | |
| 175 | ✗ | int ret, bps, hflip = 0, vflip = 0; | |
| 176 | AVFrameSideData *rotation; | ||
| 177 | ✗ | int compressed = 0; | |
| 178 | |||
| 179 | ✗ | s->data = NULL; | |
| 180 | ✗ | s->data_size = 0; | |
| 181 | |||
| 182 | ✗ | bytestream2_init(gb, avpkt->data, avpkt->size); | |
| 183 | |||
| 184 | ✗ | while (bytestream2_get_bytes_left(gb) > 8) { | |
| 185 | char codec_name[1024]; | ||
| 186 | uint32_t key, length; | ||
| 187 | float framerate; | ||
| 188 | int width, height; | ||
| 189 | |||
| 190 | ✗ | key = bytestream2_get_le32(gb); | |
| 191 | ✗ | length = bytestream2_get_le32(gb); | |
| 192 | |||
| 193 | ✗ | switch (key) { | |
| 194 | ✗ | case 1: | |
| 195 | ✗ | if (length != 4) | |
| 196 | ✗ | return AVERROR_INVALIDDATA; | |
| 197 | |||
| 198 | ✗ | if (bytestream2_get_le32(gb) != MKTAG('D', 'V', 'C', 'C')) | |
| 199 | ✗ | return AVERROR_INVALIDDATA; | |
| 200 | ✗ | break; | |
| 201 | ✗ | case 100: | |
| 202 | ✗ | if (length < 16) | |
| 203 | ✗ | return AVERROR_INVALIDDATA; | |
| 204 | ✗ | width = bytestream2_get_le32(gb); | |
| 205 | ✗ | height = bytestream2_get_le32(gb); | |
| 206 | ✗ | s->color_model = bytestream2_get_le32(gb); | |
| 207 | ✗ | if (bytestream2_get_le32(gb) != 1) | |
| 208 | ✗ | return AVERROR_INVALIDDATA; | |
| 209 | ✗ | ret = ff_set_dimensions(avctx, width, height); | |
| 210 | ✗ | if (ret < 0) | |
| 211 | ✗ | return ret; | |
| 212 | ✗ | length -= 16; | |
| 213 | ✗ | goto skip; | |
| 214 | ✗ | case 101: | |
| 215 | ✗ | if (length != 4) | |
| 216 | ✗ | return AVERROR_INVALIDDATA; | |
| 217 | |||
| 218 | ✗ | if (bytestream2_get_le32(gb) != 0) | |
| 219 | ✗ | return AVERROR_INVALIDDATA; | |
| 220 | ✗ | break; | |
| 221 | ✗ | case 102:; | |
| 222 | ✗ | int read_len = FFMIN(length, sizeof(codec_name) - 1); | |
| 223 | ✗ | if (read_len != bytestream2_get_buffer(gb, codec_name, read_len)) | |
| 224 | ✗ | return AVERROR_INVALIDDATA; | |
| 225 | ✗ | length -= read_len; | |
| 226 | ✗ | if (strncmp(codec_name, "cintel_craw", read_len)) | |
| 227 | ✗ | return AVERROR_INVALIDDATA; | |
| 228 | ✗ | compressed = 1; | |
| 229 | ✗ | goto skip; | |
| 230 | ✗ | case 103: | |
| 231 | ✗ | if (bytestream2_get_bytes_left(gb) < length) | |
| 232 | ✗ | return AVERROR_INVALIDDATA; | |
| 233 | ✗ | s->data = gb->buffer; | |
| 234 | ✗ | s->data_size = length; | |
| 235 | ✗ | goto skip; | |
| 236 | ✗ | case 105: | |
| 237 | ✗ | if (length <= 0) | |
| 238 | ✗ | return AVERROR_INVALIDDATA; | |
| 239 | ✗ | hflip = bytestream2_get_byte(gb) != 0; | |
| 240 | ✗ | length--; | |
| 241 | ✗ | goto skip; | |
| 242 | ✗ | case 106: | |
| 243 | ✗ | if (length <= 0) | |
| 244 | ✗ | return AVERROR_INVALIDDATA; | |
| 245 | ✗ | vflip = bytestream2_get_byte(gb) != 0; | |
| 246 | ✗ | length--; | |
| 247 | ✗ | goto skip; | |
| 248 | ✗ | case 107: | |
| 249 | ✗ | if (length != 4) | |
| 250 | ✗ | return AVERROR_INVALIDDATA; | |
| 251 | ✗ | framerate = av_int2float(bytestream2_get_le32(gb)); | |
| 252 | ✗ | avctx->framerate.num = framerate * 1000; | |
| 253 | ✗ | avctx->framerate.den = 1000; | |
| 254 | ✗ | break; | |
| 255 | ✗ | case 119: | |
| 256 | ✗ | if (length != 32) | |
| 257 | ✗ | return AVERROR_INVALIDDATA; | |
| 258 | |||
| 259 | ✗ | for (int i = 0; i < 4; i++) | |
| 260 | ✗ | s->tile_size[i] = bytestream2_get_le64(gb); | |
| 261 | ✗ | break; | |
| 262 | ✗ | default: | |
| 263 | ✗ | av_log(avctx, AV_LOG_DEBUG, "skipping unknown key %u of length %u\n", key, length); | |
| 264 | ✗ | skip: | |
| 265 | ✗ | bytestream2_skip(gb, length); | |
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | ✗ | switch (s->color_model) { | |
| 270 | ✗ | case 76: | |
| 271 | case 88: | ||
| 272 | ✗ | avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR16; | |
| 273 | ✗ | break; | |
| 274 | ✗ | case 77: | |
| 275 | case 89: | ||
| 276 | ✗ | avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG16; | |
| 277 | ✗ | break; | |
| 278 | ✗ | case 78: | |
| 279 | case 90: | ||
| 280 | ✗ | avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB16; | |
| 281 | ✗ | break; | |
| 282 | ✗ | case 45: | |
| 283 | case 79: | ||
| 284 | case 91: | ||
| 285 | ✗ | avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG16; | |
| 286 | ✗ | break; | |
| 287 | } | ||
| 288 | |||
| 289 | ✗ | switch (s->color_model) { | |
| 290 | ✗ | case 45: | |
| 291 | ✗ | bps = 10; | |
| 292 | ✗ | break; | |
| 293 | ✗ | case 76: | |
| 294 | case 77: | ||
| 295 | case 78: | ||
| 296 | case 79: | ||
| 297 | ✗ | bps = 12; | |
| 298 | ✗ | break; | |
| 299 | ✗ | case 88: | |
| 300 | case 89: | ||
| 301 | case 90: | ||
| 302 | case 91: | ||
| 303 | ✗ | bps = 16; | |
| 304 | ✗ | break; | |
| 305 | ✗ | default: | |
| 306 | ✗ | return AVERROR_INVALIDDATA; | |
| 307 | } | ||
| 308 | |||
| 309 | ✗ | if (compressed) { | |
| 310 | ✗ | for (int i = 0; i < 4; i++) { | |
| 311 | ✗ | if (s->tile_size[i] >= s->data_size) | |
| 312 | ✗ | return AVERROR_INVALIDDATA; | |
| 313 | } | ||
| 314 | |||
| 315 | ✗ | if (s->tile_size[0] + s->tile_size[1] + s->tile_size[2] + s->tile_size[3] != | |
| 316 | ✗ | s->data_size) | |
| 317 | ✗ | return AVERROR_INVALIDDATA; | |
| 318 | } | ||
| 319 | |||
| 320 | ✗ | if (!s->data || !s->data_size) | |
| 321 | ✗ | return AVERROR_INVALIDDATA; | |
| 322 | |||
| 323 | ✗ | if (avctx->skip_frame >= AVDISCARD_ALL) | |
| 324 | ✗ | return avpkt->size; | |
| 325 | |||
| 326 | ✗ | if ((ret = ff_thread_get_buffer(avctx, p, 0)) < 0) | |
| 327 | ✗ | return ret; | |
| 328 | |||
| 329 | ✗ | avctx->bits_per_raw_sample = bps; | |
| 330 | |||
| 331 | ✗ | if (!compressed && s->color_model == 45) { | |
| 332 | ✗ | uint16_t *dst = (uint16_t *)p->data[0]; | |
| 333 | GetByteContext gb; | ||
| 334 | |||
| 335 | ✗ | bytestream2_init(&gb, s->data, s->data_size); | |
| 336 | ✗ | unpack_10bit(&gb, dst, 4, avctx->width, avctx->height, p->linesize[0] / 2); | |
| 337 | ✗ | } else if (!compressed) { | |
| 338 | GetBitContext gbit; | ||
| 339 | ✗ | const int shift = 16 - bps; | |
| 340 | |||
| 341 | ✗ | ret = init_get_bits8(&gbit, s->data, s->data_size); | |
| 342 | ✗ | if (ret < 0) | |
| 343 | ✗ | return ret; | |
| 344 | |||
| 345 | ✗ | for (int y = 0; y < avctx->height; y++) { | |
| 346 | ✗ | uint16_t *dst = (uint16_t *)(p->data[0] + y * p->linesize[0]); | |
| 347 | |||
| 348 | ✗ | if (get_bits_left(&gbit) < avctx->width * bps) | |
| 349 | ✗ | break; | |
| 350 | |||
| 351 | ✗ | for (int x = 0; x < avctx->width; x++) | |
| 352 | ✗ | dst[x] = get_bits(&gbit, bps) << shift; | |
| 353 | } | ||
| 354 | } else { | ||
| 355 | ✗ | unsigned offset = 0; | |
| 356 | |||
| 357 | ✗ | for (int tile = 0; tile < 4; tile++) { | |
| 358 | ✗ | av_packet_unref(s->jpkt); | |
| 359 | ✗ | s->jpkt->data = (uint8_t *)s->data + offset; | |
| 360 | ✗ | s->jpkt->size = s->tile_size[tile]; | |
| 361 | |||
| 362 | ✗ | ret = avcodec_send_packet(s->jpeg_avctx, s->jpkt); | |
| 363 | ✗ | if (ret < 0) { | |
| 364 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n"); | |
| 365 | ✗ | return ret; | |
| 366 | } | ||
| 367 | |||
| 368 | ✗ | ret = avcodec_receive_frame(s->jpeg_avctx, s->jpgframe); | |
| 369 | ✗ | if (ret < 0 || s->jpgframe->format != AV_PIX_FMT_GRAY16 || | |
| 370 | ✗ | s->jpeg_avctx->width * 2 != avctx->width || | |
| 371 | ✗ | s->jpeg_avctx->height * 2 != avctx->height) { | |
| 372 | ✗ | if (ret < 0) { | |
| 373 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 374 | "JPEG decoding error (%d).\n", ret); | ||
| 375 | } else { | ||
| 376 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 377 | "JPEG invalid format.\n"); | ||
| 378 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 379 | } | ||
| 380 | |||
| 381 | /* Normally skip, if error explode */ | ||
| 382 | ✗ | if (avctx->err_recognition & AV_EF_EXPLODE) | |
| 383 | ✗ | return ret; | |
| 384 | else | ||
| 385 | ✗ | return 0; | |
| 386 | } | ||
| 387 | |||
| 388 | ✗ | for (int y = 0; y < s->jpeg_avctx->height; y++) { | |
| 389 | ✗ | const int hw = s->jpgframe->width / 2; | |
| 390 | ✗ | uint16_t *dst = (uint16_t *)(p->data[0] + (y * 2) * p->linesize[0] + tile * hw * 2); | |
| 391 | ✗ | const uint16_t *src = (const uint16_t *)(s->jpgframe->data[0] + y * s->jpgframe->linesize[0]); | |
| 392 | |||
| 393 | ✗ | memcpy(dst, src, hw * 2); | |
| 394 | ✗ | src += hw; | |
| 395 | ✗ | dst += p->linesize[0] / 2; | |
| 396 | ✗ | memcpy(dst, src, hw * 2); | |
| 397 | } | ||
| 398 | |||
| 399 | ✗ | av_frame_unref(s->jpgframe); | |
| 400 | ✗ | offset += s->tile_size[tile]; | |
| 401 | } | ||
| 402 | } | ||
| 403 | |||
| 404 | ✗ | if (hflip || vflip) { | |
| 405 | ✗ | ff_frame_new_side_data(avctx, p, AV_FRAME_DATA_DISPLAYMATRIX, | |
| 406 | sizeof(int32_t) * 9, &rotation); | ||
| 407 | ✗ | if (rotation) { | |
| 408 | ✗ | av_display_rotation_set((int32_t *)rotation->data, 0.f); | |
| 409 | ✗ | av_display_matrix_flip((int32_t *)rotation->data, hflip, vflip); | |
| 410 | } | ||
| 411 | } | ||
| 412 | |||
| 413 | ✗ | *got_frame = 1; | |
| 414 | |||
| 415 | ✗ | return 0; | |
| 416 | } | ||
| 417 | |||
| 418 | ✗ | static av_cold int cri_decode_close(AVCodecContext *avctx) | |
| 419 | { | ||
| 420 | ✗ | CRIContext *s = avctx->priv_data; | |
| 421 | |||
| 422 | ✗ | av_frame_free(&s->jpgframe); | |
| 423 | ✗ | av_packet_free(&s->jpkt); | |
| 424 | ✗ | avcodec_free_context(&s->jpeg_avctx); | |
| 425 | |||
| 426 | ✗ | return 0; | |
| 427 | } | ||
| 428 | |||
| 429 | const FFCodec ff_cri_decoder = { | ||
| 430 | .p.name = "cri", | ||
| 431 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 432 | .p.id = AV_CODEC_ID_CRI, | ||
| 433 | .priv_data_size = sizeof(CRIContext), | ||
| 434 | .init = cri_decode_init, | ||
| 435 | FF_CODEC_DECODE_CB(cri_decode_frame), | ||
| 436 | .close = cri_decode_close, | ||
| 437 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, | ||
| 438 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | | ||
| 439 | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
| 440 | CODEC_LONG_NAME("Cintel RAW"), | ||
| 441 | }; | ||
| 442 |