2 * Copyright (c) 2018 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "mlp_parser.h"
27 typedef struct AccessUnit {
33 typedef struct TrueHDCoreContext {
39 static int truehd_core_filter(AVBSFContext *ctx, AVPacket *out)
41 TrueHDCoreContext *s = ctx->priv_data;
43 AccessUnit units[MAX_SUBSTREAMS];
45 int ret, i, size, last_offset = 0;
46 int in_size, out_size;
48 int substream_bits = 0;
52 ret = ff_bsf_get_packet(ctx, &in);
59 ret = init_get_bits(&gbc, in->data, 32);
64 in_size = get_bits(&gbc, 12) * 2;
65 if (in_size < 4 || in_size > in->size)
69 dts = get_bits(&gbc, 16);
71 ret = init_get_bits8(&gbc, in->data + 4, in->size - 4);
75 if (show_bits_long(&gbc, 32) == 0xf8726fba) {
76 if ((ret = ff_mlp_read_major_sync(ctx, &s->hdr, &gbc)) != 0)
81 if (s->hdr.num_substreams > MAX_SUBSTREAMS)
84 start = get_bits_count(&gbc);
85 for (i = 0; i < s->hdr.num_substreams; i++) {
86 for (int j = 0; j < 4; j++)
87 units[i].bits[j] = get_bits1(&gbc);
89 units[i].offset = get_bits(&gbc, 12) * 2;
90 if (i < FFMIN(s->hdr.num_substreams, 3)) {
91 last_offset = units[i].offset;
95 if (units[i].bits[0]) {
96 units[i].optional = get_bits(&gbc, 16);
97 if (i < FFMIN(s->hdr.num_substreams, 3))
101 end = get_bits_count(&gbc);
103 size = ((end + 7) >> 3) + 4 + last_offset;
104 if (size >= 0 && size <= in->size)
106 if (out_size < in_size) {
107 int bpos = 0, reduce = (end - start - substream_bits) >> 4;
108 uint16_t parity_nibble = 0;
111 ret = av_new_packet(out, out_size);
115 AV_WB16(out->data + 2, dts);
117 out->size -= reduce * 2;
118 parity_nibble ^= out->size / 2;
121 memcpy(out->data + 4, in->data + 4, 28);
122 out->data[16 + 4] = (out->data[16 + 4] & 0x0f) | (FFMIN(s->hdr.num_substreams, 3) << 4);
123 out->data[25 + 4] = out->data[25 + 4] & 0xfe;
124 out->data[26 + 4] = 0xff;
125 out->data[27 + 4] = 0xff;
126 AV_WL16(out->data + 4 + 26, ff_mlp_checksum16(out->data + 4, 26));
129 for (i = 0; i < FFMIN(s->hdr.num_substreams, 3); i++) {
130 uint16_t substr_hdr = 0;
132 substr_hdr |= (units[i].bits[0] << 15);
133 substr_hdr |= (units[i].bits[1] << 14);
134 substr_hdr |= (units[i].bits[2] << 13);
135 substr_hdr |= (units[i].bits[3] << 12);
136 substr_hdr |= (units[i].offset / 2) & 0x0FFF;
138 AV_WB16(out->data + have_header * 28 + 4 + bpos, substr_hdr);
140 parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
141 parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
143 if (units[i].bits[0]) {
144 AV_WB16(out->data + have_header * 28 + 4 + bpos, units[i].optional);
146 parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
147 parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
151 parity_nibble ^= parity_nibble >> 8;
152 parity_nibble ^= parity_nibble >> 4;
153 parity_nibble &= 0xF;
155 memcpy(out->data + have_header * 28 + 4 + bpos,
156 in->data + 4 + (end >> 3),
157 out_size - (4 + (end >> 3)));
158 auheader = (parity_nibble ^ 0xF) << 12;
159 auheader |= (out->size / 2) & 0x0fff;
160 AV_WB16(out->data, auheader);
162 ret = av_packet_copy_props(out, in);
164 av_packet_move_ref(out, in);
169 av_packet_unref(out);
175 static const enum AVCodecID codec_ids[] = {
176 AV_CODEC_ID_TRUEHD, AV_CODEC_ID_NONE,
179 const AVBitStreamFilter ff_truehd_core_bsf = {
180 .name = "truehd_core",
181 .priv_data_size = sizeof(TrueHDCoreContext),
182 .filter = truehd_core_filter,
183 .codec_ids = codec_ids,