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

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: Adjusted inclusion of hevc source files in gyp/gn Created 5 years, 10 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
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 scoped_ptr<mp4::HEVCDecoderConfigurationRecord> hevc_config;
26
27 DVLOG(3) << __FUNCTION__;
28 if (packet == NULL || !packet->data)
29 return false;
30
31 // Calculate the needed output buffer size.
32 if (!hevc_config_) {
33 if (!stream_codec_context_->extradata ||
34 stream_codec_context_->extradata_size <= 0) {
35 DVLOG(1) << "HEVCDecoderConfiguration not found, no extra codec data";
36 return false;
37 }
38
39 hevc_config_.reset(new mp4::HEVCDecoderConfigurationRecord());
40
41 if (!hevc_config_->Parse(
42 stream_codec_context_->extradata,
43 stream_codec_context_->extradata_size)) {
44 DVLOG(1) << "Parsing HEVCDecoderConfiguration failed";
45 return false;
46 }
47 }
48
49 std::vector<uint8> input_frame;
50 // TODO(servolk): Performance could be improved here, by reducing unnecessary
51 // data copying, but first annex b conversion code needs to be refactored to
52 // allow that.
53 input_frame.insert(input_frame.end(),
54 packet->data, packet->data + packet->size);
55 int naluSizeLen = hevc_config_->lengthSizeMinusOne + 1;
56 if (!mp4::AVC::ConvertFrameToAnnexB(naluSizeLen, &input_frame)) {
57 DVLOG(1) << "AnnexB conversion failed";
58 return false;
59 }
60
61 if (packet->flags & AV_PKT_FLAG_KEY ) {
62 std::vector<SubsampleEntry> subsamples;
63 RCHECK(mp4::HEVC::InsertParamSetsAnnexB(*hevc_config_.get(),
64 &input_frame, &subsamples));
65 DVLOG(4) << "Inserted HEVC decoder params";
66 }
67
68 uint32 output_packet_size = input_frame.size();
69
70 if (output_packet_size == 0)
71 return false; // Invalid input packet.
72
73 // Allocate new packet for the output.
74 AVPacket dest_packet;
75 if (av_new_packet(&dest_packet, output_packet_size) != 0)
76 return false; // Memory allocation failure.
77
78 // This is a bit tricky: since the interface does not allow us to replace
79 // the pointer of the old packet with a new one, we will initially copy the
80 // metadata from old packet to new bigger packet.
81 av_packet_copy_props(&dest_packet, packet);
82
83 // Proceed with the conversion of the actual in-band NAL units, leave room
84 // for configuration in the beginning.
85 memcpy(dest_packet.data, &input_frame[0], input_frame.size());
86
87 // At the end we must destroy the old packet.
88 av_free_packet(packet);
89 *packet = dest_packet; // Finally, replace the values in the input packet.
90
91 return true;
92 }
93
94 } // namespace media
95
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698