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
34
36 {
38
40
42
45
47 {
49 return 0;
50 }
51
54 const uint8_t **poutbuf, int *poutbuf_size,
55 const uint8_t *buf, int buf_size)
56 {
58 int sync_present;
59 uint8_t parity_bits;
60 int next;
63
65
66 *poutbuf_size = 0;
68 if (buf_size == 0)
69 return 0;
70
72 next = buf_size;
73 } else {
75 // Not in sync - find a major sync header
76
77 for (
i = 0;
i < buf_size;
i++) {
78 mp->pc.state = (
mp->pc.state << 8) | buf[
i];
79 if ((
mp->pc.state & 0xfffffffe) == 0xf8726fba &&
80 // ignore if we do not have the data for the start of header
81 mp->pc.index +
i >= 7) {
84 break;
85 }
86 }
87
91 return buf_size;
92 }
93
97 }
98
100 }
101
102 if (
mp->bytes_left == 0) {
103 // Find length of this packet
104
105 /* Copy overread bytes from last frame into buffer. */
106 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 if (
mp->pc.index + buf_size < 2) {
113 return buf_size;
114 }
115
116 mp->bytes_left = ((
mp->pc.index > 0 ?
mp->pc.buffer[0] : buf[0]) << 8)
117 | (
mp->pc.index > 1 ?
mp->pc.buffer[1] : buf[1-
mp->pc.index]);
118 mp->bytes_left = (
mp->bytes_left & 0xfff) * 2;
119 if (
mp->bytes_left <= 0) {
// prevent infinite loop
120 goto lost_sync;
121 }
122 mp->bytes_left -=
mp->pc.index;
123 }
124
126
128 mp->bytes_left -= buf_size;
129 return buf_size;
130 }
131
133 }
134
135 sync_present = buf_size >= 8 && (
AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
136
137 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
143
144 parity_bits = 0;
145 for (
i = -1;
i <
mp->num_substreams;
i++) {
146 parity_bits ^= buf[p++];
147 parity_bits ^= buf[p++];
148
149 if (
i < 0 || buf[p-2] & 0x80) {
150 parity_bits ^= buf[p++];
151 parity_bits ^= buf[p++];
152 }
153 }
154
155 if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
157 goto lost_sync;
158 }
159 } else {
162
165 goto lost_sync;
166
168
172 else
176 s->duration =
mh.access_unit_size;
177
179 if (
mh.stream_type == 0xbb) {
180 /* MLP stream */
183 } else { /* mh.stream_type == 0xba */
184 /* TrueHD stream */
185 if (!
mh.channels_thd_stream2) {
188 } else {
191 }
192 }
193 }
194
195 if (!
mh.is_vbr)
/* Stream is CBR */
197
198 mp->num_substreams =
mh.num_substreams;
199 }
200
201 *poutbuf = buf;
202 *poutbuf_size = buf_size;
203
204 return next;
205
206 lost_sync:
208 return 1;
209 }
210
217 };