GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/tak.c Lines: 61 66 92.4 %
Date: 2020-08-14 10:39:37 Branches: 26 32 81.2 %

Line Branch Exec Source
1
/*
2
 * TAK common code
3
 * Copyright (c) 2012 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 "libavutil/crc.h"
23
#include "libavutil/intreadwrite.h"
24
25
#define BITSTREAM_READER_LE
26
#include "tak.h"
27
28
static const int64_t tak_channel_layouts[] = {
29
    0,
30
    AV_CH_FRONT_LEFT,
31
    AV_CH_FRONT_RIGHT,
32
    AV_CH_FRONT_CENTER,
33
    AV_CH_LOW_FREQUENCY,
34
    AV_CH_BACK_LEFT,
35
    AV_CH_BACK_RIGHT,
36
    AV_CH_FRONT_LEFT_OF_CENTER,
37
    AV_CH_FRONT_RIGHT_OF_CENTER,
38
    AV_CH_BACK_CENTER,
39
    AV_CH_SIDE_LEFT,
40
    AV_CH_SIDE_RIGHT,
41
    AV_CH_TOP_CENTER,
42
    AV_CH_TOP_FRONT_LEFT,
43
    AV_CH_TOP_FRONT_CENTER,
44
    AV_CH_TOP_FRONT_RIGHT,
45
    AV_CH_TOP_BACK_LEFT,
46
    AV_CH_TOP_BACK_CENTER,
47
    AV_CH_TOP_BACK_RIGHT,
48
};
49
50
static const uint16_t frame_duration_type_quants[] = {
51
    3, 4, 6, 8, 4096, 8192, 16384, 512, 1024, 2048,
52
};
53
54
22
static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type)
55
{
56
    int nb_samples, max_nb_samples;
57
58
22
    if (type <= TAK_FST_250ms) {
59
20
        nb_samples     = sample_rate * frame_duration_type_quants[type] >>
60
                         TAK_FRAME_DURATION_QUANT_SHIFT;
61
20
        max_nb_samples = 16384;
62
2
    } else if (type < FF_ARRAY_ELEMS(frame_duration_type_quants)) {
63
1
        nb_samples     = frame_duration_type_quants[type];
64
1
        max_nb_samples = sample_rate *
65
1
                         frame_duration_type_quants[TAK_FST_250ms] >>
66
                         TAK_FRAME_DURATION_QUANT_SHIFT;
67
    } else {
68
1
        return AVERROR_INVALIDDATA;
69
    }
70
71

21
    if (nb_samples <= 0 || nb_samples > max_nb_samples)
72
        return AVERROR_INVALIDDATA;
73
74
21
    return nb_samples;
75
}
76
77
83
int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
78
{
79
    uint32_t crc, CRC;
80
81
83
    if (buf_size < 4)
82
        return AVERROR_INVALIDDATA;
83
83
    buf_size -= 3;
84
85
83
    CRC = AV_RB24(buf + buf_size);
86
83
    crc = av_crc(av_crc_get_table(AV_CRC_24_IEEE), 0xCE04B7U, buf, buf_size);
87
83
    if (CRC != crc)
88
8
        return AVERROR_INVALIDDATA;
89
90
75
    return 0;
91
}
92
93
22
void ff_tak_parse_streaminfo(TAKStreamInfo *s, GetBitContext *gb)
94
{
95
22
    uint64_t channel_mask = 0;
96
    int frame_type, i;
97
98
22
    s->codec = get_bits(gb, TAK_ENCODER_CODEC_BITS);
99
22
    skip_bits(gb, TAK_ENCODER_PROFILE_BITS);
100
101
22
    frame_type = get_bits(gb, TAK_SIZE_FRAME_DURATION_BITS);
102
22
    s->samples = get_bits64(gb, TAK_SIZE_SAMPLES_NUM_BITS);
103
104
22
    s->data_type   = get_bits(gb, TAK_FORMAT_DATA_TYPE_BITS);
105
22
    s->sample_rate = get_bits(gb, TAK_FORMAT_SAMPLE_RATE_BITS) +
106
                     TAK_SAMPLE_RATE_MIN;
107
22
    s->bps         = get_bits(gb, TAK_FORMAT_BPS_BITS) +
108
                     TAK_BPS_MIN;
109
22
    s->channels    = get_bits(gb, TAK_FORMAT_CHANNEL_BITS) +
110
                     TAK_CHANNELS_MIN;
111
112
22
    if (get_bits1(gb)) {
113
2
        skip_bits(gb, TAK_FORMAT_VALID_BITS);
114
2
        if (get_bits1(gb)) {
115
23
            for (i = 0; i < s->channels; i++) {
116
21
                int value = get_bits(gb, TAK_FORMAT_CH_LAYOUT_BITS);
117
118
21
                if (value < FF_ARRAY_ELEMS(tak_channel_layouts))
119
7
                    channel_mask |= tak_channel_layouts[value];
120
            }
121
        }
122
    }
123
124
22
    s->ch_layout     = channel_mask;
125
22
    s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
126
22
}
127
128
1
int avpriv_tak_parse_streaminfo(TAKStreamInfo *s, const uint8_t *buf, int size)
129
{
130
    GetBitContext gb;
131
1
    int ret = init_get_bits8(&gb, buf, size);
132
133
1
    if (ret < 0)
134
        return AVERROR_INVALIDDATA;
135
136
1
    ff_tak_parse_streaminfo(s, &gb);
137
138
1
    return 0;
139
}
140
141
124
int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
142
                               TAKStreamInfo *ti, int log_level_offset)
143
{
144
124
    if (get_bits(gb, TAK_FRAME_HEADER_SYNC_ID_BITS) != TAK_FRAME_HEADER_SYNC_ID) {
145
        av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n");
146
        return AVERROR_INVALIDDATA;
147
    }
148
149
124
    ti->flags     = get_bits(gb, TAK_FRAME_HEADER_FLAGS_BITS);
150
124
    ti->frame_num = get_bits(gb, TAK_FRAME_HEADER_NO_BITS);
151
152
124
    if (ti->flags & TAK_FRAME_FLAG_IS_LAST) {
153
12
        ti->last_frame_samples = get_bits(gb, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + 1;
154
12
        skip_bits(gb, 2);
155
    } else {
156
112
        ti->last_frame_samples = 0;
157
    }
158
159
124
    if (ti->flags & TAK_FRAME_FLAG_HAS_INFO) {
160
21
        ff_tak_parse_streaminfo(ti, gb);
161
162
21
        if (get_bits(gb, 6))
163
3
            skip_bits(gb, 25);
164
21
        align_get_bits(gb);
165
    }
166
167
124
    if (ti->flags & TAK_FRAME_FLAG_HAS_METADATA)
168
3
        return AVERROR_INVALIDDATA;
169
170
121
    skip_bits(gb, 24);
171
172
121
    return 0;
173
}