Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/mlp_parser.c |
Date: | 2022-07-04 19:11:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 69 | 86 | 80.2% |
Branches: | 44 | 60 | 73.3% |
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 | 4 | static av_cold int mlp_init(AVCodecParserContext *s) | |
47 | { | ||
48 | 4 | ff_mlp_init_crc(); | |
49 | 4 | return 0; | |
50 | } | ||
51 | |||
52 | 18263 | 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 | 18263 | MLPParseContext *mp = s->priv_data; | |
58 | int sync_present; | ||
59 | uint8_t parity_bits; | ||
60 | int next; | ||
61 | int ret; | ||
62 | 18263 | int i, p = 0; | |
63 | |||
64 | 18263 | s->key_frame = 0; | |
65 | |||
66 | 18263 | *poutbuf_size = 0; | |
67 | 18263 | *poutbuf = NULL; | |
68 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 18259 times.
|
18263 | if (buf_size == 0) |
69 | 4 | return 0; | |
70 | |||
71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18259 times.
|
18259 | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
72 | ✗ | next = buf_size; | |
73 | } else { | ||
74 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 18251 times.
|
18259 | if (!mp->in_sync) { |
75 | // Not in sync - find a major sync header | ||
76 | |||
77 |
2/2✓ Branch 0 taken 4748 times.
✓ Branch 1 taken 4 times.
|
4752 | for (i = 0; i < buf_size; i++) { |
78 | 4748 | mp->pc.state = (mp->pc.state << 8) | buf[i]; | |
79 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4744 times.
|
4748 | if ((mp->pc.state & 0xfffffffe) == 0xf8726fba && |
80 | // ignore if we do not have the data for the start of header | ||
81 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | mp->pc.index + i >= 7) { |
82 | 4 | mp->in_sync = 1; | |
83 | 4 | mp->bytes_left = 0; | |
84 | 4 | break; | |
85 | } | ||
86 | } | ||
87 | |||
88 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (!mp->in_sync) { |
89 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1) |
90 | ✗ | av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n"); | |
91 | 4 | return buf_size; | |
92 | } | ||
93 | |||
94 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if ((ret = ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size)) < 0) { |
95 | ✗ | av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n"); | |
96 | ✗ | return ret; | |
97 | } | ||
98 | |||
99 | 4 | return i - 7; | |
100 | } | ||
101 | |||
102 |
2/2✓ Branch 0 taken 15918 times.
✓ Branch 1 taken 2333 times.
|
18251 | if (mp->bytes_left == 0) { |
103 | // Find length of this packet | ||
104 | |||
105 | /* Copy overread bytes from last frame into buffer. */ | ||
106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15918 times.
|
15918 | for(; mp->pc.overread>0; mp->pc.overread--) { |
107 | ✗ | mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++]; | |
108 | } | ||
109 | |||
110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15918 times.
|
15918 | if (mp->pc.index + buf_size < 2) { |
111 | ✗ | if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1) | |
112 | ✗ | av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n"); | |
113 | ✗ | return buf_size; | |
114 | } | ||
115 | |||
116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15918 times.
|
15918 | mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8) |
117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15918 times.
|
15918 | | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]); |
118 | 15918 | mp->bytes_left = (mp->bytes_left & 0xfff) * 2; | |
119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15918 times.
|
15918 | if (mp->bytes_left <= 0) { // prevent infinite loop |
120 | ✗ | goto lost_sync; | |
121 | } | ||
122 | 15918 | mp->bytes_left -= mp->pc.index; | |
123 | } | ||
124 | |||
125 |
2/2✓ Branch 0 taken 15915 times.
✓ Branch 1 taken 2336 times.
|
18251 | next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left; |
126 | |||
127 |
2/2✓ Branch 1 taken 2336 times.
✓ Branch 2 taken 15915 times.
|
18251 | if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) { |
128 | 2336 | mp->bytes_left -= buf_size; | |
129 | 2336 | return buf_size; | |
130 | } | ||
131 | |||
132 | 15915 | mp->bytes_left = 0; | |
133 | } | ||
134 | |||
135 |
3/4✓ Branch 0 taken 15915 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1544 times.
✓ Branch 3 taken 14371 times.
|
15915 | sync_present = buf_size >= 8 && (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba; |
136 | |||
137 |
2/2✓ Branch 0 taken 14371 times.
✓ Branch 1 taken 1544 times.
|
15915 | 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 | 14371 | s->key_frame = 0; | |
143 | |||
144 | 14371 | parity_bits = 0; | |
145 |
2/2✓ Branch 0 taken 35515 times.
✓ Branch 1 taken 14371 times.
|
49886 | for (i = -1; i < mp->num_substreams; i++) { |
146 | 35515 | parity_bits ^= buf[p++]; | |
147 | 35515 | parity_bits ^= buf[p++]; | |
148 | |||
149 |
4/4✓ Branch 0 taken 21144 times.
✓ Branch 1 taken 14371 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 21036 times.
|
35515 | if (i < 0 || buf[p-2] & 0x80) { |
150 | 14479 | parity_bits ^= buf[p++]; | |
151 | 14479 | parity_bits ^= buf[p++]; | |
152 | } | ||
153 | } | ||
154 | |||
155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14371 times.
|
14371 | if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) { |
156 | ✗ | av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n"); | |
157 | ✗ | goto lost_sync; | |
158 | } | ||
159 | } else { | ||
160 | GetBitContext gb; | ||
161 | MLPHeaderInfo mh; | ||
162 | |||
163 | 1544 | init_get_bits(&gb, buf + 4, (buf_size - 4) << 3); | |
164 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1544 times.
|
1544 | if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0) |
165 | ✗ | goto lost_sync; | |
166 | |||
167 | 1544 | s->key_frame = 1; | |
168 | |||
169 | 1544 | avctx->bits_per_raw_sample = mh.group1_bits; | |
170 |
2/2✓ Branch 0 taken 429 times.
✓ Branch 1 taken 1115 times.
|
1544 | if (avctx->bits_per_raw_sample > 16) |
171 | 429 | avctx->sample_fmt = AV_SAMPLE_FMT_S32; | |
172 | else | ||
173 | 1115 | avctx->sample_fmt = AV_SAMPLE_FMT_S16; | |
174 | 1544 | avctx->sample_rate = mh.group1_samplerate; | |
175 | 1544 | avctx->frame_size = | |
176 | 1544 | s->duration = mh.access_unit_size; | |
177 | |||
178 | 1544 | av_channel_layout_uninit(&avctx->ch_layout); | |
179 |
2/2✓ Branch 0 taken 1115 times.
✓ Branch 1 taken 429 times.
|
1544 | if (mh.stream_type == 0xbb) { |
180 | /* MLP stream */ | ||
181 | 1115 | av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_mlp); | |
182 | } else { /* mh.stream_type == 0xba */ | ||
183 | /* TrueHD stream */ | ||
184 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 429 times.
|
429 | if (!mh.channels_thd_stream2) { |
185 | ✗ | av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_thd_stream1); | |
186 | } else { | ||
187 | 429 | av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_thd_stream2); | |
188 | } | ||
189 | } | ||
190 | |||
191 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1544 times.
|
1544 | if (!mh.is_vbr) /* Stream is CBR */ |
192 | ✗ | avctx->bit_rate = mh.peak_bitrate; | |
193 | |||
194 | 1544 | mp->num_substreams = mh.num_substreams; | |
195 | } | ||
196 | |||
197 | 15915 | *poutbuf = buf; | |
198 | 15915 | *poutbuf_size = buf_size; | |
199 | |||
200 | 15915 | return next; | |
201 | |||
202 | ✗ | lost_sync: | |
203 | ✗ | mp->in_sync = 0; | |
204 | ✗ | return 1; | |
205 | } | ||
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 | }; | ||
214 |