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