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