GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/mlp_parser.c Lines: 70 88 79.5 %
Date: 2020-04-04 00:26:16 Branches: 47 64 73.4 %

Line Branch Exec Source
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
3
static av_cold int mlp_init(AVCodecParserContext *s)
47
{
48
3
    ff_mlp_init_crc();
49
3
    return 0;
50
}
51
52
18076
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
{
57
18076
    MLPParseContext *mp = s->priv_data;
58
    int sync_present;
59
    uint8_t parity_bits;
60
    int next;
61
    int ret;
62
18076
    int i, p = 0;
63
64
18076
    s->key_frame = 0;
65
66
18076
    *poutbuf_size = 0;
67
18076
    if (buf_size == 0)
68
3
        return 0;
69
70
18073
    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
71
        next = buf_size;
72
    } else {
73
18073
        if (!mp->in_sync) {
74
            // Not in sync - find a major sync header
75
76
4744
            for (i = 0; i < buf_size; i++) {
77
4740
                mp->pc.state = (mp->pc.state << 8) | buf[i];
78
4740
                if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
79
                    // ignore if we do not have the data for the start of header
80
3
                    mp->pc.index + i >= 7) {
81
3
                    mp->in_sync = 1;
82
3
                    mp->bytes_left = 0;
83
3
                    break;
84
                }
85
            }
86
87
7
            if (!mp->in_sync) {
88
4
                if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
89
                    av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
90
4
                return buf_size;
91
            }
92
93
3
            if ((ret = ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size)) < 0) {
94
                av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
95
                return ret;
96
            }
97
98
3
            return i - 7;
99
        }
100
101
18066
        if (mp->bytes_left == 0) {
102
            // Find length of this packet
103
104
            /* Copy overread bytes from last frame into buffer. */
105
15790
            for(; mp->pc.overread>0; mp->pc.overread--) {
106
                mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
107
            }
108
109
15790
            if (mp->pc.index + buf_size < 2) {
110
                if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
111
                    av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
112
                return buf_size;
113
            }
114
115
15790
            mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
116
15790
                           |  (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
117
15790
            mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
118
15790
            if (mp->bytes_left <= 0) { // prevent infinite loop
119
                goto lost_sync;
120
            }
121
15790
            mp->bytes_left -= mp->pc.index;
122
        }
123
124
18066
        next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
125
126
18066
        if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
127
2279
            mp->bytes_left -= buf_size;
128
2279
            return buf_size;
129
        }
130
131
15787
        mp->bytes_left = 0;
132
    }
133
134

15787
    sync_present = buf_size >= 8 && (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
135
136
15787
    if (!sync_present) {
137
        /* The first nibble of a frame is a parity check of the 4-byte
138
         * access unit header and all the 2- or 4-byte substream headers. */
139
        // Only check when this isn't a sync frame - syncs have a checksum.
140
141
14244
        s->key_frame = 0;
142
143
14244
        parity_bits = 0;
144
49124
        for (i = -1; i < mp->num_substreams; i++) {
145
34880
            parity_bits ^= buf[p++];
146
34880
            parity_bits ^= buf[p++];
147
148

34880
            if (i < 0 || buf[p-2] & 0x80) {
149
14348
                parity_bits ^= buf[p++];
150
14348
                parity_bits ^= buf[p++];
151
            }
152
        }
153
154
14244
        if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
155
            av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
156
            goto lost_sync;
157
        }
158
    } else {
159
        GetBitContext gb;
160
        MLPHeaderInfo mh;
161
162
1543
        init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
163
1543
        if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
164
            goto lost_sync;
165
166
1543
        s->key_frame = 1;
167
168
1543
        avctx->bits_per_raw_sample = mh.group1_bits;
169
1543
        if (avctx->bits_per_raw_sample > 16)
170
428
            avctx->sample_fmt = AV_SAMPLE_FMT_S32;
171
        else
172
1115
            avctx->sample_fmt = AV_SAMPLE_FMT_S16;
173
1543
        avctx->sample_rate = mh.group1_samplerate;
174
1543
        avctx->frame_size =
175
1543
        s->duration = mh.access_unit_size;
176
177

1543
        if(!avctx->channels || !avctx->channel_layout) {
178
3
        if (mh.stream_type == 0xbb) {
179
            /* MLP stream */
180
1
            avctx->channels       = mh.channels_mlp;
181
1
            avctx->channel_layout = mh.channel_layout_mlp;
182
        } else { /* mh.stream_type == 0xba */
183
            /* TrueHD stream */
184
2
            if (!mh.channels_thd_stream2) {
185
                avctx->channels       = mh.channels_thd_stream1;
186
                avctx->channel_layout = mh.channel_layout_thd_stream1;
187
            } else {
188
2
                avctx->channels       = mh.channels_thd_stream2;
189
2
                avctx->channel_layout = mh.channel_layout_thd_stream2;
190
            }
191
        }
192
        }
193
194
1543
        if (!mh.is_vbr) /* Stream is CBR */
195
            avctx->bit_rate = mh.peak_bitrate;
196
197
1543
        mp->num_substreams = mh.num_substreams;
198
    }
199
200
15787
    *poutbuf = buf;
201
15787
    *poutbuf_size = buf_size;
202
203
15787
    return next;
204
205
lost_sync:
206
    mp->in_sync = 0;
207
    return 1;
208
}
209
210
AVCodecParser ff_mlp_parser = {
211
    .codec_ids      = { AV_CODEC_ID_MLP, AV_CODEC_ID_TRUEHD },
212
    .priv_data_size = sizeof(MLPParseContext),
213
    .parser_init    = mlp_init,
214
    .parser_parse   = mlp_parse,
215
    .parser_close   = ff_parse_close,
216
};