jpeg_encode_accelerator.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2017 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 COMPONENTS_CHROMEOS_CAMERA_JPEG_ENCODE_ACCELERATOR_H_
  5. #define COMPONENTS_CHROMEOS_CAMERA_JPEG_ENCODE_ACCELERATOR_H_
  6. #include <stdint.h>
  7. #include "media/base/bitstream_buffer.h"
  8. #include "media/base/video_frame.h"
  9. namespace chromeos_camera {
  10. // JPEG encoder interface.
  11. class JpegEncodeAccelerator {
  12. public:
  13. static constexpr int32_t kInvalidBitstreamBufferId = -1;
  14. enum Status {
  15. ENCODE_OK,
  16. HW_JPEG_ENCODE_NOT_SUPPORTED,
  17. // Eg. creation of encoder thread failed.
  18. THREAD_CREATION_FAILED,
  19. // Invalid argument was passed to an API method, e.g. the format of
  20. // VideoFrame is not supported.
  21. INVALID_ARGUMENT,
  22. // Output buffer is inaccessible, e.g. failed to map on another process.
  23. INACCESSIBLE_OUTPUT_BUFFER,
  24. // Failed to parse the incoming YUV image.
  25. PARSE_IMAGE_FAILED,
  26. // A fatal failure occurred in the GPU process layer or one of its
  27. // dependencies. Examples of such failures include hardware failures,
  28. // driver failures, library failures, and so on. Client is responsible for
  29. // destroying JEA after receiving this.
  30. PLATFORM_FAILURE,
  31. // Largest used enum. This should be adjusted when new errors are added.
  32. LARGEST_ERROR_ENUM = PLATFORM_FAILURE,
  33. };
  34. // Callback for JPEG encoder initialization.
  35. using InitCB = base::OnceCallback<void(Status)>;
  36. class Client {
  37. public:
  38. // Callback called after each successful Encode().
  39. // Parameters:
  40. // |buffer_id| is |output_buffer.id()| of the corresponding Encode() call.
  41. // |encoded_picture_size| is the actual size of encoded JPEG image in
  42. // the BitstreamBuffer provided through encode().
  43. virtual void VideoFrameReady(int32_t buffer_id,
  44. size_t encoded_picture_size) = 0;
  45. // Callback to notify errors. Client is responsible for destroying JEA when
  46. // receiving a fatal error, i.e. PLATFORM_FAILURE. For other errors, client
  47. // is informed about the buffer that failed to encode and may continue
  48. // using the same instance of JEA.
  49. // Parameters:
  50. // |buffer_id| is |output_buffer.id()| of the corresponding Encode() call
  51. // that resulted in the error.
  52. // |status| would be one of the values of Status except ENCODE_OK.
  53. virtual void NotifyError(int32_t buffer_id, Status status) = 0;
  54. protected:
  55. virtual ~Client() {}
  56. };
  57. // Destroys the encoder: all pending inputs are dropped immediately. This
  58. // call may asynchronously free system resources, but its client-visible
  59. // effects are synchronous. After destructor returns, no more callbacks
  60. // will be made on the client.
  61. virtual ~JpegEncodeAccelerator() = 0;
  62. // Initializes the JPEG encoder. Should be called once per encoder
  63. // construction. This call is asynchronous and executes |init_cb| upon
  64. // completion. Parameters:
  65. // |client| is the Client interface for encode callback. The provided
  66. // pointer must be valid until destructor is called.
  67. // |init_cb| is the JPEG encoder initialization status report callback.
  68. //
  69. // |init_cb| is called on the same thread as InitializeAsync() and can
  70. // potentially be called even after the JpegEncodeAccelerator destructor.
  71. // TODO(b/192342780): take a WeakPtr<Client> instead of a Client*.
  72. virtual void InitializeAsync(Client* client, InitCB init_cb) = 0;
  73. // Gets the maximum possible encoded result size.
  74. virtual size_t GetMaxCodedBufferSize(const gfx::Size& picture_size) = 0;
  75. // Encodes the given |video_frame| that contains a YUV image. Client will
  76. // receive the encoded result in Client::VideoFrameReady() callback with the
  77. // corresponding |output_buffer.id()|, or receive
  78. // Client::NotifyError() callback.
  79. // Parameters:
  80. // |video_frame| contains the YUV image to be encoded.
  81. // |quality| of JPEG image. The range is from 1~100. High value means high
  82. // quality.
  83. // |exif_buffer| contains Exif data to be inserted into JPEG image. If it's
  84. // nullptr, the JFIF APP0 segment will be inserted.
  85. // |output_buffer| that contains output buffer for encoded result. Clients
  86. // should call GetMaxCodedBufferSize() and allocate the buffer accordingly.
  87. // The buffer needs to be valid until VideoFrameReady() or NotifyError() is
  88. // called.
  89. virtual void Encode(scoped_refptr<media::VideoFrame> video_frame,
  90. int quality,
  91. media::BitstreamBuffer* exif_buffer,
  92. media::BitstreamBuffer output_buffer) = 0;
  93. // Encodes the given |video_frame| that contains a YUV image. Client will
  94. // receive the encoded result in Client::VideoFrameReady() callback, or
  95. // receive Client::NotifyError() callback.
  96. // Parameters:
  97. // |input_frame| contains the YUV image to be encoded.
  98. // |output_frame| is used to represent the output Dma-buf layout.
  99. // |quality| of JPEG image. The range is from 1~100. High value means high
  100. // quality.
  101. // |task_id| is an identifier started from zero that passed from the client
  102. // side. Could be used to identify different encode tasks.
  103. // |exif_buffer| contains Exif data to be inserted into JPEG image. If it's
  104. // nullptr, the JFIF APP0 segment will be inserted.
  105. virtual void EncodeWithDmaBuf(scoped_refptr<media::VideoFrame> input_frame,
  106. scoped_refptr<media::VideoFrame> output_frame,
  107. int quality,
  108. int32_t task_id,
  109. media::BitstreamBuffer* exif_buffer) = 0;
  110. };
  111. } // namespace chromeos_camera
  112. #endif // COMPONENTS_CHROMEOS_CAMERA_JPEG_ENCODE_ACCELERATOR_H_