ffmpeg_h264_to_annex_b_bitstream_converter.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2012 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. #include "media/filters/ffmpeg_h264_to_annex_b_bitstream_converter.h"
  5. #include <stdint.h>
  6. #include "base/logging.h"
  7. #include "media/ffmpeg/ffmpeg_common.h"
  8. #include "media/formats/mp4/box_definitions.h"
  9. namespace media {
  10. FFmpegH264ToAnnexBBitstreamConverter::FFmpegH264ToAnnexBBitstreamConverter(
  11. AVCodecParameters* stream_codec_parameters)
  12. : configuration_processed_(false),
  13. stream_codec_parameters_(stream_codec_parameters) {
  14. CHECK(stream_codec_parameters_);
  15. }
  16. FFmpegH264ToAnnexBBitstreamConverter::~FFmpegH264ToAnnexBBitstreamConverter() =
  17. default;
  18. bool FFmpegH264ToAnnexBBitstreamConverter::ConvertPacket(AVPacket* packet) {
  19. std::unique_ptr<mp4::AVCDecoderConfigurationRecord> avc_config;
  20. if (packet == NULL || !packet->data) {
  21. DVLOG(2) << __func__ << ": Null or empty packet";
  22. return false;
  23. }
  24. // Calculate the needed output buffer size.
  25. if (!configuration_processed_) {
  26. if (!stream_codec_parameters_->extradata ||
  27. stream_codec_parameters_->extradata_size <= 0) {
  28. DVLOG(2) << __func__ << ": Empty extra data";
  29. return false;
  30. }
  31. avc_config.reset(new mp4::AVCDecoderConfigurationRecord());
  32. if (!converter_.ParseConfiguration(stream_codec_parameters_->extradata,
  33. stream_codec_parameters_->extradata_size,
  34. avc_config.get())) {
  35. DVLOG(2) << __func__ << ": ParseConfiguration() failure";
  36. return false;
  37. }
  38. }
  39. uint32_t output_packet_size = converter_.CalculateNeededOutputBufferSize(
  40. packet->data, packet->size, avc_config.get());
  41. if (output_packet_size == 0) {
  42. DVLOG(2) << __func__ << ": zero |output_packet_size|";
  43. return false; // Invalid input packet.
  44. }
  45. // Allocate new packet for the output.
  46. AVPacket dest_packet;
  47. if (av_new_packet(&dest_packet, output_packet_size) != 0) {
  48. DVLOG(2) << __func__ << ": Memory allocation failure";
  49. return false;
  50. }
  51. // This is a bit tricky: since the interface does not allow us to replace
  52. // the pointer of the old packet with a new one, we will initially copy the
  53. // metadata from old packet to new bigger packet.
  54. av_packet_copy_props(&dest_packet, packet);
  55. // Proceed with the conversion of the actual in-band NAL units, leave room
  56. // for configuration in the beginning.
  57. uint32_t io_size = dest_packet.size;
  58. if (!converter_.ConvertNalUnitStreamToByteStream(
  59. packet->data, packet->size,
  60. avc_config.get(),
  61. dest_packet.data, &io_size)) {
  62. DVLOG(2) << __func__ << ": ConvertNalUnitStreamToByteStream() failure";
  63. return false;
  64. }
  65. if (avc_config)
  66. configuration_processed_ = true;
  67. // At the end we must destroy the old packet.
  68. av_packet_unref(packet);
  69. // Finally, replace the values in the input packet.
  70. memcpy(packet, &dest_packet, sizeof(*packet));
  71. return true;
  72. }
  73. } // namespace media