FFmpeg
hls_sample_encryption.c
Go to the documentation of this file.
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 
30 
31 #include "hls_sample_encryption.h"
32 
33 #include "libavcodec/adts_header.h"
34 #include "libavcodec/adts_parser.h"
36 
37 
38 typedef struct NALUnit {
39  uint8_t *data;
40  int type;
41  int length;
43 } NALUnit;
44 
45 typedef struct AudioFrame {
46  uint8_t *data;
47  int length;
49 } AudioFrame;
50 
51 typedef struct CodecParserContext {
52  const uint8_t *buf_ptr;
53  const uint8_t *buf_end;
55 
56 static const int eac3_sample_rate_tab[] = { 48000, 44100, 32000, 0 };
57 
59 {
60  if (size < 8)
61  return;
62 
63  info->codec_tag = AV_RL32(buf);
64 
65  if (info->codec_tag == MKTAG('z','a','a','c'))
66  info->codec_id = AV_CODEC_ID_AAC;
67  else if (info->codec_tag == MKTAG('z','a','c','3'))
68  info->codec_id = AV_CODEC_ID_AC3;
69  else if (info->codec_tag == MKTAG('z','e','c','3'))
70  info->codec_id = AV_CODEC_ID_EAC3;
71  else
72  info->codec_id = AV_CODEC_ID_NONE;
73 
74  buf += 4;
75  info->priming = AV_RL16(buf);
76  buf += 2;
77  info->version = *buf++;
78  info->setup_data_length = *buf++;
79 
80  if (info->setup_data_length > size - 8)
81  info->setup_data_length = size - 8;
82 
83  if (info->setup_data_length > HLS_MAX_AUDIO_SETUP_DATA_LEN)
84  return;
85 
86  memcpy(info->setup_data, buf, info->setup_data_length);
87 }
88 
90 {
91  int ret = 0;
92 
93  st->codecpar->codec_tag = info->codec_tag;
94 
95  if (st->codecpar->codec_id == AV_CODEC_ID_AAC)
96  return 0;
97 
99  return AVERROR_INVALIDDATA;
100 
101  if (st->codecpar->codec_id == AV_CODEC_ID_AC3) {
102  AC3HeaderInfo *ac3hdr = NULL;
103 
104  ret = avpriv_ac3_parse_header(&ac3hdr, info->setup_data, info->setup_data_length);
105  if (ret < 0) {
106  if (ret != AVERROR(ENOMEM))
107  av_free(ac3hdr);
108  return ret;
109  }
110 
111  st->codecpar->sample_rate = ac3hdr->sample_rate;
112  st->codecpar->channels = ac3hdr->channels;
113  st->codecpar->channel_layout = ac3hdr->channel_layout;
114  st->codecpar->bit_rate = ac3hdr->bit_rate;
115 
116  av_free(ac3hdr);
117  } else { /* Parse 'dec3' EC3SpecificBox */
118  GetBitContext gb;
119  int data_rate, fscod, acmod, lfeon;
120 
121  ret = init_get_bits8(&gb, info->setup_data, info->setup_data_length);
122  if (ret < 0)
123  return AVERROR_INVALIDDATA;
124 
125  data_rate = get_bits(&gb, 13);
126  skip_bits(&gb, 3);
127  fscod = get_bits(&gb, 2);
128  skip_bits(&gb, 10);
129  acmod = get_bits(&gb, 3);
130  lfeon = get_bits(&gb, 1);
131 
133 
135  if (lfeon)
137 
139 
140  st->codecpar->bit_rate = data_rate*1000;
141  }
142 
143  return 0;
144 }
145 
146 /*
147  * Remove start code emulation prevention 0x03 bytes
148  */
149 static void remove_scep_3_bytes(NALUnit *nalu)
150 {
151  int i = 0;
152  int j = 0;
153 
154  uint8_t *data = nalu->data;
155 
156  while (i < nalu->length) {
157  if (nalu->length - i > 3 && AV_RB24(&data[i]) == 0x000003) {
158  data[j++] = data[i++];
159  data[j++] = data[i++];
160  i++;
161  } else {
162  data[j++] = data[i++];
163  }
164  }
165 
166  nalu->length = j;
167 }
168 
170 {
171  const uint8_t *nalu_start = ctx->buf_ptr;
172 
173  if (ctx->buf_end - ctx->buf_ptr >= 4 && AV_RB32(ctx->buf_ptr) == 0x00000001)
174  nalu->start_code_length = 4;
175  else if (ctx->buf_end - ctx->buf_ptr >= 3 && AV_RB24(ctx->buf_ptr) == 0x000001)
176  nalu->start_code_length = 3;
177  else /* No start code at the beginning of the NAL unit */
178  return -1;
179 
180  ctx->buf_ptr += nalu->start_code_length;
181 
182  while (ctx->buf_ptr < ctx->buf_end) {
183  if (ctx->buf_end - ctx->buf_ptr >= 4 && AV_RB32(ctx->buf_ptr) == 0x00000001)
184  break;
185  else if (ctx->buf_end - ctx->buf_ptr >= 3 && AV_RB24(ctx->buf_ptr) == 0x000001)
186  break;
187  ctx->buf_ptr++;
188  }
189 
190  nalu->data = (uint8_t *)nalu_start + nalu->start_code_length;
191  nalu->length = ctx->buf_ptr - nalu->data;
192  nalu->type = *nalu->data & 0x1F;
193 
194  return 0;
195 }
196 
197 static int decrypt_nal_unit(HLSCryptoContext *crypto_ctx, NALUnit *nalu)
198 {
199  int ret = 0;
200  int rem_bytes;
201  uint8_t *data;
202  uint8_t iv[16];
203 
204  ret = av_aes_init(crypto_ctx->aes_ctx, crypto_ctx->key, 16 * 8, 1);
205  if (ret < 0)
206  return ret;
207 
208  /* Remove start code emulation prevention 0x03 bytes */
209  remove_scep_3_bytes(nalu);
210 
211  data = nalu->data + 32;
212  rem_bytes = nalu->length - 32;
213 
214  memcpy(iv, crypto_ctx->iv, 16);
215 
216  while (rem_bytes > 0) {
217  if (rem_bytes > 16) {
218  av_aes_crypt(crypto_ctx->aes_ctx, data, data, 1, iv, 1);
219  data += 16;
220  rem_bytes -= 16;
221  }
222  data += FFMIN(144, rem_bytes);
223  rem_bytes -= FFMIN(144, rem_bytes);
224  }
225 
226  return 0;
227 }
228 
230 {
231  int ret = 0;
233  NALUnit nalu;
234  uint8_t *data_ptr;
235  int move_nalu = 0;
236 
237  memset(&ctx, 0, sizeof(ctx));
238  ctx.buf_ptr = pkt->data;
239  ctx.buf_end = pkt->data + pkt->size;
240 
241  data_ptr = pkt->data;
242 
243  while (ctx.buf_ptr < ctx.buf_end) {
244  memset(&nalu, 0, sizeof(nalu));
245  ret = get_next_nal_unit(&ctx, &nalu);
246  if (ret < 0)
247  return ret;
248  if ((nalu.type == 0x01 || nalu.type == 0x05) && nalu.length > 48) {
249  int encrypted_nalu_length = nalu.length;
250  ret = decrypt_nal_unit(crypto_ctx, &nalu);
251  if (ret < 0)
252  return ret;
253  move_nalu = nalu.length != encrypted_nalu_length;
254  }
255  if (move_nalu)
256  memmove(data_ptr, nalu.data - nalu.start_code_length, nalu.start_code_length + nalu.length);
257  data_ptr += nalu.start_code_length + nalu.length;
258  }
259 
260  av_shrink_packet(pkt, data_ptr - pkt->data);
261 
262  return 0;
263 }
264 
266 {
267  int ret = 0;
268 
269  AACADTSHeaderInfo *adts_hdr = NULL;
270 
271  /* Find next sync word 0xFFF */
272  while (ctx->buf_ptr < ctx->buf_end - 1) {
273  if (*ctx->buf_ptr == 0xFF && (*(ctx->buf_ptr + 1) & 0xF0) == 0xF0)
274  break;
275  ctx->buf_ptr++;
276  }
277 
278  if (ctx->buf_ptr >= ctx->buf_end - 1)
279  return -1;
280 
281  frame->data = (uint8_t*)ctx->buf_ptr;
282 
283  ret = avpriv_adts_header_parse (&adts_hdr, frame->data, ctx->buf_end - frame->data);
284  if (ret < 0)
285  return ret;
286 
287  frame->header_length = adts_hdr->crc_absent ? AV_AAC_ADTS_HEADER_SIZE : AV_AAC_ADTS_HEADER_SIZE + 2;
288  frame->length = adts_hdr->frame_length;
289 
290  av_free(adts_hdr);
291 
292  return 0;
293 }
294 
296 {
297  int ret = 0;
298 
299  AC3HeaderInfo *hdr = NULL;
300 
301  /* Find next sync word 0x0B77 */
302  while (ctx->buf_ptr < ctx->buf_end - 1) {
303  if (*ctx->buf_ptr == 0x0B && *(ctx->buf_ptr + 1) == 0x77)
304  break;
305  ctx->buf_ptr++;
306  }
307 
308  if (ctx->buf_ptr >= ctx->buf_end - 1)
309  return -1;
310 
311  frame->data = (uint8_t*)ctx->buf_ptr;
312  frame->header_length = 0;
313 
314  ret = avpriv_ac3_parse_header(&hdr, frame->data, ctx->buf_end - frame->data);
315  if (ret < 0) {
316  if (ret != AVERROR(ENOMEM))
317  av_free(hdr);
318  return ret;
319  }
320 
321  frame->length = hdr->frame_size;
322 
323  av_free(hdr);
324 
325  return 0;
326 }
327 
329 {
330  if (codec_id == AV_CODEC_ID_AAC)
331  return get_next_adts_frame(ctx, frame);
334  else
335  return AVERROR_INVALIDDATA;
336 }
337 
339 {
340  int ret = 0;
341  uint8_t *data;
342  int num_of_encrypted_blocks;
343 
344  ret = av_aes_init(crypto_ctx->aes_ctx, crypto_ctx->key, 16 * 8, 1);
345  if (ret < 0)
346  return ret;
347 
348  data = frame->data + frame->header_length + 16;
349 
350  num_of_encrypted_blocks = (frame->length - frame->header_length - 16)/16;
351 
352  av_aes_crypt(crypto_ctx->aes_ctx, data, data, num_of_encrypted_blocks, crypto_ctx->iv, 1);
353 
354  return 0;
355 }
356 
358 {
359  int ret = 0;
362 
363  memset(&ctx, 0, sizeof(ctx));
364  ctx.buf_ptr = pkt->data;
365  ctx.buf_end = pkt->data + pkt->size;
366 
367  while (ctx.buf_ptr < ctx.buf_end) {
368  memset(&frame, 0, sizeof(frame));
370  if (ret < 0)
371  return ret;
372  if (frame.length - frame.header_length > 31) {
373  ret = decrypt_sync_frame(codec_id, crypto_ctx, &frame);
374  if (ret < 0)
375  return ret;
376  }
377  ctx.buf_ptr += frame.length;
378  }
379 
380  return 0;
381 }
382 
384 {
385  if (codec_id == AV_CODEC_ID_H264)
386  return decrypt_video_frame(crypto_ctx, pkt);
388  return decrypt_audio_frame(codec_id, crypto_ctx, pkt);
389 
390  return AVERROR_INVALIDDATA;
391 }
AudioFrame::data
uint8_t * data
Definition: hls_sample_encryption.c:46
get_next_adts_frame
static int get_next_adts_frame(CodecParserContext *ctx, AudioFrame *frame)
Definition: hls_sample_encryption.c:265
av_aes_init
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
Definition: aes.c:195
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:426
ff_ac3_channel_layout_tab
const uint16_t ff_ac3_channel_layout_tab[8]
Map audio coding mode (acmod) to channel layout mask.
Definition: ac3_channel_layout_tab.h:31
decrypt_video_frame
static int decrypt_video_frame(HLSCryptoContext *crypto_ctx, AVPacket *pkt)
Definition: hls_sample_encryption.c:229
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
CodecParserContext::buf_ptr
const uint8_t * buf_ptr
Definition: hls_sample_encryption.c:52
AV_AAC_ADTS_HEADER_SIZE
#define AV_AAC_ADTS_HEADER_SIZE
Definition: adts_parser.h:25
get_next_sync_frame
static int get_next_sync_frame(enum AVCodecID codec_id, CodecParserContext *ctx, AudioFrame *frame)
Definition: hls_sample_encryption.c:328
NALUnit::start_code_length
int start_code_length
Definition: hls_sample_encryption.c:42
AVPacket::data
uint8_t * data
Definition: packet.h:373
data
const char data[16]
Definition: mxf.c:143
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
get_next_ac3_eac3_sync_frame
static int get_next_ac3_eac3_sync_frame(CodecParserContext *ctx, AudioFrame *frame)
Definition: hls_sample_encryption.c:295
CodecParserContext::buf_end
const uint8_t * buf_end
Definition: hls_sample_encryption.c:53
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:468
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:380
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
eac3_sample_rate_tab
static const int eac3_sample_rate_tab[]
Definition: hls_sample_encryption.c:56
AC3HeaderInfo::channel_layout
uint64_t channel_layout
Definition: ac3.h:205
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:114
GetBitContext
Definition: get_bits.h:62
AC3HeaderInfo
Definition: ac3.h:177
CodecParserContext
Definition: hls_sample_encryption.c:51
AC3HeaderInfo::frame_size
uint16_t frame_size
Definition: ac3.h:204
ff_hls_senc_read_audio_setup_info
void ff_hls_senc_read_audio_setup_info(HLSAudioSetupInfo *info, const uint8_t *buf, size_t size)
Definition: hls_sample_encryption.c:58
HLSCryptoContext::iv
uint8_t iv[16]
Definition: hls_sample_encryption.h:45
get_next_nal_unit
static int get_next_nal_unit(CodecParserContext *ctx, NALUnit *nalu)
Definition: hls_sample_encryption.c:169
pkt
AVPacket * pkt
Definition: movenc.c:59
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:678
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:52
HLSCryptoContext::aes_ctx
struct AVAES * aes_ctx
Definition: hls_sample_encryption.h:43
info
MIPS optimizations info
Definition: mips.txt:2
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
AC3HeaderInfo::sample_rate
uint16_t sample_rate
Definition: ac3.h:201
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:369
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:77
AudioFrame::length
int length
Definition: hls_sample_encryption.c:47
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
NULL
#define NULL
Definition: coverity.c:32
decrypt_audio_frame
static int decrypt_audio_frame(enum AVCodecID codec_id, HLSCryptoContext *crypto_ctx, AVPacket *pkt)
Definition: hls_sample_encryption.c:357
av_aes_crypt
void av_aes_crypt(AVAES *a, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
Encrypt or decrypt a buffer using a previously initialized context.
Definition: aes.c:163
AACADTSHeaderInfo::frame_length
uint32_t frame_length
Definition: adts_header.h:37
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
av_get_channel_layout_nb_channels
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Definition: channel_layout.c:226
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:463
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:425
avpriv_adts_header_parse
int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_t size)
Parse the ADTS frame header contained in the buffer, which is the first 54 bits.
Definition: adts_parser.c:46
AC3HeaderInfo::channels
uint8_t channels
Definition: ac3.h:203
ac3_parser_internal.h
AVPacket::size
int size
Definition: packet.h:374
HLSCryptoContext
Definition: hls_sample_encryption.h:42
size
int size
Definition: twinvq_data.h:10344
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
NALUnit::length
int length
Definition: hls_sample_encryption.c:41
hls_sample_encryption.h
NALUnit::type
int type
Definition: hls_sample_encryption.c:40
ff_hls_senc_decrypt_frame
int ff_hls_senc_decrypt_frame(enum AVCodecID codec_id, HLSCryptoContext *crypto_ctx, AVPacket *pkt)
Definition: hls_sample_encryption.c:383
NALUnit::data
uint8_t * data
Definition: hls_sample_encryption.c:39
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
HLS_MAX_AUDIO_SETUP_DATA_LEN
#define HLS_MAX_AUDIO_SETUP_DATA_LEN
Definition: hls_sample_encryption.h:40
avpriv_ac3_parse_header
int avpriv_ac3_parse_header(AC3HeaderInfo **phdr, const uint8_t *buf, size_t size)
Definition: ac3_parser.c:253
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ff_hls_senc_parse_audio_setup_info
int ff_hls_senc_parse_audio_setup_info(AVStream *st, HLSAudioSetupInfo *info)
Definition: hls_sample_encryption.c:89
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
remove_scep_3_bytes
static void remove_scep_3_bytes(NALUnit *nalu)
Definition: hls_sample_encryption.c:149
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
adts_parser.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AACADTSHeaderInfo::crc_absent
uint8_t crc_absent
Definition: adts_header.h:32
NALUnit
Definition: hls_sample_encryption.c:38
channel_layout.h
decrypt_sync_frame
static int decrypt_sync_frame(enum AVCodecID codec_id, HLSCryptoContext *crypto_ctx, AudioFrame *frame)
Definition: hls_sample_encryption.c:338
decrypt_nal_unit
static int decrypt_nal_unit(HLSCryptoContext *crypto_ctx, NALUnit *nalu)
Definition: hls_sample_encryption.c:197
adts_header.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AudioFrame
Definition: audio_frame_queue.h:27
AudioFrame::header_length
int header_length
Definition: hls_sample_encryption.c:48
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
HLSAudioSetupInfo
Definition: hls_sample_encryption.h:48
AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:97
AC3HeaderInfo::bit_rate
uint32_t bit_rate
Definition: ac3.h:202
AACADTSHeaderInfo
Definition: adts_header.h:28
HLSCryptoContext::key
uint8_t key[16]
Definition: hls_sample_encryption.h:44