video_decoder.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_BASE_VIDEO_DECODER_H_
  5. #define MEDIA_BASE_VIDEO_DECODER_H_
  6. #include "base/memory/ref_counted.h"
  7. #include "media/base/decoder.h"
  8. #include "media/base/decoder_status.h"
  9. #include "media/base/media_export.h"
  10. #include "media/base/pipeline_status.h"
  11. #include "media/base/waiting.h"
  12. #include "ui/gfx/geometry/size.h"
  13. namespace media {
  14. class CdmContext;
  15. class DecoderBuffer;
  16. class VideoDecoderConfig;
  17. class VideoFrame;
  18. class MEDIA_EXPORT VideoDecoder : public Decoder {
  19. public:
  20. // Callback for Decoder initialization.
  21. using InitCB = base::OnceCallback<void(DecoderStatus)>;
  22. // Callback for VideoDecoder to return a decoded frame whenever it becomes
  23. // available. Only non-EOS frames should be returned via this callback.
  24. using OutputCB = base::RepeatingCallback<void(scoped_refptr<VideoFrame>)>;
  25. // Callback type for Decode(). Called after the decoder has completed decoding
  26. // corresponding DecoderBuffer, indicating that it's ready to accept another
  27. // buffer to decode. |kOk| implies success, |kAborted| implies that the
  28. // decode was aborted, which does not necessarily indicate an error. For
  29. // example, a Reset() can trigger this. Any other status code indicates that
  30. // the decoder encountered an error, and must be reset.
  31. using DecodeCB = base::OnceCallback<void(DecoderStatus)>;
  32. VideoDecoder();
  33. VideoDecoder(const VideoDecoder&) = delete;
  34. VideoDecoder& operator=(const VideoDecoder&) = delete;
  35. ~VideoDecoder() override;
  36. // Initializes a VideoDecoder with the given |config|, executing the
  37. // |init_cb| upon completion. |output_cb| is called for each output frame
  38. // decoded by Decode().
  39. //
  40. // If |low_delay| is true then the decoder is not allowed to queue frames,
  41. // except for out-of-order frames, i.e. if the next frame can be returned it
  42. // must be returned without waiting for Decode() to be called again.
  43. // Initialization should fail if |low_delay| is true and the decoder cannot
  44. // satisfy the requirements above.
  45. //
  46. // |cdm_context| can be used to handle encrypted buffers. May be null if the
  47. // stream is not encrypted.
  48. //
  49. // |waiting_cb| is called whenever the decoder is stalled waiting for
  50. // something, e.g. decryption key. May be called at any time after
  51. // Initialize().
  52. //
  53. // Note:
  54. // 1) The VideoDecoder will be reinitialized if it was initialized before.
  55. // Upon reinitialization, all internal buffered frames will be dropped.
  56. // 2) This method should not be called during pending decode or reset.
  57. // 3) No VideoDecoder calls should be made before |init_cb| is executed.
  58. // 4) VideoDecoders should take care to run |output_cb| as soon as the frame
  59. // is ready (i.e. w/o thread trampolining) since it can strongly affect frame
  60. // delivery times with high-frame-rate material. See Decode() for additional
  61. // notes.
  62. // 5) |init_cb| may be called before this returns.
  63. virtual void Initialize(const VideoDecoderConfig& config,
  64. bool low_delay,
  65. CdmContext* cdm_context,
  66. InitCB init_cb,
  67. const OutputCB& output_cb,
  68. const WaitingCB& waiting_cb) = 0;
  69. // Requests a |buffer| to be decoded. The status of the decoder and decoded
  70. // frame are returned via the provided callback. Some decoders may allow
  71. // decoding multiple buffers in parallel. Callers should call
  72. // GetMaxDecodeRequests() to get number of buffers that may be decoded in
  73. // parallel.
  74. //
  75. // Implementations guarantee that the |decode_cb| will not be called from
  76. // within this method, and that it will be called even if Decode() is never
  77. // called again.
  78. //
  79. // After decoding is finished the decoder calls |output_cb| specified in
  80. // Initialize() for each decoded frame. |output_cb| may be called before or
  81. // after |decode_cb|, including before Decode() returns.
  82. //
  83. // If |buffer| is an EOS buffer then the decoder must be flushed, i.e.
  84. // |output_cb| must be called for each frame pending in the queue and
  85. // |decode_cb| must be called after that. Callers will not call Decode()
  86. // again until after the flush completes.
  87. virtual void Decode(scoped_refptr<DecoderBuffer> buffer,
  88. DecodeCB decode_cb) = 0;
  89. // Resets decoder state. All pending Decode() requests will be finished or
  90. // aborted before |closure| is called.
  91. // Note: No VideoDecoder calls should be made before |closure| is executed.
  92. virtual void Reset(base::OnceClosure closure) = 0;
  93. // Returns true if the decoder needs bitstream conversion before decoding.
  94. virtual bool NeedsBitstreamConversion() const;
  95. // Returns true if the decoder currently has the ability to decode and return
  96. // a VideoFrame. Most implementations can allocate a new VideoFrame and hence
  97. // this will always return true. Override and return false for decoders that
  98. // use a fixed set of VideoFrames for decoding.
  99. virtual bool CanReadWithoutStalling() const;
  100. // Returns maximum number of parallel decode requests.
  101. virtual int GetMaxDecodeRequests() const;
  102. // If true, the VideoDecoder outputs frames that hold resources which must be
  103. // kept alive for as long as the decoder's client needs them. This is only
  104. // relevant for VideoDecoders owned directly by the MojoVideoDecoderService.
  105. virtual bool FramesHoldExternalResources() const;
  106. // Returns the recommended number of threads for software video decoding. If
  107. // the --video-threads command line option is specified and is valid, that
  108. // value is returned. Otherwise |desired_threads| is clamped to the number of
  109. // logical processors and then further clamped to
  110. // [|limits::kMinVideoDecodeThreads|, |limits::kMaxVideoDecodeThreads|].
  111. static int GetRecommendedThreadCount(int desired_threads);
  112. // Returns the type of the decoder for statistics recording purposes.
  113. // For meta-decoders (those which wrap other decoders, ie, MojoVideoDecoder)
  114. // this should return the underlying type, if it is known, otherwise return
  115. // its own type.
  116. virtual VideoDecoderType GetDecoderType() const = 0;
  117. };
  118. } // namespace media
  119. #endif // MEDIA_BASE_VIDEO_DECODER_H_