OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/filters/ffmpeg_h265_to_annex_b_bitstream_converter.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "media/base/decrypt_config.h" |
| 9 #include "media/ffmpeg/ffmpeg_common.h" |
| 10 #include "media/formats/mp4/avc.h" |
| 11 #include "media/formats/mp4/box_definitions.h" |
| 12 #include "media/formats/mp4/hevc.h" |
| 13 |
| 14 namespace media { |
| 15 |
| 16 FFmpegH265ToAnnexBBitstreamConverter::FFmpegH265ToAnnexBBitstreamConverter( |
| 17 AVCodecContext* stream_codec_context) |
| 18 : stream_codec_context_(stream_codec_context) { |
| 19 CHECK(stream_codec_context_); |
| 20 } |
| 21 |
| 22 FFmpegH265ToAnnexBBitstreamConverter::~FFmpegH265ToAnnexBBitstreamConverter() {} |
| 23 |
| 24 bool FFmpegH265ToAnnexBBitstreamConverter::ConvertPacket(AVPacket* packet) { |
| 25 DVLOG(3) << __FUNCTION__; |
| 26 if (packet == NULL || !packet->data) |
| 27 return false; |
| 28 |
| 29 // Calculate the needed output buffer size. |
| 30 if (!hevc_config_) { |
| 31 if (!stream_codec_context_->extradata || |
| 32 stream_codec_context_->extradata_size <= 0) { |
| 33 DVLOG(1) << "HEVCDecoderConfiguration not found, no extra codec data"; |
| 34 return false; |
| 35 } |
| 36 |
| 37 hevc_config_.reset(new mp4::HEVCDecoderConfigurationRecord()); |
| 38 |
| 39 if (!hevc_config_->Parse( |
| 40 stream_codec_context_->extradata, |
| 41 stream_codec_context_->extradata_size)) { |
| 42 DVLOG(1) << "Parsing HEVCDecoderConfiguration failed"; |
| 43 return false; |
| 44 } |
| 45 } |
| 46 |
| 47 std::vector<uint8> input_frame; |
| 48 std::vector<SubsampleEntry> subsamples; |
| 49 // TODO(servolk): Performance could be improved here, by reducing unnecessary |
| 50 // data copying, but first annex b conversion code needs to be refactored to |
| 51 // allow that (see crbug.com/455379). |
| 52 input_frame.insert(input_frame.end(), |
| 53 packet->data, packet->data + packet->size); |
| 54 int nalu_size_len = hevc_config_->lengthSizeMinusOne + 1; |
| 55 if (!mp4::AVC::ConvertFrameToAnnexB(nalu_size_len, &input_frame, |
| 56 &subsamples)) { |
| 57 DVLOG(1) << "AnnexB conversion failed"; |
| 58 return false; |
| 59 } |
| 60 |
| 61 if (packet->flags & AV_PKT_FLAG_KEY) { |
| 62 RCHECK(mp4::HEVC::InsertParamSetsAnnexB(*hevc_config_.get(), |
| 63 &input_frame, &subsamples)); |
| 64 DVLOG(4) << "Inserted HEVC decoder params"; |
| 65 } |
| 66 |
| 67 uint32 output_packet_size = input_frame.size(); |
| 68 |
| 69 if (output_packet_size == 0) |
| 70 return false; // Invalid input packet. |
| 71 |
| 72 // Allocate new packet for the output. |
| 73 AVPacket dest_packet; |
| 74 if (av_new_packet(&dest_packet, output_packet_size) != 0) |
| 75 return false; // Memory allocation failure. |
| 76 |
| 77 // This is a bit tricky: since the interface does not allow us to replace |
| 78 // the pointer of the old packet with a new one, we will initially copy the |
| 79 // metadata from old packet to new bigger packet. |
| 80 av_packet_copy_props(&dest_packet, packet); |
| 81 |
| 82 // Proceed with the conversion of the actual in-band NAL units, leave room |
| 83 // for configuration in the beginning. |
| 84 memcpy(dest_packet.data, &input_frame[0], input_frame.size()); |
| 85 |
| 86 // At the end we must destroy the old packet. |
| 87 av_free_packet(packet); |
| 88 *packet = dest_packet; // Finally, replace the values in the input packet. |
| 89 |
| 90 return true; |
| 91 } |
| 92 |
| 93 } // namespace media |
OLD | NEW |