4998
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3
|
|
4 #include "config.h"
|
|
5 #include "mp_msg.h"
|
|
6
|
|
7 #include "vd_internal.h"
|
|
8
|
|
9 static vd_info_t info =
|
|
10 {
|
5465
|
11 "MPEG 1/2 Video decoder v2.0",
|
4998
|
12 "libmpeg2",
|
|
13 VFM_MPEG,
|
|
14 "A'rpi",
|
|
15 "Aaron & Walken",
|
|
16 "native"
|
|
17 };
|
|
18
|
|
19 LIBVD_EXTERN(libmpeg2)
|
|
20
|
5465
|
21 #define USE_SIGJMP_TRICK
|
4998
|
22
|
5465
|
23 #ifdef USE_SIGJMP_TRICK
|
|
24 #include <signal.h>
|
|
25 #include <setjmp.h>
|
|
26 #endif
|
|
27
|
|
28 //#include "libmpdemux/parse_es.h"
|
|
29
|
|
30 #include "libvo/video_out.h" // FIXME!!!
|
|
31
|
4998
|
32 #include "libmpeg2/mpeg2.h"
|
|
33 #include "libmpeg2/mpeg2_internal.h"
|
5465
|
34 #include "libmpeg2/mm_accel.h"
|
4998
|
35
|
5465
|
36 #include "../cpudetect.h"
|
|
37
|
|
38 mpeg2_config_t config; // FIXME!!!
|
|
39 static picture_t *picture=NULL; // exported from libmpeg2/decode.c
|
|
40
|
|
41 static int table_init_state=0;
|
|
42
|
4998
|
43 // to set/get/query special features/parameters
|
|
44 static int control(sh_video_t *sh,int cmd,void* arg,...){
|
|
45 return CONTROL_UNKNOWN;
|
|
46 }
|
|
47
|
5465
|
48 static vo_frame_t frames[3];
|
|
49
|
4998
|
50 // init driver
|
|
51 static int init(sh_video_t *sh){
|
5465
|
52
|
|
53 config.flags = 0;
|
|
54 if(gCpuCaps.hasMMX)
|
|
55 config.flags |= MM_ACCEL_X86_MMX;
|
|
56 if(gCpuCaps.hasMMX2)
|
|
57 config.flags |= MM_ACCEL_X86_MMXEXT;
|
|
58 if(gCpuCaps.has3DNow)
|
|
59 config.flags |= MM_ACCEL_X86_3DNOW;
|
|
60 #ifdef HAVE_MLIB
|
|
61 config.flags |= MM_ACCEL_MLIB;
|
|
62 #endif
|
|
63
|
|
64 picture=malloc(sizeof(picture_t)); // !!! NEW HACK :) !!!
|
|
65 memset(picture,0,sizeof(picture_t));
|
|
66 header_state_init (picture);
|
|
67
|
|
68 if(!table_init_state){
|
|
69 idct_init ();
|
|
70 motion_comp_init ();
|
|
71 table_init_state=1;
|
|
72 }
|
|
73
|
5003
|
74 picture->pp_options=divx_quality;
|
5465
|
75
|
|
76 picture->forward_reference_frame=&frames[0];
|
|
77 picture->backward_reference_frame=&frames[1];
|
|
78 picture->temp_frame=&frames[2];
|
|
79 picture->current_frame=NULL;
|
|
80
|
4998
|
81 // send seq header to the decoder: *** HACK ***
|
5465
|
82 // mpeg2_decode_data(NULL,videobuffer,videobuffer+videobuf_len,0);
|
|
83 // mpeg2_allocate_image_buffers (picture);
|
5124
|
84 return mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12);
|
4998
|
85 }
|
|
86
|
|
87 // uninit driver
|
|
88 static void uninit(sh_video_t *sh){
|
5465
|
89 // mpeg2_free_image_buffers (picture);
|
|
90 }
|
|
91
|
|
92 static void draw_slice (vo_frame_t * frame, uint8_t ** src){
|
|
93 vo_functions_t * output = frame->vo;
|
|
94 int stride[3];
|
|
95 int y=picture->slice<<4;
|
|
96
|
|
97 stride[0]=picture->coded_picture_width;
|
|
98 stride[1]=stride[2]=stride[0]/2;
|
|
99
|
|
100 output->draw_slice (src,
|
|
101 stride, picture->display_picture_width,
|
|
102 (y+16<=picture->display_picture_height) ? 16 :
|
|
103 picture->display_picture_height-y,
|
|
104 0, y);
|
|
105
|
|
106 ++picture->slice;
|
4998
|
107 }
|
|
108
|
5465
|
109 static int in_slice_flag=0; // FIXME! move to picture struct
|
|
110 static int drop_frame=0; // FIXME! move to picture struct
|
|
111
|
|
112 static mp_image_t* parse_chunk (sh_video_t* sh, int code, uint8_t * buffer, int framedrop){
|
|
113 mp_image_t* mpi=NULL;
|
|
114
|
|
115 // stats_header (code, buffer);
|
|
116
|
|
117 if (in_slice_flag && ((!code) || (code >= 0xb0))) {
|
|
118 // ok, we've completed decoding a frame/field!
|
|
119 in_slice_flag = 0;
|
|
120 mpi=picture->display_frame->mpi;
|
|
121 if(picture->picture_structure!=FRAME_PICTURE && !picture->second_field)
|
|
122 mpi=NULL; // we don't draw first fields!
|
|
123 }
|
|
124
|
|
125 switch (code) {
|
|
126 case 0x00: /* picture_start_code */
|
|
127 if (header_process_picture_header (picture, buffer)) {
|
|
128 printf ("bad picture header\n");
|
|
129 }
|
|
130 drop_frame = framedrop && (picture->picture_coding_type == B_TYPE);
|
|
131 drop_frame |= framedrop>=2; // hard drop
|
|
132 break;
|
|
133
|
|
134 case 0xb3: /* sequence_header_code */
|
|
135 if (header_process_sequence_header (picture, buffer)) {
|
|
136 printf ("bad sequence header\n");
|
|
137 }
|
|
138 break;
|
|
139
|
|
140 case 0xb5: /* extension_start_code */
|
|
141 if (header_process_extension (picture, buffer)) {
|
|
142 printf ("bad extension\n");
|
|
143 }
|
|
144 break;
|
|
145
|
|
146 default:
|
|
147 if (code >= 0xb0) break;
|
|
148
|
|
149 if (!in_slice_flag) {
|
|
150 in_slice_flag = 1;
|
|
151
|
|
152 // set current_frame pointer:
|
|
153 if (!picture->second_field){
|
|
154 mp_image_t* mpi;
|
|
155 int flags;
|
|
156 if (picture->picture_coding_type == B_TYPE){
|
5485
|
157 flags=vd_use_slices?MP_IMGFLAG_DRAW_CALLBACK:0;
|
5465
|
158 picture->display_frame=
|
|
159 picture->current_frame = picture->temp_frame;
|
|
160 } else {
|
|
161 flags=MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE;
|
|
162 picture->current_frame = picture->forward_reference_frame;
|
|
163 picture->display_frame=
|
|
164 picture->forward_reference_frame = picture->backward_reference_frame;
|
|
165 picture->backward_reference_frame = picture->current_frame;
|
|
166 }
|
|
167 mpi=mpcodecs_get_image(sh,MP_IMGTYPE_IPB, flags,
|
|
168 picture->coded_picture_width,
|
|
169 picture->coded_picture_height);
|
|
170 // ok, lets see what did we get:
|
|
171 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK &&
|
|
172 !(mpi->flags&MP_IMGFLAG_DIRECT)){
|
|
173 // nice, filter/vo likes draw_callback :)
|
|
174 picture->current_frame->copy=draw_slice;
|
|
175 } else
|
|
176 picture->current_frame->copy=NULL;
|
|
177 // let's, setup pointers!
|
|
178 picture->current_frame->base[0]=mpi->planes[0];
|
|
179 picture->current_frame->base[1]=mpi->planes[1];
|
|
180 picture->current_frame->base[2]=mpi->planes[2];
|
|
181 picture->current_frame->mpi=mpi; // tricky!
|
5516
|
182 #ifdef MPEG12_POSTPROC
|
5515
|
183 mpi->qscale=&picture->current_frame->quant_store[1][1];
|
|
184 mpi->qstride=(MPEG2_MBC+1);
|
5516
|
185 #endif
|
5465
|
186 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"mpeg2: [%c] %p %s \n",
|
|
187 (picture->picture_coding_type == B_TYPE) ? 'B':'P',
|
|
188 mpi, (mpi->flags&MP_IMGFLAG_DIRECT)?"DR!":"");
|
|
189 }
|
|
190
|
|
191 picture->current_frame->vo=sh->video_out;
|
|
192 picture->slice=0;
|
|
193
|
|
194 }
|
|
195
|
|
196 if (!drop_frame) {
|
|
197 slice_process (picture, code, buffer);
|
|
198 #ifdef ARCH_X86
|
|
199 if (config.flags & MM_ACCEL_X86_MMX) __asm__ __volatile__ ("emms");
|
|
200 #endif
|
|
201 }
|
|
202
|
5145
|
203 }
|
4998
|
204 return mpi;
|
|
205 }
|
|
206
|
5465
|
207 #ifdef USE_SIGJMP_TRICK
|
|
208
|
|
209 static jmp_buf mpeg2_jmp_buf;
|
|
210
|
|
211 static void mpeg2_sighandler(int sig){
|
|
212 longjmp(mpeg2_jmp_buf,1);
|
|
213 }
|
|
214 #endif
|
|
215
|
|
216 // decode a frame
|
|
217 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
|
|
218 static uint32_t code;
|
|
219 static uint8_t* pos;
|
|
220 static uint8_t* current;
|
|
221 uint8_t* end=data+len;
|
|
222 static mp_image_t* mpi;
|
|
223 mp_image_t* ret=NULL;
|
|
224 int framedrop=flags&3;
|
|
225 void* old_sigh;
|
|
226
|
|
227 // Note: static is REQUIRED because of longjmp() may destroy stack!
|
|
228 pos=NULL;
|
|
229 current=data;
|
|
230 mpi=NULL;
|
|
231
|
|
232 #ifdef USE_SIGJMP_TRICK
|
|
233 old_sigh=signal(SIGSEGV,mpeg2_sighandler);
|
|
234 #endif
|
|
235
|
|
236 while(current<end){
|
|
237 // FIND NEXT HEAD:
|
|
238 static unsigned int head;
|
|
239 static uint8_t c;
|
|
240 head=-1;
|
|
241 //--------------------
|
|
242 while(current<end){
|
|
243 c=current[0];
|
|
244 ++current;
|
|
245 head<<=8;
|
|
246 if(head==0x100) break; // synced
|
|
247 head|=c;
|
|
248 }
|
|
249 //--------------------
|
|
250 if(pos){
|
|
251 #ifdef USE_SIGJMP_TRICK
|
|
252 if(setjmp(mpeg2_jmp_buf)){
|
|
253 #ifdef ARCH_X86
|
|
254 if (config.flags & MM_ACCEL_X86_MMX) __asm__ __volatile__ ("emms");
|
|
255 #endif
|
|
256 printf("@@@ libmpeg2 returned from sig11... (bad file?) @@@\n");
|
|
257 } else
|
|
258 #endif
|
|
259 {
|
|
260 ret=parse_chunk(sh, code&0xFF, pos, framedrop);
|
|
261 if(ret) mpi=ret;
|
|
262 }
|
|
263 }
|
|
264 //--------------------
|
|
265 pos=current;code=head|c;
|
|
266 }
|
|
267
|
|
268 #ifdef USE_SIGJMP_TRICK
|
|
269 signal(SIGSEGV,old_sigh); // restore sighandler
|
|
270 #endif
|
|
271
|
|
272 if(code==0x1FF){
|
|
273 ret=parse_chunk(sh, 0xFF, NULL, framedrop); // send 'end of frame'
|
|
274 if(ret) mpi=ret;
|
|
275 }
|
|
276
|
|
277 return mpi;
|
|
278 }
|