GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/truehd_core_bsf.c Lines: 71 85 83.5 %
Date: 2020-10-23 17:01:47 Branches: 31 40 77.5 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2018 Paul B Mahol
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#include "bsf.h"
22
#include "bsf_internal.h"
23
#include "get_bits.h"
24
#include "mlp_parse.h"
25
#include "mlp.h"
26
27
typedef struct AccessUnit {
28
    uint8_t bits[4];
29
    uint16_t offset;
30
    uint16_t optional;
31
} AccessUnit;
32
33
typedef struct TrueHDCoreContext {
34
    MLPHeaderInfo hdr;
35
} TrueHDCoreContext;
36
37
256
static int truehd_core_filter(AVBSFContext *ctx, AVPacket *pkt)
38
{
39
256
    TrueHDCoreContext *s = ctx->priv_data;
40
    GetBitContext gbc;
41
    AccessUnit units[MAX_SUBSTREAMS];
42
256
    int ret, i, last_offset = 0;
43
    int in_size, out_size;
44
256
    int have_header = 0;
45
256
    int substream_bytes = 0;
46
    int end;
47
48
256
    ret = ff_bsf_get_packet_ref(ctx, pkt);
49
256
    if (ret < 0)
50
128
        return ret;
51
52
128
    if (pkt->size < 4) {
53
        ret = AVERROR_INVALIDDATA;
54
        goto fail;
55
    }
56
57
128
    in_size = (AV_RB16(pkt->data) & 0xFFF) * 2;
58

128
    if (in_size < 4 || in_size > pkt->size) {
59
        ret = AVERROR_INVALIDDATA;
60
        goto fail;
61
    }
62
63
128
    ret = init_get_bits8(&gbc, pkt->data + 4, pkt->size - 4);
64
128
    if (ret < 0)
65
        goto fail;
66
67
128
    if (show_bits_long(&gbc, 32) == 0xf8726fba) {
68
1
        if ((ret = ff_mlp_read_major_sync(ctx, &s->hdr, &gbc)) < 0)
69
            goto fail;
70
1
        have_header = 1;
71
    }
72
73
128
    if (s->hdr.num_substreams > MAX_SUBSTREAMS) {
74
        ret = AVERROR_INVALIDDATA;
75
        goto fail;
76
    }
77
78
640
    for (i = 0; i < s->hdr.num_substreams; i++) {
79
2560
        for (int j = 0; j < 4; j++)
80
2048
            units[i].bits[j] = get_bits1(&gbc);
81
82
512
        units[i].offset = get_bits(&gbc, 12);
83
512
        if (i < 3) {
84
384
            last_offset = units[i].offset * 2;
85
384
            substream_bytes += 2;
86
        }
87
88
512
        if (units[i].bits[0]) {
89
4
            units[i].optional = get_bits(&gbc, 16);
90
4
            if (i < 3)
91
3
                substream_bytes += 2;
92
        }
93
    }
94
128
    end = get_bits_count(&gbc) >> 3;
95
96
128
    out_size = end + 4 + last_offset;
97
128
    if (out_size < in_size) {
98
128
        int bpos = 0, reduce = end - have_header * 28 - substream_bytes;
99
128
        uint16_t parity_nibble, dts = AV_RB16(pkt->data + 2);
100
        uint16_t auheader;
101
        uint8_t header[28];
102
103
        av_assert1(reduce >= 0 && reduce % 2 == 0);
104
105
128
        if (have_header) {
106
1
            memcpy(header, pkt->data + 4, 28);
107
1
            header[16]  = (header[16] & 0x0c) | (FFMIN(s->hdr.num_substreams, 3) << 4);
108
1
            header[17] &= 0x7f;
109
1
            header[25] &= 0xfe;
110
1
            AV_WL16(header + 26, ff_mlp_checksum16(header, 26));
111
        }
112
113
128
        pkt->data += reduce;
114
128
        out_size  -= reduce;
115
128
        pkt->size  = out_size;
116
117
128
        ret = av_packet_make_writable(pkt);
118
128
        if (ret < 0)
119
            goto fail;
120
121
128
        AV_WB16(pkt->data + 2, dts);
122
128
        parity_nibble = dts;
123
128
        parity_nibble ^= out_size / 2;
124
125
512
        for (i = 0; i < FFMIN(s->hdr.num_substreams, 3); i++) {
126
384
            uint16_t substr_hdr = 0;
127
128
384
            substr_hdr |= (units[i].bits[0] << 15);
129
384
            substr_hdr |= (units[i].bits[1] << 14);
130
384
            substr_hdr |= (units[i].bits[2] << 13);
131
384
            substr_hdr |= (units[i].bits[3] << 12);
132
384
            substr_hdr |=  units[i].offset;
133
134
384
            AV_WB16(pkt->data + have_header * 28 + 4 + bpos, substr_hdr);
135
136
384
            parity_nibble ^= substr_hdr;
137
384
            bpos          += 2;
138
139
384
            if (units[i].bits[0]) {
140
3
                AV_WB16(pkt->data + have_header * 28 + 4 + bpos, units[i].optional);
141
142
3
                parity_nibble ^= units[i].optional;
143
3
                bpos          += 2;
144
            }
145
        }
146
147
128
        parity_nibble ^= parity_nibble >> 8;
148
128
        parity_nibble ^= parity_nibble >> 4;
149
128
        parity_nibble &= 0xF;
150
151
128
        auheader  = (parity_nibble ^ 0xF) << 12;
152
128
        auheader |= (out_size / 2) & 0x0fff;
153
128
        AV_WB16(pkt->data, auheader);
154
155
128
        if (have_header)
156
1
            memcpy(pkt->data + 4, header, 28);
157
    }
158
159
fail:
160
128
    if (ret < 0)
161
        av_packet_unref(pkt);
162
163
128
    return ret;
164
}
165
166
static void truehd_core_flush(AVBSFContext *ctx)
167
{
168
    TrueHDCoreContext *s = ctx->priv_data;
169
    memset(&s->hdr, 0, sizeof(s->hdr));
170
}
171
172
static const enum AVCodecID codec_ids[] = {
173
    AV_CODEC_ID_TRUEHD, AV_CODEC_ID_NONE,
174
};
175
176
const AVBitStreamFilter ff_truehd_core_bsf = {
177
    .name           = "truehd_core",
178
    .priv_data_size = sizeof(TrueHDCoreContext),
179
    .filter         = truehd_core_filter,
180
    .flush          = truehd_core_flush,
181
    .codec_ids      = codec_ids,
182
};