• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_MINI_BENCHMARK_LIBJPEG_HANDLE_H_
16 #define TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_MINI_BENCHMARK_LIBJPEG_HANDLE_H_
17 
18 #include <stddef.h>
19 #include <stdio.h>
20 
21 #include <memory>
22 #include <string>
23 
24 #include "tensorflow/lite/experimental/acceleration/mini_benchmark/decode_jpeg_status.h"
25 #include "tensorflow/lite/experimental/acceleration/mini_benchmark/libjpeg.h"
26 
27 namespace tflite {
28 namespace acceleration {
29 namespace decode_jpeg_kernel {
30 
31 // This class offers a handle to Libjpeg shared object library on Android.
32 // It offers pointers to functions in Libjpeg that are required for decoding
33 // JPEG images.
34 // TODO(b/172544567): Support Apple.
35 class LibjpegHandle {
36  public:
37   // Factory for creating an initialised instance of LibjpegHandle.
38   // Loads the libjpeg dynamic library and gets handle to all the functions
39   // required for decompressing JPEGs. Returns an initialised instance of
40   // LibjpegHandle if successful, else nullptr. Stores initialisation status in
41   // `status`.
42   static std::unique_ptr<LibjpegHandle> Create(Status& status);
43   // Closes the dynamic library loaded in libjpeg_.
44   ~LibjpegHandle();
45   LibjpegHandle(LibjpegHandle const&) = delete;
46   LibjpegHandle& operator=(const LibjpegHandle&) = delete;
47   LibjpegHandle(LibjpegHandle&& LibjpegHandle) = delete;
48   LibjpegHandle& operator=(LibjpegHandle&& other) = delete;
49   // Based on our analysis of Android devices in the ODML lab, it is reasonable
50   // to expect 62 (6b) as the version of libjpeg on all Android devices from SDK
51   // 22 onwards.
52   static const int kLibjpegVersion = 62;
53   // Definitions of the functions below can be found in
54   // third_party/libjpeg_turbo/src/jpeglib.h
55   struct jpeg_error_mgr* (*jpeg_std_error_)(struct jpeg_error_mgr*);
56   void (*jpeg_destroy_decompress_)(j_decompress_ptr);
57   void (*jpeg_create_decompress_)(j_decompress_ptr, int, size_t);
58   void (*jpeg_stdio_src_)(j_decompress_ptr, FILE*);
59   int (*jpeg_read_header_)(j_decompress_ptr, boolean);
60   boolean (*jpeg_start_decompress_)(j_decompress_ptr);
61   unsigned int (*jpeg_read_scanlines_)(j_decompress_ptr, JSAMPARRAY,
62                                        JDIMENSION);
63   boolean (*jpeg_finish_decompress_)(j_decompress_ptr);
64 
65  private:
LibjpegHandle()66   LibjpegHandle() {}
67   void* libjpeg_ = nullptr;
68 };
69 
70 }  // namespace decode_jpeg_kernel
71 }  // namespace acceleration
72 }  // namespace tflite
73 #endif  // TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_MINI_BENCHMARK_LIBJPEG_HANDLE_H_
74