1
0

mp4_jpeg_decoder.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "mp4_jpeg_decoder.h"
  2. #include "api__jpeg.h"
  3. #include <api/service/waservicefactory.h>
  4. MP4JPEGDecoder::MP4JPEGDecoder()
  5. {
  6. jpegLoader=0;
  7. width=0;
  8. height=0;
  9. decoded_image = 0;
  10. }
  11. int MP4JPEGDecoder::Open(MP4FileHandle mp4_file, MP4TrackId mp4_track)
  12. {
  13. // load JPEG loader
  14. jpegLoader = new JpgLoad;
  15. if (jpegLoader)
  16. return MP4_VIDEO_SUCCESS;
  17. else
  18. return MP4_VIDEO_FAILURE;
  19. }
  20. void MP4JPEGDecoder::Close()
  21. {
  22. delete jpegLoader;
  23. delete this;
  24. }
  25. int MP4JPEGDecoder::GetOutputFormat(int *x, int *y, int *color_format, double *aspect_ratio)
  26. {
  27. if (!height || !width)
  28. return MP4_VIDEO_FAILURE;
  29. *x = width;
  30. *y = height;
  31. *color_format = '23GR'; // RGB32
  32. return MP4_VIDEO_SUCCESS;
  33. }
  34. int MP4JPEGDecoder::DecodeSample(const void *inputBuffer, size_t inputBufferBytes, MP4Timestamp timestamp)
  35. {
  36. bool change_in_size=false;
  37. if (decoded_image)
  38. WASABI_API_MEMMGR->sysFree(decoded_image);
  39. int decode_width, decode_height;
  40. decoded_image = jpegLoader->loadImage(inputBuffer, (int)inputBufferBytes, &decode_width, &decode_height);
  41. if (!decoded_image)
  42. return MP4_VIDEO_FAILURE;
  43. if (width && decode_width != width // if we have a different width from last time
  44. || height && decode_height != height)
  45. change_in_size = true;
  46. width = decode_width;
  47. height = decode_height;
  48. return change_in_size?MP4_VIDEO_OUTPUT_FORMAT_CHANGED:MP4_VIDEO_SUCCESS;
  49. }
  50. int MP4JPEGDecoder::CanHandleCodec(const char *codecName)
  51. {
  52. return !strcmp(codecName, "jpeg");
  53. }
  54. int MP4JPEGDecoder::GetPicture(void **data, void **decoder_data, MP4Timestamp *timestamp)
  55. {
  56. if (!decoded_image)
  57. return MP4_VIDEO_FAILURE;
  58. *data = decoded_image;
  59. *decoder_data = 0;
  60. decoded_image = 0; // wipe our hands clean of it so we don't double free
  61. return MP4_VIDEO_SUCCESS;
  62. }
  63. void MP4JPEGDecoder::FreePicture(void *data, void *decoder_data)
  64. {
  65. WASABI_API_MEMMGR->sysFree(data);
  66. }
  67. #define CBCLASS MP4JPEGDecoder
  68. START_DISPATCH;
  69. CB(MPEG4_VIDEO_OPEN, Open)
  70. CB(MPEG4_VIDEO_GETOUTPUTFORMAT, GetOutputFormat)
  71. CB(MPEG4_VIDEO_DECODE, DecodeSample)
  72. CB(MPEG4_VIDEO_HANDLES_CODEC, CanHandleCodec)
  73. CB(MPEG4_VIDEO_GET_PICTURE, GetPicture)
  74. VCB(MPEG4_VIDEO_FREE_PICTURE, FreePicture)
  75. END_DISPATCH;
  76. #undef CBCLASS