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
|
|
55 static int drop_flag = 0;
|
|
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
|
|
154 static int parse_chunk (vo_functions_t * output, int code, uint8_t * buffer)
|
|
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
|
36
|
166 if (((picture->picture_structure == FRAME_PICTURE) ||
|
1
|
167 (picture->second_field))
|
36
|
168 ) {
|
41
|
169 #ifdef MPEG12_POSTPROC
|
|
170 if(picture->pp_options){
|
|
171 // apply OpenDivX postprocess filter
|
|
172 int stride[3];
|
|
173 stride[0]=picture->coded_picture_width;
|
|
174 stride[1]=stride[2]=stride[0]/2;
|
|
175 postprocess((picture->picture_coding_type == B_TYPE) ?
|
|
176 picture->current_frame->base :
|
|
177 picture->forward_reference_frame->base,
|
|
178 stride[0], frames[3].base, stride[0],
|
|
179 picture->coded_picture_width, picture->coded_picture_height,
|
|
180 &quant_store[1][1], (MBC+1), picture->pp_options);
|
|
181 output->draw_slice (frames[3].base, stride,
|
|
182 picture->display_picture_width,
|
|
183 picture->display_picture_height, 0, 0);
|
79
|
184 }// else
|
41
|
185 #endif
|
79
|
186 #if 0
|
36
|
187 if (picture->picture_coding_type != B_TYPE) {
|
|
188 int stride[3];
|
|
189 stride[0]=picture->coded_picture_width;
|
|
190 stride[1]=stride[2]=stride[0]/2;
|
|
191 output->draw_slice (picture->forward_reference_frame->base,
|
|
192 stride,
|
1
|
193 picture->display_picture_width,
|
|
194 picture->display_picture_height, 0, 0);
|
|
195 }
|
36
|
196 #endif
|
|
197 }
|
1
|
198 #ifdef ARCH_X86
|
36
|
199 if (config.flags & MM_ACCEL_X86_MMX) emms();
|
1
|
200 #endif
|
116
|
201 // output->flip_page();
|
1
|
202 }
|
|
203
|
|
204 switch (code) {
|
|
205 case 0x00: /* picture_start_code */
|
|
206 if (header_process_picture_header (picture, buffer)) {
|
|
207 printf ("bad picture header\n");
|
114
|
208 //exit (1);
|
1
|
209 }
|
|
210
|
|
211 drop_frame = drop_flag && (picture->picture_coding_type == B_TYPE);
|
|
212 //decode_reorder_frames ();
|
|
213 break;
|
|
214
|
|
215 case 0xb3: /* sequence_header_code */
|
|
216 if (header_process_sequence_header (picture, buffer)) {
|
|
217 printf ("bad sequence header\n");
|
114
|
218 //exit (1);
|
1
|
219 }
|
|
220 break;
|
|
221
|
|
222 case 0xb5: /* extension_start_code */
|
|
223 if (header_process_extension (picture, buffer)) {
|
|
224 printf ("bad extension\n");
|
114
|
225 //exit (1);
|
1
|
226 }
|
|
227 break;
|
|
228
|
|
229 default:
|
|
230 // if (code >= 0xb9) printf ("stream not demultiplexed ?\n");
|
|
231 if (code >= 0xb0) break;
|
|
232
|
|
233 if (!(in_slice_flag)) {
|
|
234 in_slice_flag = 1;
|
|
235
|
36
|
236 // if(!(picture->second_field)) decode_reorder_frames ();
|
|
237
|
|
238 // set current_frame pointer:
|
|
239 if (picture->second_field){
|
|
240 // vo_field (picture->current_frame, picture->picture_structure);
|
|
241 } else {
|
|
242 if (picture->picture_coding_type == B_TYPE){
|
79
|
243 picture->display_frame=
|
36
|
244 picture->current_frame = &frames[2];
|
79
|
245 // picture->current_frame->copy=copy_slice;
|
|
246 } else {
|
|
247 picture->current_frame = picture->forward_reference_frame;
|
|
248 picture->display_frame=
|
|
249 picture->forward_reference_frame = picture->backward_reference_frame;
|
|
250 picture->backward_reference_frame = picture->current_frame;
|
|
251 // picture->current_frame->copy=NULL;
|
|
252 }
|
|
253 }
|
|
254
|
|
255 #if 1
|
41
|
256 #ifdef MPEG12_POSTPROC
|
|
257 if(picture->pp_options)
|
|
258 picture->current_frame->copy=NULL; else
|
|
259 #endif
|
36
|
260 picture->current_frame->copy=copy_slice;
|
79
|
261 #endif
|
36
|
262
|
|
263 picture->current_frame->vo=output;
|
79
|
264 picture->slice=0;
|
36
|
265
|
1
|
266 }
|
|
267
|
|
268 if (!drop_frame) {
|
|
269
|
|
270 slice_process (picture, code, buffer);
|
|
271
|
|
272 #ifdef ARCH_X86
|
|
273 if (config.flags & MM_ACCEL_X86_MMX) emms ();
|
|
274 #endif
|
|
275
|
|
276 }
|
|
277 }
|
|
278
|
|
279 return is_frame_done;
|
|
280 }
|
|
281
|
111
|
282 static jmp_buf mpeg2_jmp_buf;
|
|
283
|
|
284 static void mpeg2_sighandler(int sig){
|
|
285 longjmp(mpeg2_jmp_buf,1);
|
|
286 }
|
1
|
287
|
|
288 int mpeg2_decode_data (vo_functions_t *output, uint8_t *current, uint8_t *end)
|
|
289 {
|
|
290 //static uint8_t code = 0xff;
|
|
291 //static uint8_t chunk_buffer[65536];
|
|
292 //static uint8_t *chunk_ptr = chunk_buffer;
|
|
293 //static uint32_t shift = 0;
|
|
294 uint8_t code;
|
|
295 uint8_t *pos=NULL;
|
|
296 uint8_t *start=current;
|
|
297 int ret = 0;
|
111
|
298 void* old_sigh;
|
|
299
|
|
300 if(setjmp(mpeg2_jmp_buf)!=0){
|
|
301 printf("@@@ FATAL!!!??? libmpeg2 returned from sig11 before the actual decoding! @@@\n");
|
|
302 return 0;
|
|
303 }
|
|
304
|
|
305 old_sigh=signal(SIGSEGV,mpeg2_sighandler);
|
1
|
306
|
|
307 // printf("RCVD %d bytes\n",end-current);
|
|
308
|
|
309 while(current<end){
|
|
310 // FIND NEXT HEAD:
|
|
311 unsigned int head=-1;
|
|
312 uint8_t c;
|
|
313 //--------------------
|
|
314 while(current<end){
|
|
315 c=current[0];
|
|
316 ++current;
|
|
317 head<<=8;
|
|
318 if(head==0x100) break; // synced
|
|
319 head|=c;
|
|
320 }
|
|
321 //--------------------
|
|
322 if(pos){
|
|
323 //if((code&0x100)!=0x100) printf("libmpeg2: FATAL! code=%X\n",code);
|
|
324 //printf("pos=%d chunk %3X size=%d next-code=%X\n",pos-start,code,current-pos,head|c);
|
111
|
325 if(setjmp(mpeg2_jmp_buf)==0){
|
|
326 ret+=parse_chunk(output, code&0xFF, pos);
|
|
327 } else {
|
204
|
328 #ifdef ARCH_X86
|
|
329 if (config.flags & MM_ACCEL_X86_MMX) emms ();
|
|
330 #endif
|
499
|
331 printf("@@@ libmpeg2 returned from sig11... (bad file?) @@@\n");
|
111
|
332 }
|
1
|
333 }
|
|
334 //--------------------
|
|
335 pos=current;code=head|c;
|
|
336 }
|
|
337
|
111
|
338 signal(SIGSEGV,old_sigh); // restore sighandler
|
|
339
|
1
|
340 if(code==0x1FF) ret+=parse_chunk(output, 0xFF, NULL); // send 'end of frame'
|
|
341
|
|
342 return ret;
|
|
343 }
|
|
344
|
|
345 void mpeg2_drop (int flag)
|
|
346 {
|
|
347 drop_flag = flag;
|
|
348 }
|
|
349
|