Coverage Report

Created: 2025-08-28 07:12

/src/ffmpeg/libavcodec/mlp_parser.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * MLP parser
3
 * Copyright (c) 2007 Ian Caulfield
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
/**
23
 * @file
24
 * MLP parser
25
 */
26
27
#include <stdint.h>
28
29
#include "libavutil/internal.h"
30
#include "get_bits.h"
31
#include "parser.h"
32
#include "mlp_parse.h"
33
#include "mlp.h"
34
35
typedef struct MLPParseContext
36
{
37
    ParseContext pc;
38
39
    int bytes_left;
40
41
    int in_sync;
42
43
    int num_substreams;
44
} MLPParseContext;
45
46
static av_cold int mlp_init(AVCodecParserContext *s)
47
19.0k
{
48
19.0k
    ff_mlp_init_crc();
49
19.0k
    return 0;
50
19.0k
}
51
52
static int mlp_parse(AVCodecParserContext *s,
53
                     AVCodecContext *avctx,
54
                     const uint8_t **poutbuf, int *poutbuf_size,
55
                     const uint8_t *buf, int buf_size)
56
12.7M
{
57
12.7M
    MLPParseContext *mp = s->priv_data;
58
12.7M
    int sync_present;
59
12.7M
    uint8_t parity_bits;
60
12.7M
    int next;
61
12.7M
    int ret;
62
12.7M
    int i, p = 0;
63
64
12.7M
    s->key_frame = 0;
65
66
12.7M
    *poutbuf_size = 0;
67
12.7M
    *poutbuf = NULL;
68
12.7M
    if (buf_size == 0)
69
16.8k
        return 0;
70
71
12.7M
    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
72
0
        next = buf_size;
73
12.7M
    } else {
74
12.7M
        if (!mp->in_sync) {
75
            // Not in sync - find a major sync header
76
77
181M
            for (i = 0; i < buf_size; i++) {
78
180M
                mp->pc.state = (mp->pc.state << 8) | buf[i];
79
180M
                if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
80
                    // ignore if we do not have the data for the start of header
81
180M
                    mp->pc.index + i >= 7) {
82
2.10M
                    mp->in_sync = 1;
83
2.10M
                    mp->bytes_left = 0;
84
2.10M
                    break;
85
2.10M
                }
86
180M
            }
87
88
3.09M
            if (!mp->in_sync) {
89
995k
                if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
90
0
                    av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
91
995k
                return buf_size;
92
995k
            }
93
94
2.10M
            if ((ret = ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size)) < 0) {
95
0
                av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
96
0
                return ret;
97
0
            }
98
99
2.10M
            return i - 7;
100
2.10M
        }
101
102
9.63M
        if (mp->bytes_left == 0) {
103
            // Find length of this packet
104
105
            /* Copy overread bytes from last frame into buffer. */
106
9.28M
            for(; mp->pc.overread>0; mp->pc.overread--) {
107
108k
                mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
108
108k
            }
109
110
9.17M
            if (mp->pc.index + buf_size < 2) {
111
4.47k
                if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
112
0
                    av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
113
4.47k
                return buf_size;
114
4.47k
            }
115
116
9.17M
            mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
117
9.17M
                           |  (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
118
9.17M
            mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
119
9.17M
            if (mp->bytes_left <= 0) { // prevent infinite loop
120
43.7k
                goto lost_sync;
121
43.7k
            }
122
9.13M
            mp->bytes_left -= mp->pc.index;
123
9.13M
        }
124
125
9.58M
        next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
126
127
9.58M
        if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
128
454k
            mp->bytes_left -= buf_size;
129
454k
            return buf_size;
130
454k
        }
131
132
9.12M
        mp->bytes_left = 0;
133
9.12M
    }
134
135
9.12M
    sync_present = buf_size >= 8 && (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
136
137
9.12M
    if (!sync_present) {
138
        /* The first nibble of a frame is a parity check of the 4-byte
139
         * access unit header and all the 2- or 4-byte substream headers. */
140
        // Only check when this isn't a sync frame - syncs have a checksum.
141
142
8.72M
        s->key_frame = 0;
143
144
8.72M
        parity_bits = 0;
145
18.5M
        for (i = -1; i < mp->num_substreams; i++) {
146
9.83M
            parity_bits ^= buf[p++];
147
9.83M
            parity_bits ^= buf[p++];
148
149
9.83M
            if (i < 0 || buf[p-2] & 0x80) {
150
9.20M
                parity_bits ^= buf[p++];
151
9.20M
                parity_bits ^= buf[p++];
152
9.20M
            }
153
9.83M
        }
154
155
8.72M
        if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
156
1.75M
            av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
157
1.75M
            goto lost_sync;
158
1.75M
        }
159
8.72M
    } else {
160
401k
        GetBitContext gb;
161
401k
        MLPHeaderInfo mh;
162
163
401k
        init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
164
401k
        if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
165
295k
            goto lost_sync;
166
167
106k
        s->key_frame = 1;
168
169
106k
        avctx->bits_per_raw_sample = mh.group1_bits;
170
106k
        if (avctx->bits_per_raw_sample > 16)
171
85.4k
            avctx->sample_fmt = AV_SAMPLE_FMT_S32;
172
21.0k
        else
173
21.0k
            avctx->sample_fmt = AV_SAMPLE_FMT_S16;
174
106k
        avctx->sample_rate = mh.group1_samplerate;
175
106k
        avctx->frame_size =
176
106k
        s->duration = mh.access_unit_size;
177
178
106k
        av_channel_layout_uninit(&avctx->ch_layout);
179
106k
        if (mh.stream_type == 0xbb) {
180
            /* MLP stream */
181
46.3k
            av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_mlp);
182
60.2k
        } else { /* mh.stream_type == 0xba */
183
            /* TrueHD stream */
184
60.2k
            if (!mh.channels_thd_stream2) {
185
11.8k
                av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_thd_stream1);
186
48.3k
            } else {
187
48.3k
                av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_thd_stream2);
188
48.3k
            }
189
60.2k
        }
190
191
106k
        if (!mh.is_vbr) /* Stream is CBR */
192
92.3k
            avctx->bit_rate = mh.peak_bitrate;
193
194
106k
        mp->num_substreams = mh.num_substreams;
195
106k
    }
196
197
7.07M
    *poutbuf = buf;
198
7.07M
    *poutbuf_size = buf_size;
199
200
7.07M
    return next;
201
202
2.09M
lost_sync:
203
2.09M
    mp->in_sync = 0;
204
2.09M
    return 1;
205
9.12M
}
206
207
const AVCodecParser ff_mlp_parser = {
208
    .codec_ids      = { AV_CODEC_ID_MLP, AV_CODEC_ID_TRUEHD },
209
    .priv_data_size = sizeof(MLPParseContext),
210
    .parser_init    = mlp_init,
211
    .parser_parse   = mlp_parse,
212
    .parser_close   = ff_parse_close,
213
};