GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/notchlc.c Lines: 0 308 0.0 %
Date: 2020-10-23 17:01:47 Branches: 0 176 0.0 %

Line Branch Exec Source
1
/*
2
 * NotchLC decoder
3
 * Copyright (c) 2020 Paul B Mahol
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#define BITSTREAM_READER_LE
27
#include "libavutil/intreadwrite.h"
28
#include "avcodec.h"
29
#include "bytestream.h"
30
#include "get_bits.h"
31
#include "internal.h"
32
#include "lzf.h"
33
#include "thread.h"
34
35
typedef struct NotchLCContext {
36
    unsigned compressed_size;
37
    unsigned format;
38
39
    uint8_t *uncompressed_buffer;
40
    unsigned uncompressed_size;
41
42
    uint8_t *lzf_buffer;
43
    int64_t lzf_size;
44
45
    unsigned texture_size_x;
46
    unsigned texture_size_y;
47
    unsigned y_data_row_offsets;
48
    unsigned uv_offset_data_offset;
49
    unsigned y_control_data_offset;
50
    unsigned a_control_word_offset;
51
    unsigned y_data_offset;
52
    unsigned uv_data_offset;
53
    unsigned y_data_size;
54
    unsigned a_data_offset;
55
    unsigned uv_count_offset;
56
    unsigned a_count_size;
57
    unsigned data_end;
58
59
    GetByteContext gb;
60
    PutByteContext pb;
61
} NotchLCContext;
62
63
static av_cold int decode_init(AVCodecContext *avctx)
64
{
65
    avctx->pix_fmt = AV_PIX_FMT_YUVA444P12;
66
    avctx->color_range = AVCOL_RANGE_JPEG;
67
    avctx->colorspace = AVCOL_SPC_RGB;
68
    avctx->color_primaries = AVCOL_PRI_BT709;
69
    avctx->color_trc = AVCOL_TRC_IEC61966_2_1;
70
71
    return 0;
72
}
73
74
#define HISTORY_SIZE (64 * 1024)
75
76
static int lz4_decompress(AVCodecContext *avctx,
77
                          GetByteContext *gb,
78
                          PutByteContext *pb)
79
{
80
    unsigned reference_pos, match_length, delta, pos = 0;
81
    uint8_t history[64 * 1024];
82
83
    while (bytestream2_get_bytes_left(gb) > 0) {
84
        uint8_t token = bytestream2_get_byte(gb);
85
        unsigned num_literals = token >> 4;
86
87
        if (num_literals == 15) {
88
            unsigned char current;
89
            do {
90
                current = bytestream2_get_byte(gb);
91
                num_literals += current;
92
            } while (current == 255);
93
        }
94
95
        if (pos + num_literals < HISTORY_SIZE) {
96
            bytestream2_get_buffer(gb, history + pos, num_literals);
97
            pos += num_literals;
98
        } else {
99
            while (num_literals-- > 0) {
100
                history[pos++] = bytestream2_get_byte(gb);
101
                if (pos == HISTORY_SIZE) {
102
                    bytestream2_put_buffer(pb, history, HISTORY_SIZE);
103
                    pos = 0;
104
                }
105
            }
106
        }
107
108
        if (bytestream2_get_bytes_left(gb) <= 0)
109
            break;
110
111
        delta = bytestream2_get_le16(gb);
112
        if (delta == 0)
113
            return 0;
114
        match_length = 4 + (token & 0x0F);
115
        if (match_length == 4 + 0x0F) {
116
            uint8_t current;
117
118
            do {
119
                current = bytestream2_get_byte(gb);
120
                match_length += current;
121
            } while (current == 255);
122
        }
123
        reference_pos = (pos >= delta) ? (pos - delta) : (HISTORY_SIZE + pos - delta);
124
        if (pos + match_length < HISTORY_SIZE && reference_pos + match_length < HISTORY_SIZE) {
125
            if (pos >= reference_pos + match_length || reference_pos >= pos + match_length) {
126
                memcpy(history + pos, history + reference_pos, match_length);
127
                pos += match_length;
128
            } else {
129
                while (match_length-- > 0)
130
                    history[pos++] = history[reference_pos++];
131
            }
132
        } else {
133
            while (match_length-- > 0) {
134
                history[pos++] = history[reference_pos++];
135
                if (pos == HISTORY_SIZE) {
136
                    bytestream2_put_buffer(pb, history, HISTORY_SIZE);
137
                    pos = 0;
138
                }
139
                reference_pos %= HISTORY_SIZE;
140
            }
141
        }
142
    }
143
144
    bytestream2_put_buffer(pb, history, pos);
145
146
    return bytestream2_tell_p(pb);
147
}
148
149
static int decode_blocks(AVCodecContext *avctx, AVFrame *p, ThreadFrame *frame,
150
                         unsigned uncompressed_size)
151
{
152
    NotchLCContext *s = avctx->priv_data;
153
    GetByteContext rgb, dgb, *gb = &s->gb;
154
    GetBitContext bit;
155
    int ylinesize, ulinesize, vlinesize, alinesize;
156
    uint16_t *dsty, *dstu, *dstv, *dsta;
157
    int ret;
158
159
    s->texture_size_x = bytestream2_get_le32(gb);
160
    s->texture_size_y = bytestream2_get_le32(gb);
161
162
    ret = ff_set_dimensions(avctx, s->texture_size_x, s->texture_size_y);
163
    if (ret < 0)
164
        return ret;
165
166
    s->uv_offset_data_offset = bytestream2_get_le32(gb);
167
    if (s->uv_offset_data_offset >= UINT_MAX / 4)
168
        return AVERROR_INVALIDDATA;
169
    s->uv_offset_data_offset *= 4;
170
    if (s->uv_offset_data_offset >= uncompressed_size)
171
        return AVERROR_INVALIDDATA;
172
173
    s->y_control_data_offset = bytestream2_get_le32(gb);
174
    if (s->y_control_data_offset >= UINT_MAX / 4)
175
        return AVERROR_INVALIDDATA;
176
    s->y_control_data_offset *= 4;
177
    if (s->y_control_data_offset >= uncompressed_size)
178
        return AVERROR_INVALIDDATA;
179
180
    s->a_control_word_offset = bytestream2_get_le32(gb);
181
    if (s->a_control_word_offset >= UINT_MAX / 4)
182
        return AVERROR_INVALIDDATA;
183
    s->a_control_word_offset *= 4;
184
    if (s->a_control_word_offset >= uncompressed_size)
185
        return AVERROR_INVALIDDATA;
186
187
    s->uv_data_offset = bytestream2_get_le32(gb);
188
    if (s->uv_data_offset >= UINT_MAX / 4)
189
        return AVERROR_INVALIDDATA;
190
    s->uv_data_offset *= 4;
191
    if (s->uv_data_offset >= uncompressed_size)
192
        return AVERROR_INVALIDDATA;
193
194
    s->y_data_size = bytestream2_get_le32(gb);
195
    if (s->y_data_size >= UINT_MAX / 4)
196
        return AVERROR_INVALIDDATA;
197
198
    s->a_data_offset = bytestream2_get_le32(gb);
199
    if (s->a_data_offset >= UINT_MAX / 4)
200
        return AVERROR_INVALIDDATA;
201
    s->a_data_offset *= 4;
202
    if (s->a_data_offset >= uncompressed_size)
203
        return AVERROR_INVALIDDATA;
204
205
    s->a_count_size = bytestream2_get_le32(gb);
206
    if (s->a_count_size >= UINT_MAX / 4)
207
        return AVERROR_INVALIDDATA;
208
    s->a_count_size *= 4;
209
    if (s->a_count_size >= uncompressed_size)
210
        return AVERROR_INVALIDDATA;
211
212
    s->data_end = bytestream2_get_le32(gb);
213
    if (s->data_end > uncompressed_size)
214
        return AVERROR_INVALIDDATA;
215
216
    s->y_data_row_offsets = bytestream2_tell(gb);
217
    if (s->data_end <= s->y_data_size)
218
        return AVERROR_INVALIDDATA;
219
    s->y_data_offset = s->data_end - s->y_data_size;
220
    if (s->y_data_offset <= s->a_data_offset)
221
        return AVERROR_INVALIDDATA;
222
    s->uv_count_offset = s->y_data_offset - s->a_data_offset;
223
224
    if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
225
        return ret;
226
227
    rgb = *gb;
228
    dgb = *gb;
229
    bytestream2_seek(&rgb, s->y_data_row_offsets, SEEK_SET);
230
    bytestream2_seek(gb, s->y_control_data_offset, SEEK_SET);
231
232
    dsty = (uint16_t *)p->data[0];
233
    dsta = (uint16_t *)p->data[3];
234
    ylinesize = p->linesize[0] / 2;
235
    alinesize = p->linesize[3] / 2;
236
237
    for (int y = 0; y < avctx->height; y += 4) {
238
        const unsigned row_offset = bytestream2_get_le32(&rgb);
239
240
        bytestream2_seek(&dgb, s->y_data_offset + row_offset, SEEK_SET);
241
242
        init_get_bits8(&bit, dgb.buffer, bytestream2_get_bytes_left(&dgb));
243
        for (int x = 0; x < avctx->width; x += 4) {
244
            unsigned item = bytestream2_get_le32(gb);
245
            unsigned y_min = item & 4095;
246
            unsigned y_max = (item >> 12) & 4095;
247
            unsigned y_diff = y_max - y_min;
248
            unsigned control[4];
249
250
            control[0] = (item >> 24) & 3;
251
            control[1] = (item >> 26) & 3;
252
            control[2] = (item >> 28) & 3;
253
            control[3] = (item >> 30) & 3;
254
255
            for (int i = 0; i < 4; i++) {
256
                const int nb_bits = control[i] + 1;
257
                const int div = (1 << nb_bits) - 1;
258
                const int add = div - 1;
259
260
                dsty[x + i * ylinesize + 0] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
261
                dsty[x + i * ylinesize + 1] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
262
                dsty[x + i * ylinesize + 2] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
263
                dsty[x + i * ylinesize + 3] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
264
            }
265
        }
266
267
        dsty += 4 * ylinesize;
268
    }
269
270
    rgb = *gb;
271
    dgb = *gb;
272
    bytestream2_seek(gb, s->a_control_word_offset, SEEK_SET);
273
    if (s->uv_count_offset == s->a_control_word_offset) {
274
        for (int y = 0; y < avctx->height; y++) {
275
            for (int x = 0; x < avctx->width; x++)
276
                dsta[x] = 4095;
277
            dsta += alinesize;
278
        }
279
    } else {
280
        for (int y = 0; y < avctx->height; y += 16) {
281
            for (int x = 0; x < avctx->width; x += 16) {
282
                unsigned m = bytestream2_get_le32(gb);
283
                unsigned offset = bytestream2_get_le32(gb);
284
                unsigned alpha0, alpha1;
285
                uint64_t control;
286
287
                if (offset >= UINT_MAX / 4)
288
                    return AVERROR_INVALIDDATA;
289
                offset = offset * 4 + s->uv_data_offset + s->a_data_offset;
290
                if (offset >= s->data_end)
291
                    return AVERROR_INVALIDDATA;
292
293
                bytestream2_seek(&dgb, offset, SEEK_SET);
294
                control = bytestream2_get_le64(&dgb);
295
                alpha0 = control & 0xFF;
296
                alpha1 = (control >> 8) & 0xFF;
297
                control = control >> 16;
298
299
                for (int by = 0; by < 4; by++) {
300
                    for (int bx = 0; bx < 4; bx++) {
301
                        switch (m & 3) {
302
                        case 0:
303
                            for (int i = 0; i < 4; i++) {
304
                                for (int j = 0; j < 4; j++) {
305
                                    dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = 0;
306
                                }
307
                            }
308
                            break;
309
                        case 1:
310
                            for (int i = 0; i < 4; i++) {
311
                                for (int j = 0; j < 4; j++) {
312
                                    dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = 4095;
313
                                }
314
                            }
315
                            break;
316
                        case 2:
317
                            for (int i = 0; i < 4; i++) {
318
                                for (int j = 0; j < 4; j++) {
319
                                    dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = (alpha0 + (alpha1 - alpha0) * (control & 7)) << 4;
320
                                }
321
                            }
322
                            break;
323
                        default:
324
                            return AVERROR_INVALIDDATA;
325
                        }
326
327
                        control >>= 3;
328
                        m >>= 2;
329
                    }
330
                }
331
            }
332
333
            dsta += 16 * alinesize;
334
        }
335
    }
336
337
    bytestream2_seek(&rgb, s->uv_offset_data_offset, SEEK_SET);
338
339
    dstu = (uint16_t *)p->data[1];
340
    dstv = (uint16_t *)p->data[2];
341
    ulinesize = p->linesize[1] / 2;
342
    vlinesize = p->linesize[2] / 2;
343
344
    for (int y = 0; y < avctx->height; y += 16) {
345
        for (int x = 0; x < avctx->width; x += 16) {
346
            unsigned offset = bytestream2_get_le32(&rgb) * 4;
347
            int u[16][16] = { 0 }, v[16][16] = { 0 };
348
            int u0, v0, u1, v1, udif, vdif;
349
            unsigned escape, is8x8, loc;
350
351
            bytestream2_seek(&dgb, s->uv_data_offset + offset, SEEK_SET);
352
353
            is8x8 = bytestream2_get_le16(&dgb);
354
            escape = bytestream2_get_le16(&dgb);
355
356
            if (escape == 0 && is8x8 == 0) {
357
                u0 = bytestream2_get_byte(&dgb);
358
                v0 = bytestream2_get_byte(&dgb);
359
                u1 = bytestream2_get_byte(&dgb);
360
                v1 = bytestream2_get_byte(&dgb);
361
                loc = bytestream2_get_le32(&dgb);
362
                u0 = (u0 << 4) | (u0 & 0xF);
363
                v0 = (v0 << 4) | (v0 & 0xF);
364
                u1 = (u1 << 4) | (u1 & 0xF);
365
                v1 = (v1 << 4) | (v1 & 0xF);
366
                udif = u1 - u0;
367
                vdif = v1 - v0;
368
369
                for (int i = 0; i < 16; i += 4) {
370
                    for (int j = 0; j < 16; j += 4) {
371
                        for (int ii = 0; ii < 4; ii++) {
372
                            for (int jj = 0; jj < 4; jj++) {
373
                                u[i + ii][j + jj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
374
                                v[i + ii][j + jj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
375
                            }
376
                        }
377
378
                        loc >>= 2;
379
                    }
380
                }
381
            } else {
382
                for (int i = 0; i < 16; i += 8) {
383
                    for (int j = 0; j < 16; j += 8) {
384
                        if (is8x8 & 1) {
385
                            u0 = bytestream2_get_byte(&dgb);
386
                            v0 = bytestream2_get_byte(&dgb);
387
                            u1 = bytestream2_get_byte(&dgb);
388
                            v1 = bytestream2_get_byte(&dgb);
389
                            loc = bytestream2_get_le32(&dgb);
390
                            u0 = (u0 << 4) | (u0 & 0xF);
391
                            v0 = (v0 << 4) | (v0 & 0xF);
392
                            u1 = (u1 << 4) | (u1 & 0xF);
393
                            v1 = (v1 << 4) | (v1 & 0xF);
394
                            udif = u1 - u0;
395
                            vdif = v1 - v0;
396
397
                            for (int ii = 0; ii < 8; ii += 2) {
398
                                for (int jj = 0; jj < 8; jj += 2) {
399
                                    for (int iii = 0; iii < 2; iii++) {
400
                                        for (int jjj = 0; jjj < 2; jjj++) {
401
                                            u[i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
402
                                            v[i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
403
                                        }
404
                                    }
405
406
                                    loc >>= 2;
407
                                }
408
                            }
409
                        } else if (escape) {
410
                            for (int ii = 0; ii < 8; ii += 4) {
411
                                for (int jj = 0; jj < 8; jj += 4) {
412
                                    u0 = bytestream2_get_byte(&dgb);
413
                                    v0 = bytestream2_get_byte(&dgb);
414
                                    u1 = bytestream2_get_byte(&dgb);
415
                                    v1 = bytestream2_get_byte(&dgb);
416
                                    loc = bytestream2_get_le32(&dgb);
417
                                    u0 = (u0 << 4) | (u0 & 0xF);
418
                                    v0 = (v0 << 4) | (v0 & 0xF);
419
                                    u1 = (u1 << 4) | (u1 & 0xF);
420
                                    v1 = (v1 << 4) | (v1 & 0xF);
421
                                    udif = u1 - u0;
422
                                    vdif = v1 - v0;
423
424
                                    for (int iii = 0; iii < 4; iii++) {
425
                                        for (int jjj = 0; jjj < 4; jjj++) {
426
                                            u[i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
427
                                            v[i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
428
429
                                            loc >>= 2;
430
                                        }
431
                                    }
432
                                }
433
                            }
434
                        }
435
436
                        is8x8 >>= 1;
437
                    }
438
                }
439
            }
440
441
            for (int i = 0; i < 16; i++) {
442
                for (int j = 0; j < 16; j++) {
443
                    dstu[x + i * ulinesize + j] = u[i][j];
444
                    dstv[x + i * vlinesize + j] = v[i][j];
445
                }
446
            }
447
        }
448
449
        dstu += 16 * ulinesize;
450
        dstv += 16 * vlinesize;
451
    }
452
453
    return 0;
454
}
455
456
static int decode_frame(AVCodecContext *avctx,
457
                        void *data, int *got_frame,
458
                        AVPacket *avpkt)
459
{
460
    NotchLCContext *s = avctx->priv_data;
461
    ThreadFrame frame = { .f = data };
462
    GetByteContext *gb = &s->gb;
463
    PutByteContext *pb = &s->pb;
464
    unsigned uncompressed_size;
465
    AVFrame *p = data;
466
    int ret;
467
468
    if (avpkt->size <= 40)
469
        return AVERROR_INVALIDDATA;
470
471
    bytestream2_init(gb, avpkt->data, avpkt->size);
472
473
    if (bytestream2_get_le32(gb) != MKBETAG('N','L','C','1'))
474
        return AVERROR_INVALIDDATA;
475
476
    uncompressed_size = bytestream2_get_le32(gb);
477
    s->compressed_size = bytestream2_get_le32(gb);
478
    s->format = bytestream2_get_le32(gb);
479
480
    if (s->format > 2)
481
        return AVERROR_PATCHWELCOME;
482
483
    if (s->format == 0) {
484
        ret = ff_lzf_uncompress(gb, &s->lzf_buffer, &s->lzf_size);
485
        if (ret < 0)
486
            return ret;
487
488
        if (uncompressed_size > s->lzf_size)
489
            return AVERROR_INVALIDDATA;
490
491
        bytestream2_init(gb, s->lzf_buffer, uncompressed_size);
492
    } else if (s->format == 1) {
493
        av_fast_padded_malloc(&s->uncompressed_buffer, &s->uncompressed_size,
494
                              uncompressed_size);
495
        if (!s->uncompressed_buffer)
496
            return AVERROR(ENOMEM);
497
498
        bytestream2_init_writer(pb, s->uncompressed_buffer, s->uncompressed_size);
499
500
        ret = lz4_decompress(avctx, gb, pb);
501
        if (ret != uncompressed_size)
502
            return AVERROR_INVALIDDATA;
503
504
        bytestream2_init(gb, s->uncompressed_buffer, uncompressed_size);
505
    }
506
507
    ret = decode_blocks(avctx, p, &frame, uncompressed_size);
508
    if (ret < 0)
509
        return ret;
510
511
    p->pict_type = AV_PICTURE_TYPE_I;
512
    p->key_frame = 1;
513
514
    *got_frame = 1;
515
516
    return avpkt->size;
517
}
518
519
static av_cold int decode_end(AVCodecContext *avctx)
520
{
521
    NotchLCContext *s = avctx->priv_data;
522
523
    av_freep(&s->uncompressed_buffer);
524
    s->uncompressed_size = 0;
525
    av_freep(&s->lzf_buffer);
526
    s->lzf_size = 0;
527
528
    return 0;
529
}
530
531
AVCodec ff_notchlc_decoder = {
532
    .name             = "notchlc",
533
    .long_name        = NULL_IF_CONFIG_SMALL("NotchLC"),
534
    .type             = AVMEDIA_TYPE_VIDEO,
535
    .id               = AV_CODEC_ID_NOTCHLC,
536
    .priv_data_size   = sizeof(NotchLCContext),
537
    .init             = decode_init,
538
    .close            = decode_end,
539
    .decode           = decode_frame,
540
    .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
541
};