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