3 * Copyright (c) 2007 Ian Caulfield
5 * This file is part of FFmpeg.
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.
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.
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
29 #include "libavutil/internal.h"
32 #include "mlp_parse.h"
35 typedef struct MLPParseContext
46 static av_cold int mlp_init(AVCodecParserContext *s)
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)
57 MLPParseContext *mp = s->priv_data;
70 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
74 // Not in sync - find a major sync header
76 for (i = 0; i < buf_size; i++) {
77 mp->pc.state = (mp->pc.state << 8) | buf[i];
78 if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
79 // ignore if we do not have the data for the start of header
80 mp->pc.index + i >= 7) {
88 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");
93 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");
101 if (mp->bytes_left == 0) {
102 // Find length of this packet
104 /* Copy overread bytes from last frame into buffer. */
105 for(; mp->pc.overread>0; mp->pc.overread--) {
106 mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
109 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");
115 mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
116 | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
117 mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
118 if (mp->bytes_left <= 0) { // prevent infinite loop
121 mp->bytes_left -= mp->pc.index;
124 next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
126 if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
127 mp->bytes_left -= buf_size;
134 sync_present = buf_size >= 8 && (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
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.
144 for (i = -1; i < mp->num_substreams; i++) {
145 parity_bits ^= buf[p++];
146 parity_bits ^= buf[p++];
148 if (i < 0 || buf[p-2] & 0x80) {
149 parity_bits ^= buf[p++];
150 parity_bits ^= buf[p++];
154 if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
155 av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
162 init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
163 if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
168 avctx->bits_per_raw_sample = mh.group1_bits;
169 if (avctx->bits_per_raw_sample > 16)
170 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
172 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
173 avctx->sample_rate = mh.group1_samplerate;
175 s->duration = mh.access_unit_size;
177 if(!avctx->channels || !avctx->channel_layout) {
178 if (mh.stream_type == 0xbb) {
180 avctx->channels = mh.channels_mlp;
181 avctx->channel_layout = mh.channel_layout_mlp;
182 } else { /* mh.stream_type == 0xba */
184 if (!mh.channels_thd_stream2) {
185 avctx->channels = mh.channels_thd_stream1;
186 avctx->channel_layout = mh.channel_layout_thd_stream1;
188 avctx->channels = mh.channels_thd_stream2;
189 avctx->channel_layout = mh.channel_layout_thd_stream2;
194 if (!mh.is_vbr) /* Stream is CBR */
195 avctx->bit_rate = mh.peak_bitrate;
197 mp->num_substreams = mh.num_substreams;
201 *poutbuf_size = buf_size;
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,