ffmpeg_video_decoder.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_
  5. #define MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/sequence_checker.h"
  11. #include "media/base/supported_video_decoder_config.h"
  12. #include "media/base/video_decoder.h"
  13. #include "media/base/video_decoder_config.h"
  14. #include "media/base/video_frame_pool.h"
  15. #include "media/ffmpeg/ffmpeg_deleters.h"
  16. struct AVCodecContext;
  17. struct AVFrame;
  18. namespace media {
  19. class DecoderBuffer;
  20. class FFmpegDecodingLoop;
  21. class MediaLog;
  22. class MEDIA_EXPORT FFmpegVideoDecoder : public VideoDecoder {
  23. public:
  24. static bool IsCodecSupported(VideoCodec codec);
  25. static SupportedVideoDecoderConfigs SupportedConfigsForWebRTC();
  26. explicit FFmpegVideoDecoder(MediaLog* media_log);
  27. FFmpegVideoDecoder(const FFmpegVideoDecoder&) = delete;
  28. FFmpegVideoDecoder& operator=(const FFmpegVideoDecoder&) = delete;
  29. ~FFmpegVideoDecoder() override;
  30. // Allow decoding of individual NALU. Entire frames are required by default.
  31. // Disables low-latency mode. Must be called before Initialize().
  32. void set_decode_nalus(bool decode_nalus) { decode_nalus_ = decode_nalus; }
  33. // VideoDecoder implementation.
  34. VideoDecoderType GetDecoderType() const override;
  35. void Initialize(const VideoDecoderConfig& config,
  36. bool low_delay,
  37. CdmContext* cdm_context,
  38. InitCB init_cb,
  39. const OutputCB& output_cb,
  40. const WaitingCB& waiting_cb) override;
  41. void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
  42. void Reset(base::OnceClosure closure) override;
  43. // Callback called from within FFmpeg to allocate a buffer based on
  44. // the dimensions of |codec_context|. See AVCodecContext.get_buffer2
  45. // documentation inside FFmpeg.
  46. int GetVideoBuffer(struct AVCodecContext* codec_context,
  47. AVFrame* frame,
  48. int flags);
  49. void force_allocation_error_for_testing() { force_allocation_error_ = true; }
  50. private:
  51. enum class DecoderState { kUninitialized, kNormal, kDecodeFinished, kError };
  52. // Handles decoding of an unencrypted encoded buffer. A return value of false
  53. // indicates that an error has occurred.
  54. bool FFmpegDecode(const DecoderBuffer& buffer);
  55. bool OnNewFrame(AVFrame* frame);
  56. // Handles (re-)initializing the decoder with a (new) config.
  57. // Returns true if initialization was successful.
  58. bool ConfigureDecoder(const VideoDecoderConfig& config, bool low_delay);
  59. // Releases resources associated with |codec_context_|.
  60. void ReleaseFFmpegResources();
  61. SEQUENCE_CHECKER(sequence_checker_);
  62. const raw_ptr<MediaLog> media_log_;
  63. DecoderState state_ = DecoderState::kUninitialized;
  64. OutputCB output_cb_;
  65. // FFmpeg structures owned by this object.
  66. std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext> codec_context_;
  67. VideoDecoderConfig config_;
  68. VideoFramePool frame_pool_;
  69. bool decode_nalus_ = false;
  70. bool force_allocation_error_ = false;
  71. std::unique_ptr<FFmpegDecodingLoop> decoding_loop_;
  72. };
  73. } // namespace media
  74. #endif // MEDIA_FILTERS_FFMPEG_VIDEO_DECODER_H_