mirror of https://github.com/FFmpeg/FFmpeg.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
5.0 KiB
183 lines
5.0 KiB
6 years ago
|
/*
|
||
|
* Copyright (c) 2018 Paul B Mahol
|
||
|
*
|
||
|
* This file is part of FFmpeg.
|
||
|
*
|
||
|
* FFmpeg is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU Lesser General Public
|
||
|
* License as published by the Free Software Foundation; either
|
||
|
* version 2.1 of the License, or (at your option) any later version.
|
||
|
*
|
||
|
* FFmpeg is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
* Lesser General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Lesser General Public
|
||
|
* License along with FFmpeg; if not, write to the Free Software
|
||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||
|
*/
|
||
|
|
||
5 years ago
|
#include "bsf.h"
|
||
5 years ago
|
#include "bsf_internal.h"
|
||
6 years ago
|
#include "get_bits.h"
|
||
6 years ago
|
#include "mlp_parse.h"
|
||
6 years ago
|
#include "mlp.h"
|
||
|
|
||
|
typedef struct AccessUnit {
|
||
|
uint8_t bits[4];
|
||
|
uint16_t offset;
|
||
|
uint16_t optional;
|
||
|
} AccessUnit;
|
||
|
|
||
|
typedef struct TrueHDCoreContext {
|
||
|
MLPHeaderInfo hdr;
|
||
|
} TrueHDCoreContext;
|
||
|
|
||
5 years ago
|
static int truehd_core_filter(AVBSFContext *ctx, AVPacket *pkt)
|
||
6 years ago
|
{
|
||
|
TrueHDCoreContext *s = ctx->priv_data;
|
||
|
GetBitContext gbc;
|
||
|
AccessUnit units[MAX_SUBSTREAMS];
|
||
5 years ago
|
int ret, i, last_offset = 0;
|
||
6 years ago
|
int in_size, out_size;
|
||
|
int have_header = 0;
|
||
5 years ago
|
int substream_bytes = 0;
|
||
5 years ago
|
int end;
|
||
6 years ago
|
|
||
5 years ago
|
ret = ff_bsf_get_packet_ref(ctx, pkt);
|
||
6 years ago
|
if (ret < 0)
|
||
|
return ret;
|
||
|
|
||
5 years ago
|
if (pkt->size < 4) {
|
||
5 years ago
|
ret = AVERROR_INVALIDDATA;
|
||
6 years ago
|
goto fail;
|
||
5 years ago
|
}
|
||
6 years ago
|
|
||
5 years ago
|
in_size = (AV_RB16(pkt->data) & 0xFFF) * 2;
|
||
|
if (in_size < 4 || in_size > pkt->size) {
|
||
5 years ago
|
ret = AVERROR_INVALIDDATA;
|
||
6 years ago
|
goto fail;
|
||
5 years ago
|
}
|
||
6 years ago
|
|
||
5 years ago
|
ret = init_get_bits8(&gbc, pkt->data + 4, pkt->size - 4);
|
||
6 years ago
|
if (ret < 0)
|
||
|
goto fail;
|
||
|
|
||
|
if (show_bits_long(&gbc, 32) == 0xf8726fba) {
|
||
5 years ago
|
if ((ret = ff_mlp_read_major_sync(ctx, &s->hdr, &gbc)) < 0)
|
||
6 years ago
|
goto fail;
|
||
|
have_header = 1;
|
||
|
}
|
||
|
|
||
5 years ago
|
if (s->hdr.num_substreams > MAX_SUBSTREAMS) {
|
||
|
ret = AVERROR_INVALIDDATA;
|
||
6 years ago
|
goto fail;
|
||
5 years ago
|
}
|
||
6 years ago
|
|
||
|
for (i = 0; i < s->hdr.num_substreams; i++) {
|
||
|
for (int j = 0; j < 4; j++)
|
||
|
units[i].bits[j] = get_bits1(&gbc);
|
||
|
|
||
5 years ago
|
units[i].offset = get_bits(&gbc, 12);
|
||
|
if (i < 3) {
|
||
|
last_offset = units[i].offset * 2;
|
||
5 years ago
|
substream_bytes += 2;
|
||
6 years ago
|
}
|
||
|
|
||
|
if (units[i].bits[0]) {
|
||
|
units[i].optional = get_bits(&gbc, 16);
|
||
5 years ago
|
if (i < 3)
|
||
5 years ago
|
substream_bytes += 2;
|
||
6 years ago
|
}
|
||
|
}
|
||
5 years ago
|
end = get_bits_count(&gbc) >> 3;
|
||
6 years ago
|
|
||
5 years ago
|
out_size = end + 4 + last_offset;
|
||
6 years ago
|
if (out_size < in_size) {
|
||
5 years ago
|
int bpos = 0, reduce = end - have_header * 28 - substream_bytes;
|
||
5 years ago
|
uint16_t parity_nibble, dts = AV_RB16(pkt->data + 2);
|
||
6 years ago
|
uint16_t auheader;
|
||
5 years ago
|
uint8_t header[28];
|
||
6 years ago
|
|
||
5 years ago
|
av_assert1(reduce >= 0 && reduce % 2 == 0);
|
||
5 years ago
|
|
||
5 years ago
|
if (have_header) {
|
||
|
memcpy(header, pkt->data + 4, 28);
|
||
|
header[16] = (header[16] & 0x0c) | (FFMIN(s->hdr.num_substreams, 3) << 4);
|
||
|
header[17] &= 0x7f;
|
||
|
header[25] &= 0xfe;
|
||
|
AV_WL16(header + 26, ff_mlp_checksum16(header, 26));
|
||
|
}
|
||
|
|
||
|
pkt->data += reduce;
|
||
|
out_size -= reduce;
|
||
|
pkt->size = out_size;
|
||
|
|
||
|
ret = av_packet_make_writable(pkt);
|
||
6 years ago
|
if (ret < 0)
|
||
|
goto fail;
|
||
|
|
||
5 years ago
|
AV_WB16(pkt->data + 2, dts);
|
||
6 years ago
|
parity_nibble = dts;
|
||
5 years ago
|
parity_nibble ^= out_size / 2;
|
||
6 years ago
|
|
||
|
for (i = 0; i < FFMIN(s->hdr.num_substreams, 3); i++) {
|
||
|
uint16_t substr_hdr = 0;
|
||
|
|
||
|
substr_hdr |= (units[i].bits[0] << 15);
|
||
|
substr_hdr |= (units[i].bits[1] << 14);
|
||
|
substr_hdr |= (units[i].bits[2] << 13);
|
||
|
substr_hdr |= (units[i].bits[3] << 12);
|
||
5 years ago
|
substr_hdr |= units[i].offset;
|
||
6 years ago
|
|
||
5 years ago
|
AV_WB16(pkt->data + have_header * 28 + 4 + bpos, substr_hdr);
|
||
6 years ago
|
|
||
5 years ago
|
parity_nibble ^= substr_hdr;
|
||
|
bpos += 2;
|
||
6 years ago
|
|
||
|
if (units[i].bits[0]) {
|
||
5 years ago
|
AV_WB16(pkt->data + have_header * 28 + 4 + bpos, units[i].optional);
|
||
6 years ago
|
|
||
5 years ago
|
parity_nibble ^= units[i].optional;
|
||
|
bpos += 2;
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
parity_nibble ^= parity_nibble >> 8;
|
||
|
parity_nibble ^= parity_nibble >> 4;
|
||
|
parity_nibble &= 0xF;
|
||
|
|
||
|
auheader = (parity_nibble ^ 0xF) << 12;
|
||
5 years ago
|
auheader |= (out_size / 2) & 0x0fff;
|
||
|
AV_WB16(pkt->data, auheader);
|
||
6 years ago
|
|
||
5 years ago
|
if (have_header)
|
||
|
memcpy(pkt->data + 4, header, 28);
|
||
6 years ago
|
}
|
||
|
|
||
|
fail:
|
||
|
if (ret < 0)
|
||
5 years ago
|
av_packet_unref(pkt);
|
||
6 years ago
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
6 years ago
|
static void truehd_core_flush(AVBSFContext *ctx)
|
||
|
{
|
||
|
TrueHDCoreContext *s = ctx->priv_data;
|
||
|
memset(&s->hdr, 0, sizeof(s->hdr));
|
||
|
}
|
||
|
|
||
6 years ago
|
static const enum AVCodecID codec_ids[] = {
|
||
|
AV_CODEC_ID_TRUEHD, AV_CODEC_ID_NONE,
|
||
|
};
|
||
|
|
||
3 years ago
|
const FFBitStreamFilter ff_truehd_core_bsf = {
|
||
|
.p.name = "truehd_core",
|
||
|
.p.codec_ids = codec_ids,
|
||
6 years ago
|
.priv_data_size = sizeof(TrueHDCoreContext),
|
||
|
.filter = truehd_core_filter,
|
||
6 years ago
|
.flush = truehd_core_flush,
|
||
6 years ago
|
};
|