1 /*
2 * Apple HTTP Live Streaming Sample Encryption/Decryption
3 *
4 * Copyright (c) 2021 Nachiket Tarate
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Apple HTTP Live Streaming Sample Encryption
26 * https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/HLS_Sample_Encryption
27 */
28
32
34
39
40
47
53
58
60
62 {
64 return;
65
67
68 /* Always keep this list in sync with the one from hls_read_header() */
69 if (
info->codec_tag ==
MKTAG(
'z',
'a',
'a',
'c'))
71 else if (
info->codec_tag ==
MKTAG(
'z',
'a',
'c',
'3'))
73 else if (
info->codec_tag ==
MKTAG(
'z',
'e',
'c',
'3'))
75 else
77
78 buf += 4;
80 buf += 2;
81 info->version = *buf++;
82 info->setup_data_length = *buf++;
83
84 if (
info->setup_data_length >
size - 8)
86
88 return;
89
90 memcpy(
info->setup_data, buf,
info->setup_data_length);
91 }
92
94 {
96
98
100 return 0;
101
104
107
112 }
113
118
120 } else { /* Parse 'dec3' EC3SpecificBox */
123 int data_rate, fscod, acmod, lfeon;
124
128
135
137
139 if (lfeon)
141
144
146 }
147
148 return 0;
149 }
150
151 /*
152 * Remove start code emulation prevention 0x03 bytes
153 */
155 {
157 int j = 0;
158
160
161 while (i < nalu->length) {
166 } else {
168 }
169 }
170
172 }
173
175 {
176 const uint8_t *nalu_start =
ctx->buf_ptr;
177
178 if (
ctx->buf_end -
ctx->buf_ptr >= 4 &&
AV_RB32(
ctx->buf_ptr) == 0x00000001)
180 else if (
ctx->buf_end -
ctx->buf_ptr >= 3 &&
AV_RB24(
ctx->buf_ptr) == 0x000001)
182 else /* No start code at the beginning of the NAL unit */
183 return -1;
184
186
187 while (
ctx->buf_ptr <
ctx->buf_end) {
188 if (
ctx->buf_end -
ctx->buf_ptr >= 4 &&
AV_RB32(
ctx->buf_ptr) == 0x00000001)
189 break;
190 else if (
ctx->buf_end -
ctx->buf_ptr >= 3 &&
AV_RB24(
ctx->buf_ptr) == 0x000001)
191 break;
193 }
194
198
199 return 0;
200 }
201
203 {
205 int rem_bytes;
207 uint8_t iv[16];
208
212
213 /* Remove start code emulation prevention 0x03 bytes */
215
217 rem_bytes = nalu->
length - 32;
218
219 memcpy(iv, crypto_ctx->
iv, 16);
220
221 while (rem_bytes > 0) {
222 if (rem_bytes > 16) {
225 rem_bytes -= 16;
226 }
228 rem_bytes -=
FFMIN(144, rem_bytes);
229 }
230
231 return 0;
232 }
233
235 {
239 uint8_t *data_ptr;
240 int move_nalu = 0;
241
242 memset(&
ctx, 0,
sizeof(
ctx));
245
247
248 while (
ctx.buf_ptr <
ctx.buf_end) {
249 memset(&nalu, 0, sizeof(nalu));
253 if ((nalu.
type == 0x01 || nalu.
type == 0x05) && nalu.
length > 48) {
254 int encrypted_nalu_length = nalu.
length;
258 move_nalu = nalu.
length != encrypted_nalu_length;
259 }
260 if (move_nalu)
263 }
264
266
267 return 0;
268 }
269
271 {
273
275
276 /* Find next sync word 0xFFF */
277 while (
ctx->buf_ptr <
ctx->buf_end - 1) {
278 if (*
ctx->buf_ptr == 0xFF && (*(
ctx->buf_ptr + 1) & 0xF0) == 0xF0)
279 break;
281 }
282
283 if (
ctx->buf_ptr >=
ctx->buf_end - 1)
284 return -1;
285
286 frame->data = (uint8_t*)
ctx->buf_ptr;
287
291
294
296
297 return 0;
298 }
299
301 {
303
305
306 /* Find next sync word 0x0B77 */
307 while (
ctx->buf_ptr <
ctx->buf_end - 1) {
308 if (*
ctx->buf_ptr == 0x0B && *(
ctx->buf_ptr + 1) == 0x77)
309 break;
311 }
312
313 if (
ctx->buf_ptr >=
ctx->buf_end - 1)
314 return -1;
315
316 frame->data = (uint8_t*)
ctx->buf_ptr;
317 frame->header_length = 0;
318
323 }
324
326
328
329 return 0;
330 }
331
333 {
338 else
340 }
341
343 {
346 int num_of_encrypted_blocks;
347
351
353
354 num_of_encrypted_blocks = (
frame->length -
frame->header_length - 16)/16;
355
357
358 return 0;
359 }
360
362 {
366
367 memset(&
ctx, 0,
sizeof(
ctx));
370
371 while (
ctx.buf_ptr <
ctx.buf_end) {
376 if (
frame.length -
frame.header_length > 31) {
380 }
382 }
383
384 return 0;
385 }
386
388 {
393
395 }