comparison libmpeg2/decode.c @ 36:846535ace7a2

libmpeg2-0.2.0 merge
author arpi_esp
date Sun, 04 Mar 2001 21:01:54 +0000
parents 3b5f5d1c5041
children 0d76b2b962ad
comparison
equal deleted inserted replaced
35:25f148e9890a 36:846535ace7a2
1 /* Copyright (C) Aaron Holtzman <aholtzma@ess.engr.uvic.ca> - Nov 1999 */ 1 /* Copyright (C) Aaron Holtzman <aholtzma@ess.engr.uvic.ca> - Nov 1999 */
2 /* Some cleanup & hacking by A'rpi/ESP-team - Oct 2000 */ 2 /* Some cleanup & hacking by A'rpi/ESP-team - Oct 2000 */
3 3
4 /* mpeg2dec version: */ 4 /* mpeg2dec version: */
5 #define PACKAGE "mpeg2dec" 5 #define PACKAGE "mpeg2dec"
6 //#define VERSION "0.1.7-cvs" 6 #define VERSION "0.2.0-release"
7 #define VERSION "0.1.8-cvs"
8 7
9 #include <stdio.h> 8 #include <stdio.h>
10 #include <stdlib.h> 9 #include <stdlib.h>
11 #include <unistd.h> 10 #include <unistd.h>
12 #include <fcntl.h> 11 #include <fcntl.h>
13 #include <errno.h> 12 #include <errno.h>
14 13
15 #include "config.h" 14 #include "config.h"
16 15
17 //#include "video_out.h" 16 #include "video_out.h"
17 #include <inttypes.h>
18 18
19 #include "mpeg2.h" 19 #include "mpeg2.h"
20 #include "mpeg2_internal.h" 20 #include "mpeg2_internal.h"
21 21
22 #include "../linux/shmem.h" 22 #include "../linux/shmem.h"
30 #include "attributes.h" 30 #include "attributes.h"
31 #ifdef __i386__ 31 #ifdef __i386__
32 #include "mmx.h" 32 #include "mmx.h"
33 #endif 33 #endif
34 34
35 #include "mm_accel.h"
36
37
35 //this is where we keep the state of the decoder 38 //this is where we keep the state of the decoder
36 //picture_t picture_data; 39 //picture_t picture_data;
37 //picture_t *picture=&picture_data; 40 //picture_t *picture=&picture_data;
38 picture_t *picture=NULL; 41 picture_t *picture=NULL;
39 42
46 //static uint32_t shift = 0; 49 //static uint32_t shift = 0;
47 50
48 static int drop_flag = 0; 51 static int drop_flag = 0;
49 static int drop_frame = 0; 52 static int drop_frame = 0;
50 53
54 #ifdef POSTPROC
51 int quant_store[MBR+1][MBC+1]; // [Review] 55 int quant_store[MBR+1][MBC+1]; // [Review]
56 #endif
52 57
53 void mpeg2_init (void) 58 void mpeg2_init (void)
54 { 59 {
55 60
56 printf (PACKAGE"-"VERSION" (C) 2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>\n"); 61 printf (PACKAGE"-"VERSION" (C) 2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>\n");
71 printf("libmpeg2 config flags = 0x%X\n",config.flags); 76 printf("libmpeg2 config flags = 0x%X\n",config.flags);
72 77
73 picture=shmem_alloc(sizeof(picture_t)); // !!! NEW HACK :) !!! 78 picture=shmem_alloc(sizeof(picture_t)); // !!! NEW HACK :) !!!
74 79
75 header_state_init (picture); 80 header_state_init (picture);
76 picture->repeat_count=0; 81 // picture->repeat_count=0;
77 82
78 picture->pp_options=0; 83 picture->pp_options=0;
79 84
80 idct_init (); 85 idct_init ();
81 motion_comp_init (); 86 motion_comp_init ();
82 } 87 }
83 88
89 static vo_frame_t frames[3];
90
84 void mpeg2_allocate_image_buffers (picture_t * picture) 91 void mpeg2_allocate_image_buffers (picture_t * picture)
85 { 92 {
86 int frame_size,buff_size; 93 int frame_size,buff_size;
87 unsigned char *base=NULL; 94 unsigned char *base=NULL;
95 int i;
88 96
89 // height+1 requires for yuv2rgb_mmx code (it reads next line after last) 97 // 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); 98 frame_size = picture->coded_picture_width * (1+picture->coded_picture_height);
91 frame_size = (frame_size+31)&(~31); // align to 32 byte boundary 99 frame_size = (frame_size+31)&(~31); // align to 32 byte boundary
92 buff_size = frame_size + (frame_size/4)*2; // 4Y + 1U + 1V 100 buff_size = frame_size + (frame_size/4)*2; // 4Y + 1U + 1V
93 101
94 // allocate images in YV12 format 102 // allocate images in YV12 format
95 base = shmem_alloc(buff_size); 103 for(i=0;i<3;i++){
96 picture->throwaway_frame[0] = base; 104 base = shmem_alloc(buff_size);
97 picture->throwaway_frame[1] = base + frame_size * 5 / 4; 105 frames[i].base[0] = base;
98 picture->throwaway_frame[2] = base + frame_size; 106 frames[i].base[1] = base + frame_size * 5 / 4;
99 107 frames[i].base[2] = base + frame_size;
100 base = shmem_alloc(buff_size); 108 frames[i].copy = NULL;
101 picture->backward_reference_frame[0] = base; 109 frames[i].vo = NULL;
102 picture->backward_reference_frame[1] = base + frame_size * 5 / 4; 110 frames[i].slice=0;
103 picture->backward_reference_frame[2] = base + frame_size; 111 }
104 112
105 base = shmem_alloc(buff_size); 113 picture->forward_reference_frame=&frames[0];
106 picture->forward_reference_frame[0] = base; 114 picture->backward_reference_frame=&frames[1];
107 picture->forward_reference_frame[1] = base + frame_size * 5 / 4; 115 picture->current_frame=&frames[2];
108 picture->forward_reference_frame[2] = base + frame_size; 116
109 117 #ifdef POSTPROC
110 base = shmem_alloc(buff_size); 118 base = shmem_alloc(buff_size);
111 picture->pp_frame[0] = base; 119 picture->pp_frame[0] = base;
112 picture->pp_frame[1] = base + frame_size * 5 / 4; 120 picture->pp_frame[1] = base + frame_size * 5 / 4;
113 picture->pp_frame[2] = base + frame_size; 121 picture->pp_frame[2] = base + frame_size;
114 122 #endif
115 } 123
116 124 }
117 static void decode_reorder_frames (void) 125
118 { 126 static void copy_slice (vo_frame_t * frame, uint8_t ** src){
119 if (picture->picture_coding_type != B_TYPE) { 127 vo_functions_t * output = frame->vo;
120 128 int stride[3];
121 //reuse the soon to be outdated forward reference frame 129 int y=frame->slice*16;
122 picture->current_frame[0] = picture->forward_reference_frame[0]; 130
123 picture->current_frame[1] = picture->forward_reference_frame[1]; 131 stride[0]=picture->coded_picture_width;
124 picture->current_frame[2] = picture->forward_reference_frame[2]; 132 stride[1]=stride[2]=stride[0]/2;
125 133
126 //make the backward reference frame the new forward reference frame 134 output->draw_slice (src, stride,
127 picture->forward_reference_frame[0] = 135 picture->display_picture_width,
128 picture->backward_reference_frame[0]; 136 (y+16<=picture->display_picture_height) ? 16 :
129 picture->forward_reference_frame[1] = 137 picture->display_picture_height-y,
130 picture->backward_reference_frame[1]; 138 0, y);
131 picture->forward_reference_frame[2] = 139
132 picture->backward_reference_frame[2]; 140 ++frame->slice;
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 } 141 }
146 142
147 static int in_slice_flag=0; 143 static int in_slice_flag=0;
148 144
149 static int parse_chunk (vo_functions_t * output, int code, uint8_t * buffer) 145 static int parse_chunk (vo_functions_t * output, int code, uint8_t * buffer)
154 150
155 is_frame_done = in_slice_flag && ((!code) || (code >= 0xb0)); 151 is_frame_done = in_slice_flag && ((!code) || (code >= 0xb0));
156 if (is_frame_done) { 152 if (is_frame_done) {
157 in_slice_flag = 0; 153 in_slice_flag = 0;
158 154
159 if(picture->picture_structure != FRAME_PICTURE) printf("Field! %d \n",picture->second_field); 155 // if(picture->picture_structure != FRAME_PICTURE) printf("Field! %d \n",picture->second_field);
160 156
161 if ( ((HACK_MODE == 2) || (picture->mpeg1)) 157 if (((picture->picture_structure == FRAME_PICTURE) ||
162 && ((picture->picture_structure == FRAME_PICTURE) ||
163 (picture->second_field)) 158 (picture->second_field))
164 ) { 159 ) {
165 uint8_t ** bar; 160 #if 1
166 int stride[3]; 161 if (picture->picture_coding_type != B_TYPE) {
167 162 int stride[3];
168 if (picture->picture_coding_type == B_TYPE) 163 stride[0]=picture->coded_picture_width;
169 bar = picture->throwaway_frame; 164 stride[1]=stride[2]=stride[0]/2;
170 else 165 output->draw_slice (picture->forward_reference_frame->base,
171 bar = picture->forward_reference_frame; 166 stride,
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, 167 picture->display_picture_width,
188 picture->display_picture_height, 0, 0); 168 picture->display_picture_height, 0, 0);
189 } 169 }
190 170 #endif
191 } 171 }
192 #ifdef ARCH_X86 172 #ifdef ARCH_X86
193 if (config.flags & MM_ACCEL_X86_MMX) emms (); 173 if (config.flags & MM_ACCEL_X86_MMX) emms();
194 #endif 174 #endif
195 output->flip_page (); 175 output->flip_page();
196 } 176 }
197 177
198 switch (code) { 178 switch (code) {
199 case 0x00: /* picture_start_code */ 179 case 0x00: /* picture_start_code */
200 if (header_process_picture_header (picture, buffer)) { 180 if (header_process_picture_header (picture, buffer)) {
225 if (code >= 0xb0) break; 205 if (code >= 0xb0) break;
226 206
227 if (!(in_slice_flag)) { 207 if (!(in_slice_flag)) {
228 in_slice_flag = 1; 208 in_slice_flag = 1;
229 209
230 if(!(picture->second_field)) decode_reorder_frames (); 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
231 } 230 }
232 231
233 if (!drop_frame) { 232 if (!drop_frame) {
234 uint8_t ** bar;
235 233
236 slice_process (picture, code, buffer); 234 slice_process (picture, code, buffer);
237 235
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 236 #ifdef ARCH_X86
265 if (config.flags & MM_ACCEL_X86_MMX) emms (); 237 if (config.flags & MM_ACCEL_X86_MMX) emms ();
266 #endif 238 #endif
267 239
268 } 240 }