mjpeg_decoder.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright 2012 The LibYuv Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef INCLUDE_LIBYUV_MJPEG_DECODER_H_
  11. #define INCLUDE_LIBYUV_MJPEG_DECODER_H_
  12. #include "libyuv/basic_types.h"
  13. #ifdef __cplusplus
  14. // NOTE: For a simplified public API use convert.h MJPGToI420().
  15. struct jpeg_common_struct;
  16. struct jpeg_decompress_struct;
  17. struct jpeg_source_mgr;
  18. namespace libyuv {
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size);
  23. #ifdef __cplusplus
  24. } // extern "C"
  25. #endif
  26. static const uint32 kUnknownDataSize = 0xFFFFFFFF;
  27. enum JpegSubsamplingType {
  28. kJpegYuv420,
  29. kJpegYuv422,
  30. kJpegYuv444,
  31. kJpegYuv400,
  32. kJpegUnknown
  33. };
  34. struct Buffer {
  35. const uint8* data;
  36. int len;
  37. };
  38. struct BufferVector {
  39. Buffer* buffers;
  40. int len;
  41. int pos;
  42. };
  43. struct SetJmpErrorMgr;
  44. // MJPEG ("Motion JPEG") is a pseudo-standard video codec where the frames are
  45. // simply independent JPEG images with a fixed huffman table (which is omitted).
  46. // It is rarely used in video transmission, but is common as a camera capture
  47. // format, especially in Logitech devices. This class implements a decoder for
  48. // MJPEG frames.
  49. //
  50. // See http://tools.ietf.org/html/rfc2435
  51. class LIBYUV_API MJpegDecoder {
  52. public:
  53. typedef void (*CallbackFunction)(void* opaque,
  54. const uint8* const* data,
  55. const int* strides,
  56. int rows);
  57. static const int kColorSpaceUnknown;
  58. static const int kColorSpaceGrayscale;
  59. static const int kColorSpaceRgb;
  60. static const int kColorSpaceYCbCr;
  61. static const int kColorSpaceCMYK;
  62. static const int kColorSpaceYCCK;
  63. MJpegDecoder();
  64. ~MJpegDecoder();
  65. // Loads a new frame, reads its headers, and determines the uncompressed
  66. // image format.
  67. // Returns LIBYUV_TRUE if image looks valid and format is supported.
  68. // If return value is LIBYUV_TRUE, then the values for all the following
  69. // getters are populated.
  70. // src_len is the size of the compressed mjpeg frame in bytes.
  71. LIBYUV_BOOL LoadFrame(const uint8* src, size_t src_len);
  72. // Returns width of the last loaded frame in pixels.
  73. int GetWidth();
  74. // Returns height of the last loaded frame in pixels.
  75. int GetHeight();
  76. // Returns format of the last loaded frame. The return value is one of the
  77. // kColorSpace* constants.
  78. int GetColorSpace();
  79. // Number of color components in the color space.
  80. int GetNumComponents();
  81. // Sample factors of the n-th component.
  82. int GetHorizSampFactor(int component);
  83. int GetVertSampFactor(int component);
  84. int GetHorizSubSampFactor(int component);
  85. int GetVertSubSampFactor(int component);
  86. // Public for testability.
  87. int GetImageScanlinesPerImcuRow();
  88. // Public for testability.
  89. int GetComponentScanlinesPerImcuRow(int component);
  90. // Width of a component in bytes.
  91. int GetComponentWidth(int component);
  92. // Height of a component.
  93. int GetComponentHeight(int component);
  94. // Width of a component in bytes with padding for DCTSIZE. Public for testing.
  95. int GetComponentStride(int component);
  96. // Size of a component in bytes.
  97. int GetComponentSize(int component);
  98. // Call this after LoadFrame() if you decide you don't want to decode it
  99. // after all.
  100. LIBYUV_BOOL UnloadFrame();
  101. // Decodes the entire image into a one-buffer-per-color-component format.
  102. // dst_width must match exactly. dst_height must be <= to image height; if
  103. // less, the image is cropped. "planes" must have size equal to at least
  104. // GetNumComponents() and they must point to non-overlapping buffers of size
  105. // at least GetComponentSize(i). The pointers in planes are incremented
  106. // to point to after the end of the written data.
  107. // TODO(fbarchard): Add dst_x, dst_y to allow specific rect to be decoded.
  108. LIBYUV_BOOL DecodeToBuffers(uint8** planes, int dst_width, int dst_height);
  109. // Decodes the entire image and passes the data via repeated calls to a
  110. // callback function. Each call will get the data for a whole number of
  111. // image scanlines.
  112. // TODO(fbarchard): Add dst_x, dst_y to allow specific rect to be decoded.
  113. LIBYUV_BOOL DecodeToCallback(CallbackFunction fn,
  114. void* opaque,
  115. int dst_width,
  116. int dst_height);
  117. // The helper function which recognizes the jpeg sub-sampling type.
  118. static JpegSubsamplingType JpegSubsamplingTypeHelper(
  119. int* subsample_x,
  120. int* subsample_y,
  121. int number_of_components);
  122. private:
  123. void AllocOutputBuffers(int num_outbufs);
  124. void DestroyOutputBuffers();
  125. LIBYUV_BOOL StartDecode();
  126. LIBYUV_BOOL FinishDecode();
  127. void SetScanlinePointers(uint8** data);
  128. LIBYUV_BOOL DecodeImcuRow();
  129. int GetComponentScanlinePadding(int component);
  130. // A buffer holding the input data for a frame.
  131. Buffer buf_;
  132. BufferVector buf_vec_;
  133. jpeg_decompress_struct* decompress_struct_;
  134. jpeg_source_mgr* source_mgr_;
  135. SetJmpErrorMgr* error_mgr_;
  136. // LIBYUV_TRUE iff at least one component has scanline padding. (i.e.,
  137. // GetComponentScanlinePadding() != 0.)
  138. LIBYUV_BOOL has_scanline_padding_;
  139. // Temporaries used to point to scanline outputs.
  140. int num_outbufs_; // Outermost size of all arrays below.
  141. uint8*** scanlines_;
  142. int* scanlines_sizes_;
  143. // Temporary buffer used for decoding when we can't decode directly to the
  144. // output buffers. Large enough for just one iMCU row.
  145. uint8** databuf_;
  146. int* databuf_strides_;
  147. };
  148. } // namespace libyuv
  149. #endif // __cplusplus
  150. #endif // INCLUDE_LIBYUV_MJPEG_DECODER_H_