comparison libmpeg2/header.c @ 1:3b5f5d1c5041

Initial revision
author arpi_esp
date Sat, 24 Feb 2001 20:28:24 +0000
parents
children 846535ace7a2
comparison
equal deleted inserted replaced
0:c1bb2c071d63 1:3b5f5d1c5041
1 /*
2 * slice.c
3 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
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
29 // default intra quant matrix, in zig-zag order
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 {
50 // Zig-Zag scan pattern
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 {
63 // Alternate scan pattern
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 //FIXME we should set pointers to the real scan matrices here (mmx vs
73 //normal) instead of the ifdefs in header_process_picture_coding_extension
74
75 picture->scan = scan_norm;
76 }
77
78 static const int frameratecode2framerate[16] = {
79 0, 24000*10000/1001, 24*10000,25*10000, 30000*10000/1001, 30*10000,50*10000,60000*10000/1001,
80 60*10000, 0,0,0,0,0,0,0
81 };
82
83 int header_process_sequence_header (picture_t * picture, uint8_t * buffer)
84 {
85 unsigned int h_size;
86 unsigned int v_size;
87 int i;
88
89 if ((buffer[6] & 0x20) != 0x20)
90 return 1; // missing marker_bit
91
92 v_size = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
93
94 picture->display_picture_width = (v_size >> 12);
95 picture->display_picture_height = (v_size & 0xfff);
96
97 h_size = ((v_size >> 12) + 15) & ~15;
98 v_size = ((v_size & 0xfff) + 15) & ~15;
99
100 if ((h_size > 768) || (v_size > 576))
101 return 1; // size restrictions for MP@ML or MPEG1
102
103 //XXX this needs field fixups
104 picture->coded_picture_width = h_size;
105 picture->coded_picture_height = v_size;
106 picture->last_mba = ((h_size * v_size) >> 8) - 1;
107
108 // this is not used by the decoder
109 picture->aspect_ratio_information = buffer[3] >> 4;
110 picture->frame_rate_code = buffer[3] & 15;
111 picture->frame_rate = frameratecode2framerate[picture->frame_rate_code];
112
113 picture->bitrate = (buffer[4]<<10)|(buffer[5]<<2)|(buffer[6]>>6);
114
115 if (buffer[7] & 2) {
116 for (i = 0; i < 64; i++)
117 picture->intra_quantizer_matrix[scan_norm[i]] =
118 (buffer[i+7] << 7) | (buffer[i+8] >> 1);
119 buffer += 64;
120 } else {
121 for (i = 0; i < 64; i++)
122 picture->intra_quantizer_matrix[scan_norm[i]] =
123 default_intra_quantizer_matrix [i];
124 }
125
126 if (buffer[7] & 1) {
127 for (i = 0; i < 64; i++)
128 picture->non_intra_quantizer_matrix[scan_norm[i]] =
129 buffer[i+8];
130 } else {
131 for (i = 0; i < 64; i++)
132 picture->non_intra_quantizer_matrix[i] = 16;
133 }
134
135 // MPEG1 - for testing only
136 picture->mpeg1 = 1;
137 picture->intra_dc_precision = 0;
138 picture->frame_pred_frame_dct = 1;
139 picture->q_scale_type = 0;
140 picture->concealment_motion_vectors = 0;
141 //picture->alternate_scan = 0;
142 picture->picture_structure = FRAME_PICTURE;
143 //picture->second_field = 0;
144
145 return 0;
146 }
147
148 static int header_process_sequence_extension (picture_t * picture,
149 uint8_t * buffer)
150 {
151 // MPEG1 - for testing only
152 picture->mpeg1 = 0;
153
154 // check chroma format, size extensions, marker bit
155 if(((buffer[1]>>1)&3)!=1){
156 printf("This CHROMA format not yet supported :(\n");
157 return 1;
158 }
159 if ((buffer[1] & 1) || (buffer[2] & 0xe0)){
160 printf("Big resolution video not yet supported :(\n");
161 return 1;
162 }
163 if((buffer[3] & 0x01) != 0x01) return 1; // marker bit
164
165
166 // this is not used by the decoder
167 picture->progressive_sequence = (buffer[1] >> 3) & 1;
168
169 if (picture->progressive_sequence)
170 picture->coded_picture_height =
171 (picture->coded_picture_height + 31) & ~31;
172 picture->bitrate>>=1; // hack
173
174 return 0;
175 }
176
177 static int header_process_quant_matrix_extension (picture_t * picture,
178 uint8_t * buffer)
179 {
180 int i;
181
182 if (buffer[0] & 8) {
183 for (i = 0; i < 64; i++)
184 picture->intra_quantizer_matrix[scan_norm[i]] =
185 (buffer[i] << 5) | (buffer[i+1] >> 3);
186 buffer += 64;
187 }
188
189 if (buffer[0] & 4) {
190 for (i = 0; i < 64; i++)
191 picture->non_intra_quantizer_matrix[scan_norm[i]] =
192 (buffer[i] << 6) | (buffer[i+1] >> 2);
193 }
194
195 return 0;
196 }
197
198 static int header_process_picture_coding_extension (picture_t * picture, uint8_t * buffer)
199 {
200 //pre subtract 1 for use later in compute_motion_vector
201 picture->f_code[0][0] = (buffer[0] & 15) - 1;
202 picture->f_code[0][1] = (buffer[1] >> 4) - 1;
203 picture->f_code[1][0] = (buffer[1] & 15) - 1;
204 picture->f_code[1][1] = (buffer[2] >> 4) - 1;
205
206 picture->intra_dc_precision = (buffer[2] >> 2) & 3;
207 picture->picture_structure = buffer[2] & 3;
208 picture->frame_pred_frame_dct = (buffer[3] >> 6) & 1;
209 picture->concealment_motion_vectors = (buffer[3] >> 5) & 1;
210 picture->q_scale_type = (buffer[3] >> 4) & 1;
211 picture->intra_vlc_format = (buffer[3] >> 3) & 1;
212
213 if (buffer[3] & 4) // alternate_scan
214 picture->scan = scan_alt;
215 else
216 picture->scan = scan_norm;
217
218 // these are not used by the decoder
219 picture->top_field_first = buffer[3] >> 7;
220 picture->repeat_first_field = (buffer[3] >> 1) & 1;
221 picture->progressive_frame = buffer[4] >> 7;
222
223 // repeat_first implementation by A'rpi/ESP-team, based on libmpeg3:
224 if(picture->repeat_count>=100) picture->repeat_count=0;
225 if(picture->repeat_first_field){
226 if(picture->progressive_sequence){
227 if(picture->top_field_first)
228 picture->repeat_count+=200;
229 else
230 picture->repeat_count+=100;
231 } else
232 if(picture->progressive_frame){
233 picture->repeat_count+=50;
234 }
235 }
236
237 return 0;
238 }
239
240 int header_process_extension (picture_t * picture, uint8_t * buffer)
241 {
242 switch (buffer[0] & 0xf0) {
243 case 0x10: // sequence extension
244 return header_process_sequence_extension (picture, buffer);
245
246 case 0x30: // quant matrix extension
247 return header_process_quant_matrix_extension (picture, buffer);
248
249 case 0x80: // picture coding extension
250 return header_process_picture_coding_extension (picture, buffer);
251 }
252
253 return 0;
254 }
255
256 int header_process_picture_header (picture_t *picture, uint8_t * buffer)
257 {
258 picture->picture_coding_type = (buffer [1] >> 3) & 7;
259
260 // forward_f_code and backward_f_code - used in mpeg1 only
261 picture->f_code[0][1] = (buffer[3] >> 2) & 1;
262 picture->f_code[0][0] =
263 (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1;
264 picture->f_code[1][1] = (buffer[4] >> 6) & 1;
265 picture->f_code[1][0] = ((buffer[4] >> 3) & 7) - 1;
266
267 // move in header_process_picture_header
268 picture->second_field =
269 (picture->picture_structure != FRAME_PICTURE) &&
270 !(picture->second_field);
271
272 return 0;
273 }