1
|
1 /* Copyright (C) Aaron Holtzman <aholtzma@ess.engr.uvic.ca> - Nov 1999 */
|
|
2 /* Some cleanup & hacking by A'rpi/ESP-team - Oct 2000 */
|
|
3
|
|
4 /* mpeg2dec version: */
|
|
5 #define PACKAGE "mpeg2dec"
|
36
|
6 #define VERSION "0.2.0-release"
|
1
|
7
|
|
8 #include <stdio.h>
|
|
9 #include <stdlib.h>
|
|
10 #include <unistd.h>
|
|
11 #include <fcntl.h>
|
|
12 #include <errno.h>
|
|
13
|
111
|
14 #include <signal.h>
|
|
15 #include <setjmp.h>
|
|
16
|
|
17
|
1
|
18 #include "config.h"
|
|
19
|
36
|
20 #include "video_out.h"
|
|
21 #include <inttypes.h>
|
1
|
22
|
|
23 #include "mpeg2.h"
|
|
24 #include "mpeg2_internal.h"
|
|
25
|
|
26 #include "../linux/shmem.h"
|
|
27
|
|
28 //#include "motion_comp.h"
|
|
29 //#include "idct.h"
|
|
30 //#include "header.h"
|
|
31 //#include "slice.h"
|
|
32 //#include "stats.h"
|
|
33
|
|
34 #include "attributes.h"
|
|
35 #ifdef __i386__
|
|
36 #include "mmx.h"
|
|
37 #endif
|
|
38
|
36
|
39 #include "mm_accel.h"
|
|
40
|
|
41
|
1
|
42 //this is where we keep the state of the decoder
|
|
43 //picture_t picture_data;
|
|
44 //picture_t *picture=&picture_data;
|
|
45 picture_t *picture=NULL;
|
|
46
|
|
47 //global config struct
|
|
48 mpeg2_config_t config;
|
|
49
|
|
50 // the maximum chunk size is determined by vbv_buffer_size which is 224K for
|
|
51 // MP@ML streams. (we make no pretenses ofdecoding anything more than that)
|
|
52 //static uint8_t chunk_buffer[224 * 1024 + 4];
|
|
53 //static uint32_t shift = 0;
|
|
54
|
967
|
55 //static int drop_flag = 0;
|
1
|
56 static int drop_frame = 0;
|
|
57
|
41
|
58 #ifdef MPEG12_POSTPROC
|
1
|
59 int quant_store[MBR+1][MBC+1]; // [Review]
|
36
|
60 #endif
|
1
|
61
|
|
62 void mpeg2_init (void)
|
|
63 {
|
|
64
|
499
|
65 printf (PACKAGE"-"VERSION" (C) 2000-2001 Aaron Holtzman & Michel Lespinasse\n");
|
1
|
66 config.flags = 0;
|
|
67 #ifdef HAVE_MMX
|
|
68 config.flags |= MM_ACCEL_X86_MMX;
|
|
69 #endif
|
|
70 #ifdef HAVE_SSE
|
|
71 config.flags |= MM_ACCEL_X86_MMXEXT;
|
|
72 #endif
|
|
73 #ifdef HAVE_3DNOW
|
|
74 config.flags |= MM_ACCEL_X86_3DNOW;
|
|
75 #endif
|
|
76 #ifdef HAVE_MLIB
|
|
77 config.flags |= MM_ACCEL_MLIB;
|
|
78 #endif
|
|
79
|
499
|
80 // printf("libmpeg2 config flags = 0x%X\n",config.flags);
|
1
|
81
|
|
82 picture=shmem_alloc(sizeof(picture_t)); // !!! NEW HACK :) !!!
|
|
83
|
|
84 header_state_init (picture);
|
36
|
85 // picture->repeat_count=0;
|
1
|
86
|
|
87 picture->pp_options=0;
|
|
88
|
|
89 idct_init ();
|
|
90 motion_comp_init ();
|
|
91 }
|
|
92
|
41
|
93 static vo_frame_t frames[4];
|
36
|
94
|
1
|
95 void mpeg2_allocate_image_buffers (picture_t * picture)
|
|
96 {
|
|
97 int frame_size,buff_size;
|
|
98 unsigned char *base=NULL;
|
36
|
99 int i;
|
1
|
100
|
|
101 // height+1 requires for yuv2rgb_mmx code (it reads next line after last)
|
|
102 frame_size = picture->coded_picture_width * (1+picture->coded_picture_height);
|
|
103 frame_size = (frame_size+31)&(~31); // align to 32 byte boundary
|
|
104 buff_size = frame_size + (frame_size/4)*2; // 4Y + 1U + 1V
|
|
105
|
|
106 // allocate images in YV12 format
|
41
|
107 #ifdef MPEG12_POSTPROC
|
|
108 for(i=0;i<4;i++){
|
|
109 #else
|
36
|
110 for(i=0;i<3;i++){
|
41
|
111 #endif
|
36
|
112 base = shmem_alloc(buff_size);
|
|
113 frames[i].base[0] = base;
|
|
114 frames[i].base[1] = base + frame_size * 5 / 4;
|
|
115 frames[i].base[2] = base + frame_size;
|
|
116 frames[i].copy = NULL;
|
|
117 frames[i].vo = NULL;
|
|
118 }
|
|
119
|
|
120 picture->forward_reference_frame=&frames[0];
|
|
121 picture->backward_reference_frame=&frames[1];
|
|
122 picture->current_frame=&frames[2];
|
1
|
123
|
|
124 }
|
|
125
|
36
|
126 static void copy_slice (vo_frame_t * frame, uint8_t ** src){
|
|
127 vo_functions_t * output = frame->vo;
|
|
128 int stride[3];
|
79
|
129 int y=picture->slice<<4;
|
|
130 uint8_t* src_tmp[3];
|
1
|
131
|
36
|
132 stride[0]=picture->coded_picture_width;
|
|
133 stride[1]=stride[2]=stride[0]/2;
|
79
|
134
|
|
135 if(frame!=picture->display_frame){
|
|
136 uint8_t** base=picture->display_frame->base;
|
|
137 src_tmp[0]=base[0]+stride[0]*y;
|
|
138 src_tmp[1]=base[1]+stride[1]*(y>>1);
|
|
139 src_tmp[2]=base[2]+stride[2]*(y>>1);
|
|
140 src=src_tmp;
|
|
141 }
|
|
142
|
|
143 output->draw_slice (src,
|
|
144 stride, picture->display_picture_width,
|
36
|
145 (y+16<=picture->display_picture_height) ? 16 :
|
|
146 picture->display_picture_height-y,
|
|
147 0, y);
|
1
|
148
|
79
|
149 ++picture->slice;
|
1
|
150 }
|
|
151
|
|
152 static int in_slice_flag=0;
|
|
153
|
967
|
154 static int parse_chunk (vo_functions_t * output, int code, uint8_t * buffer, int framedrop)
|
1
|
155 {
|
|
156 int is_frame_done = 0;
|
|
157
|
|
158 stats_header (code, buffer);
|
|
159
|
|
160 is_frame_done = in_slice_flag && ((!code) || (code >= 0xb0));
|
|
161 if (is_frame_done) {
|
|
162 in_slice_flag = 0;
|
|
163
|
36
|
164 // if(picture->picture_structure != FRAME_PICTURE) printf("Field! %d \n",picture->second_field);
|
1
|
165
|
967
|
166 if(!framedrop)
|
36
|
167 if (((picture->picture_structure == FRAME_PICTURE) ||
|
1
|
168 (picture->second_field))
|
36
|
169 ) {
|
41
|
170 #ifdef MPEG12_POSTPROC
|
|
171 if(picture->pp_options){
|
|
172 // apply OpenDivX postprocess filter
|
|
173 int stride[3];
|
|
174 stride[0]=picture->coded_picture_width;
|
|
175 stride[1]=stride[2]=stride[0]/2;
|
|
176 postprocess((picture->picture_coding_type == B_TYPE) ?
|
|
177 picture->current_frame->base :
|
|
178 picture->forward_reference_frame->base,
|
|
179 stride[0], frames[3].base, stride[0],
|
|
180 picture->coded_picture_width, picture->coded_picture_height,
|
|
181 &quant_store[1][1], (MBC+1), picture->pp_options);
|
|
182 output->draw_slice (frames[3].base, stride,
|
|
183 picture->display_picture_width,
|
|
184 picture->display_picture_height, 0, 0);
|
79
|
185 }// else
|
41
|
186 #endif
|
79
|
187 #if 0
|
36
|
188 if (picture->picture_coding_type != B_TYPE) {
|
|
189 int stride[3];
|
|
190 stride[0]=picture->coded_picture_width;
|
|
191 stride[1]=stride[2]=stride[0]/2;
|
|
192 output->draw_slice (picture->forward_reference_frame->base,
|
|
193 stride,
|
1
|
194 picture->display_picture_width,
|
|
195 picture->display_picture_height, 0, 0);
|
|
196 }
|
36
|
197 #endif
|
|
198 }
|
1
|
199 #ifdef ARCH_X86
|
36
|
200 if (config.flags & MM_ACCEL_X86_MMX) emms();
|
1
|
201 #endif
|
116
|
202 // output->flip_page();
|
1
|
203 }
|
|
204
|
|
205 switch (code) {
|
|
206 case 0x00: /* picture_start_code */
|
|
207 if (header_process_picture_header (picture, buffer)) {
|
|
208 printf ("bad picture header\n");
|
114
|
209 //exit (1);
|
1
|
210 }
|
|
211
|
967
|
212 drop_frame = framedrop && (picture->picture_coding_type == B_TYPE);
|
|
213 drop_frame |= framedrop>=2; // hard drop
|
1
|
214 //decode_reorder_frames ();
|
|
215 break;
|
|
216
|
|
217 case 0xb3: /* sequence_header_code */
|
|
218 if (header_process_sequence_header (picture, buffer)) {
|
|
219 printf ("bad sequence header\n");
|
114
|
220 //exit (1);
|
1
|
221 }
|
|
222 break;
|
|
223
|
|
224 case 0xb5: /* extension_start_code */
|
|
225 if (header_process_extension (picture, buffer)) {
|
|
226 printf ("bad extension\n");
|
114
|
227 //exit (1);
|
1
|
228 }
|
|
229 break;
|
|
230
|
|
231 default:
|
|
232 // if (code >= 0xb9) printf ("stream not demultiplexed ?\n");
|
|
233 if (code >= 0xb0) break;
|
|
234
|
|
235 if (!(in_slice_flag)) {
|
|
236 in_slice_flag = 1;
|
|
237
|
36
|
238 // if(!(picture->second_field)) decode_reorder_frames ();
|
|
239
|
|
240 // set current_frame pointer:
|
|
241 if (picture->second_field){
|
|
242 // vo_field (picture->current_frame, picture->picture_structure);
|
|
243 } else {
|
|
244 if (picture->picture_coding_type == B_TYPE){
|
79
|
245 picture->display_frame=
|
36
|
246 picture->current_frame = &frames[2];
|
79
|
247 // picture->current_frame->copy=copy_slice;
|
|
248 } else {
|
|
249 picture->current_frame = picture->forward_reference_frame;
|
|
250 picture->display_frame=
|
|
251 picture->forward_reference_frame = picture->backward_reference_frame;
|
|
252 picture->backward_reference_frame = picture->current_frame;
|
|
253 // picture->current_frame->copy=NULL;
|
|
254 }
|
|
255 }
|
|
256
|
|
257 #if 1
|
41
|
258 #ifdef MPEG12_POSTPROC
|
|
259 if(picture->pp_options)
|
|
260 picture->current_frame->copy=NULL; else
|
|
261 #endif
|
36
|
262 picture->current_frame->copy=copy_slice;
|
79
|
263 #endif
|
967
|
264 if(framedrop) picture->current_frame->copy=NULL;
|
36
|
265 picture->current_frame->vo=output;
|
79
|
266 picture->slice=0;
|
36
|
267
|
1
|
268 }
|
|
269
|
|
270 if (!drop_frame) {
|
|
271
|
|
272 slice_process (picture, code, buffer);
|
|
273
|
|
274 #ifdef ARCH_X86
|
|
275 if (config.flags & MM_ACCEL_X86_MMX) emms ();
|
|
276 #endif
|
|
277
|
|
278 }
|
|
279 }
|
|
280
|
|
281 return is_frame_done;
|
|
282 }
|
|
283
|
111
|
284 static jmp_buf mpeg2_jmp_buf;
|
|
285
|
|
286 static void mpeg2_sighandler(int sig){
|
|
287 longjmp(mpeg2_jmp_buf,1);
|
|
288 }
|
1
|
289
|
967
|
290 int mpeg2_decode_data (vo_functions_t *output, uint8_t *current, uint8_t *end,int framedrop)
|
1
|
291 {
|
|
292 //static uint8_t code = 0xff;
|
|
293 //static uint8_t chunk_buffer[65536];
|
|
294 //static uint8_t *chunk_ptr = chunk_buffer;
|
|
295 //static uint32_t shift = 0;
|
|
296 uint8_t code;
|
|
297 uint8_t *pos=NULL;
|
|
298 uint8_t *start=current;
|
|
299 int ret = 0;
|
111
|
300 void* old_sigh;
|
|
301
|
|
302 if(setjmp(mpeg2_jmp_buf)!=0){
|
|
303 printf("@@@ FATAL!!!??? libmpeg2 returned from sig11 before the actual decoding! @@@\n");
|
|
304 return 0;
|
|
305 }
|
|
306
|
|
307 old_sigh=signal(SIGSEGV,mpeg2_sighandler);
|
1
|
308
|
|
309 // printf("RCVD %d bytes\n",end-current);
|
|
310
|
|
311 while(current<end){
|
|
312 // FIND NEXT HEAD:
|
|
313 unsigned int head=-1;
|
|
314 uint8_t c;
|
|
315 //--------------------
|
|
316 while(current<end){
|
|
317 c=current[0];
|
|
318 ++current;
|
|
319 head<<=8;
|
|
320 if(head==0x100) break; // synced
|
|
321 head|=c;
|
|
322 }
|
|
323 //--------------------
|
|
324 if(pos){
|
|
325 //if((code&0x100)!=0x100) printf("libmpeg2: FATAL! code=%X\n",code);
|
|
326 //printf("pos=%d chunk %3X size=%d next-code=%X\n",pos-start,code,current-pos,head|c);
|
111
|
327 if(setjmp(mpeg2_jmp_buf)==0){
|
967
|
328 ret+=parse_chunk(output, code&0xFF, pos, framedrop);
|
111
|
329 } else {
|
204
|
330 #ifdef ARCH_X86
|
|
331 if (config.flags & MM_ACCEL_X86_MMX) emms ();
|
|
332 #endif
|
499
|
333 printf("@@@ libmpeg2 returned from sig11... (bad file?) @@@\n");
|
111
|
334 }
|
1
|
335 }
|
|
336 //--------------------
|
|
337 pos=current;code=head|c;
|
|
338 }
|
|
339
|
111
|
340 signal(SIGSEGV,old_sigh); // restore sighandler
|
|
341
|
967
|
342 if(code==0x1FF) ret+=parse_chunk(output, 0xFF, NULL, framedrop); // send 'end of frame'
|
1
|
343
|
|
344 return ret;
|
|
345 }
|
|
346
|
967
|
347 //void mpeg2_drop (int flag)
|
|
348 //{
|
|
349 // drop_flag = flag;
|
|
350 //}
|
1
|
351
|