5029
|
1
|
|
2 #include <stdio.h>
|
|
3 #include <stdlib.h>
|
|
4
|
|
5 #include "config.h"
|
|
6 #include "mp_msg.h"
|
|
7
|
|
8 #include <jconfig.h>
|
|
9 #include <jmorecfg.h>
|
|
10 #include <jpeglib.h>
|
|
11 #include <jpegint.h>
|
|
12 #include <jerror.h>
|
|
13
|
|
14 #include <setjmp.h>
|
|
15
|
|
16 #include "bswap.h"
|
|
17 #include "postproc/rgb2rgb.h"
|
|
18 #include "libvo/fastmemcpy.h"
|
|
19
|
|
20 #include "vd_internal.h"
|
|
21
|
|
22 static vd_info_t info = {
|
|
23 "JPEG Images decoder",
|
|
24 "ijpg",
|
|
25 VFM_IJPG,
|
|
26 "Pontscho",
|
|
27 "based on vd_mpng.c",
|
|
28 "uses Indipended JPEG Group's jpeglib"
|
|
29 };
|
|
30
|
|
31 LIBVD_EXTERN(ijpg)
|
|
32
|
|
33 static unsigned int out_fmt=0;
|
|
34
|
|
35 static int last_w=-1;
|
|
36 static int last_h=-1;
|
|
37 static int last_c=-1;
|
|
38
|
|
39 // to set/get/query special features/parameters
|
|
40 static int control(sh_video_t *sh,int cmd,void* arg,...){
|
|
41 return CONTROL_UNKNOWN;
|
|
42 }
|
|
43
|
|
44 // init driver
|
|
45 static int init(sh_video_t *sh){
|
|
46 last_w=-1;
|
|
47 return 1;
|
|
48 }
|
|
49
|
|
50 // uninit driver
|
|
51 static void uninit(sh_video_t *sh){
|
|
52 }
|
|
53
|
|
54 //mp_image_t* mpcodecs_get_image(sh_video_t *sh, int mp_imgtype, int mp_imgflag, int w, int h);
|
|
55
|
|
56 typedef struct
|
|
57 {
|
|
58 struct jpeg_source_mgr pub;
|
|
59 unsigned char * inbuf;
|
|
60 int bufsize;
|
|
61 } my_source_mgr;
|
|
62
|
|
63 typedef my_source_mgr * my_src_ptr;
|
|
64
|
|
65 METHODDEF(void) init_source (j_decompress_ptr cinfo)
|
|
66 {
|
|
67 }
|
|
68
|
|
69 METHODDEF(boolean) fill_input_buffer (j_decompress_ptr cinfo)
|
|
70 {
|
|
71 my_src_ptr src = (my_src_ptr) cinfo->src;
|
|
72 size_t nbytes;
|
|
73 src->pub.next_input_byte = src->inbuf;
|
|
74 src->pub.bytes_in_buffer = src->bufsize;
|
|
75 return TRUE;
|
|
76 }
|
|
77
|
|
78 METHODDEF(void) skip_input_data (j_decompress_ptr cinfo, long num_bytes)
|
|
79 {
|
|
80 my_src_ptr src = (my_src_ptr) cinfo->src;
|
|
81
|
|
82 if (num_bytes > 0)
|
|
83 {
|
|
84 while (num_bytes > (long) src->pub.bytes_in_buffer)
|
|
85 {
|
|
86 num_bytes -= (long) src->pub.bytes_in_buffer;
|
|
87 (void) fill_input_buffer(cinfo);
|
|
88 }
|
|
89 src->pub.next_input_byte += (size_t) num_bytes;
|
|
90 src->pub.bytes_in_buffer -= (size_t) num_bytes;
|
|
91 }
|
|
92 }
|
|
93
|
|
94 METHODDEF(void) term_source (j_decompress_ptr cinfo) { }
|
|
95
|
|
96 GLOBAL(void) jpeg_buf_src ( j_decompress_ptr cinfo, char * inbuf,int bufsize )
|
|
97 {
|
|
98 my_src_ptr src;
|
|
99 if (cinfo->src == NULL) cinfo->src=malloc( sizeof( my_source_mgr ) );
|
|
100 src = (my_src_ptr) cinfo->src;
|
|
101 src->pub.init_source = init_source;
|
|
102 src->pub.fill_input_buffer = fill_input_buffer;
|
|
103 src->pub.skip_input_data = skip_input_data;
|
|
104 src->pub.resync_to_restart = jpeg_resync_to_restart;
|
|
105 src->pub.term_source = term_source;
|
|
106 src->inbuf = inbuf;
|
|
107 src->bufsize=bufsize;
|
|
108 src->pub.bytes_in_buffer = 0;
|
|
109 src->pub.next_input_byte = NULL;
|
|
110 }
|
|
111
|
|
112 struct my_error_mgr
|
|
113 {
|
|
114 struct jpeg_error_mgr pub;
|
|
115 jmp_buf setjmp_buffer;
|
|
116 };
|
|
117
|
|
118 typedef struct my_error_mgr * my_error_ptr;
|
|
119
|
|
120 METHODDEF(void) my_error_exit (j_common_ptr cinfo)
|
|
121 {
|
|
122 my_error_ptr myerr=(my_error_ptr) cinfo->err;
|
|
123 (*cinfo->err->output_message) (cinfo);
|
|
124 longjmp(myerr->setjmp_buffer, 1);
|
|
125 }
|
|
126
|
|
127 static struct jpeg_decompress_struct cinfo;
|
|
128 static struct my_error_mgr jerr;
|
|
129 static int row_stride;
|
|
130 static int count;
|
|
131
|
|
132 // decode a frame
|
|
133 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
|
|
134 mp_image_t * mpi = NULL;
|
|
135 int width,height,depth,i,j;
|
|
136
|
|
137 if ( len <= 0 ) return NULL; // skipped frame
|
|
138
|
|
139 cinfo.err=jpeg_std_error( &jerr.pub );
|
|
140 jerr.pub.error_exit=my_error_exit;
|
|
141 if( setjmp( jerr.setjmp_buffer ) )
|
|
142 {
|
|
143 mp_msg( MSGT_DECVIDEO,MSGL_ERR,"[ijpg] setjmp error ...\n" );
|
|
144 return NULL;
|
|
145 }
|
|
146
|
|
147 jpeg_create_decompress( &cinfo );
|
|
148 jpeg_buf_src( &cinfo,data,len );
|
|
149 jpeg_read_header( &cinfo,TRUE );
|
|
150 width=cinfo.image_width;
|
|
151 height=cinfo.image_height;
|
|
152 jpeg_start_decompress( &cinfo );
|
|
153 depth=cinfo.output_components * 8;
|
|
154
|
|
155 switch( depth ) {
|
|
156 case 8: out_fmt=IMGFMT_BGR8; break;
|
|
157 case 24: out_fmt=IMGFMT_BGR24; break;
|
|
158 default: mp_msg( MSGT_DECVIDEO,MSGL_ERR,"Sorry, unsupported JPEG colorspace: %d.\n",depth ); return NULL;
|
|
159 }
|
|
160
|
|
161 if ( last_w!=width || last_h!=height || last_c!=out_fmt )
|
|
162 {
|
|
163 last_w=width; last_h=height; last_c=out_fmt;
|
|
164 if ( !out_fmt ) return NULL;
|
|
165 mpcodecs_config_vo( sh,width,height,out_fmt );
|
|
166 }
|
|
167
|
|
168 mpi=mpcodecs_get_image( sh,MP_IMGTYPE_TEMP,MP_IMGFLAG_ACCEPT_STRIDE,width,height );
|
|
169 if ( !mpi ) return NULL;
|
|
170
|
|
171 row_stride=cinfo.output_width * cinfo.output_components;
|
|
172
|
|
173 for ( i=0;i < height;i++ )
|
|
174 {
|
|
175 char * row = mpi->planes[0] + mpi->stride[0] * i;
|
|
176 jpeg_read_scanlines( &cinfo,&row,1 );
|
|
177 #warning workaround for rgb2bgr
|
|
178 if ( depth == 24 )
|
|
179 for ( j=0;j < width * 3;j+=3 )
|
|
180 {
|
|
181 char c;
|
|
182 c=row[j];
|
|
183 row[j]=row[j+2];
|
|
184 row[j+2]=c;
|
|
185 }
|
|
186 }
|
|
187
|
|
188 jpeg_finish_decompress(&cinfo);
|
|
189 jpeg_destroy_decompress(&cinfo);
|
|
190
|
|
191 return mpi;
|
|
192 }
|