blob: 393d1986b12ff98b3ce95672584c634e4ce845d9 [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 Zhangf84ed402023-02-10 22:41:46 +00005#include <jpegrecoverymap/jpegr.h>
Dichen Zhang3b2c0ce2022-12-14 19:58:55 +00006
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08007extern "C" {
8 #include "jpeglib.h"
9 #include "jerror.h"
10}
11
Kevin Lubick1175dc02022-02-28 12:41:27 -050012class SkWStream;
13
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080014class YuvToJpegEncoder {
15public:
16 /** Create an encoder based on the YUV format.
17 *
18 * @param pixelFormat The yuv pixel format as defined in ui/PixelFormat.h.
19 * @param strides The number of row bytes in each image plane.
20 * @return an encoder based on the pixelFormat.
21 */
22 static YuvToJpegEncoder* create(int pixelFormat, int* strides);
23
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070024 explicit YuvToJpegEncoder(int* strides);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080025
26 /** Encode YUV data to jpeg, which is output to a stream.
27 *
28 * @param stream The jpeg output stream.
29 * @param inYuv The input yuv data.
Dichen Zhang3b2c0ce2022-12-14 19:58:55 +000030 * @param width Width of the Yuv data in terms of pixels.
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080031 * @param height Height of the Yuv data in terms of pixels.
32 * @param offsets The offsets in each image plane with respect to inYuv.
33 * @param jpegQuality Picture quality in [0, 100].
34 * @return true if successfully compressed the stream.
35 */
36 bool encode(SkWStream* stream, void* inYuv, int width,
37 int height, int* offsets, int jpegQuality);
38
39 virtual ~YuvToJpegEncoder() {}
40
41protected:
42 int fNumPlanes;
43 int* fStrides;
44 void setJpegCompressStruct(jpeg_compress_struct* cinfo, int width,
45 int height, int quality);
46 virtual void configSamplingFactors(jpeg_compress_struct* cinfo) = 0;
47 virtual void compress(jpeg_compress_struct* cinfo,
48 uint8_t* yuv, int* offsets) = 0;
49};
50
51class Yuv420SpToJpegEncoder : public YuvToJpegEncoder {
52public:
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070053 explicit Yuv420SpToJpegEncoder(int* strides);
Andreas Gampeed6b9df2014-11-20 22:02:20 -080054 virtual ~Yuv420SpToJpegEncoder() {}
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080055
56private:
Andreas Gampeed6b9df2014-11-20 22:02:20 -080057 void configSamplingFactors(jpeg_compress_struct* cinfo);
58 void deinterleaveYuv(uint8_t* yuv, int width, int height,
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080059 uint8_t*& yPlanar, uint8_t*& uPlanar, uint8_t*& vPlanar);
Andreas Gampeed6b9df2014-11-20 22:02:20 -080060 void deinterleave(uint8_t* vuPlanar, uint8_t* uRows, uint8_t* vRows,
61 int rowIndex, int width, int height);
62 void compress(jpeg_compress_struct* cinfo, uint8_t* yuv, int* offsets);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080063};
64
65class Yuv422IToJpegEncoder : public YuvToJpegEncoder {
66public:
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070067 explicit Yuv422IToJpegEncoder(int* strides);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080068 virtual ~Yuv422IToJpegEncoder() {}
69
70private:
71 void configSamplingFactors(jpeg_compress_struct* cinfo);
72 void compress(jpeg_compress_struct* cinfo, uint8_t* yuv, int* offsets);
73 void deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows,
74 uint8_t* vRows, int rowIndex, int width, int height);
75};
76
Dichen Zhang3b2c0ce2022-12-14 19:58:55 +000077class P010Yuv420ToJpegREncoder {
78public:
79 /** Encode YUV data to jpeg/r, which is output to a stream.
Dichen Zhangf84ed402023-02-10 22:41:46 +000080 * This method will call JpegR::EncodeJPEGR() method. If encoding failed,
Dichen Zhang3b2c0ce2022-12-14 19:58:55 +000081 * Corresponding error code (defined in jpegrerrorcode.h) will be printed and this
82 * method will be terminated and return false.
83 *
84 * @param env JNI environment.
85 * @param stream The jpeg output stream.
86 * @param hdr The input yuv data (p010 format).
87 * @param hdrColorSpaceId color space id for the input hdr.
88 * @param sdr The input yuv data (yuv420p format).
89 * @param sdrColorSpaceId color space id for the input sdr.
90 * @param width Width of the Yuv data in terms of pixels.
91 * @param height Height of the Yuv data in terms of pixels.
92 * @param jpegQuality Picture quality in [0, 100].
93 * @return true if successfully compressed the stream.
94 */
95 bool encode(JNIEnv* env,
96 SkWStream* stream, void* hdr, int hdrColorSpace, void* sdr, int sdrColorSpace,
97 int width, int height, int jpegQuality);
98
99 /** Map data space (defined in DataSpace.java and data_space.h) to the color gamut
100 * used in JPEG/R
101 *
102 * @param env JNI environment.
103 * @param aDataSpace data space defined in data_space.h.
104 * @return color gamut for JPEG/R.
105 */
106 static android::recoverymap::jpegr_color_gamut findColorGamut(JNIEnv* env, int aDataSpace);
107
108 /** Map data space (defined in DataSpace.java and data_space.h) to the transfer function
109 * used in JPEG/R
110 *
111 * @param env JNI environment.
112 * @param aDataSpace data space defined in data_space.h.
113 * @return color gamut for JPEG/R.
114 */
115 static android::recoverymap::jpegr_transfer_function findHdrTransferFunction(
116 JNIEnv* env, int aDataSpace);
117};
118
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800119#endif // _ANDROID_GRAPHICS_YUV_TO_JPEG_ENCODER_H_