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
|
|
80 if ((buffer[6] & 0x20) != 0x20)
|
36
|
81 return 1; /* missing marker_bit */
|
1
|
82
|
36
|
83 height = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
|
1
|
84
|
36
|
85 picture->display_picture_width = (height >> 12);
|
|
86 picture->display_picture_height = (height & 0xfff);
|
1
|
87
|
36
|
88 width = ((height >> 12) + 15) & ~15;
|
|
89 height = ((height & 0xfff) + 15) & ~15;
|
1
|
90
|
36
|
91 if ((width > 768) || (height > 576))
|
|
92 return 1; /* size restrictions for MP@ML or MPEG1 */
|
1
|
93
|
36
|
94 picture->coded_picture_width = width;
|
|
95 picture->coded_picture_height = height;
|
|
96
|
|
97 /* this is not used by the decoder */
|
1
|
98 picture->aspect_ratio_information = buffer[3] >> 4;
|
|
99 picture->frame_rate_code = buffer[3] & 15;
|
|
100 picture->bitrate = (buffer[4]<<10)|(buffer[5]<<2)|(buffer[6]>>6);
|
|
101
|
|
102 if (buffer[7] & 2) {
|
|
103 for (i = 0; i < 64; i++)
|
|
104 picture->intra_quantizer_matrix[scan_norm[i]] =
|
|
105 (buffer[i+7] << 7) | (buffer[i+8] >> 1);
|
|
106 buffer += 64;
|
|
107 } else {
|
|
108 for (i = 0; i < 64; i++)
|
|
109 picture->intra_quantizer_matrix[scan_norm[i]] =
|
|
110 default_intra_quantizer_matrix [i];
|
|
111 }
|
|
112
|
|
113 if (buffer[7] & 1) {
|
|
114 for (i = 0; i < 64; i++)
|
|
115 picture->non_intra_quantizer_matrix[scan_norm[i]] =
|
|
116 buffer[i+8];
|
|
117 } else {
|
|
118 for (i = 0; i < 64; i++)
|
|
119 picture->non_intra_quantizer_matrix[i] = 16;
|
|
120 }
|
|
121
|
36
|
122 /* MPEG1 - for testing only */
|
1
|
123 picture->mpeg1 = 1;
|
|
124 picture->intra_dc_precision = 0;
|
|
125 picture->frame_pred_frame_dct = 1;
|
|
126 picture->q_scale_type = 0;
|
|
127 picture->concealment_motion_vectors = 0;
|
36
|
128 /* picture->alternate_scan = 0; */
|
1
|
129 picture->picture_structure = FRAME_PICTURE;
|
36
|
130 /* picture->second_field = 0; */
|
1
|
131
|
|
132 return 0;
|
|
133 }
|
|
134
|
|
135 static int header_process_sequence_extension (picture_t * picture,
|
|
136 uint8_t * buffer)
|
|
137 {
|
36
|
138 /* check chroma format, size extensions, marker bit */
|
|
139 if (((buffer[1] & 0x07) != 0x02) || (buffer[2] & 0xe0) ||
|
|
140 ((buffer[3] & 0x01) != 0x01))
|
|
141 return 1;
|
1
|
142
|
36
|
143 /* this is not used by the decoder */
|
1
|
144 picture->progressive_sequence = (buffer[1] >> 3) & 1;
|
|
145
|
|
146 if (picture->progressive_sequence)
|
|
147 picture->coded_picture_height =
|
|
148 (picture->coded_picture_height + 31) & ~31;
|
36
|
149
|
|
150 /* MPEG1 - for testing only */
|
|
151 picture->mpeg1 = 0;
|
1
|
152
|
|
153 return 0;
|
|
154 }
|
|
155
|
|
156 static int header_process_quant_matrix_extension (picture_t * picture,
|
|
157 uint8_t * buffer)
|
|
158 {
|
|
159 int i;
|
|
160
|
|
161 if (buffer[0] & 8) {
|
|
162 for (i = 0; i < 64; i++)
|
|
163 picture->intra_quantizer_matrix[scan_norm[i]] =
|
|
164 (buffer[i] << 5) | (buffer[i+1] >> 3);
|
|
165 buffer += 64;
|
|
166 }
|
|
167
|
|
168 if (buffer[0] & 4) {
|
|
169 for (i = 0; i < 64; i++)
|
|
170 picture->non_intra_quantizer_matrix[scan_norm[i]] =
|
|
171 (buffer[i] << 6) | (buffer[i+1] >> 2);
|
|
172 }
|
|
173
|
|
174 return 0;
|
|
175 }
|
|
176
|
|
177 static int header_process_picture_coding_extension (picture_t * picture, uint8_t * buffer)
|
|
178 {
|
36
|
179 /* pre subtract 1 for use later in compute_motion_vector */
|
49
|
180 picture->f_motion.f_code[0] = (buffer[0] & 15) - 1;
|
|
181 picture->f_motion.f_code[1] = (buffer[1] >> 4) - 1;
|
|
182 picture->b_motion.f_code[0] = (buffer[1] & 15) - 1;
|
|
183 picture->b_motion.f_code[1] = (buffer[2] >> 4) - 1;
|
1
|
184
|
|
185 picture->intra_dc_precision = (buffer[2] >> 2) & 3;
|
|
186 picture->picture_structure = buffer[2] & 3;
|
|
187 picture->frame_pred_frame_dct = (buffer[3] >> 6) & 1;
|
|
188 picture->concealment_motion_vectors = (buffer[3] >> 5) & 1;
|
|
189 picture->q_scale_type = (buffer[3] >> 4) & 1;
|
|
190 picture->intra_vlc_format = (buffer[3] >> 3) & 1;
|
|
191
|
36
|
192 if (buffer[3] & 4) /* alternate_scan */
|
1
|
193 picture->scan = scan_alt;
|
|
194 else
|
|
195 picture->scan = scan_norm;
|
|
196
|
36
|
197 /* these are not used by the decoder */
|
1
|
198 picture->top_field_first = buffer[3] >> 7;
|
|
199 picture->repeat_first_field = (buffer[3] >> 1) & 1;
|
|
200 picture->progressive_frame = buffer[4] >> 7;
|
|
201
|
|
202 // repeat_first implementation by A'rpi/ESP-team, based on libmpeg3:
|
|
203 if(picture->repeat_count>=100) picture->repeat_count=0;
|
|
204 if(picture->repeat_first_field){
|
|
205 if(picture->progressive_sequence){
|
|
206 if(picture->top_field_first)
|
|
207 picture->repeat_count+=200;
|
|
208 else
|
|
209 picture->repeat_count+=100;
|
|
210 } else
|
|
211 if(picture->progressive_frame){
|
|
212 picture->repeat_count+=50;
|
|
213 }
|
|
214 }
|
|
215
|
|
216 return 0;
|
|
217 }
|
|
218
|
|
219 int header_process_extension (picture_t * picture, uint8_t * buffer)
|
|
220 {
|
|
221 switch (buffer[0] & 0xf0) {
|
36
|
222 case 0x10: /* sequence extension */
|
1
|
223 return header_process_sequence_extension (picture, buffer);
|
|
224
|
36
|
225 case 0x30: /* quant matrix extension */
|
1
|
226 return header_process_quant_matrix_extension (picture, buffer);
|
|
227
|
36
|
228 case 0x80: /* picture coding extension */
|
1
|
229 return header_process_picture_coding_extension (picture, buffer);
|
|
230 }
|
|
231
|
|
232 return 0;
|
|
233 }
|
|
234
|
|
235 int header_process_picture_header (picture_t *picture, uint8_t * buffer)
|
|
236 {
|
|
237 picture->picture_coding_type = (buffer [1] >> 3) & 7;
|
|
238
|
36
|
239 /* forward_f_code and backward_f_code - used in mpeg1 only */
|
49
|
240 picture->f_motion.f_code[1] = (buffer[3] >> 2) & 1;
|
|
241 picture->f_motion.f_code[0] =
|
1
|
242 (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1;
|
49
|
243 picture->b_motion.f_code[1] = (buffer[4] >> 6) & 1;
|
|
244 picture->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1;
|
1
|
245
|
36
|
246 /* move in header_process_picture_header */
|
1
|
247 picture->second_field =
|
|
248 (picture->picture_structure != FRAME_PICTURE) &&
|
|
249 !(picture->second_field);
|
|
250
|
|
251 return 0;
|
|
252 }
|