1
|
1 /*
|
|
2 * slice.c
|
36
|
3 * Copyright (C) 1999-2001 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
|
1
|
4 *
|
|
5 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
|
|
6 *
|
|
7 * mpeg2dec is free software; you can redistribute it and/or modify
|
|
8 * it under the terms of the GNU General Public License as published by
|
|
9 * the Free Software Foundation; either version 2 of the License, or
|
|
10 * (at your option) any later version.
|
|
11 *
|
|
12 * mpeg2dec is distributed in the hope that it will be useful,
|
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 * GNU General Public License for more details.
|
|
16 *
|
|
17 * You should have received a copy of the GNU General Public License
|
|
18 * along with this program; if not, write to the Free Software
|
|
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
20 */
|
|
21
|
|
22 #include "config.h"
|
|
23
|
|
24 #include <inttypes.h>
|
|
25
|
|
26 #include "mpeg2_internal.h"
|
|
27 #include "attributes.h"
|
|
28
|
36
|
29 /* default intra quant matrix, in zig-zag order */
|
1
|
30 static uint8_t default_intra_quantizer_matrix[64] ATTR_ALIGN(16) = {
|
|
31 8,
|
|
32 16, 16,
|
|
33 19, 16, 19,
|
|
34 22, 22, 22, 22,
|
|
35 22, 22, 26, 24, 26,
|
|
36 27, 27, 27, 26, 26, 26,
|
|
37 26, 27, 27, 27, 29, 29, 29,
|
|
38 34, 34, 34, 29, 29, 29, 27, 27,
|
|
39 29, 29, 32, 32, 34, 34, 37,
|
|
40 38, 37, 35, 35, 34, 35,
|
|
41 38, 38, 40, 40, 40,
|
|
42 48, 48, 46, 46,
|
|
43 56, 56, 58,
|
|
44 69, 69,
|
|
45 83
|
|
46 };
|
|
47
|
|
48 uint8_t scan_norm[64] ATTR_ALIGN(16) =
|
|
49 {
|
36
|
50 /* Zig-Zag scan pattern */
|
1
|
51 0, 1, 8,16, 9, 2, 3,10,
|
|
52 17,24,32,25,18,11, 4, 5,
|
|
53 12,19,26,33,40,48,41,34,
|
|
54 27,20,13, 6, 7,14,21,28,
|
|
55 35,42,49,56,57,50,43,36,
|
|
56 29,22,15,23,30,37,44,51,
|
|
57 58,59,52,45,38,31,39,46,
|
|
58 53,60,61,54,47,55,62,63
|
|
59 };
|
|
60
|
|
61 uint8_t scan_alt[64] ATTR_ALIGN(16) =
|
|
62 {
|
36
|
63 /* Alternate scan pattern */
|
1
|
64 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
|
|
65 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
|
|
66 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
|
|
67 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
|
|
68 };
|
|
69
|
|
70 void header_state_init (picture_t * picture)
|
|
71 {
|
|
72 picture->scan = scan_norm;
|
|
73 }
|
|
74
|
|
75 int header_process_sequence_header (picture_t * picture, uint8_t * buffer)
|
|
76 {
|
36
|
77 int width, height;
|
1
|
78 int i;
|
|
79
|
1622
|
80 if ((buffer[6] & 0x20) != 0x20){
|
|
81 printf("missing marker bit!\n");
|
36
|
82 return 1; /* missing marker_bit */
|
1622
|
83 }
|
1
|
84
|
36
|
85 height = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
|
1
|
86
|
36
|
87 picture->display_picture_width = (height >> 12);
|
|
88 picture->display_picture_height = (height & 0xfff);
|
1
|
89
|
36
|
90 width = ((height >> 12) + 15) & ~15;
|
|
91 height = ((height & 0xfff) + 15) & ~15;
|
1
|
92
|
1622
|
93 if ((width > 768) || (height > 576)){
|
|
94 printf("size restrictions for MP@ML or MPEG1 exceeded! (%dx%d)\n",width,height);
|
|
95 // return 1; /* size restrictions for MP@ML or MPEG1 */
|
|
96 }
|
|
97
|
36
|
98 picture->coded_picture_width = width;
|
|
99 picture->coded_picture_height = height;
|
|
100
|
|
101 /* this is not used by the decoder */
|
1
|
102 picture->aspect_ratio_information = buffer[3] >> 4;
|
|
103 picture->frame_rate_code = buffer[3] & 15;
|
|
104 picture->bitrate = (buffer[4]<<10)|(buffer[5]<<2)|(buffer[6]>>6);
|
|
105
|
|
106 if (buffer[7] & 2) {
|
|
107 for (i = 0; i < 64; i++)
|
|
108 picture->intra_quantizer_matrix[scan_norm[i]] =
|
|
109 (buffer[i+7] << 7) | (buffer[i+8] >> 1);
|
|
110 buffer += 64;
|
|
111 } else {
|
|
112 for (i = 0; i < 64; i++)
|
|
113 picture->intra_quantizer_matrix[scan_norm[i]] =
|
|
114 default_intra_quantizer_matrix [i];
|
|
115 }
|
|
116
|
|
117 if (buffer[7] & 1) {
|
|
118 for (i = 0; i < 64; i++)
|
|
119 picture->non_intra_quantizer_matrix[scan_norm[i]] =
|
|
120 buffer[i+8];
|
|
121 } else {
|
|
122 for (i = 0; i < 64; i++)
|
|
123 picture->non_intra_quantizer_matrix[i] = 16;
|
|
124 }
|
|
125
|
36
|
126 /* MPEG1 - for testing only */
|
1
|
127 picture->mpeg1 = 1;
|
|
128 picture->intra_dc_precision = 0;
|
|
129 picture->frame_pred_frame_dct = 1;
|
|
130 picture->q_scale_type = 0;
|
|
131 picture->concealment_motion_vectors = 0;
|
36
|
132 /* picture->alternate_scan = 0; */
|
1
|
133 picture->picture_structure = FRAME_PICTURE;
|
36
|
134 /* picture->second_field = 0; */
|
1
|
135
|
|
136 return 0;
|
|
137 }
|
|
138
|
|
139 static int header_process_sequence_extension (picture_t * picture,
|
|
140 uint8_t * buffer)
|
|
141 {
|
36
|
142 /* check chroma format, size extensions, marker bit */
|
|
143 if (((buffer[1] & 0x07) != 0x02) || (buffer[2] & 0xe0) ||
|
|
144 ((buffer[3] & 0x01) != 0x01))
|
|
145 return 1;
|
1
|
146
|
36
|
147 /* this is not used by the decoder */
|
1
|
148 picture->progressive_sequence = (buffer[1] >> 3) & 1;
|
|
149
|
|
150 if (picture->progressive_sequence)
|
|
151 picture->coded_picture_height =
|
|
152 (picture->coded_picture_height + 31) & ~31;
|
36
|
153
|
|
154 /* MPEG1 - for testing only */
|
|
155 picture->mpeg1 = 0;
|
1
|
156
|
|
157 return 0;
|
|
158 }
|
|
159
|
|
160 static int header_process_quant_matrix_extension (picture_t * picture,
|
|
161 uint8_t * buffer)
|
|
162 {
|
|
163 int i;
|
|
164
|
|
165 if (buffer[0] & 8) {
|
|
166 for (i = 0; i < 64; i++)
|
|
167 picture->intra_quantizer_matrix[scan_norm[i]] =
|
|
168 (buffer[i] << 5) | (buffer[i+1] >> 3);
|
|
169 buffer += 64;
|
|
170 }
|
|
171
|
|
172 if (buffer[0] & 4) {
|
|
173 for (i = 0; i < 64; i++)
|
|
174 picture->non_intra_quantizer_matrix[scan_norm[i]] =
|
|
175 (buffer[i] << 6) | (buffer[i+1] >> 2);
|
|
176 }
|
|
177
|
|
178 return 0;
|
|
179 }
|
|
180
|
|
181 static int header_process_picture_coding_extension (picture_t * picture, uint8_t * buffer)
|
|
182 {
|
36
|
183 /* pre subtract 1 for use later in compute_motion_vector */
|
49
|
184 picture->f_motion.f_code[0] = (buffer[0] & 15) - 1;
|
|
185 picture->f_motion.f_code[1] = (buffer[1] >> 4) - 1;
|
|
186 picture->b_motion.f_code[0] = (buffer[1] & 15) - 1;
|
|
187 picture->b_motion.f_code[1] = (buffer[2] >> 4) - 1;
|
1
|
188
|
|
189 picture->intra_dc_precision = (buffer[2] >> 2) & 3;
|
|
190 picture->picture_structure = buffer[2] & 3;
|
|
191 picture->frame_pred_frame_dct = (buffer[3] >> 6) & 1;
|
|
192 picture->concealment_motion_vectors = (buffer[3] >> 5) & 1;
|
|
193 picture->q_scale_type = (buffer[3] >> 4) & 1;
|
|
194 picture->intra_vlc_format = (buffer[3] >> 3) & 1;
|
|
195
|
36
|
196 if (buffer[3] & 4) /* alternate_scan */
|
1
|
197 picture->scan = scan_alt;
|
|
198 else
|
|
199 picture->scan = scan_norm;
|
|
200
|
36
|
201 /* these are not used by the decoder */
|
1
|
202 picture->top_field_first = buffer[3] >> 7;
|
|
203 picture->repeat_first_field = (buffer[3] >> 1) & 1;
|
|
204 picture->progressive_frame = buffer[4] >> 7;
|
|
205
|
2426
|
206 #if 1
|
1
|
207 // repeat_first implementation by A'rpi/ESP-team, based on libmpeg3:
|
2426
|
208 picture->display_time=100;
|
1
|
209 if(picture->repeat_first_field){
|
|
210 if(picture->progressive_sequence){
|
|
211 if(picture->top_field_first)
|
2426
|
212 picture->display_time+=200;
|
1
|
213 else
|
2426
|
214 picture->display_time+=100;
|
1
|
215 } else
|
|
216 if(picture->progressive_frame){
|
2426
|
217 picture->display_time+=50;
|
1
|
218 }
|
|
219 }
|
2196
|
220 //repeat_count=display_time-100%
|
|
221 #else
|
2426
|
222
|
|
223 // buggy with file ftp://mplayerhq.hu/MPlayer/incoming/twc-shaolin_soccer-svcd-sample.mpg
|
|
224
|
2196
|
225 // repeat_first implemantation by iive, based on A'rpi/ESP-team and libmpeg3
|
|
226 if( picture->progressive_sequence == 1 )
|
|
227 {
|
|
228 if( picture->repeat_first_field == 0 ) picture->display_time=100;//normal
|
|
229 else
|
|
230 {
|
|
231 if( picture->top_field_first == 0 ) picture->display_time=200;//2 frames
|
|
232 else picture->display_time=300;//3 frames
|
|
233 }
|
|
234 }else
|
|
235 {
|
|
236 if( picture->progressive_frame == 0 )
|
|
237 picture->display_time=100;//2fields, interlaced in time
|
|
238 else
|
|
239 {
|
|
240 if( picture->top_field_first == 0 ) picture->display_time=100;//reconstruct 2 fields
|
|
241 else picture->display_time = 150;//reconstruct 3 fields
|
|
242 }
|
1
|
243
|
2196
|
244 if( picture->picture_structure!=3 ) picture->display_time/=2;//we calc on every field
|
|
245 }
|
|
246 #endif
|
1
|
247 return 0;
|
|
248 }
|
|
249
|
|
250 int header_process_extension (picture_t * picture, uint8_t * buffer)
|
|
251 {
|
|
252 switch (buffer[0] & 0xf0) {
|
36
|
253 case 0x10: /* sequence extension */
|
1
|
254 return header_process_sequence_extension (picture, buffer);
|
|
255
|
36
|
256 case 0x30: /* quant matrix extension */
|
1
|
257 return header_process_quant_matrix_extension (picture, buffer);
|
|
258
|
36
|
259 case 0x80: /* picture coding extension */
|
1
|
260 return header_process_picture_coding_extension (picture, buffer);
|
|
261 }
|
|
262
|
|
263 return 0;
|
|
264 }
|
|
265
|
|
266 int header_process_picture_header (picture_t *picture, uint8_t * buffer)
|
|
267 {
|
|
268 picture->picture_coding_type = (buffer [1] >> 3) & 7;
|
|
269
|
36
|
270 /* forward_f_code and backward_f_code - used in mpeg1 only */
|
49
|
271 picture->f_motion.f_code[1] = (buffer[3] >> 2) & 1;
|
|
272 picture->f_motion.f_code[0] =
|
1
|
273 (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1;
|
49
|
274 picture->b_motion.f_code[1] = (buffer[4] >> 6) & 1;
|
|
275 picture->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1;
|
1
|
276
|
36
|
277 /* move in header_process_picture_header */
|
1
|
278 picture->second_field =
|
|
279 (picture->picture_structure != FRAME_PICTURE) &&
|
|
280 !(picture->second_field);
|
|
281
|
|
282 return 0;
|
|
283 }
|