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