1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-04 22:03:09 +02:00

avformat/movenccenc: add support for CENC AV1 encryption

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer
2025-03-19 20:59:49 -03:00
parent 5631704d83
commit 08e334e462
10 changed files with 298 additions and 7 deletions

View File

@ -11,6 +11,7 @@ version <next>:
- Enhanced FLV v2: Multitrack audio/video, modern codec support - Enhanced FLV v2: Multitrack audio/video, modern codec support
- Animated JPEG XL encoding (via libjxl) - Animated JPEG XL encoding (via libjxl)
- VVC in Matroska - VVC in Matroska
- CENC AV1 support in MP4 muxer
version 7.1: version 7.1:
- Raw Captions with Time (RCWT) closed caption demuxer - Raw Captions with Time (RCWT) closed caption demuxer

View File

@ -377,7 +377,7 @@ OBJS-$(CONFIG_MOV_DEMUXER) += mov.o mov_chan.o mov_esds.o \
OBJS-$(CONFIG_MOV_MUXER) += movenc.o \ OBJS-$(CONFIG_MOV_MUXER) += movenc.o \
movenchint.o mov_chan.o rtp.o \ movenchint.o mov_chan.o rtp.o \
movenccenc.o movenc_ttml.o rawutils.o \ movenccenc.o movenc_ttml.o rawutils.o \
dovi_isom.o evc.o dovi_isom.o evc.o cbs.o cbs_av1.o
OBJS-$(CONFIG_MP2_MUXER) += rawenc.o OBJS-$(CONFIG_MP2_MUXER) += rawenc.o
OBJS-$(CONFIG_MP3_DEMUXER) += mp3dec.o replaygain.o OBJS-$(CONFIG_MP3_DEMUXER) += mp3dec.o replaygain.o
OBJS-$(CONFIG_MP3_MUXER) += mp3enc.o rawenc.o id3v2enc.o OBJS-$(CONFIG_MP3_MUXER) += mp3enc.o rawenc.o id3v2enc.o

2
libavformat/cbs.c Normal file
View File

@ -0,0 +1,2 @@
#include "cbs.h"
#include "libavcodec/cbs.c"

35
libavformat/cbs.h Normal file
View File

@ -0,0 +1,35 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVFORMAT_CBS_H
#define AVFORMAT_CBS_H
#define CBS_PREFIX lavf_cbs
#define CBS_WRITE 0
#define CBS_TRACE 0
#define CBS_H264 0
#define CBS_H265 0
#define CBS_H266 0
#define CBS_JPEG 0
#define CBS_MPEG2 0
#define CBS_VP8 0
#define CBS_VP9 0
#include "libavcodec/cbs.h"
#endif /* AVFORMAT_CBS_H */

5
libavformat/cbs_av1.c Normal file
View File

@ -0,0 +1,5 @@
#define CBS_AV1_OBU_TILE_LIST 0
#define CBS_AV1_OBU_METADATA 0
#define CBS_AV1_OBU_PADDING 0
#include "cbs.h"
#include "libavcodec/cbs_av1.c"

View File

@ -6767,7 +6767,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
} else { } else {
size = ff_vvc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL); size = ff_vvc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
} }
} else if (par->codec_id == AV_CODEC_ID_AV1) { } else if (par->codec_id == AV_CODEC_ID_AV1 && !trk->cenc.aes_ctr) {
if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) { if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
ret = ff_av1_filter_obus_buf(pkt->data, &reformatted_data, ret = ff_av1_filter_obus_buf(pkt->data, &reformatted_data,
&size, &offset); &size, &offset);
@ -6815,6 +6815,13 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
ret = ff_mov_cenc_avc_write_nal_units(s, &trk->cenc, nal_size_length, pb, pkt->data, size); ret = ff_mov_cenc_avc_write_nal_units(s, &trk->cenc, nal_size_length, pb, pkt->data, size);
} else if(par->codec_id == AV_CODEC_ID_VVC) { } else if(par->codec_id == AV_CODEC_ID_VVC) {
ret = AVERROR_PATCHWELCOME; ret = AVERROR_PATCHWELCOME;
} else if(par->codec_id == AV_CODEC_ID_AV1) {
av_assert0(size == pkt->size);
ret = ff_mov_cenc_av1_write_obus(s, &trk->cenc, pb, pkt);
if (ret > 0) {
size = ret;
ret = 0;
}
} else { } else {
ret = ff_mov_cenc_write_packet(&trk->cenc, pb, pkt->data, size); ret = ff_mov_cenc_write_packet(&trk->cenc, pb, pkt->data, size);
} }
@ -8135,8 +8142,8 @@ static int mov_init(AVFormatContext *s)
if (mov->encryption_scheme == MOV_ENC_CENC_AES_CTR) { if (mov->encryption_scheme == MOV_ENC_CENC_AES_CTR) {
ret = ff_mov_cenc_init(&track->cenc, mov->encryption_key, ret = ff_mov_cenc_init(&track->cenc, mov->encryption_key,
(track->par->codec_id == AV_CODEC_ID_H264 || track->par->codec_id == AV_CODEC_ID_HEVC || (track->par->codec_id == AV_CODEC_ID_H264 || track->par->codec_id == AV_CODEC_ID_HEVC ||
track->par->codec_id == AV_CODEC_ID_VVC), track->par->codec_id == AV_CODEC_ID_VVC || track->par->codec_id == AV_CODEC_ID_AV1),
s->flags & AVFMT_FLAG_BITEXACT); track->par->codec_id, s->flags & AVFMT_FLAG_BITEXACT);
if (ret) if (ret)
return ret; return ret;
} }

View File

@ -19,6 +19,9 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include "movenccenc.h" #include "movenccenc.h"
#include "libavcodec/av1_parse.h"
#include "libavcodec/bytestream.h"
#include "libavcodec/cbs_av1.h"
#include "libavutil/intreadwrite.h" #include "libavutil/intreadwrite.h"
#include "libavutil/mem.h" #include "libavutil/mem.h"
#include "avio_internal.h" #include "avio_internal.h"
@ -280,6 +283,203 @@ int ff_mov_cenc_avc_write_nal_units(AVFormatContext *s, MOVMuxCencContext* ctx,
return 0; return 0;
} }
static int write_tiles(AVFormatContext *s, MOVMuxCencContext *ctx, AVIOContext *pb, AV1_OBU_Type type,
const AV1RawFrameHeader *frame_header, const uint8_t *fh_data, size_t fh_data_size,
const AV1RawTileGroup *tile_group)
{
GetByteContext gb;
size_t tgh_data_size = tile_group->data_size;
int cur_tile_num = frame_header->tile_cols * frame_header->tile_rows;
int total = 0;
// Get the Frame Header size
if (type == AV1_OBU_FRAME)
fh_data_size -= tgh_data_size;
// Get the Tile Group Header size
tgh_data_size -= tile_group->tile_data.data_size;
if (ctx->tile_num < cur_tile_num) {
int ret = av_reallocp_array(&ctx->tile_group_sizes, cur_tile_num,
sizeof(*ctx->tile_group_sizes));
if (ret < 0) {
ctx->tile_num = 0;
return ret;
}
}
ctx->tile_num = cur_tile_num;
total = fh_data_size + tgh_data_size;
ctx->clear_bytes += total;
bytestream2_init(&gb, tile_group->tile_data.data, tile_group->tile_data.data_size);
// Build a table with block sizes for encrypted bytes and clear bytes
for (unsigned tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end; tile_num++) {
uint32_t encrypted_bytes, tile_size_bytes, tile_size = 0;
if (tile_num == tile_group->tg_end) {
tile_size = bytestream2_get_bytes_left(&gb);
encrypted_bytes = tile_size & ~0xFU;
ctx->clear_bytes += tile_size & 0xFU;
ctx->tile_group_sizes[tile_num].encrypted_bytes = encrypted_bytes;
ctx->tile_group_sizes[tile_num].aux_clear_bytes = encrypted_bytes ? ctx->clear_bytes : 0;
ctx->tile_group_sizes[tile_num].write_clear_bytes = tile_size & 0xFU;
if (encrypted_bytes)
ctx->clear_bytes = 0;
total += tile_size;
break;
}
tile_size_bytes = frame_header->tile_size_bytes_minus1 + 1;
if (bytestream2_get_bytes_left(&gb) < tile_size_bytes)
return AVERROR_INVALIDDATA;
for (int i = 0; i < tile_size_bytes; i++)
tile_size |= bytestream2_get_byteu(&gb) << 8 * i;
if (bytestream2_get_bytes_left(&gb) <= tile_size)
return AVERROR_INVALIDDATA;
tile_size++;
// The spec requires encrypted bytes to be in blocks multiple of 16
encrypted_bytes = tile_size & ~0xFU;
ctx->clear_bytes += (tile_size & 0xFU) + tile_size_bytes;
ctx->tile_group_sizes[tile_num].encrypted_bytes = encrypted_bytes;
ctx->tile_group_sizes[tile_num].aux_clear_bytes = encrypted_bytes ? ctx->clear_bytes : 0;
ctx->tile_group_sizes[tile_num].write_clear_bytes = (tile_size & 0xFU) + tile_size_bytes;
if (encrypted_bytes)
ctx->clear_bytes = 0;
total += tile_size + tile_size_bytes;
bytestream2_skipu(&gb, tile_size);
}
bytestream2_init(&gb, tile_group->tile_data.data, tile_group->tile_data.data_size);
avio_write(pb, fh_data, fh_data_size);
avio_write(pb, tile_group->data, tgh_data_size);
for (unsigned tile_num = tile_group->tg_start; tile_num <= tile_group->tg_end; tile_num++) {
const struct MOVMuxCencAV1TGInfo *sizes = &ctx->tile_group_sizes[tile_num];
avio_write(pb, gb.buffer, sizes->write_clear_bytes);
bytestream2_skipu(&gb, sizes->write_clear_bytes);
mov_cenc_write_encrypted(ctx, pb, gb.buffer, sizes->encrypted_bytes);
bytestream2_skipu(&gb, sizes->encrypted_bytes);
if (sizes->encrypted_bytes) {
unsigned clear_bytes = sizes->aux_clear_bytes;
if (clear_bytes > UINT16_MAX) {
auxiliary_info_add_subsample(ctx, UINT16_MAX, 0);
clear_bytes -= UINT16_MAX;
}
auxiliary_info_add_subsample(ctx, clear_bytes, sizes->encrypted_bytes);
}
}
return total;
}
int ff_mov_cenc_av1_write_obus(AVFormatContext *s, MOVMuxCencContext* ctx,
AVIOContext *pb, const AVPacket *pkt)
{
CodedBitstreamFragment *td = &ctx->temporal_unit;
const CodedBitstreamAV1Context *av1 = ctx->cbc->priv_data;
const AV1RawFrameHeader *frame_header = NULL;
const uint8_t *fh_data = NULL;
size_t fh_data_size;
int out_size = 0, ret;
ret = mov_cenc_start_packet(ctx);
if (ret) {
return ret;
}
ret = ff_lavf_cbs_read_packet(ctx->cbc, td, pkt);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "CENC-AV1: Failed to parse temporal unit.\n");
return ret;
}
if (!av1->sequence_header) {
av_log(s, AV_LOG_ERROR, "CENC-AV1: No sequence header available\n");
ret = AVERROR_INVALIDDATA;
goto end;
}
for (int i = 0; i < td->nb_units; i++) {
const CodedBitstreamUnit *unit = &td->units[i];
const AV1RawOBU *obu = unit->content;
switch (unit->type) {
case AV1_OBU_FRAME_HEADER:
if (!obu->obu.frame_header.show_existing_frame) {
frame_header = &obu->obu.frame_header;
fh_data = unit->data;
fh_data_size = unit->data_size;
break;
}
// fall-through
case AV1_OBU_SEQUENCE_HEADER:
case AV1_OBU_METADATA:
avio_write(pb, unit->data, unit->data_size);
ctx->clear_bytes += unit->data_size;
out_size += unit->data_size;
break;
case AV1_OBU_FRAME:
frame_header = &obu->obu.frame.header;
fh_data = unit->data;
fh_data_size = unit->data_size;
// fall-through
case AV1_OBU_TILE_GROUP:
{
const AV1RawTileGroup *tile_group;
if (!frame_header){
ret = AVERROR_INVALIDDATA;
goto end;
}
if (unit->type == AV1_OBU_FRAME)
tile_group = &obu->obu.frame.tile_group;
else
tile_group = &obu->obu.tile_group;
ret = write_tiles(s, ctx, pb, unit->type,
frame_header, fh_data, fh_data_size, tile_group);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "CENC-AV1: Failed to write tiles\n");
goto end;
}
av_assert0(ret == unit->data_size);
out_size += unit->data_size;
frame_header = NULL;
}
break;
default:
break;
}
}
if (ctx->clear_bytes)
auxiliary_info_add_subsample(ctx, ctx->clear_bytes, 0);
ctx->clear_bytes = 0;
ret = mov_cenc_end_packet(ctx);
if (ret) {
ret = AVERROR_INVALIDDATA;
goto end;
}
ret = out_size;
end:
ff_lavf_cbs_fragment_reset(td);
return ret;
}
/* TODO: reuse this function from movenc.c */ /* TODO: reuse this function from movenc.c */
static int64_t update_size(AVIOContext *pb, int64_t pos) static int64_t update_size(AVIOContext *pb, int64_t pos)
{ {
@ -388,8 +588,16 @@ int ff_mov_cenc_write_sinf_tag(MOVTrack* track, AVIOContext *pb, uint8_t* kid)
return update_size(pb, pos); return update_size(pb, pos);
} }
static const CodedBitstreamUnitType decompose_unit_types[] = {
AV1_OBU_TEMPORAL_DELIMITER,
AV1_OBU_SEQUENCE_HEADER,
AV1_OBU_FRAME_HEADER,
AV1_OBU_TILE_GROUP,
AV1_OBU_FRAME,
};
int ff_mov_cenc_init(MOVMuxCencContext* ctx, uint8_t* encryption_key, int ff_mov_cenc_init(MOVMuxCencContext* ctx, uint8_t* encryption_key,
int use_subsamples, int bitexact) int use_subsamples, enum AVCodecID codec_id, int bitexact)
{ {
int ret; int ret;
@ -409,6 +617,15 @@ int ff_mov_cenc_init(MOVMuxCencContext* ctx, uint8_t* encryption_key,
ctx->use_subsamples = use_subsamples; ctx->use_subsamples = use_subsamples;
if (codec_id == AV_CODEC_ID_AV1) {
ret = ff_lavf_cbs_init(&ctx->cbc, codec_id, NULL);
if (ret < 0)
return ret;
ctx->cbc->decompose_unit_types = decompose_unit_types;
ctx->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
}
return 0; return 0;
} }
@ -417,4 +634,8 @@ void ff_mov_cenc_free(MOVMuxCencContext* ctx)
av_aes_ctr_free(ctx->aes_ctr); av_aes_ctr_free(ctx->aes_ctr);
av_freep(&ctx->auxiliary_info); av_freep(&ctx->auxiliary_info);
av_freep(&ctx->auxiliary_info_sizes); av_freep(&ctx->auxiliary_info_sizes);
av_freep(&ctx->tile_group_sizes);
ff_lavf_cbs_fragment_free(&ctx->temporal_unit);
ff_lavf_cbs_close(&ctx->cbc);
} }

View File

@ -25,11 +25,18 @@
#include "libavutil/aes_ctr.h" #include "libavutil/aes_ctr.h"
#include "avformat.h" #include "avformat.h"
#include "avio.h" #include "avio.h"
#include "cbs.h"
#define CENC_KID_SIZE (16) #define CENC_KID_SIZE (16)
struct MOVTrack; struct MOVTrack;
struct MOVMuxCencAV1TGInfo {
uint32_t encrypted_bytes;
uint32_t write_clear_bytes;
uint32_t aux_clear_bytes;
};
typedef struct { typedef struct {
struct AVAESCTR* aes_ctr; struct AVAESCTR* aes_ctr;
uint8_t* auxiliary_info; uint8_t* auxiliary_info;
@ -43,6 +50,14 @@ typedef struct {
size_t auxiliary_info_subsample_start; size_t auxiliary_info_subsample_start;
uint8_t* auxiliary_info_sizes; uint8_t* auxiliary_info_sizes;
size_t auxiliary_info_sizes_alloc_size; size_t auxiliary_info_sizes_alloc_size;
/* AV1 */
struct MOVMuxCencAV1TGInfo *tile_group_sizes;
uint32_t clear_bytes;
int tile_num;
/* CBS */
CodedBitstreamContext *cbc;
CodedBitstreamFragment temporal_unit;
} MOVMuxCencContext; } MOVMuxCencContext;
/** /**
@ -50,7 +65,8 @@ typedef struct {
* @param key encryption key, must have a length of AES_CTR_KEY_SIZE * @param key encryption key, must have a length of AES_CTR_KEY_SIZE
* @param use_subsamples when enabled parts of a packet can be encrypted, otherwise the whole packet is encrypted * @param use_subsamples when enabled parts of a packet can be encrypted, otherwise the whole packet is encrypted
*/ */
int ff_mov_cenc_init(MOVMuxCencContext* ctx, uint8_t* encryption_key, int use_subsamples, int bitexact); int ff_mov_cenc_init(MOVMuxCencContext* ctx, uint8_t* encryption_key, int use_subsamples,
enum AVCodecID codec_id, int bitexact);
/** /**
* Free a CENC context * Free a CENC context
@ -73,6 +89,8 @@ int ff_mov_cenc_avc_parse_nal_units(MOVMuxCencContext* ctx, AVIOContext *pb, con
int ff_mov_cenc_avc_write_nal_units(AVFormatContext *s, MOVMuxCencContext* ctx, int nal_length_size, int ff_mov_cenc_avc_write_nal_units(AVFormatContext *s, MOVMuxCencContext* ctx, int nal_length_size,
AVIOContext *pb, const uint8_t *buf_in, int size); AVIOContext *pb, const uint8_t *buf_in, int size);
int ff_mov_cenc_av1_write_obus(AVFormatContext *s, MOVMuxCencContext* ctx,
AVIOContext *pb, const AVPacket *pkt);
/** /**
* Write the cenc atoms that should reside inside stbl * Write the cenc atoms that should reside inside stbl
*/ */

View File

@ -32,7 +32,7 @@
#include "version_major.h" #include "version_major.h"
#define LIBAVFORMAT_VERSION_MINOR 9 #define LIBAVFORMAT_VERSION_MINOR 9
#define LIBAVFORMAT_VERSION_MICRO 107 #define LIBAVFORMAT_VERSION_MICRO 108
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
LIBAVFORMAT_VERSION_MINOR, \ LIBAVFORMAT_VERSION_MINOR, \

View File

@ -10,6 +10,8 @@ libavdevice/riscv/cpu_common.c
libavfilter/file_open.c libavfilter/file_open.c
libavfilter/log2_tab.c libavfilter/log2_tab.c
libavfilter/riscv/cpu_common.c libavfilter/riscv/cpu_common.c
libavformat/cbs.c
libavformat/cbs_av1.c
libavformat/file_open.c libavformat/file_open.c
libavformat/golomb_tab.c libavformat/golomb_tab.c
libavformat/log2_tab.c libavformat/log2_tab.c