18 #include <libavcodec/avcodec.h> 19 #include <libavutil/encryption_info.h> 21 #include <glog/logging.h> 24 #include <unordered_map> 32 #define AVERROR_SHAKA_RESET_DEMUXER (-123456) 40 constexpr
const size_t kInitialBufferSize = 2048;
43 LOG(ERROR) <<
"Error from FFmpeg: " << av_err2str(code);
46 std::string GetCodec(
const std::string& mime, AVCodecID codec) {
47 std::unordered_map<std::string, std::string> params;
52 return avcodec_get_name(codec);
60 std::vector<uint8_t> CreatePssh(AVEncryptionInitInfo* info) {
72 DCHECK_EQ(info->system_id_size, 16u);
73 size_t pssh_size = info->data_size + 32;
74 if (info->num_key_ids) {
75 DCHECK_EQ(info->key_id_size, 16u);
76 pssh_size += 4 + info->num_key_ids * 16;
79 std::vector<uint8_t> pssh(pssh_size, 0);
80 util::BufferWriter writer(pssh.data(), pssh_size);
82 writer.Write<uint32_t>(pssh_size);
83 writer.WriteTag(
"pssh");
84 writer.Write<uint32_t>(info->num_key_ids ? 0x01000000 : 0);
85 writer.Write(info->system_id, 16);
86 if (info->num_key_ids) {
87 writer.Write<uint32_t>(info->num_key_ids);
88 for (uint32_t i = 0; i < info->num_key_ids; i++) {
89 writer.Write(info->key_ids[i], 16);
93 writer.Write<uint32_t>(info->data_size);
94 writer.Write(info->data, info->data_size);
95 DCHECK(writer.empty());
100 bool ParseAndCheckSupport(
const std::string& mime, std::string* container) {
106 if (!av_find_input_format(normalized.c_str()))
109 *container = normalized;
115 #ifndef HAS_FFMPEG_DECODER 117 void RemoveEmulationPrevention(
const uint8_t* data,
size_t size,
118 std::vector<uint8_t>* output) {
119 DCHECK_EQ(output->size(), size);
123 DCHECK_EQ(output->size(), size);
125 for (
size_t in_pos = 0; in_pos < size;) {
126 if (in_pos + 2 < size && data[in_pos] == 0 && data[in_pos + 1] == 0 &&
127 data[in_pos + 2] == 0x3) {
128 (*output)[out_pos++] = 0;
129 (*output)[out_pos++] = 0;
132 (*output)[out_pos++] = data[in_pos++];
135 output->resize(out_pos);
138 Rational<uint32_t> GetSarFromVuiParameters(util::BufferReader* reader) {
141 if (reader->ReadBits(1) == 0)
143 const uint8_t aspect_ratio_idc = reader->ReadUint8();
145 switch (aspect_ratio_idc) {
179 return {reader->ReadBits(16), reader->ReadBits(16)};
182 LOG(DFATAL) <<
"Unknown value of aspect_ratio_idc: " 183 <<
static_cast<int>(aspect_ratio_idc);
188 Rational<uint32_t> GetSarFromH264(
const std::vector<uint8_t>& extra_data) {
189 util::BufferReader reader(extra_data.data(), extra_data.size());
193 const size_t sps_count = reader.ReadUint8() & 0x1f;
200 const size_t sps_size = reader.ReadBits(16);
201 if (sps_size >= reader.BytesRemaining()) {
202 LOG(DFATAL) <<
"Invalid avcC configuration";
208 std::vector<uint8_t> temp(sps_size);
209 RemoveEmulationPrevention(reader.data(), sps_size, &temp);
210 util::BufferReader sps_reader(temp.data(), temp.size());
211 if (sps_reader.ReadUint8() != 0x67) {
212 LOG(DFATAL) <<
"Non-SPS found in avcC configuration";
217 const uint8_t profile_idc = sps_reader.ReadUint8();
219 sps_reader.ReadExpGolomb();
221 if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 ||
222 profile_idc == 244 || profile_idc == 44 || profile_idc == 83 ||
223 profile_idc == 86 || profile_idc == 118 || profile_idc == 128 ||
224 profile_idc == 138 || profile_idc == 139 || profile_idc == 134) {
225 const uint64_t chroma_format_idc = sps_reader.ReadExpGolomb();
226 if (chroma_format_idc == 3)
227 sps_reader.ReadBits(1);
228 sps_reader.ReadExpGolomb();
229 sps_reader.ReadExpGolomb();
230 sps_reader.SkipBits(1);
231 if (sps_reader.ReadBits(1) == 1) {
232 LOG(WARNING) <<
"Scaling matrix is unsupported";
236 sps_reader.ReadExpGolomb();
237 const uint64_t pic_order_cnt_type = sps_reader.ReadExpGolomb();
238 if (pic_order_cnt_type == 0) {
239 sps_reader.ReadExpGolomb();
240 }
else if (pic_order_cnt_type == 1) {
241 sps_reader.ReadBits(1);
242 sps_reader.ReadExpGolomb();
243 sps_reader.ReadExpGolomb();
244 const uint64_t count = sps_reader.ReadExpGolomb();
245 for (uint64_t i = 0; i < count; i++)
246 sps_reader.ReadExpGolomb();
248 sps_reader.ReadExpGolomb();
249 sps_reader.ReadBits(1);
250 sps_reader.ReadExpGolomb();
251 sps_reader.ReadExpGolomb();
252 if (sps_reader.ReadBits(1) == 0)
253 sps_reader.ReadBits(1);
254 sps_reader.ReadBits(1);
255 if (sps_reader.ReadBits(1) == 1) {
256 sps_reader.ReadExpGolomb();
257 sps_reader.ReadExpGolomb();
258 sps_reader.ReadExpGolomb();
259 sps_reader.ReadExpGolomb();
261 if (sps_reader.ReadBits(1) == 0)
264 return GetSarFromVuiParameters(&sps_reader);
267 void SkipHevcProfileTierLevel(
bool profile_present,
268 uint64_t max_sub_layers_minus1,
269 util::BufferReader* reader) {
270 if (profile_present) {
274 std::vector<bool> sub_layer_profile_present_flag(max_sub_layers_minus1);
275 std::vector<bool> sub_layer_level_present_flag(max_sub_layers_minus1);
276 for (uint64_t i = 0; i < max_sub_layers_minus1; i++) {
277 sub_layer_profile_present_flag[i] = reader->ReadBits(1);
278 sub_layer_level_present_flag[i] = reader->ReadBits(1);
280 if (max_sub_layers_minus1 > 0 && max_sub_layers_minus1 < 8)
281 reader->SkipBits(2 * (8 - max_sub_layers_minus1));
282 for (uint64_t i = 0; i < max_sub_layers_minus1; i++) {
283 if (sub_layer_profile_present_flag[i])
285 if (sub_layer_level_present_flag[i])
290 Rational<uint32_t> GetSarFromHevc(
const std::vector<uint8_t>& extra_data) {
291 util::BufferReader reader(extra_data.data(), extra_data.size());
295 const uint8_t num_of_arrays = reader.ReadUint8();
296 uint64_t nalu_length = 0;
298 for (uint8_t i = 0; i < num_of_arrays && !found; i++) {
299 const uint8_t nalu_type = reader.ReadUint8() & 0x3f;
300 const uint64_t num_nalus = reader.ReadBits(16);
301 for (uint64_t i = 0; i < num_nalus; i++) {
302 nalu_length = reader.ReadBits(16);
305 if (nalu_type == 33) {
309 reader.Skip(nalu_length);
317 std::vector<uint8_t> temp(nalu_length);
318 RemoveEmulationPrevention(reader.data(), nalu_length, &temp);
319 util::BufferReader sps_reader(temp.data(), temp.size());
320 const uint64_t nalu_type = (sps_reader.ReadBits(16) >> 9) & 0x3f;
321 if (nalu_type != 33) {
322 LOG(DFATAL) <<
"Invalid NALU type found in extra data";
326 sps_reader.SkipBits(4);
327 const uint64_t max_sub_layers_minus1 = sps_reader.ReadBits(3);
328 sps_reader.SkipBits(1);
329 SkipHevcProfileTierLevel(
true, max_sub_layers_minus1,
331 sps_reader.ReadExpGolomb();
332 if (sps_reader.ReadExpGolomb() == 3)
333 sps_reader.SkipBits(1);
334 sps_reader.ReadExpGolomb();
335 sps_reader.ReadExpGolomb();
336 if (sps_reader.ReadBits(1) == 1) {
337 sps_reader.ReadExpGolomb();
338 sps_reader.ReadExpGolomb();
339 sps_reader.ReadExpGolomb();
340 sps_reader.ReadExpGolomb();
342 sps_reader.ReadExpGolomb();
343 sps_reader.ReadExpGolomb();
344 sps_reader.ReadExpGolomb();
345 const uint64_t sub_layer_ordering_info_present = sps_reader.ReadBits(1);
347 (sub_layer_ordering_info_present ? 0 : max_sub_layers_minus1);
348 i <= max_sub_layers_minus1; i++) {
349 sps_reader.ReadExpGolomb();
350 sps_reader.ReadExpGolomb();
351 sps_reader.ReadExpGolomb();
353 sps_reader.ReadExpGolomb();
354 sps_reader.ReadExpGolomb();
355 sps_reader.ReadExpGolomb();
356 sps_reader.ReadExpGolomb();
357 sps_reader.ReadExpGolomb();
358 sps_reader.ReadExpGolomb();
359 if (sps_reader.ReadBits(1) == 1) {
360 LOG(WARNING) <<
"Scaling list isn't supported";
363 sps_reader.SkipBits(1);
364 sps_reader.SkipBits(1);
365 if (sps_reader.ReadBits(1) == 1) {
366 sps_reader.ReadBits(4);
367 sps_reader.ReadBits(4);
368 sps_reader.ReadExpGolomb();
369 sps_reader.ReadExpGolomb();
371 const uint64_t num_short_term_ref_pic_sets = sps_reader.ReadExpGolomb();
372 if (num_short_term_ref_pic_sets != 0) {
373 LOG(WARNING) <<
"Short-term reference pictures not supported";
376 if (sps_reader.ReadBits(1) == 1) {
377 const uint64_t num_long_term_ref_pics_sps = sps_reader.ReadExpGolomb();
378 for (uint64_t i = 0; i < num_long_term_ref_pics_sps; i++) {
379 sps_reader.ReadExpGolomb();
380 sps_reader.ReadBits(1);
383 sps_reader.ReadBits(1);
384 sps_reader.ReadBits(1);
385 if (sps_reader.ReadBits(1) != 1)
387 return GetSarFromVuiParameters(&sps_reader);
395 const std::string& mime_type,
396 const std::string& container)
397 : signal_(
"FFmpegDemuxer"),
398 mutex_(
"FFmpegDemuxer"),
399 mime_type_(mime_type),
400 container_(container),
402 demuxer_ctx_(nullptr),
405 timestamp_offset_(0),
409 state_(State::Waiting),
414 std::unique_lock<Mutex> lock(mutex_);
415 state_ = State::Stopping;
423 av_free(io_->buffer);
437 std::vector<std::shared_ptr<EncodedFrame>>*
frames) {
438 std::unique_lock<Mutex> lock(mutex_);
439 if (state_ != State::Waiting) {
440 DCHECK(state_ == State::Errored || state_ == State::Stopping);
445 timestamp_offset_ = timestamp_offset;
450 state_ = State::Parsing;
451 while (state_ == State::Parsing) {
459 return state_ == State::Waiting;
462 int FFmpegDemuxer::OnRead(
void* user, uint8_t* buffer,
int size) {
464 std::unique_lock<Mutex> lock(that->mutex_);
465 while (that->input_pos_ >= that->input_size_ &&
466 (that->state_ == State::Parsing || that->state_ == State::Waiting)) {
467 that->state_ = State::Waiting;
468 that->signal_.SignalAll();
469 that->signal_.ResetAndWaitWhileUnlocked(lock);
471 if (that->state_ != State::Parsing) {
472 DCHECK(that->state_ == State::Errored || that->state_ == State::Stopping);
476 DCHECK_LT(that->input_pos_, that->input_size_);
477 size_t to_read = std::min<size_t>(size, that->input_size_ - that->input_pos_);
478 memcpy(buffer, that->input_ + that->input_pos_, to_read);
479 that->input_pos_ += to_read;
483 void FFmpegDemuxer::ThreadMain() {
487 io_ = avio_alloc_context(
488 reinterpret_cast<unsigned char*>(av_malloc(kInitialBufferSize)),
497 if (!ReinitDemuxer())
503 if (demuxer_ctx_->duration == 0 ||
504 demuxer_ctx_->duration == AV_NOPTS_VALUE) {
512 std::unique_lock<Mutex> lock(mutex_);
514 while (state_ == State::Waiting) {
518 if (state_ != State::Parsing)
524 int ret = av_read_frame(demuxer_ctx_.get(), &pkt);
527 VLOG(1) <<
"Reinitializing demuxer";
529 std::unique_lock<Mutex> lock(mutex_);
534 if (!ReinitDemuxer())
536 ret = av_read_frame(demuxer_ctx_.get(), &pkt);
539 av_packet_unref(&pkt);
544 UpdateEncryptionInfo();
548 pkt.flags &= ~AV_PKT_FLAG_DISCARD;
550 VLOG(3) <<
"Read frame at dts=" << pkt.dts;
551 DCHECK_EQ(pkt.stream_index, 0);
552 DCHECK_EQ(demuxer_ctx_->nb_streams, 1u);
560 output_->emplace_back(
frame);
562 av_packet_unref(&pkt);
563 state_ = State::Errored;
570 bool FFmpegDemuxer::ReinitDemuxer() {
571 demuxer_ctx_.reset();
574 AVFormatContext* demuxer = avformat_alloc_context();
590 AVDictionary* dict =
nullptr;
591 CHECK_EQ(av_dict_set_int(&dict,
"parse_encryption", 1, 0), 0);
593 AVInputFormat* format = av_find_input_format(container_.c_str());
594 CHECK(format) <<
"Should have checked for support before creating.";
595 const int open_code = avformat_open_input(&demuxer,
nullptr, format, &dict);
602 demuxer_ctx_.reset(demuxer);
603 const int find_code = avformat_find_stream_info(demuxer,
nullptr);
609 if (demuxer_ctx_->nb_streams == 0) {
610 LOG(ERROR) <<
"FFmpeg was unable to find any streams";
613 if (demuxer_ctx_->nb_streams > 1) {
614 LOG(ERROR) <<
"Multiple streams in input not supported";
618 AVStream* stream = demuxer_ctx_->streams[0];
619 AVCodecParameters* params = stream->codecpar;
620 const std::string expected_codec = GetCodec(mime_type_, params->codec_id);
622 const char* actual_codec = avcodec_get_name(params->codec_id);
624 LOG(ERROR) <<
"Mismatch between codec string and media. Codec string: '" 625 << expected_codec <<
"', media codec: '" << actual_codec
626 <<
"' (0x" << std::hex << params->codec_id <<
")";
630 std::vector<uint8_t> extra_data{params->extradata,
631 params->extradata + params->extradata_size};
633 params->sample_aspect_ratio.den};
634 #ifndef HAS_FFMPEG_DECODER 636 switch (params->codec_id) {
637 case AV_CODEC_ID_H264:
638 sar = GetSarFromH264(extra_data);
640 case AV_CODEC_ID_H265:
641 sar = GetSarFromHevc(extra_data);
650 mime_type_, expected_codec, params->codec_type == AVMEDIA_TYPE_VIDEO,
651 {stream->time_base.num, stream->time_base.den}, sar, extra_data,
652 params->width, params->height, params->channels, params->sample_rate));
656 void FFmpegDemuxer::UpdateEncryptionInfo() {
661 const uint8_t* side_data = av_stream_get_side_data(
662 demuxer_ctx_->streams[0], AV_PKT_DATA_ENCRYPTION_INIT_INFO,
665 AVEncryptionInitInfo* info =
666 av_encryption_init_info_get_side_data(side_data, side_data_size);
667 std::vector<uint8_t> pssh;
668 for (
auto* cur_info = info; cur_info; cur_info = cur_info->next) {
669 if (cur_info->system_id_size) {
670 const std::vector<uint8_t> temp = CreatePssh(cur_info);
671 pssh.insert(pssh.end(), temp.begin(), temp.end());
673 for (
size_t i = 0; i < cur_info->num_key_ids; i++) {
675 cur_info->key_ids[i], cur_info->key_id_size);
683 av_encryption_init_info_free(info);
685 av_stream_remove_side_data(demuxer_ctx_->streams[0],
686 AV_PKT_DATA_ENCRYPTION_INIT_INFO);
690 void FFmpegDemuxer::OnError() {
691 std::unique_lock<Mutex> lock(mutex_);
692 if (state_ != State::Stopping)
693 state_ = State::Errored;
700 return ParseAndCheckSupport(mime_type, &unused);
705 auto* impl = avcodec_find_decoder_by_name(norm.c_str());
707 return impl->type == AVMEDIA_TYPE_VIDEO;
708 return norm ==
"h264" || norm ==
"hevc" || norm ==
"vp8" || norm ==
"vp9";
713 std::string container;
714 if (!ParseAndCheckSupport(mime_type, &container))
717 return std::unique_ptr<Demuxer>(
718 new (std::nothrow)
FFmpegDemuxer(client, mime_type, container));
std::shared_ptr< shaka::media::DecodedFrame > frame
#define AVERROR_SHAKA_RESET_DEMUXER
T ResetAndWaitWhileUnlocked(std::unique_lock< _Mutex > &lock)
std::list< std::shared_ptr< BaseFrame > > frames
#define LogError(code, extra_info)