FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cri.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 0 270 0.0%
Functions: 0 4 0.0%
Branches: 0 149 0.0%

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