aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat/hls_sample_encryption.c
blob: f9f410f3da15f314e6f002b4f03698d694dd3856 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * Apple HTTP Live Streaming Sample Encryption/Decryption
 *
 * Copyright (c) 2021 Nachiket Tarate
 *
 * 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
 */

/**
 * @file
 * Apple HTTP Live Streaming Sample Encryption
 * https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/HLS_Sample_Encryption
 */

#include "libavutil/aes.h"
#include "libavutil/channel_layout.h"
#include "libavutil/mem.h"

#include "hls_sample_encryption.h"

#include "libavcodec/adts_header.h"
#include "libavcodec/adts_parser.h"
#include "libavcodec/ac3tab.h"
#include "libavcodec/ac3_parser_internal.h"


typedef struct NALUnit {
    uint8_t     *data;
    int         type;
    int         length;
    int         start_code_length;
} NALUnit;

typedef struct AudioFrame {
    uint8_t     *data;
    int         length;
    int         header_length;
} AudioFrame;

typedef struct CodecParserContext {
    const uint8_t   *buf_ptr;
    const uint8_t   *buf_end;
} CodecParserContext;

static const int eac3_sample_rate_tab[] = { 48000, 44100, 32000, 0 };

void ff_hls_senc_read_audio_setup_info(HLSAudioSetupInfo *info, const uint8_t *buf, size_t size)
{
    if (size < 8)
        return;

    info->codec_tag = AV_RL32(buf);

    /* Always keep this list in sync with the one from hls_read_header() */
    if (info->codec_tag == MKTAG('z','a','a','c'))
        info->codec_id = AV_CODEC_ID_AAC;
    else if (info->codec_tag == MKTAG('z','a','c','3'))
        info->codec_id = AV_CODEC_ID_AC3;
    else if (info->codec_tag == MKTAG('z','e','c','3'))
        info->codec_id = AV_CODEC_ID_EAC3;
    else
        info->codec_id = AV_CODEC_ID_NONE;

    buf += 4;
    info->priming               = AV_RL16(buf);
    buf += 2;
    info->version               = *buf++;
    info->setup_data_length     = *buf++;

    if (info->setup_data_length > size - 8)
        info->setup_data_length = size - 8;

    if (info->setup_data_length > HLS_MAX_AUDIO_SETUP_DATA_LEN)
        return;

    memcpy(info->setup_data, buf, info->setup_data_length);
}

int ff_hls_senc_parse_audio_setup_info(AVStream *st, HLSAudioSetupInfo *info)
{
    int ret = 0;

    st->codecpar->codec_tag = info->codec_tag;

    if (st->codecpar->codec_id == AV_CODEC_ID_AAC)
        return 0;

    if (st->codecpar->codec_id != AV_CODEC_ID_AC3 && st->codecpar->codec_id != AV_CODEC_ID_EAC3)
        return AVERROR_INVALIDDATA;

    if (st->codecpar->codec_id == AV_CODEC_ID_AC3) {
        AC3HeaderInfo *ac3hdr = NULL;

        ret = avpriv_ac3_parse_header(&ac3hdr, info->setup_data, info->setup_data_length);
        if (ret < 0) {
            av_free(ac3hdr);
            return ret;
        }

        st->codecpar->sample_rate       = ac3hdr->sample_rate;
        av_channel_layout_uninit(&st->codecpar->ch_layout);
        av_channel_layout_from_mask(&st->codecpar->ch_layout, ac3hdr->channel_layout);
        st->codecpar->bit_rate          = ac3hdr->bit_rate;

        av_free(ac3hdr);
    } else {  /*  Parse 'dec3' EC3SpecificBox */
        GetBitContext gb;
        uint64_t mask;
        int data_rate, fscod, acmod, lfeon;

        ret = init_get_bits8(&gb, info->setup_data, info->setup_data_length);
        if (ret < 0)
            return AVERROR_INVALIDDATA;

        data_rate = get_bits(&gb, 13);
        skip_bits(&gb, 3);
        fscod = get_bits(&gb, 2);
        skip_bits(&gb, 10);
        acmod = get_bits(&gb, 3);
        lfeon = get_bits(&gb, 1);

        st->codecpar->sample_rate = eac3_sample_rate_tab[fscod];

        mask = ff_ac3_channel_layout_tab[acmod];
        if (lfeon)
            mask |= AV_CH_LOW_FREQUENCY;

        av_channel_layout_uninit(&st->codecpar->ch_layout);
        av_channel_layout_from_mask(&st->codecpar->ch_layout, mask);

        st->codecpar->bit_rate = data_rate*1000;
    }

    return 0;
}

/*
 * Remove start code emulation prevention 0x03 bytes
 */
static void remove_scep_3_bytes(NALUnit *nalu)
{
    int i = 0;
    int j = 0;

    uint8_t *data = nalu->data;

    while (i < nalu->length) {
        if (nalu->length - i > 3 && AV_RB24(&data[i]) == 0x000003) {
            data[j++] = data[i++];
            data[j++] = data[i++];
            i++;
        } else {
            data[j++] = data[i++];
        }
    }

    nalu->length = j;
}

static int get_next_nal_unit(CodecParserContext *ctx, NALUnit *nalu)
{
    const uint8_t *nalu_start = ctx->buf_ptr;

    if (ctx->buf_end - ctx->buf_ptr >= 4 && AV_RB32(ctx->buf_ptr) == 0x00000001)
        nalu->start_code_length = 4;
    else if (ctx->buf_end - ctx->buf_ptr >= 3 && AV_RB24(ctx->buf_ptr) == 0x000001)
        nalu->start_code_length = 3;
    else /* No start code at the beginning of the NAL unit */
        return -1;

    ctx->buf_ptr += nalu->start_code_length;

    while (ctx->buf_ptr < ctx->buf_end) {
        if (ctx->buf_end - ctx->buf_ptr >= 4 && AV_RB32(ctx->buf_ptr) == 0x00000001)
            break;
        else if (ctx->buf_end - ctx->buf_ptr >= 3 && AV_RB24(ctx->buf_ptr) == 0x000001)
            break;
        ctx->buf_ptr++;
    }

    nalu->data   = (uint8_t *)nalu_start + nalu->start_code_length;
    nalu->length = ctx->buf_ptr - nalu->data;
    nalu->type   = *nalu->data & 0x1F;

    return 0;
}

static int decrypt_nal_unit(HLSCryptoContext *crypto_ctx, NALUnit *nalu)
{
    int ret = 0;
    int rem_bytes;
    uint8_t *data;
    uint8_t iv[16];

    ret = av_aes_init(crypto_ctx->aes_ctx, crypto_ctx->key, 16 * 8, 1);
    if (ret < 0)
        return ret;

    /* Remove start code emulation prevention 0x03 bytes */
    remove_scep_3_bytes(nalu);

    data = nalu->data + 32;
    rem_bytes = nalu->length - 32;

    memcpy(iv, crypto_ctx->iv, 16);

    while (rem_bytes > 0) {
        if (rem_bytes > 16) {
            av_aes_crypt(crypto_ctx->aes_ctx, data, data, 1, iv, 1);
            data += 16;
            rem_bytes -= 16;
        }
        data += FFMIN(144, rem_bytes);
        rem_bytes -= FFMIN(144, rem_bytes);
    }

    return 0;
}

static int decrypt_video_frame(HLSCryptoContext *crypto_ctx, AVPacket *pkt)
{
    int ret = 0;
    CodecParserContext  ctx;
    NALUnit nalu;
    uint8_t *data_ptr;
    int move_nalu = 0;

    memset(&ctx, 0, sizeof(ctx));
    ctx.buf_ptr  = pkt->data;
    ctx.buf_end = pkt->data + pkt->size;

    data_ptr = pkt->data;

    while (ctx.buf_ptr < ctx.buf_end) {
        memset(&nalu, 0, sizeof(nalu));
        ret = get_next_nal_unit(&ctx, &nalu);
        if (ret < 0)
            return ret;
        if ((nalu.type == 0x01 || nalu.type == 0x05) && nalu.length > 48) {
            int encrypted_nalu_length = nalu.length;
            ret = decrypt_nal_unit(crypto_ctx, &nalu);
            if (ret < 0)
                return ret;
            move_nalu = nalu.length != encrypted_nalu_length;
        }
        if (move_nalu)
            memmove(data_ptr, nalu.data - nalu.start_code_length, nalu.start_code_length + nalu.length);
        data_ptr += nalu.start_code_length + nalu.length;
    }

    av_shrink_packet(pkt, data_ptr - pkt->data);

    return 0;
}

static int get_next_adts_frame(CodecParserContext *ctx, AudioFrame *frame)
{
    int ret = 0;

    AACADTSHeaderInfo *adts_hdr = NULL;

    /* Find next sync word 0xFFF */
    while (ctx->buf_ptr < ctx->buf_end - 1) {
        if (*ctx->buf_ptr == 0xFF && (*(ctx->buf_ptr + 1) & 0xF0) == 0xF0)
            break;
        ctx->buf_ptr++;
    }

    if (ctx->buf_ptr >= ctx->buf_end - 1)
        return -1;

    frame->data = (uint8_t*)ctx->buf_ptr;

    ret = avpriv_adts_header_parse (&adts_hdr, frame->data, ctx->buf_end - frame->data);
    if (ret < 0)
        return ret;

    frame->header_length = adts_hdr->crc_absent ? AV_AAC_ADTS_HEADER_SIZE : AV_AAC_ADTS_HEADER_SIZE + 2;
    frame->length = adts_hdr->frame_length;

    av_free(adts_hdr);

    return 0;
}

static int get_next_ac3_eac3_sync_frame(CodecParserContext *ctx, AudioFrame *frame)
{
    int ret = 0;

    AC3HeaderInfo *hdr = NULL;

    /* Find next sync word 0x0B77 */
    while (ctx->buf_ptr < ctx->buf_end - 1) {
        if (*ctx->buf_ptr == 0x0B && *(ctx->buf_ptr + 1) == 0x77)
            break;
        ctx->buf_ptr++;
    }

    if (ctx->buf_ptr >= ctx->buf_end - 1)
        return -1;

    frame->data = (uint8_t*)ctx->buf_ptr;
    frame->header_length = 0;

    ret = avpriv_ac3_parse_header(&hdr, frame->data, ctx->buf_end - frame->data);
    if (ret < 0) {
        av_free(hdr);
        return ret;
    }

    frame->length = hdr->frame_size;

    av_free(hdr);

    return 0;
}

static int get_next_sync_frame(enum AVCodecID codec_id, CodecParserContext *ctx, AudioFrame *frame)
{
    if (codec_id == AV_CODEC_ID_AAC)
        return get_next_adts_frame(ctx, frame);
    else if (codec_id == AV_CODEC_ID_AC3 || codec_id == AV_CODEC_ID_EAC3)
        return get_next_ac3_eac3_sync_frame(ctx, frame);
    else
        return AVERROR_INVALIDDATA;
}

static int decrypt_sync_frame(enum AVCodecID codec_id, HLSCryptoContext *crypto_ctx, AudioFrame *frame)
{
    int ret = 0;
    uint8_t *data;
    int num_of_encrypted_blocks;

    ret = av_aes_init(crypto_ctx->aes_ctx, crypto_ctx->key, 16 * 8, 1);
    if (ret < 0)
        return ret;

    data = frame->data + frame->header_length + 16;

    num_of_encrypted_blocks = (frame->length - frame->header_length - 16)/16;

    av_aes_crypt(crypto_ctx->aes_ctx, data, data, num_of_encrypted_blocks, crypto_ctx->iv, 1);

    return 0;
}

static int decrypt_audio_frame(enum AVCodecID codec_id, HLSCryptoContext *crypto_ctx, AVPacket *pkt)
{
    int ret = 0;
    CodecParserContext  ctx;
    AudioFrame frame;

    memset(&ctx, 0, sizeof(ctx));
    ctx.buf_ptr = pkt->data;
    ctx.buf_end = pkt->data + pkt->size;

    while (ctx.buf_ptr < ctx.buf_end) {
        memset(&frame, 0, sizeof(frame));
        ret = get_next_sync_frame(codec_id, &ctx, &frame);
        if (ret < 0)
            return ret;
        if (frame.length - frame.header_length > 31) {
            ret = decrypt_sync_frame(codec_id, crypto_ctx, &frame);
            if (ret < 0)
                return ret;
        }
        ctx.buf_ptr += frame.length;
    }

    return 0;
}

int ff_hls_senc_decrypt_frame(enum AVCodecID codec_id, HLSCryptoContext *crypto_ctx, AVPacket *pkt)
{
    if (codec_id == AV_CODEC_ID_H264)
        return decrypt_video_frame(crypto_ctx, pkt);
    else if (codec_id == AV_CODEC_ID_AAC || codec_id == AV_CODEC_ID_AC3 || codec_id == AV_CODEC_ID_EAC3)
        return decrypt_audio_frame(codec_id, crypto_ctx, pkt);

    return AVERROR_INVALIDDATA;
}