2 * MOV CENC (Common Encryption) writer
3 * Copyright (c) 2015 Eran Kornblau <erankor at gmail dot com>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef AVFORMAT_MOVENCCENC_H
23 #define AVFORMAT_MOVENCCENC_H
25 #include "libavutil/aes_ctr.h"
30 #define CENC_KID_SIZE (16)
34 struct MOVMuxCencAV1TGInfo
{
35 uint32_t encrypted_bytes
;
36 uint32_t write_clear_bytes
;
37 uint32_t aux_clear_bytes
;
41 struct AVAESCTR
* aes_ctr
;
42 uint8_t* auxiliary_info
;
43 size_t auxiliary_info_size
;
44 size_t auxiliary_info_alloc_size
;
45 uint32_t auxiliary_info_entries
;
47 /* subsample support */
49 uint16_t subsample_count
;
50 size_t auxiliary_info_subsample_start
;
51 uint8_t* auxiliary_info_sizes
;
52 size_t auxiliary_info_sizes_alloc_size
;
55 struct MOVMuxCencAV1TGInfo
*tile_group_sizes
;
59 CodedBitstreamContext
*cbc
;
60 CodedBitstreamFragment temporal_unit
;
64 * Initialize a CENC context
65 * @param key encryption key, must have a length of AES_CTR_KEY_SIZE
66 * @param use_subsamples when enabled parts of a packet can be encrypted, otherwise the whole packet is encrypted
68 int ff_mov_cenc_init(MOVMuxCencContext
* ctx
, uint8_t* encryption_key
, int use_subsamples
,
69 enum AVCodecID codec_id
, int bitexact
);
74 void ff_mov_cenc_free(MOVMuxCencContext
* ctx
);
77 * Write a fully encrypted packet
79 int ff_mov_cenc_write_packet(MOVMuxCencContext
* ctx
, AVIOContext
*pb
, const uint8_t *buf_in
, int size
);
82 * Parse AVC NAL units from annex B format, the nal size and type are written in the clear while the body is encrypted
84 int ff_mov_cenc_avc_parse_nal_units(MOVMuxCencContext
* ctx
, AVIOContext
*pb
, const uint8_t *buf_in
, int size
);
87 * Write AVC NAL units that are in MP4 format, the nal size and type are written in the clear while the body is encrypted
89 int ff_mov_cenc_avc_write_nal_units(AVFormatContext
*s
, MOVMuxCencContext
* ctx
, int nal_length_size
,
90 AVIOContext
*pb
, const uint8_t *buf_in
, int size
);
92 int ff_mov_cenc_av1_write_obus(AVFormatContext
*s
, MOVMuxCencContext
* ctx
,
93 AVIOContext
*pb
, const AVPacket
*pkt
);
96 * Clear subsample data. To be called for fragmented output.
98 void ff_mov_cenc_flush(MOVMuxCencContext
* ctx
);
101 * Write the cenc atoms that should reside inside stbl
103 void ff_mov_cenc_write_stbl_atoms(MOVMuxCencContext
* ctx
, AVIOContext
*pb
, int64_t moof_offset
);
106 * Write the sinf atom, contained inside stsd
108 int ff_mov_cenc_write_sinf_tag(struct MOVTrack
* track
, AVIOContext
*pb
, uint8_t* kid
);
110 #endif /* AVFORMAT_MOVENCCENC_H */