blob: 0cca112ce677801f935d0ff5321576c4b265cd86 [file] [log] [blame]
Sheng-Hao Tsao88d99072017-10-18 07:58:181// 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 Lee01d63ef2019-05-09 09:24:505#ifndef COMPONENTS_CHROMEOS_CAMERA_JPEG_ENCODE_ACCELERATOR_H_
6#define COMPONENTS_CHROMEOS_CAMERA_JPEG_ENCODE_ACCELERATOR_H_
Sheng-Hao Tsao88d99072017-10-18 07:58:187
8#include <stdint.h>
9
10#include "media/base/bitstream_buffer.h"
Sheng-Hao Tsao88d99072017-10-18 07:58:1811#include "media/base/video_frame.h"
12
Wei Lee01d63ef2019-05-09 09:24:5013namespace chromeos_camera {
Sheng-Hao Tsao88d99072017-10-18 07:58:1814
15// JPEG encoder interface.
Wei Lee01d63ef2019-05-09 09:24:5016class JpegEncodeAccelerator {
Sheng-Hao Tsao88d99072017-10-18 07:58:1817 public:
Moja Hsu6f961aa2018-11-13 10:55:4918 static constexpr int32_t kInvalidBitstreamBufferId = -1;
Sheng-Hao Tsao88d99072017-10-18 07:58:1819 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 Lee01d63ef2019-05-09 09:24:5047 class Client {
Sheng-Hao Tsao88d99072017-10-18 07:58:1848 public:
49 // Callback called after each successful Encode().
50 // Parameters:
Sheng-Hao Tsaoeead1e72018-03-09 19:43:3051 // |buffer_id| is |output_buffer.id()| of the corresponding Encode() call.
Sheng-Hao Tsao88d99072017-10-18 07:58:1852 // |encoded_picture_size| is the actual size of encoded JPEG image in
53 // the BitstreamBuffer provided through encode().
Sheng-Hao Tsaoeead1e72018-03-09 19:43:3054 virtual void VideoFrameReady(int32_t buffer_id,
Sheng-Hao Tsao88d99072017-10-18 07:58:1855 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 Tsaoeead1e72018-03-09 19:43:3062 // |buffer_id| is |output_buffer.id()| of the corresponding Encode() call
Sheng-Hao Tsao88d99072017-10-18 07:58:1863 // that resulted in the error.
64 // |status| would be one of the values of Status except ENCODE_OK.
Sheng-Hao Tsaoeead1e72018-03-09 19:43:3065 virtual void NotifyError(int32_t buffer_id, Status status) = 0;
Sheng-Hao Tsao88d99072017-10-18 07:58:1866
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 Tsao941a9cd2018-03-20 04:47:0675 virtual ~JpegEncodeAccelerator() = 0;
Sheng-Hao Tsao88d99072017-10-18 07:58:1876
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 Tsaoeead1e72018-03-09 19:43:3090 // corresponding |output_buffer.id()|, or receive
Sheng-Hao Tsao88d99072017-10-18 07:58:1891 // Client::NotifyError() callback.
92 // Parameters:
93 // |video_frame| contains the YUV image to be encoded.
Moja Hsu6f961aa2018-11-13 10:55:4994 // |quality| of JPEG image. The range is from 1~100. High value means high
95 // quality.
Sheng-Hao Tsaoeead1e72018-03-09 19:43:3096 // |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 Tsao88d99072017-10-18 07:58:1899 // 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 Cary1ad95fe82019-05-15 12:51:33104 media::BitstreamBuffer* exif_buffer,
105 media::BitstreamBuffer output_buffer) = 0;
Wei Leec4d29442019-04-29 04:32:51106
107 // Encodes the given |video_frame| that contains a YUV image. Client will
Wei Lee94dbd9af2019-05-24 04:23:06108 // receive the encoded result in Client::VideoFrameReady() callback, or
109 // receive Client::NotifyError() callback.
Wei Leec4d29442019-04-29 04:32:51110 // 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 Lee94dbd9af2019-05-24 04:23:06115 // |task_id| is an identifier started from zero that passed from the client
116 // side. Could be used to identify different encode tasks.
Wei Leec4d29442019-04-29 04:32:51117 // |exif_buffer| contains Exif data to be inserted into JPEG image. If it's
118 // nullptr, the JFIF APP0 segment will be inserted.
Wei Lee01d63ef2019-05-09 09:24:50119 virtual void EncodeWithDmaBuf(scoped_refptr<media::VideoFrame> input_frame,
120 scoped_refptr<media::VideoFrame> output_frame,
Wei Leec4d29442019-04-29 04:32:51121 int quality,
Wei Lee94dbd9af2019-05-24 04:23:06122 int32_t task_id,
123 media::BitstreamBuffer* exif_buffer) = 0;
Sheng-Hao Tsao88d99072017-10-18 07:58:18124};
125
Wei Lee01d63ef2019-05-09 09:24:50126} // namespace chromeos_camera
Sheng-Hao Tsao88d99072017-10-18 07:58:18127
Wei Lee01d63ef2019-05-09 09:24:50128#endif // COMPONENTS_CHROMEOS_CAMERA_JPEG_ENCODE_ACCELERATOR_H_