]> git.sesse.net Git - ffmpeg/blob - libavcodec/truehd_core_bsf.c
Merge commit 'e39a9212ab37a55b346801c77487d8a47b6f9fe2'
[ffmpeg] / libavcodec / truehd_core_bsf.c
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 "avcodec.h"
22 #include "bsf.h"
23 #include "get_bits.h"
24 #include "mlp_parser.h"
25 #include "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     const AVClass *class;
35
36     MLPHeaderInfo hdr;
37 } TrueHDCoreContext;
38
39 static int truehd_core_filter(AVBSFContext *ctx, AVPacket *out)
40 {
41     TrueHDCoreContext *s = ctx->priv_data;
42     GetBitContext gbc;
43     AccessUnit units[MAX_SUBSTREAMS];
44     AVPacket *in;
45     int ret, i, size, last_offset = 0;
46     int in_size, out_size;
47     int have_header = 0;
48     int substream_bits = 0;
49     int start, end;
50     uint16_t dts;
51
52     ret = ff_bsf_get_packet(ctx, &in);
53     if (ret < 0)
54         return ret;
55
56     if (in->size < 4)
57         goto fail;
58
59     ret = init_get_bits(&gbc, in->data, 32);
60     if (ret < 0)
61         goto fail;
62
63     skip_bits(&gbc, 4);
64     in_size = get_bits(&gbc, 12) * 2;
65     if (in_size < 4 || in_size > in->size)
66         goto fail;
67
68     out_size = in_size;
69     dts = get_bits(&gbc, 16);
70
71     ret = init_get_bits8(&gbc, in->data + 4, in->size - 4);
72     if (ret < 0)
73         goto fail;
74
75     if (show_bits_long(&gbc, 32) == 0xf8726fba) {
76         if ((ret = ff_mlp_read_major_sync(ctx, &s->hdr, &gbc)) != 0)
77             goto fail;
78         have_header = 1;
79     }
80
81     if (s->hdr.num_substreams > MAX_SUBSTREAMS)
82         goto fail;
83
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);
88
89         units[i].offset = get_bits(&gbc, 12) * 2;
90         if (i < FFMIN(s->hdr.num_substreams, 3)) {
91             last_offset = units[i].offset;
92             substream_bits += 16;
93         }
94
95         if (units[i].bits[0]) {
96             units[i].optional = get_bits(&gbc, 16);
97             if (i < FFMIN(s->hdr.num_substreams, 3))
98                 substream_bits += 16;
99         }
100     }
101     end = get_bits_count(&gbc);
102
103     size = ((end + 7) >> 3) + 4 + last_offset;
104     if (size >= 0 && size <= in->size)
105         out_size = size;
106     if (out_size < in_size) {
107         int bpos = 0, reduce = (end - start - substream_bits) >> 4;
108         uint16_t parity_nibble = 0;
109         uint16_t auheader;
110
111         ret = av_new_packet(out, out_size);
112         if (ret < 0)
113             goto fail;
114
115         AV_WB16(out->data + 2, dts);
116         parity_nibble = dts;
117         out->size -= reduce * 2;
118         parity_nibble ^= out->size / 2;
119
120         if (have_header) {
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));
127         }
128
129         for (i = 0; i < FFMIN(s->hdr.num_substreams, 3); i++) {
130             uint16_t substr_hdr = 0;
131
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;
137
138             AV_WB16(out->data + have_header * 28 + 4 + bpos, substr_hdr);
139
140             parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
141             parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
142
143             if (units[i].bits[0]) {
144                 AV_WB16(out->data + have_header * 28 + 4 + bpos, units[i].optional);
145
146                 parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
147                 parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
148             }
149         }
150
151         parity_nibble ^= parity_nibble >> 8;
152         parity_nibble ^= parity_nibble >> 4;
153         parity_nibble &= 0xF;
154
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);
161
162         ret = av_packet_copy_props(out, in);
163     } else {
164         av_packet_move_ref(out, in);
165     }
166
167 fail:
168     if (ret < 0)
169         av_packet_unref(out);
170     av_packet_free(&in);
171
172     return ret;
173 }
174
175 static const enum AVCodecID codec_ids[] = {
176     AV_CODEC_ID_TRUEHD, AV_CODEC_ID_NONE,
177 };
178
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,
184 };