| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2018 Paul B Mahol | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 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. | ||
| 10 | * | ||
| 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. | ||
| 15 | * | ||
| 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 | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include "libavcodec/bsf.h" | ||
| 22 | #include "libavcodec/bsf_internal.h" | ||
| 23 | #include "libavcodec/get_bits.h" | ||
| 24 | #include "libavcodec/mlp_parse.h" | ||
| 25 | #include "libavcodec/mlp.h" | ||
| 26 | |||
| 27 | typedef struct AccessUnit { | ||
| 28 | uint8_t bits[4]; | ||
| 29 | uint16_t offset; | ||
| 30 | uint16_t optional; | ||
| 31 | } AccessUnit; | ||
| 32 | |||
| 33 | typedef struct TrueHDCoreContext { | ||
| 34 | MLPHeaderInfo hdr; | ||
| 35 | } TrueHDCoreContext; | ||
| 36 | |||
| 37 | 1 | static int truehd_core_init(AVBSFContext *ctx) | |
| 38 | { | ||
| 39 | 1 | ctx->par_out->profile = AV_PROFILE_UNKNOWN; | |
| 40 | |||
| 41 | 1 | return 0; | |
| 42 | } | ||
| 43 | |||
| 44 | 257 | static int truehd_core_filter(AVBSFContext *ctx, AVPacket *pkt) | |
| 45 | { | ||
| 46 | 257 | TrueHDCoreContext *s = ctx->priv_data; | |
| 47 | GetBitContext gbc; | ||
| 48 | AccessUnit units[MAX_SUBSTREAMS]; | ||
| 49 | 257 | int ret, i, last_offset = 0; | |
| 50 | int in_size, out_size; | ||
| 51 | 257 | int have_header = 0; | |
| 52 | 257 | int substream_bytes = 0; | |
| 53 | int end; | ||
| 54 | |||
| 55 | 257 | ret = ff_bsf_get_packet_ref(ctx, pkt); | |
| 56 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 128 times.
|
257 | if (ret < 0) |
| 57 | 129 | return ret; | |
| 58 | |||
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (pkt->size < 4) { |
| 60 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 61 | ✗ | goto fail; | |
| 62 | } | ||
| 63 | |||
| 64 | 128 | in_size = (AV_RB16(pkt->data) & 0xFFF) * 2; | |
| 65 |
2/4✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 128 times.
|
128 | if (in_size < 4 || in_size > pkt->size) { |
| 66 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 67 | ✗ | goto fail; | |
| 68 | } | ||
| 69 | |||
| 70 | 128 | ret = init_get_bits8(&gbc, pkt->data + 4, pkt->size - 4); | |
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (ret < 0) |
| 72 | ✗ | goto fail; | |
| 73 | |||
| 74 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 127 times.
|
128 | if (show_bits_long(&gbc, 32) == 0xf8726fba) { |
| 75 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = ff_mlp_read_major_sync(ctx, &s->hdr, &gbc)) < 0) |
| 76 | ✗ | goto fail; | |
| 77 | 1 | have_header = 1; | |
| 78 | } | ||
| 79 | |||
| 80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (s->hdr.num_substreams > MAX_SUBSTREAMS) { |
| 81 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 82 | ✗ | goto fail; | |
| 83 | } | ||
| 84 | |||
| 85 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 128 times.
|
640 | for (i = 0; i < s->hdr.num_substreams; i++) { |
| 86 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 512 times.
|
2560 | for (int j = 0; j < 4; j++) |
| 87 | 2048 | units[i].bits[j] = get_bits1(&gbc); | |
| 88 | |||
| 89 | 512 | units[i].offset = get_bits(&gbc, 12); | |
| 90 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 128 times.
|
512 | if (i < 3) { |
| 91 | 384 | last_offset = units[i].offset * 2; | |
| 92 | 384 | substream_bytes += 2; | |
| 93 | } | ||
| 94 | |||
| 95 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 508 times.
|
512 | if (units[i].bits[0]) { |
| 96 | 4 | units[i].optional = get_bits(&gbc, 16); | |
| 97 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | if (i < 3) |
| 98 | 3 | substream_bytes += 2; | |
| 99 | } | ||
| 100 | } | ||
| 101 | 128 | end = get_bits_count(&gbc) >> 3; | |
| 102 | |||
| 103 | 128 | out_size = end + 4 + last_offset; | |
| 104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (out_size < in_size) { |
| 105 | 128 | int bpos = 0, reduce = end - have_header * 28 - substream_bytes; | |
| 106 | 128 | uint16_t parity_nibble, dts = AV_RB16(pkt->data + 2); | |
| 107 | uint16_t auheader; | ||
| 108 | uint8_t header[28]; | ||
| 109 | |||
| 110 | av_assert1(reduce >= 0 && reduce % 2 == 0); | ||
| 111 | |||
| 112 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 127 times.
|
128 | if (have_header) { |
| 113 | 1 | memcpy(header, pkt->data + 4, 28); | |
| 114 | 1 | header[16] = (header[16] & 0x0c) | (FFMIN(s->hdr.num_substreams, 3) << 4); | |
| 115 | 1 | header[17] &= 0x7f; | |
| 116 | 1 | header[25] &= 0xfe; | |
| 117 | 1 | AV_WL16(header + 26, ff_mlp_checksum16(header, 26)); | |
| 118 | } | ||
| 119 | |||
| 120 | 128 | pkt->data += reduce; | |
| 121 | 128 | out_size -= reduce; | |
| 122 | 128 | pkt->size = out_size; | |
| 123 | |||
| 124 | 128 | ret = av_packet_make_writable(pkt); | |
| 125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (ret < 0) |
| 126 | ✗ | goto fail; | |
| 127 | |||
| 128 | 128 | AV_WB16(pkt->data + 2, dts); | |
| 129 | 128 | parity_nibble = dts; | |
| 130 | 128 | parity_nibble ^= out_size / 2; | |
| 131 | |||
| 132 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 128 times.
|
512 | for (i = 0; i < FFMIN(s->hdr.num_substreams, 3); i++) { |
| 133 | 384 | uint16_t substr_hdr = 0; | |
| 134 | |||
| 135 | 384 | substr_hdr |= (units[i].bits[0] << 15); | |
| 136 | 384 | substr_hdr |= (units[i].bits[1] << 14); | |
| 137 | 384 | substr_hdr |= (units[i].bits[2] << 13); | |
| 138 | 384 | substr_hdr |= (units[i].bits[3] << 12); | |
| 139 | 384 | substr_hdr |= units[i].offset; | |
| 140 | |||
| 141 | 384 | AV_WB16(pkt->data + have_header * 28 + 4 + bpos, substr_hdr); | |
| 142 | |||
| 143 | 384 | parity_nibble ^= substr_hdr; | |
| 144 | 384 | bpos += 2; | |
| 145 | |||
| 146 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 381 times.
|
384 | if (units[i].bits[0]) { |
| 147 | 3 | AV_WB16(pkt->data + have_header * 28 + 4 + bpos, units[i].optional); | |
| 148 | |||
| 149 | 3 | parity_nibble ^= units[i].optional; | |
| 150 | 3 | bpos += 2; | |
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | 128 | parity_nibble ^= parity_nibble >> 8; | |
| 155 | 128 | parity_nibble ^= parity_nibble >> 4; | |
| 156 | 128 | parity_nibble &= 0xF; | |
| 157 | |||
| 158 | 128 | auheader = (parity_nibble ^ 0xF) << 12; | |
| 159 | 128 | auheader |= (out_size / 2) & 0x0fff; | |
| 160 | 128 | AV_WB16(pkt->data, auheader); | |
| 161 | |||
| 162 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 127 times.
|
128 | if (have_header) |
| 163 | 1 | memcpy(pkt->data + 4, header, 28); | |
| 164 | } | ||
| 165 | |||
| 166 | ✗ | fail: | |
| 167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (ret < 0) |
| 168 | ✗ | av_packet_unref(pkt); | |
| 169 | |||
| 170 | 128 | return ret; | |
| 171 | } | ||
| 172 | |||
| 173 | ✗ | static void truehd_core_flush(AVBSFContext *ctx) | |
| 174 | { | ||
| 175 | ✗ | TrueHDCoreContext *s = ctx->priv_data; | |
| 176 | ✗ | memset(&s->hdr, 0, sizeof(s->hdr)); | |
| 177 | ✗ | } | |
| 178 | |||
| 179 | static const enum AVCodecID codec_ids[] = { | ||
| 180 | AV_CODEC_ID_TRUEHD, AV_CODEC_ID_NONE, | ||
| 181 | }; | ||
| 182 | |||
| 183 | const FFBitStreamFilter ff_truehd_core_bsf = { | ||
| 184 | .p.name = "truehd_core", | ||
| 185 | .p.codec_ids = codec_ids, | ||
| 186 | .priv_data_size = sizeof(TrueHDCoreContext), | ||
| 187 | .init = truehd_core_init, | ||
| 188 | .filter = truehd_core_filter, | ||
| 189 | .flush = truehd_core_flush, | ||
| 190 | }; | ||
| 191 |