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
|
36
|
54 #ifdef 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
|
36
|
89 static vo_frame_t frames[3];
|
|
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
|
36
|
103 for(i=0;i<3;i++){
|
|
104 base = shmem_alloc(buff_size);
|
|
105 frames[i].base[0] = base;
|
|
106 frames[i].base[1] = base + frame_size * 5 / 4;
|
|
107 frames[i].base[2] = base + frame_size;
|
|
108 frames[i].copy = NULL;
|
|
109 frames[i].vo = NULL;
|
|
110 frames[i].slice=0;
|
|
111 }
|
|
112
|
|
113 picture->forward_reference_frame=&frames[0];
|
|
114 picture->backward_reference_frame=&frames[1];
|
|
115 picture->current_frame=&frames[2];
|
1
|
116
|
36
|
117 #ifdef POSTPROC
|
1
|
118 base = shmem_alloc(buff_size);
|
|
119 picture->pp_frame[0] = base;
|
|
120 picture->pp_frame[1] = base + frame_size * 5 / 4;
|
|
121 picture->pp_frame[2] = base + frame_size;
|
36
|
122 #endif
|
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];
|
|
129 int y=frame->slice*16;
|
1
|
130
|
36
|
131 stride[0]=picture->coded_picture_width;
|
|
132 stride[1]=stride[2]=stride[0]/2;
|
1
|
133
|
36
|
134 output->draw_slice (src, stride,
|
|
135 picture->display_picture_width,
|
|
136 (y+16<=picture->display_picture_height) ? 16 :
|
|
137 picture->display_picture_height-y,
|
|
138 0, y);
|
1
|
139
|
36
|
140 ++frame->slice;
|
1
|
141 }
|
|
142
|
|
143 static int in_slice_flag=0;
|
|
144
|
|
145 static int parse_chunk (vo_functions_t * output, int code, uint8_t * buffer)
|
|
146 {
|
|
147 int is_frame_done = 0;
|
|
148
|
|
149 stats_header (code, buffer);
|
|
150
|
|
151 is_frame_done = in_slice_flag && ((!code) || (code >= 0xb0));
|
|
152 if (is_frame_done) {
|
|
153 in_slice_flag = 0;
|
|
154
|
36
|
155 // if(picture->picture_structure != FRAME_PICTURE) printf("Field! %d \n",picture->second_field);
|
1
|
156
|
36
|
157 if (((picture->picture_structure == FRAME_PICTURE) ||
|
1
|
158 (picture->second_field))
|
36
|
159 ) {
|
|
160 #if 1
|
|
161 if (picture->picture_coding_type != B_TYPE) {
|
|
162 int stride[3];
|
|
163 stride[0]=picture->coded_picture_width;
|
|
164 stride[1]=stride[2]=stride[0]/2;
|
|
165 output->draw_slice (picture->forward_reference_frame->base,
|
|
166 stride,
|
1
|
167 picture->display_picture_width,
|
|
168 picture->display_picture_height, 0, 0);
|
|
169 }
|
36
|
170 #endif
|
|
171 }
|
1
|
172 #ifdef ARCH_X86
|
36
|
173 if (config.flags & MM_ACCEL_X86_MMX) emms();
|
1
|
174 #endif
|
36
|
175 output->flip_page();
|
1
|
176 }
|
|
177
|
|
178 switch (code) {
|
|
179 case 0x00: /* picture_start_code */
|
|
180 if (header_process_picture_header (picture, buffer)) {
|
|
181 printf ("bad picture header\n");
|
|
182 exit (1);
|
|
183 }
|
|
184
|
|
185 drop_frame = drop_flag && (picture->picture_coding_type == B_TYPE);
|
|
186 //decode_reorder_frames ();
|
|
187 break;
|
|
188
|
|
189 case 0xb3: /* sequence_header_code */
|
|
190 if (header_process_sequence_header (picture, buffer)) {
|
|
191 printf ("bad sequence header\n");
|
|
192 exit (1);
|
|
193 }
|
|
194 break;
|
|
195
|
|
196 case 0xb5: /* extension_start_code */
|
|
197 if (header_process_extension (picture, buffer)) {
|
|
198 printf ("bad extension\n");
|
|
199 exit (1);
|
|
200 }
|
|
201 break;
|
|
202
|
|
203 default:
|
|
204 // if (code >= 0xb9) printf ("stream not demultiplexed ?\n");
|
|
205 if (code >= 0xb0) break;
|
|
206
|
|
207 if (!(in_slice_flag)) {
|
|
208 in_slice_flag = 1;
|
|
209
|
36
|
210 // if(!(picture->second_field)) decode_reorder_frames ();
|
|
211
|
|
212 // set current_frame pointer:
|
|
213 if (picture->second_field){
|
|
214 // vo_field (picture->current_frame, picture->picture_structure);
|
|
215 } else {
|
|
216 if (picture->picture_coding_type == B_TYPE){
|
|
217 picture->current_frame = &frames[2];
|
|
218 picture->current_frame->copy=copy_slice;
|
|
219 } else {
|
|
220 picture->current_frame = picture->forward_reference_frame;
|
|
221 picture->forward_reference_frame = picture->backward_reference_frame;
|
|
222 picture->backward_reference_frame = picture->current_frame;
|
|
223 picture->current_frame->copy=NULL;
|
|
224 }
|
|
225 }
|
|
226
|
|
227 picture->current_frame->vo=output;
|
|
228 picture->current_frame->slice=0;
|
|
229
|
1
|
230 }
|
|
231
|
|
232 if (!drop_frame) {
|
|
233
|
|
234 slice_process (picture, code, buffer);
|
|
235
|
|
236 #ifdef ARCH_X86
|
|
237 if (config.flags & MM_ACCEL_X86_MMX) emms ();
|
|
238 #endif
|
|
239
|
|
240 }
|
|
241 }
|
|
242
|
|
243 return is_frame_done;
|
|
244 }
|
|
245
|
|
246
|
|
247 int mpeg2_decode_data (vo_functions_t *output, uint8_t *current, uint8_t *end)
|
|
248 {
|
|
249 //static uint8_t code = 0xff;
|
|
250 //static uint8_t chunk_buffer[65536];
|
|
251 //static uint8_t *chunk_ptr = chunk_buffer;
|
|
252 //static uint32_t shift = 0;
|
|
253 uint8_t code;
|
|
254 uint8_t *pos=NULL;
|
|
255 uint8_t *start=current;
|
|
256 int ret = 0;
|
|
257
|
|
258 // printf("RCVD %d bytes\n",end-current);
|
|
259
|
|
260 while(current<end){
|
|
261 // FIND NEXT HEAD:
|
|
262 unsigned int head=-1;
|
|
263 uint8_t c;
|
|
264 //--------------------
|
|
265 while(current<end){
|
|
266 c=current[0];
|
|
267 ++current;
|
|
268 head<<=8;
|
|
269 if(head==0x100) break; // synced
|
|
270 head|=c;
|
|
271 }
|
|
272 //--------------------
|
|
273 if(pos){
|
|
274 //if((code&0x100)!=0x100) printf("libmpeg2: FATAL! code=%X\n",code);
|
|
275 //printf("pos=%d chunk %3X size=%d next-code=%X\n",pos-start,code,current-pos,head|c);
|
|
276 ret+=parse_chunk(output, code&0xFF, pos);
|
|
277 }
|
|
278 //--------------------
|
|
279 pos=current;code=head|c;
|
|
280 }
|
|
281
|
|
282 if(code==0x1FF) ret+=parse_chunk(output, 0xFF, NULL); // send 'end of frame'
|
|
283
|
|
284 return ret;
|
|
285 }
|
|
286
|
|
287 void mpeg2_drop (int flag)
|
|
288 {
|
|
289 drop_flag = flag;
|
|
290 }
|
|
291
|