1
0

avi_mjpg_decoder.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "api__jpeg.h"
  2. #include "avi_mjpg_decoder.h"
  3. #include "../Winamp/wa_ipc.h"
  4. #include <limits.h>
  5. #include <intsafe.h>
  6. #include <setjmp.h>
  7. extern "C"
  8. {
  9. #undef FAR
  10. #include "jpeglib.h"
  11. };
  12. uint8_t mjpeg_header[0x1A4] =
  13. {
  14. /* JPEG DHT Segment for YCrCb omitted from MJPG data */
  15. 0xFF,0xC4,0x01,0xA2,
  16. 0x00,0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  17. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x01,0x00,0x03,0x01,0x01,0x01,0x01,
  18. 0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  19. 0x08,0x09,0x0A,0x0B,0x10,0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,
  20. 0x00,0x01,0x7D,0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,
  21. 0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xA1,0x08,0x23,0x42,0xB1,0xC1,0x15,0x52,0xD1,0xF0,0x24,
  22. 0x33,0x62,0x72,0x82,0x09,0x0A,0x16,0x17,0x18,0x19,0x1A,0x25,0x26,0x27,0x28,0x29,0x2A,0x34,
  23. 0x35,0x36,0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,0x55,0x56,
  24. 0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x73,0x74,0x75,0x76,0x77,0x78,
  25. 0x79,0x7A,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,
  26. 0x9A,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,
  27. 0xBA,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,
  28. 0xDA,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,
  29. 0xF8,0xF9,0xFA,0x11,0x00,0x02,0x01,0x02,0x04,0x04,0x03,0x04,0x07,0x05,0x04,0x04,0x00,0x01,
  30. 0x02,0x77,0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,
  31. 0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,0xA1,0xB1,0xC1,0x09,0x23,0x33,0x52,0xF0,0x15,0x62,
  32. 0x72,0xD1,0x0A,0x16,0x24,0x34,0xE1,0x25,0xF1,0x17,0x18,0x19,0x1A,0x26,0x27,0x28,0x29,0x2A,
  33. 0x35,0x36,0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,0x55,0x56,
  34. 0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x73,0x74,0x75,0x76,0x77,0x78,
  35. 0x79,0x7A,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,
  36. 0x99,0x9A,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,
  37. 0xB9,0xBA,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,
  38. 0xD9,0xDA,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,
  39. 0xF9,0xFA
  40. };
  41. int AVIDecoderCreator::CreateVideoDecoder( const nsavi::AVIH *avi_header, const nsavi::STRH *stream_header, const nsavi::STRF *stream_format, const nsavi::STRD *stream_data, ifc_avivideodecoder **decoder )
  42. {
  43. nsavi::video_format *format = (nsavi::video_format *)stream_format;
  44. if ( format )
  45. {
  46. if ( format->compression == 'GPJM' ) // mjpg
  47. {
  48. *decoder = AVIMJPEG::CreateDecoder( format );
  49. if ( *decoder )
  50. return CREATEDECODER_SUCCESS;
  51. else
  52. return CREATEDECODER_FAILURE;
  53. }
  54. }
  55. return CREATEDECODER_NOT_MINE;
  56. }
  57. #define CBCLASS AVIDecoderCreator
  58. START_DISPATCH;
  59. CB( CREATE_VIDEO_DECODER, CreateVideoDecoder )
  60. END_DISPATCH;
  61. #undef CBCLASS
  62. AVIMJPEG *AVIMJPEG::CreateDecoder( nsavi::video_format *stream_format )
  63. {
  64. AVIMJPEG *decoder = new AVIMJPEG( stream_format );
  65. if ( !decoder )
  66. {
  67. return 0;
  68. }
  69. return decoder;
  70. }
  71. AVIMJPEG::AVIMJPEG( nsavi::video_format *stream_format ) : stream_format( stream_format )
  72. {
  73. cinfo.err = jpeg_std_error( &jerr );
  74. jpeg_create_decompress( &cinfo );
  75. jpegLoader = 0;
  76. width = 0;
  77. height = 0;
  78. decoded_image = 0;
  79. image_size = 0;
  80. image_outputted = true;
  81. }
  82. int AVIMJPEG::GetOutputProperties( int *x, int *y, int *color_format, double *aspect_ratio, int *flip )
  83. {
  84. *x = stream_format->width;
  85. *y = stream_format->height;
  86. *color_format = '23GR'; // RGB32
  87. *aspect_ratio = 1.0;
  88. *flip = 0;
  89. return AVI_SUCCESS;
  90. }
  91. struct mjpeg_source_mgr
  92. {
  93. jpeg_source_mgr src;
  94. int state;
  95. const JOCTET *input_buffer;
  96. size_t input_buffer_bytes;
  97. };
  98. static void init_source( j_decompress_ptr cinfo )
  99. {}
  100. static const JOCTET jpeg_eof[] = {(JOCTET) 0xFF, (JOCTET) JPEG_EOI};
  101. static boolean fill_input_buffer( j_decompress_ptr cinfo )
  102. {
  103. mjpeg_source_mgr *msrc = (mjpeg_source_mgr *)cinfo->src;
  104. jpeg_source_mgr *src = &msrc->src;
  105. switch ( msrc->state++ )
  106. {
  107. case 0:
  108. src->next_input_byte = mjpeg_header;
  109. src->bytes_in_buffer = sizeof( mjpeg_header );
  110. return TRUE;
  111. case 1:
  112. src->next_input_byte = msrc->input_buffer + 2;
  113. src->bytes_in_buffer = msrc->input_buffer_bytes - 2;
  114. return TRUE;
  115. case 2:
  116. src->next_input_byte = jpeg_eof;
  117. src->bytes_in_buffer = 2;
  118. return TRUE;
  119. }
  120. return TRUE;
  121. }
  122. void skip_input_data( j_decompress_ptr cinfo, long num_bytes )
  123. {
  124. if ( num_bytes > 0 )
  125. {
  126. if ( num_bytes > (long)cinfo->src->bytes_in_buffer )
  127. {
  128. fill_input_buffer( cinfo );
  129. }
  130. else
  131. {
  132. cinfo->src->next_input_byte += (size_t)num_bytes;
  133. cinfo->src->bytes_in_buffer -= (size_t)num_bytes;
  134. }
  135. }
  136. }
  137. void term_source( j_decompress_ptr cinfo )
  138. {}
  139. static void wasabi_jpgload_error_exit( j_common_ptr cinfo )
  140. {
  141. jmp_buf *stack_env = (jmp_buf *)cinfo->client_data;
  142. longjmp( *stack_env, 1 );
  143. }
  144. int AVIMJPEG::DecodeChunk( uint16_t type, const void *inputBuffer, size_t inputBufferBytes )
  145. {
  146. mjpeg_source_mgr src = { {(const JOCTET *)inputBuffer,2,init_source,fill_input_buffer,skip_input_data,jpeg_resync_to_restart,term_source}, 0, (const JOCTET *)inputBuffer, inputBufferBytes };
  147. cinfo.src = &src.src;
  148. /* set up error handling. basically C style exceptions :) */
  149. jmp_buf stack_env;
  150. cinfo.client_data = &stack_env;
  151. cinfo.err->error_exit = wasabi_jpgload_error_exit;
  152. if ( setjmp( stack_env ) )
  153. {
  154. // longjmp will goto here
  155. jpeg_destroy_decompress( &cinfo );
  156. return AVI_FAILURE;
  157. }
  158. if ( jpeg_read_header( &cinfo, TRUE ) == JPEG_HEADER_OK )
  159. {
  160. cinfo.out_color_space = JCS_RGB;
  161. jpeg_start_decompress( &cinfo );
  162. int this_image_size = cinfo.output_width * cinfo.output_height * 4;
  163. if ( this_image_size > image_size )
  164. {
  165. image_size = this_image_size;
  166. decoded_image = malloc( image_size );
  167. }
  168. ARGB32 *p = (ARGB32 *)decoded_image;// + (cinfo.output_width * cinfo.output_height);
  169. while ( cinfo.output_scanline < cinfo.output_height )
  170. {
  171. //p -= cinfo.output_width;
  172. jpeg_read_scanlines( &cinfo, (JSAMPARRAY)&p, 1 );
  173. // this is pretty homo, but it has to be done
  174. for ( unsigned int i = 0; i < cinfo.output_width; i++ )
  175. // full alpha shift red to correct place leave green alone shift blue to correct place
  176. p[ i ] = ( 0xff000000 | ( ( p[ i ] & 0x0000ff ) << 16 ) | ( p[ i ] & 0x00ff00 ) | ( ( p[ i ] & 0xff0000 ) >> 16 ) );
  177. p += cinfo.output_width;
  178. }
  179. //if (w) *w = cinfo.output_width;
  180. //if (h) *h = cinfo.output_height;
  181. jpeg_finish_decompress( &cinfo );
  182. }
  183. //jpeg_destroy_decompress(&cinfo);
  184. image_outputted = false;
  185. return AVI_SUCCESS;
  186. }
  187. void AVIMJPEG::Flush()
  188. {
  189. }
  190. int AVIMJPEG::GetPicture( void **data, void **decoder_data )
  191. {
  192. if ( image_outputted )
  193. return AVI_FAILURE;
  194. *data = decoded_image;
  195. *decoder_data = 0;
  196. image_outputted = true;
  197. return AVI_SUCCESS;
  198. }
  199. void AVIMJPEG::FreePicture( void *data, void *decoder_data )
  200. {
  201. }
  202. void AVIMJPEG::Close()
  203. {
  204. delete jpegLoader;
  205. delete this;
  206. }
  207. #define CBCLASS AVIMJPEG
  208. START_DISPATCH;
  209. CB( GET_OUTPUT_PROPERTIES, GetOutputProperties )
  210. CB( DECODE_CHUNK, DecodeChunk )
  211. VCB( FLUSH, Flush )
  212. VCB( CLOSE, Close )
  213. CB( GET_PICTURE, GetPicture )
  214. VCB( FREE_PICTURE, FreePicture )
  215. END_DISPATCH;
  216. #undef CBCLASS