18 #include <libavutil/encryption_info.h> 21 #include <glog/logging.h> 33 constexpr
const uint32_t kCencScheme = 0x63656e63;
34 constexpr
const uint32_t kCbcsScheme = 0x63626373;
36 bool MakeEncryptionInfo(
const AVPacket* packet,
37 std::shared_ptr<eme::FrameEncryptionInfo>* info) {
39 uint8_t* side_data = av_packet_get_side_data(
40 packet, AV_PKT_DATA_ENCRYPTION_INFO, &side_data_size);
45 std::unique_ptr<AVEncryptionInfo, void (*)(AVEncryptionInfo*)> enc_info(
46 av_encryption_info_get_side_data(side_data, side_data_size),
47 &av_encryption_info_free);
49 LOG(DFATAL) <<
"Could not allocate new encryption info structure.";
53 std::vector<eme::SubsampleInfo> subsamples;
54 for (uint32_t i = 0; i < enc_info->subsample_count; i++) {
55 subsamples.emplace_back(enc_info->subsamples[i].bytes_of_clear_data,
56 enc_info->subsamples[i].bytes_of_protected_data);
60 eme::EncryptionPattern pattern{0, 0};
61 if (enc_info->scheme == kCencScheme) {
63 }
else if (enc_info->scheme == kCbcsScheme) {
65 pattern = eme::EncryptionPattern{enc_info->crypt_byte_block,
66 enc_info->skip_byte_block};
68 LOG(DFATAL) <<
"Unsupported scheme";
72 auto make_vec = [](
const uint8_t* data,
size_t size) {
73 return std::vector<uint8_t>(data, data + size);
75 *info = std::make_shared<eme::FrameEncryptionInfo>(
76 scheme, pattern, make_vec(enc_info->key_id, enc_info->key_id_size),
77 make_vec(enc_info->iv, enc_info->iv_size), subsamples);
85 AVPacket* pkt, std::shared_ptr<const StreamInfo> info,
86 double timestamp_offset) {
87 const double factor = info->time_scale;
90 const double duration = pkt->duration * factor;
93 if (!MakeEncryptionInfo(pkt, &encryption_info))
96 return new (std::nothrow)
98 encryption_info, timestamp_offset);
102 av_packet_unref(&packet_);
106 size_t size =
sizeof(*this) + packet_.size;
107 for (
int i = packet_.side_data_elems; i; i--)
108 size += packet_.side_data[i - 1].size;
112 FFmpegEncodedFrame::FFmpegEncodedFrame(
114 std::shared_ptr<const StreamInfo> info,
117 :
EncodedFrame(info, pts, dts, duration, is_key_frame, pkt->data, pkt->size,
118 timestamp_offset, encryption_info) {
119 av_packet_move_ref(&packet_, pkt);