blob: 629f1e64726b672314f60420bf1a9d8e16eb0c49 [file] [log] [blame]
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001#ifndef _ANDROID_GRAPHICS_YUV_TO_JPEG_ENCODER_H_
2#define _ANDROID_GRAPHICS_YUV_TO_JPEG_ENCODER_H_
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08003
Dichen Zhang3b2c0ce2022-12-14 19:58:55 +00004#include <android/data_space.h>
Dichen Zhangbfd23802023-10-26 01:29:19 +00005#include <nativehelper/ScopedPrimitiveArray.h>
Dichen Zhang7087aaf2023-04-14 19:01:05 +00006#include <ultrahdr/jpegr.h>
Dichen Zhang3b2c0ce2022-12-14 19:58:55 +00007
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08008extern "C" {
9 #include "jpeglib.h"
10 #include "jerror.h"
11}
12
Kevin Lubick1175dc02022-02-28 12:41:27 -050013class SkWStream;
14
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080015class YuvToJpegEncoder {
16public:
17 /** Create an encoder based on the YUV format.
18 *
19 * @param pixelFormat The yuv pixel format as defined in ui/PixelFormat.h.
20 * @param strides The number of row bytes in each image plane.
21 * @return an encoder based on the pixelFormat.
22 */
23 static YuvToJpegEncoder* create(int pixelFormat, int* strides);
24
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070025 explicit YuvToJpegEncoder(int* strides);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080026
27 /** Encode YUV data to jpeg, which is output to a stream.
28 *
29 * @param stream The jpeg output stream.
30 * @param inYuv The input yuv data.
Dichen Zhang3b2c0ce2022-12-14 19:58:55 +000031 * @param width Width of the Yuv data in terms of pixels.
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080032 * @param height Height of the Yuv data in terms of pixels.
33 * @param offsets The offsets in each image plane with respect to inYuv.
34 * @param jpegQuality Picture quality in [0, 100].
35 * @return true if successfully compressed the stream.
36 */
37 bool encode(SkWStream* stream, void* inYuv, int width,
38 int height, int* offsets, int jpegQuality);
39
40 virtual ~YuvToJpegEncoder() {}
41
42protected:
43 int fNumPlanes;
44 int* fStrides;
45 void setJpegCompressStruct(jpeg_compress_struct* cinfo, int width,
46 int height, int quality);
47 virtual void configSamplingFactors(jpeg_compress_struct* cinfo) = 0;
48 virtual void compress(jpeg_compress_struct* cinfo,
49 uint8_t* yuv, int* offsets) = 0;
50};
51
52class Yuv420SpToJpegEncoder : public YuvToJpegEncoder {
53public:
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070054 explicit Yuv420SpToJpegEncoder(int* strides);
Andreas Gampeed6b9df2014-11-20 22:02:20 -080055 virtual ~Yuv420SpToJpegEncoder() {}
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080056
57private:
Andreas Gampeed6b9df2014-11-20 22:02:20 -080058 void configSamplingFactors(jpeg_compress_struct* cinfo);
59 void deinterleaveYuv(uint8_t* yuv, int width, int height,
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080060 uint8_t*& yPlanar, uint8_t*& uPlanar, uint8_t*& vPlanar);
Andreas Gampeed6b9df2014-11-20 22:02:20 -080061 void deinterleave(uint8_t* vuPlanar, uint8_t* uRows, uint8_t* vRows,
62 int rowIndex, int width, int height);
63 void compress(jpeg_compress_struct* cinfo, uint8_t* yuv, int* offsets);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080064};
65
66class Yuv422IToJpegEncoder : public YuvToJpegEncoder {
67public:
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070068 explicit Yuv422IToJpegEncoder(int* strides);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080069 virtual ~Yuv422IToJpegEncoder() {}
70
71private:
72 void configSamplingFactors(jpeg_compress_struct* cinfo);
73 void compress(jpeg_compress_struct* cinfo, uint8_t* yuv, int* offsets);
74 void deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows,
75 uint8_t* vRows, int rowIndex, int width, int height);
76};
77
Dichen Zhang3b2c0ce2022-12-14 19:58:55 +000078class P010Yuv420ToJpegREncoder {
79public:
80 /** Encode YUV data to jpeg/r, which is output to a stream.
Dichen Zhangf84ed402023-02-10 22:41:46 +000081 * This method will call JpegR::EncodeJPEGR() method. If encoding failed,
Dichen Zhang3b2c0ce2022-12-14 19:58:55 +000082 * Corresponding error code (defined in jpegrerrorcode.h) will be printed and this
83 * method will be terminated and return false.
84 *
85 * @param env JNI environment.
86 * @param stream The jpeg output stream.
87 * @param hdr The input yuv data (p010 format).
88 * @param hdrColorSpaceId color space id for the input hdr.
89 * @param sdr The input yuv data (yuv420p format).
90 * @param sdrColorSpaceId color space id for the input sdr.
91 * @param width Width of the Yuv data in terms of pixels.
92 * @param height Height of the Yuv data in terms of pixels.
93 * @param jpegQuality Picture quality in [0, 100].
Dichen Zhangbfd23802023-10-26 01:29:19 +000094 * @param exif Buffer holds EXIF package.
Dichen Zhang843f1bd2023-10-26 02:38:21 +000095 * @param hdrStrides The number of row bytes in each image plane of the HDR input.
96 * @param sdrStrides The number of row bytes in each image plane of the SDR input.
Dichen Zhang3b2c0ce2022-12-14 19:58:55 +000097 * @return true if successfully compressed the stream.
98 */
99 bool encode(JNIEnv* env,
100 SkWStream* stream, void* hdr, int hdrColorSpace, void* sdr, int sdrColorSpace,
Dichen Zhang843f1bd2023-10-26 02:38:21 +0000101 int width, int height, int jpegQuality, ScopedByteArrayRO* exif,
102 ScopedIntArrayRO* hdrStrides, ScopedIntArrayRO* sdrStrides);
Dichen Zhang3b2c0ce2022-12-14 19:58:55 +0000103
104 /** Map data space (defined in DataSpace.java and data_space.h) to the color gamut
105 * used in JPEG/R
106 *
107 * @param env JNI environment.
108 * @param aDataSpace data space defined in data_space.h.
109 * @return color gamut for JPEG/R.
110 */
Harish Mahendrakar6b09b822023-10-12 20:45:45 +0000111 static ultrahdr::ultrahdr_color_gamut findColorGamut(JNIEnv* env, int aDataSpace);
Dichen Zhang3b2c0ce2022-12-14 19:58:55 +0000112
113 /** Map data space (defined in DataSpace.java and data_space.h) to the transfer function
114 * used in JPEG/R
115 *
116 * @param env JNI environment.
117 * @param aDataSpace data space defined in data_space.h.
118 * @return color gamut for JPEG/R.
119 */
Harish Mahendrakar6b09b822023-10-12 20:45:45 +0000120 static ultrahdr::ultrahdr_transfer_function findHdrTransferFunction(JNIEnv* env,
121 int aDataSpace);
Dichen Zhang3b2c0ce2022-12-14 19:58:55 +0000122};
123
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800124#endif // _ANDROID_GRAPHICS_YUV_TO_JPEG_ENCODER_H_