summaryrefslogtreecommitdiff
path: root/libavformat/movenc.c
diff options
context:
space:
mode:
Diffstat (limited to 'libavformat/movenc.c')
-rw-r--r--libavformat/movenc.c92
1 files changed, 85 insertions, 7 deletions
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index e3bab96587..3cb3263dd8 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -83,6 +83,9 @@ static const AVOption options[] = {
{ "fragment_index", "Fragment number of the next fragment", offsetof(MOVMuxContext, fragments), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
{ "mov_gamma", "gamma value for gama atom", offsetof(MOVMuxContext, gamma), AV_OPT_TYPE_FLOAT, {.dbl = 0.0 }, 0.0, 10, AV_OPT_FLAG_ENCODING_PARAM},
{ "frag_interleave", "Interleave samples within fragments (max number of consecutive samples, lower is tighter interleaving, but with more overhead)", offsetof(MOVMuxContext, frag_interleave), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
+ { "encryption_scheme", "Configures the encryption scheme, allowed values are none, cenc-aes-ctr", offsetof(MOVMuxContext, encryption_scheme_str), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM, .unit = "movflags" },
+ { "encryption_key", "The media encryption key (hex)", offsetof(MOVMuxContext, encryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_ENCODING_PARAM, .unit = "movflags" },
+ { "encryption_kid", "The media encryption key identifier (hex)", offsetof(MOVMuxContext, encryption_kid), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_ENCODING_PARAM, .unit = "movflags" },
{ NULL },
};
@@ -891,7 +894,7 @@ static int get_samples_per_packet(MOVTrack *track)
return first_duration;
}
-static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
+static int mov_write_audio_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track)
{
int64_t pos = avio_tell(pb);
int version = 0;
@@ -912,7 +915,11 @@ static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
}
avio_wb32(pb, 0); /* size */
- avio_wl32(pb, tag); // store it byteswapped
+ if (mov->encryption_scheme != MOV_ENC_NONE) {
+ ffio_wfourcc(pb, "enca");
+ } else {
+ avio_wl32(pb, tag); // store it byteswapped
+ }
avio_wb32(pb, 0); /* Reserved */
avio_wb16(pb, 0); /* Reserved */
avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
@@ -1000,6 +1007,10 @@ static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
mov_write_chan_tag(pb, track);
+ if (mov->encryption_scheme != MOV_ENC_NONE) {
+ ff_mov_cenc_write_sinf_tag(track, pb, mov->encryption_kid);
+ }
+
return update_size(pb, pos);
}
@@ -1659,7 +1670,11 @@ static int mov_write_video_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *tr
int avid = 0;
avio_wb32(pb, 0); /* size */
- avio_wl32(pb, track->tag); // store it byteswapped
+ if (mov->encryption_scheme != MOV_ENC_NONE) {
+ ffio_wfourcc(pb, "encv");
+ } else {
+ avio_wl32(pb, track->tag); // store it byteswapped
+ }
avio_wb32(pb, 0); /* Reserved */
avio_wb16(pb, 0); /* Reserved */
avio_wb16(pb, 1); /* Data-reference index */
@@ -1750,6 +1765,10 @@ static int mov_write_video_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *tr
mov_write_pasp_tag(pb, track);
}
+ if (mov->encryption_scheme != MOV_ENC_NONE) {
+ ff_mov_cenc_write_sinf_tag(track, pb, mov->encryption_kid);
+ }
+
/* extra padding for avid stsd */
/* https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-61112 */
if (avid)
@@ -1848,9 +1867,9 @@ static int mov_write_stsd_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *tra
avio_wb32(pb, 0); /* version & flags */
avio_wb32(pb, 1); /* entry count */
if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
- mov_write_video_tag(pb, mov, track);
+ mov_write_video_tag(pb, mov, track);
else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
- mov_write_audio_tag(pb, track);
+ mov_write_audio_tag(pb, mov, track);
else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
mov_write_subtitle_tag(pb, track);
else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
@@ -1980,6 +1999,9 @@ static int mov_write_stbl_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *tra
mov_write_stsc_tag(pb, track);
mov_write_stsz_tag(pb, track);
mov_write_stco_tag(pb, track);
+ if (mov->encryption_scheme == MOV_ENC_CENC_AES_CTR) {
+ ff_mov_cenc_write_stbl_atoms(&track->cenc, pb);
+ }
return update_size(pb, pos);
}
@@ -4419,7 +4441,15 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
&size);
avio_write(pb, reformatted_data, size);
} else {
- size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
+ if (mov->encryption_scheme == MOV_ENC_CENC_AES_CTR) {
+ size = ff_mov_cenc_avc_parse_nal_units(&trk->cenc, pb, pkt->data, size);
+ if (size < 0) {
+ ret = size;
+ goto err;
+ }
+ } else {
+ size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
+ }
}
} else if (enc->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
(AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
@@ -4440,7 +4470,20 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
avio_write(pb, pkt->data, size);
#endif
} else {
- avio_write(pb, pkt->data, size);
+ if (mov->encryption_scheme == MOV_ENC_CENC_AES_CTR) {
+ if (enc->codec_id == AV_CODEC_ID_H264 && enc->extradata_size > 4) {
+ int nal_size_length = (enc->extradata[4] & 0x3) + 1;
+ ret = ff_mov_cenc_avc_write_nal_units(s, &trk->cenc, nal_size_length, pb, pkt->data, size);
+ } else {
+ ret = ff_mov_cenc_write_packet(&trk->cenc, pb, pkt->data, size);
+ }
+
+ if (ret) {
+ goto err;
+ }
+ } else {
+ avio_write(pb, pkt->data, size);
+ }
}
if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
@@ -4913,6 +4956,8 @@ static void mov_free(AVFormatContext *s)
if (mov->tracks[i].vos_len)
av_freep(&mov->tracks[i].vos_data);
+
+ ff_mov_cenc_free(&mov->tracks[i].cenc);
}
av_freep(&mov->tracks);
@@ -5123,6 +5168,31 @@ static int mov_write_header(AVFormatContext *s)
if (!mov->tracks)
return AVERROR(ENOMEM);
+ if (mov->encryption_scheme_str != NULL && strcmp(mov->encryption_scheme_str, "none") != 0) {
+ if (strcmp(mov->encryption_scheme_str, "cenc-aes-ctr") == 0) {
+ mov->encryption_scheme = MOV_ENC_CENC_AES_CTR;
+
+ if (mov->encryption_key_len != AES_CTR_KEY_SIZE) {
+ av_log(s, AV_LOG_ERROR, "Invalid encryption key len %d expected %d\n",
+ mov->encryption_key_len, AES_CTR_KEY_SIZE);
+ ret = AVERROR(EINVAL);
+ goto error;
+ }
+
+ if (mov->encryption_kid_len != CENC_KID_SIZE) {
+ av_log(s, AV_LOG_ERROR, "Invalid encryption kid len %d expected %d\n",
+ mov->encryption_kid_len, CENC_KID_SIZE);
+ ret = AVERROR(EINVAL);
+ goto error;
+ }
+ } else {
+ av_log(s, AV_LOG_ERROR, "unsupported encryption scheme %s\n",
+ mov->encryption_scheme_str);
+ ret = AVERROR(EINVAL);
+ goto error;
+ }
+ }
+
for (i = 0; i < s->nb_streams; i++) {
AVStream *st= s->streams[i];
MOVTrack *track= &mov->tracks[i];
@@ -5241,6 +5311,14 @@ static int mov_write_header(AVFormatContext *s)
memcpy(track->vos_data, st->codec->extradata, track->vos_len);
}
}
+
+ if (mov->encryption_scheme == MOV_ENC_CENC_AES_CTR) {
+ ret = ff_mov_cenc_init(&track->cenc, mov->encryption_key,
+ track->enc->codec_id == AV_CODEC_ID_H264, s->flags & AVFMT_FLAG_BITEXACT);
+ if (ret) {
+ goto error;
+ }
+ }
}
for (i = 0; i < s->nb_streams; i++) {