summaryrefslogtreecommitdiffstats
path: root/media/filters/ffmpeg_h265_to_annex_b_bitstream_converter.cc
blob: a643f5347586ffd0664c4cce3bf163b1d524576d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/filters/ffmpeg_h265_to_annex_b_bitstream_converter.h"

#include "base/logging.h"
#include "media/base/decrypt_config.h"
#include "media/ffmpeg/ffmpeg_common.h"
#include "media/formats/mp4/avc.h"
#include "media/formats/mp4/box_definitions.h"
#include "media/formats/mp4/hevc.h"

namespace media {

FFmpegH265ToAnnexBBitstreamConverter::FFmpegH265ToAnnexBBitstreamConverter(
    AVCodecContext* stream_codec_context)
    : stream_codec_context_(stream_codec_context) {
  CHECK(stream_codec_context_);
}

FFmpegH265ToAnnexBBitstreamConverter::~FFmpegH265ToAnnexBBitstreamConverter() {}

bool FFmpegH265ToAnnexBBitstreamConverter::ConvertPacket(AVPacket* packet) {
  DVLOG(3) << __FUNCTION__;
  if (packet == NULL || !packet->data)
    return false;

  // Calculate the needed output buffer size.
  if (!hevc_config_) {
    if (!stream_codec_context_->extradata ||
        stream_codec_context_->extradata_size <= 0) {
      DVLOG(1) << "HEVCDecoderConfiguration not found, no extra codec data";
      return false;
    }

    hevc_config_.reset(new mp4::HEVCDecoderConfigurationRecord());

    if (!hevc_config_->Parse(
            stream_codec_context_->extradata,
            stream_codec_context_->extradata_size)) {
      DVLOG(1) << "Parsing HEVCDecoderConfiguration failed";
      return false;
    }
  }

  std::vector<uint8> input_frame;
  std::vector<SubsampleEntry> subsamples;
  // TODO(servolk): Performance could be improved here, by reducing unnecessary
  // data copying, but first annex b conversion code needs to be refactored to
  // allow that (see crbug.com/455379).
  input_frame.insert(input_frame.end(),
                     packet->data, packet->data + packet->size);
  int nalu_size_len = hevc_config_->lengthSizeMinusOne + 1;
  if (!mp4::AVC::ConvertFrameToAnnexB(nalu_size_len, &input_frame,
                                      &subsamples)) {
    DVLOG(1) << "AnnexB conversion failed";
    return false;
  }

  if (packet->flags & AV_PKT_FLAG_KEY) {
    RCHECK(mp4::HEVC::InsertParamSetsAnnexB(*hevc_config_.get(),
                                            &input_frame, &subsamples));
    DVLOG(4) << "Inserted HEVC decoder params";
  }

  uint32 output_packet_size = input_frame.size();

  if (output_packet_size == 0)
    return false;  // Invalid input packet.

  // Allocate new packet for the output.
  AVPacket dest_packet;
  if (av_new_packet(&dest_packet, output_packet_size) != 0)
    return false;  // Memory allocation failure.

  // This is a bit tricky: since the interface does not allow us to replace
  // the pointer of the old packet with a new one, we will initially copy the
  // metadata from old packet to new bigger packet.
  av_packet_copy_props(&dest_packet, packet);

  // Proceed with the conversion of the actual in-band NAL units, leave room
  // for configuration in the beginning.
  memcpy(dest_packet.data, &input_frame[0], input_frame.size());

  // At the end we must destroy the old packet.
  av_free_packet(packet);
  *packet = dest_packet;  // Finally, replace the values in the input packet.

  return true;
}

}  // namespace media