Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: media/filters/ffmpeg_h265_to_annex_b_bitstream_converter.cc

Issue 816353010: Implemented HEVC video demuxing and parsing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR feedback Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #if defined(USE_PROPRIETARY_CODECS) && defined(ENABLE_HEVC_DEMUXING)
5
6 #include "media/filters/ffmpeg_h265_to_annex_b_bitstream_converter.h"
7
8 #include "base/logging.h"
9 #include "media/base/decrypt_config.h"
10 #include "media/ffmpeg/ffmpeg_common.h"
11 #include "media/formats/mp4/avc.h"
12 #include "media/formats/mp4/box_definitions.h"
13 #include "media/formats/mp4/hevc.h"
14
15 namespace media {
16
17 FFmpegH265ToAnnexBBitstreamConverter::FFmpegH265ToAnnexBBitstreamConverter(
18 AVCodecContext* stream_codec_context)
19 : stream_codec_context_(stream_codec_context) {
20 CHECK(stream_codec_context_);
21 }
22
23 FFmpegH265ToAnnexBBitstreamConverter::~FFmpegH265ToAnnexBBitstreamConverter() {}
24
25 bool FFmpegH265ToAnnexBBitstreamConverter::ConvertPacket(AVPacket* packet) {
26 DVLOG(3) << __FUNCTION__;
27 if (packet == NULL || !packet->data)
28 return false;
29
30 // Calculate the needed output buffer size.
31 if (!hevc_config_) {
32 if (!stream_codec_context_->extradata ||
33 stream_codec_context_->extradata_size <= 0) {
34 DVLOG(1) << "HEVCDecoderConfiguration not found, no extra codec data";
35 return false;
36 }
37
38 hevc_config_.reset(new mp4::HEVCDecoderConfigurationRecord());
39
40 if (!hevc_config_->Parse(
41 stream_codec_context_->extradata,
42 stream_codec_context_->extradata_size)) {
43 DVLOG(1) << "Parsing HEVCDecoderConfiguration failed";
44 return false;
45 }
46 }
47
48 std::vector<uint8> input_frame;
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 DVLOG(1) << "AnnexB conversion failed";
57 return false;
58 }
59
60 if (packet->flags & AV_PKT_FLAG_KEY) {
61 std::vector<SubsampleEntry> subsamples;
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
94
95 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698