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>
|
3839
|
25 #include <stdio.h>
|
1
|
26
|
|
27 #include "mpeg2_internal.h"
|
|
28 #include "attributes.h"
|
|
29
|
36
|
30 /* default intra quant matrix, in zig-zag order */
|
1
|
31 static uint8_t default_intra_quantizer_matrix[64] ATTR_ALIGN(16) = {
|
|
32 8,
|
|
33 16, 16,
|
|
34 19, 16, 19,
|
|
35 22, 22, 22, 22,
|
|
36 22, 22, 26, 24, 26,
|
|
37 27, 27, 27, 26, 26, 26,
|
|
38 26, 27, 27, 27, 29, 29, 29,
|
|
39 34, 34, 34, 29, 29, 29, 27, 27,
|
|
40 29, 29, 32, 32, 34, 34, 37,
|
|
41 38, 37, 35, 35, 34, 35,
|
|
42 38, 38, 40, 40, 40,
|
|
43 48, 48, 46, 46,
|
|
44 56, 56, 58,
|
|
45 69, 69,
|
|
46 83
|
|
47 };
|
|
48
|
|
49 uint8_t scan_norm[64] ATTR_ALIGN(16) =
|
|
50 {
|
36
|
51 /* Zig-Zag scan pattern */
|
1
|
52 0, 1, 8,16, 9, 2, 3,10,
|
|
53 17,24,32,25,18,11, 4, 5,
|
|
54 12,19,26,33,40,48,41,34,
|
|
55 27,20,13, 6, 7,14,21,28,
|
|
56 35,42,49,56,57,50,43,36,
|
|
57 29,22,15,23,30,37,44,51,
|
|
58 58,59,52,45,38,31,39,46,
|
|
59 53,60,61,54,47,55,62,63
|
|
60 };
|
|
61
|
|
62 uint8_t scan_alt[64] ATTR_ALIGN(16) =
|
|
63 {
|
36
|
64 /* Alternate scan pattern */
|
1
|
65 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
|
|
66 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
|
|
67 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
|
|
68 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
|
|
69 };
|
|
70
|
|
71 void header_state_init (picture_t * picture)
|
|
72 {
|
|
73 picture->scan = scan_norm;
|
|
74 }
|
|
75
|
|
76 int header_process_sequence_header (picture_t * picture, uint8_t * buffer)
|
|
77 {
|
36
|
78 int width, height;
|
1
|
79 int i;
|
|
80
|
1622
|
81 if ((buffer[6] & 0x20) != 0x20){
|
|
82 printf("missing marker bit!\n");
|
36
|
83 return 1; /* missing marker_bit */
|
1622
|
84 }
|
1
|
85
|
36
|
86 height = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
|
1
|
87
|
36
|
88 picture->display_picture_width = (height >> 12);
|
|
89 picture->display_picture_height = (height & 0xfff);
|
1
|
90
|
36
|
91 width = ((height >> 12) + 15) & ~15;
|
|
92 height = ((height & 0xfff) + 15) & ~15;
|
1
|
93
|
1622
|
94 if ((width > 768) || (height > 576)){
|
|
95 printf("size restrictions for MP@ML or MPEG1 exceeded! (%dx%d)\n",width,height);
|
|
96 // return 1; /* size restrictions for MP@ML or MPEG1 */
|
|
97 }
|
|
98
|
36
|
99 picture->coded_picture_width = width;
|
|
100 picture->coded_picture_height = height;
|
|
101
|
|
102 /* this is not used by the decoder */
|
1
|
103 picture->aspect_ratio_information = buffer[3] >> 4;
|
|
104 picture->frame_rate_code = buffer[3] & 15;
|
|
105 picture->bitrate = (buffer[4]<<10)|(buffer[5]<<2)|(buffer[6]>>6);
|
|
106
|
|
107 if (buffer[7] & 2) {
|
|
108 for (i = 0; i < 64; i++)
|
|
109 picture->intra_quantizer_matrix[scan_norm[i]] =
|
|
110 (buffer[i+7] << 7) | (buffer[i+8] >> 1);
|
|
111 buffer += 64;
|
|
112 } else {
|
|
113 for (i = 0; i < 64; i++)
|
|
114 picture->intra_quantizer_matrix[scan_norm[i]] =
|
|
115 default_intra_quantizer_matrix [i];
|
|
116 }
|
|
117
|
|
118 if (buffer[7] & 1) {
|
|
119 for (i = 0; i < 64; i++)
|
|
120 picture->non_intra_quantizer_matrix[scan_norm[i]] =
|
|
121 buffer[i+8];
|
|
122 } else {
|
|
123 for (i = 0; i < 64; i++)
|
|
124 picture->non_intra_quantizer_matrix[i] = 16;
|
|
125 }
|
|
126
|
36
|
127 /* MPEG1 - for testing only */
|
1
|
128 picture->mpeg1 = 1;
|
|
129 picture->intra_dc_precision = 0;
|
|
130 picture->frame_pred_frame_dct = 1;
|
|
131 picture->q_scale_type = 0;
|
|
132 picture->concealment_motion_vectors = 0;
|
36
|
133 /* picture->alternate_scan = 0; */
|
1
|
134 picture->picture_structure = FRAME_PICTURE;
|
36
|
135 /* picture->second_field = 0; */
|
1
|
136
|
|
137 return 0;
|
|
138 }
|
|
139
|
|
140 static int header_process_sequence_extension (picture_t * picture,
|
|
141 uint8_t * buffer)
|
|
142 {
|
36
|
143 /* check chroma format, size extensions, marker bit */
|
|
144 if (((buffer[1] & 0x07) != 0x02) || (buffer[2] & 0xe0) ||
|
|
145 ((buffer[3] & 0x01) != 0x01))
|
|
146 return 1;
|
1
|
147
|
36
|
148 /* this is not used by the decoder */
|
1
|
149 picture->progressive_sequence = (buffer[1] >> 3) & 1;
|
|
150
|
|
151 if (picture->progressive_sequence)
|
|
152 picture->coded_picture_height =
|
|
153 (picture->coded_picture_height + 31) & ~31;
|
36
|
154
|
|
155 /* MPEG1 - for testing only */
|
|
156 picture->mpeg1 = 0;
|
1
|
157
|
|
158 return 0;
|
|
159 }
|
|
160
|
|
161 static int header_process_quant_matrix_extension (picture_t * picture,
|
|
162 uint8_t * buffer)
|
|
163 {
|
|
164 int i;
|
|
165
|
|
166 if (buffer[0] & 8) {
|
|
167 for (i = 0; i < 64; i++)
|
|
168 picture->intra_quantizer_matrix[scan_norm[i]] =
|
|
169 (buffer[i] << 5) | (buffer[i+1] >> 3);
|
|
170 buffer += 64;
|
|
171 }
|
|
172
|
|
173 if (buffer[0] & 4) {
|
|
174 for (i = 0; i < 64; i++)
|
|
175 picture->non_intra_quantizer_matrix[scan_norm[i]] =
|
|
176 (buffer[i] << 6) | (buffer[i+1] >> 2);
|
|
177 }
|
|
178
|
|
179 return 0;
|
|
180 }
|
|
181
|
|
182 static int header_process_picture_coding_extension (picture_t * picture, uint8_t * buffer)
|
|
183 {
|
36
|
184 /* pre subtract 1 for use later in compute_motion_vector */
|
49
|
185 picture->f_motion.f_code[0] = (buffer[0] & 15) - 1;
|
|
186 picture->f_motion.f_code[1] = (buffer[1] >> 4) - 1;
|
|
187 picture->b_motion.f_code[0] = (buffer[1] & 15) - 1;
|
|
188 picture->b_motion.f_code[1] = (buffer[2] >> 4) - 1;
|
1
|
189
|
|
190 picture->intra_dc_precision = (buffer[2] >> 2) & 3;
|
|
191 picture->picture_structure = buffer[2] & 3;
|
|
192 picture->frame_pred_frame_dct = (buffer[3] >> 6) & 1;
|
|
193 picture->concealment_motion_vectors = (buffer[3] >> 5) & 1;
|
|
194 picture->q_scale_type = (buffer[3] >> 4) & 1;
|
|
195 picture->intra_vlc_format = (buffer[3] >> 3) & 1;
|
|
196
|
36
|
197 if (buffer[3] & 4) /* alternate_scan */
|
1
|
198 picture->scan = scan_alt;
|
|
199 else
|
|
200 picture->scan = scan_norm;
|
|
201
|
36
|
202 /* these are not used by the decoder */
|
1
|
203 picture->top_field_first = buffer[3] >> 7;
|
|
204 picture->repeat_first_field = (buffer[3] >> 1) & 1;
|
|
205 picture->progressive_frame = buffer[4] >> 7;
|
|
206
|
|
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 }
|
2562
|
220 //temopral hack. We calc time on every field, so if we have 2 fields
|
|
221 // interlaced we'll end with double time for 1 frame
|
|
222 if( picture->picture_structure!=3 ) picture->display_time/=2;
|
1
|
223 return 0;
|
|
224 }
|
|
225
|
|
226 int header_process_extension (picture_t * picture, uint8_t * buffer)
|
|
227 {
|
|
228 switch (buffer[0] & 0xf0) {
|
36
|
229 case 0x10: /* sequence extension */
|
1
|
230 return header_process_sequence_extension (picture, buffer);
|
|
231
|
36
|
232 case 0x30: /* quant matrix extension */
|
1
|
233 return header_process_quant_matrix_extension (picture, buffer);
|
|
234
|
36
|
235 case 0x80: /* picture coding extension */
|
1
|
236 return header_process_picture_coding_extension (picture, buffer);
|
|
237 }
|
|
238
|
|
239 return 0;
|
|
240 }
|
|
241
|
|
242 int header_process_picture_header (picture_t *picture, uint8_t * buffer)
|
|
243 {
|
|
244 picture->picture_coding_type = (buffer [1] >> 3) & 7;
|
|
245
|
36
|
246 /* forward_f_code and backward_f_code - used in mpeg1 only */
|
49
|
247 picture->f_motion.f_code[1] = (buffer[3] >> 2) & 1;
|
|
248 picture->f_motion.f_code[0] =
|
1
|
249 (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1;
|
49
|
250 picture->b_motion.f_code[1] = (buffer[4] >> 6) & 1;
|
|
251 picture->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1;
|
1
|
252
|
36
|
253 /* move in header_process_picture_header */
|
1
|
254 picture->second_field =
|
|
255 (picture->picture_structure != FRAME_PICTURE) &&
|
|
256 !(picture->second_field);
|
|
257
|
|
258 return 0;
|
|
259 }
|