Mercurial > mplayer.hg
annotate libmpcodecs/vd_ijpg.c @ 6406:9bbbcd3a6762
Aii 10l forgot to commit with my last x11_common.c commit
author | atmos4 |
---|---|
date | Thu, 13 Jun 2002 00:26:17 +0000 |
parents | 3e4936b52df4 |
children | 28677d779205 |
rev | line source |
---|---|
5029 | 1 |
2 #include <stdio.h> | |
3 #include <stdlib.h> | |
4 | |
5 #include "config.h" | |
6 #include "mp_msg.h" | |
7 | |
8 #include <jpeglib.h> | |
5048 | 9 #define UINT16 IJPG_UINT16 |
10 #define INT16 IJPG_INT16 | |
5029 | 11 |
12 #include <setjmp.h> | |
13 | |
14 #include "bswap.h" | |
15 #include "postproc/rgb2rgb.h" | |
16 #include "libvo/fastmemcpy.h" | |
17 | |
18 #include "vd_internal.h" | |
19 | |
20 static vd_info_t info = { | |
21 "JPEG Images decoder", | |
22 "ijpg", | |
23 VFM_IJPG, | |
24 "Pontscho", | |
25 "based on vd_mpng.c", | |
26 "uses Indipended JPEG Group's jpeglib" | |
27 }; | |
28 | |
29 LIBVD_EXTERN(ijpg) | |
30 | |
31 static int last_w=-1; | |
32 static int last_h=-1; | |
33 | |
34 // to set/get/query special features/parameters | |
35 static int control(sh_video_t *sh,int cmd,void* arg,...){ | |
36 return CONTROL_UNKNOWN; | |
37 } | |
38 | |
39 // init driver | |
40 static int init(sh_video_t *sh){ | |
41 last_w=-1; | |
42 return 1; | |
43 } | |
44 | |
45 // uninit driver | |
46 static void uninit(sh_video_t *sh){ | |
47 } | |
48 | |
49 //mp_image_t* mpcodecs_get_image(sh_video_t *sh, int mp_imgtype, int mp_imgflag, int w, int h); | |
50 | |
51 typedef struct | |
52 { | |
53 struct jpeg_source_mgr pub; | |
54 unsigned char * inbuf; | |
55 int bufsize; | |
56 } my_source_mgr; | |
57 | |
58 typedef my_source_mgr * my_src_ptr; | |
59 | |
60 METHODDEF(void) init_source (j_decompress_ptr cinfo) | |
61 { | |
62 } | |
63 | |
64 METHODDEF(boolean) fill_input_buffer (j_decompress_ptr cinfo) | |
65 { | |
66 my_src_ptr src = (my_src_ptr) cinfo->src; | |
67 size_t nbytes; | |
68 src->pub.next_input_byte = src->inbuf; | |
69 src->pub.bytes_in_buffer = src->bufsize; | |
70 return TRUE; | |
71 } | |
72 | |
73 METHODDEF(void) skip_input_data (j_decompress_ptr cinfo, long num_bytes) | |
74 { | |
75 my_src_ptr src = (my_src_ptr) cinfo->src; | |
76 | |
77 if (num_bytes > 0) | |
78 { | |
79 while (num_bytes > (long) src->pub.bytes_in_buffer) | |
80 { | |
81 num_bytes -= (long) src->pub.bytes_in_buffer; | |
82 (void) fill_input_buffer(cinfo); | |
83 } | |
84 src->pub.next_input_byte += (size_t) num_bytes; | |
85 src->pub.bytes_in_buffer -= (size_t) num_bytes; | |
86 } | |
87 } | |
88 | |
89 METHODDEF(void) term_source (j_decompress_ptr cinfo) { } | |
90 | |
91 GLOBAL(void) jpeg_buf_src ( j_decompress_ptr cinfo, char * inbuf,int bufsize ) | |
92 { | |
93 my_src_ptr src; | |
94 if (cinfo->src == NULL) cinfo->src=malloc( sizeof( my_source_mgr ) ); | |
95 src = (my_src_ptr) cinfo->src; | |
96 src->pub.init_source = init_source; | |
97 src->pub.fill_input_buffer = fill_input_buffer; | |
98 src->pub.skip_input_data = skip_input_data; | |
99 src->pub.resync_to_restart = jpeg_resync_to_restart; | |
100 src->pub.term_source = term_source; | |
101 src->inbuf = inbuf; | |
102 src->bufsize=bufsize; | |
103 src->pub.bytes_in_buffer = 0; | |
104 src->pub.next_input_byte = NULL; | |
105 } | |
106 | |
107 struct my_error_mgr | |
108 { | |
109 struct jpeg_error_mgr pub; | |
110 jmp_buf setjmp_buffer; | |
111 }; | |
112 | |
113 typedef struct my_error_mgr * my_error_ptr; | |
114 | |
115 METHODDEF(void) my_error_exit (j_common_ptr cinfo) | |
116 { | |
117 my_error_ptr myerr=(my_error_ptr) cinfo->err; | |
118 (*cinfo->err->output_message) (cinfo); | |
119 longjmp(myerr->setjmp_buffer, 1); | |
120 } | |
121 | |
122 static struct jpeg_decompress_struct cinfo; | |
123 static struct my_error_mgr jerr; | |
124 static int row_stride; | |
6097 | 125 static unsigned char *temp_row=NULL; |
126 | |
5029 | 127 // decode a frame |
128 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){ | |
129 mp_image_t * mpi = NULL; | |
6097 | 130 int width,height,depth,i; |
5029 | 131 |
132 if ( len <= 0 ) return NULL; // skipped frame | |
133 | |
5366 | 134 cinfo.err=jpeg_std_error( &jerr.pub ); |
135 jerr.pub.error_exit=my_error_exit; | |
5029 | 136 if( setjmp( jerr.setjmp_buffer ) ) |
137 { | |
138 mp_msg( MSGT_DECVIDEO,MSGL_ERR,"[ijpg] setjmp error ...\n" ); | |
139 return NULL; | |
140 } | |
141 | |
142 jpeg_create_decompress( &cinfo ); | |
143 jpeg_buf_src( &cinfo,data,len ); | |
144 jpeg_read_header( &cinfo,TRUE ); | |
5365
b87743434e1f
Issue a warning in VDec if disp_w and disp_h weren't set by codec and try workaround.
atmos4
parents:
5124
diff
changeset
|
145 sh->disp_w=width=cinfo.image_width; |
b87743434e1f
Issue a warning in VDec if disp_w and disp_h weren't set by codec and try workaround.
atmos4
parents:
5124
diff
changeset
|
146 sh->disp_h=height=cinfo.image_height; |
5029 | 147 jpeg_start_decompress( &cinfo ); |
148 depth=cinfo.output_components * 8; | |
149 | |
150 switch( depth ) { | |
6097 | 151 case 8: |
152 case 24: break; | |
5029 | 153 default: mp_msg( MSGT_DECVIDEO,MSGL_ERR,"Sorry, unsupported JPEG colorspace: %d.\n",depth ); return NULL; |
154 } | |
155 | |
6097 | 156 if ( last_w!=width || last_h!=height ) |
5029 | 157 { |
6097 | 158 if(!mpcodecs_config_vo( sh,width,height, IMGFMT_RGB24 )) return NULL; |
159 if(temp_row) free(temp_row); | |
160 temp_row=malloc(3*width+16); | |
161 last_w=width; last_h=height; | |
5029 | 162 } |
163 | |
164 mpi=mpcodecs_get_image( sh,MP_IMGTYPE_TEMP,MP_IMGFLAG_ACCEPT_STRIDE,width,height ); | |
165 if ( !mpi ) return NULL; | |
166 | |
167 row_stride=cinfo.output_width * cinfo.output_components; | |
168 | |
169 for ( i=0;i < height;i++ ) | |
170 { | |
6097 | 171 unsigned char * drow = mpi->planes[0] + mpi->stride[0] * i; |
172 unsigned char * row = (mpi->imgfmt==IMGFMT_RGB24 && depth==24) ? drow : temp_row; | |
5042 | 173 jpeg_read_scanlines( &cinfo,(JSAMPLE**)&row,1 ); |
6097 | 174 if(depth==8){ |
175 // grayscale -> rgb/bgr 24/32 | |
176 int x; | |
177 if(mpi->bpp==32) | |
178 for(x=0;x<width;x++) drow[4*x]=0x010101*row[x]; | |
179 else | |
180 for(x=0;x<width;x++) drow[3*x+0]=drow[3*x+1]=drow[3*x+2]=row[x]; | |
181 } else { | |
182 int x; | |
183 switch(mpi->imgfmt){ | |
184 // rgb24 -> bgr24 | |
185 case IMGFMT_BGR24: | |
186 for(x=0;x<3*width;x+=3){ | |
187 drow[x+0]=row[x+2]; | |
188 drow[x+1]=row[x+1]; | |
189 drow[x+2]=row[x+0]; | |
190 } | |
191 break; | |
192 // rgb24 -> bgr32 | |
193 case IMGFMT_BGR32: | |
194 for(x=0;x<width;x++){ | |
195 drow[4*x+0]=row[3*x+2]; | |
196 drow[4*x+1]=row[3*x+1]; | |
197 drow[4*x+2]=row[3*x+0]; | |
198 } | |
199 break; | |
200 } | |
201 } | |
5029 | 202 } |
203 | |
204 jpeg_finish_decompress(&cinfo); | |
205 jpeg_destroy_decompress(&cinfo); | |
206 | |
207 return mpi; | |
208 } |