Skip to content

Commit 6a108d4

Browse files
Malek Assaadjamrial
authored andcommitted
avformat/mov: add support for pssh box
* Enable PSSH box writing to mov & moof. * Enable senc, saio & saiz writing to moof when writing fragments. Signed-off-by: James Almer <[email protected]>
1 parent 7b73ea5 commit 6a108d4

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

libavformat/movenc.c

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3221,8 +3221,8 @@ static int mov_write_stbl_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext
32213221
mov_write_stsc_tag(pb, track);
32223222
mov_write_stsz_tag(pb, track);
32233223
mov_write_stco_tag(pb, track);
3224-
if (track->cenc.aes_ctr) {
3225-
ff_mov_cenc_write_stbl_atoms(&track->cenc, pb);
3224+
if (track->cenc.aes_ctr && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
3225+
ff_mov_cenc_write_stbl_atoms(&track->cenc, pb, 0);
32263226
}
32273227
if (track->par->codec_id == AV_CODEC_ID_OPUS || track->par->codec_id == AV_CODEC_ID_AAC) {
32283228
mov_preroll_write_stbl_atoms(pb, track);
@@ -4899,6 +4899,43 @@ static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
48994899
return 0;
49004900
}
49014901

4902+
static int mov_write_pssh_tag(AVIOContext *pb, AVStream *st)
4903+
{
4904+
AVEncryptionInitInfo *info;
4905+
const AVPacketSideData *sd = av_packet_side_data_get(st->codecpar->coded_side_data,
4906+
st->codecpar->nb_coded_side_data,
4907+
AV_PKT_DATA_ENCRYPTION_INIT_INFO);
4908+
if (!sd)
4909+
return 0;
4910+
4911+
info = av_encryption_init_info_get_side_data(sd->data, sd->size);
4912+
for (AVEncryptionInitInfo *copy = info; copy; copy = copy->next) {
4913+
int64_t pos;
4914+
4915+
if (!copy->data_size && !copy->num_key_ids)
4916+
continue;
4917+
4918+
pos = avio_tell(pb);
4919+
avio_wb32(pb, 0); /* size placeholder */
4920+
ffio_wfourcc(pb, "pssh");
4921+
avio_w8(pb, 1); /* version */
4922+
avio_wb24(pb, 0);
4923+
for (int i = 0; i < copy->system_id_size; i++)
4924+
avio_w8(pb, copy->system_id[i]);
4925+
avio_wb32(pb, copy->num_key_ids);
4926+
for (int i = 0; i < copy->num_key_ids; i++)
4927+
for (int j = 0; j < copy->key_id_size; j++)
4928+
avio_w8(pb, copy->key_ids[i][j]);
4929+
avio_wb32(pb, copy->data_size);
4930+
avio_write(pb, copy->data, copy->data_size);
4931+
update_size(pb, pos);
4932+
}
4933+
4934+
av_encryption_init_info_free(info);
4935+
4936+
return 0;
4937+
}
4938+
49024939
static void build_chunks(MOVTrack *trk)
49034940
{
49044941
int i;
@@ -5046,6 +5083,8 @@ static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
50465083
mov_write_uuidusmt_tag(pb, s);
50475084
else if (mov->mode != MODE_AVIF)
50485085
mov_write_udta_tag(pb, mov, s);
5086+
for (i = 0; i < mov->nb_streams; i++)
5087+
mov_write_pssh_tag(pb, mov->tracks[i].st);
50495088

50505089
return update_size(pb, pos);
50515090
}
@@ -5499,6 +5538,9 @@ static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
54995538
}
55005539
}
55015540

5541+
if (track->cenc.aes_ctr && (mov->flags & FF_MOV_FLAG_FRAGMENT))
5542+
ff_mov_cenc_write_stbl_atoms(&track->cenc, pb, moof_offset);
5543+
55025544
return update_size(pb, pos);
55035545
}
55045546

@@ -5519,6 +5561,8 @@ static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov,
55195561
continue;
55205562
if (!track->entry)
55215563
continue;
5564+
if (track->cenc.aes_ctr && (mov->flags & FF_MOV_FLAG_FRAGMENT))
5565+
mov_write_pssh_tag(pb, track->st);
55225566
mov_write_traf_tag(pb, mov, track, pos, moof_size);
55235567
}
55245568

libavformat/movenccenc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,13 @@ static int mov_cenc_write_saiz_tag(MOVMuxCencContext* ctx, AVIOContext *pb)
338338
return update_size(pb, pos);
339339
}
340340

341-
void ff_mov_cenc_write_stbl_atoms(MOVMuxCencContext* ctx, AVIOContext *pb)
341+
void ff_mov_cenc_write_stbl_atoms(MOVMuxCencContext* ctx, AVIOContext *pb,
342+
int64_t moof_offset)
342343
{
343344
int64_t auxiliary_info_offset;
344345

345346
mov_cenc_write_senc_tag(ctx, pb, &auxiliary_info_offset);
346-
mov_cenc_write_saio_tag(pb, auxiliary_info_offset);
347+
mov_cenc_write_saio_tag(pb, auxiliary_info_offset - moof_offset);
347348
mov_cenc_write_saiz_tag(ctx, pb);
348349
}
349350

libavformat/movenccenc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int ff_mov_cenc_avc_write_nal_units(AVFormatContext *s, MOVMuxCencContext* ctx,
7676
/**
7777
* Write the cenc atoms that should reside inside stbl
7878
*/
79-
void ff_mov_cenc_write_stbl_atoms(MOVMuxCencContext* ctx, AVIOContext *pb);
79+
void ff_mov_cenc_write_stbl_atoms(MOVMuxCencContext* ctx, AVIOContext *pb, int64_t moof_offset);
8080

8181
/**
8282
* Write the sinf atom, contained inside stsd

0 commit comments

Comments
 (0)