Mercurial > mplayer.hg
annotate libmpeg2/header.c @ 31315:ad274a5b093b
For vos that support the new libvo API (provide UPDATE_SCREENINFO),
avoid duplicating some Gui-related code.
author | reimar |
---|---|
date | Sun, 13 Jun 2010 12:04:13 +0000 |
parents | a51baa308479 |
children | 60709ef498be |
rev | line source |
---|---|
1 | 1 /* |
9852 | 2 * header.c |
12932 | 3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org> |
4 * Copyright (C) 2003 Regis Duchesne <hpreg@zoy.org> | |
9852 | 5 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> |
1 | 6 * |
7 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. | |
9852 | 8 * See http://libmpeg2.sourceforge.net/ for updates. |
1 | 9 * |
10 * mpeg2dec is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; either version 2 of the License, or | |
13 * (at your option) any later version. | |
14 * | |
15 * mpeg2dec is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program; if not, write to the Free Software | |
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
23 */ | |
24 | |
25 #include "config.h" | |
26 | |
27 #include <inttypes.h> | |
9852 | 28 #include <stdlib.h> /* defines NULL */ |
29 #include <string.h> /* memcmp */ | |
1 | 30 |
9852 | 31 #include "mpeg2.h" |
12932 | 32 #include "attributes.h" |
1 | 33 #include "mpeg2_internal.h" |
34 | |
9852 | 35 #define SEQ_EXT 2 |
36 #define SEQ_DISPLAY_EXT 4 | |
37 #define QUANT_MATRIX_EXT 8 | |
38 #define COPYRIGHT_EXT 0x10 | |
39 #define PIC_DISPLAY_EXT 0x80 | |
40 #define PIC_CODING_EXT 0x100 | |
41 | |
36 | 42 /* default intra quant matrix, in zig-zag order */ |
9852 | 43 static const uint8_t default_intra_quantizer_matrix[64] ATTR_ALIGN(16) = { |
1 | 44 8, |
45 16, 16, | |
46 19, 16, 19, | |
47 22, 22, 22, 22, | |
48 22, 22, 26, 24, 26, | |
49 27, 27, 27, 26, 26, 26, | |
50 26, 27, 27, 27, 29, 29, 29, | |
51 34, 34, 34, 29, 29, 29, 27, 27, | |
52 29, 29, 32, 32, 34, 34, 37, | |
53 38, 37, 35, 35, 34, 35, | |
54 38, 38, 40, 40, 40, | |
55 48, 48, 46, 46, | |
56 56, 56, 58, | |
57 69, 69, | |
58 83 | |
59 }; | |
60 | |
9852 | 61 uint8_t mpeg2_scan_norm[64] ATTR_ALIGN(16) = { |
36 | 62 /* Zig-Zag scan pattern */ |
9852 | 63 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, |
64 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, | |
65 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, | |
66 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63 | |
67 }; | |
68 | |
69 uint8_t mpeg2_scan_alt[64] ATTR_ALIGN(16) = { | |
70 /* Alternate scan pattern */ | |
71 0, 8, 16, 24, 1, 9, 2, 10, 17, 25, 32, 40, 48, 56, 57, 49, | |
72 41, 33, 26, 18, 3, 11, 4, 12, 19, 27, 34, 42, 50, 58, 35, 43, | |
73 51, 59, 20, 28, 5, 13, 6, 14, 21, 29, 36, 44, 52, 60, 37, 45, | |
74 53, 61, 22, 30, 7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63 | |
1 | 75 }; |
76 | |
9852 | 77 void mpeg2_header_state_init (mpeg2dec_t * mpeg2dec) |
1 | 78 { |
12932 | 79 if (mpeg2dec->sequence.width != (unsigned)-1) { |
80 int i; | |
81 | |
82 mpeg2dec->sequence.width = (unsigned)-1; | |
83 if (!mpeg2dec->custom_fbuf) | |
84 for (i = mpeg2dec->alloc_index_user; | |
85 i < mpeg2dec->alloc_index; i++) { | |
86 mpeg2_free (mpeg2dec->fbuf_alloc[i].fbuf.buf[0]); | |
87 mpeg2_free (mpeg2dec->fbuf_alloc[i].fbuf.buf[1]); | |
88 mpeg2_free (mpeg2dec->fbuf_alloc[i].fbuf.buf[2]); | |
89 } | |
90 if (mpeg2dec->convert_start) | |
91 for (i = 0; i < 3; i++) { | |
92 mpeg2_free (mpeg2dec->yuv_buf[i][0]); | |
93 mpeg2_free (mpeg2dec->yuv_buf[i][1]); | |
94 mpeg2_free (mpeg2dec->yuv_buf[i][2]); | |
95 } | |
96 if (mpeg2dec->decoder.convert_id) | |
97 mpeg2_free (mpeg2dec->decoder.convert_id); | |
98 } | |
99 mpeg2dec->decoder.coding_type = I_TYPE; | |
100 mpeg2dec->decoder.convert = NULL; | |
101 mpeg2dec->decoder.convert_id = NULL; | |
9852 | 102 mpeg2dec->picture = mpeg2dec->pictures; |
103 mpeg2dec->fbuf[0] = &mpeg2dec->fbuf_alloc[0].fbuf; | |
104 mpeg2dec->fbuf[1] = &mpeg2dec->fbuf_alloc[1].fbuf; | |
105 mpeg2dec->fbuf[2] = &mpeg2dec->fbuf_alloc[2].fbuf; | |
106 mpeg2dec->first = 1; | |
107 mpeg2dec->alloc_index = 0; | |
108 mpeg2dec->alloc_index_user = 0; | |
12932 | 109 mpeg2dec->first_decode_slice = 1; |
110 mpeg2dec->nb_decode_slices = 0xb0 - 1; | |
111 mpeg2dec->convert = NULL; | |
112 mpeg2dec->convert_start = NULL; | |
113 mpeg2dec->custom_fbuf = 0; | |
114 mpeg2dec->yuv_index = 0; | |
1 | 115 } |
116 | |
12932 | 117 void mpeg2_reset_info (mpeg2_info_t * info) |
1 | 118 { |
9852 | 119 info->current_picture = info->current_picture_2nd = NULL; |
120 info->display_picture = info->display_picture_2nd = NULL; | |
121 info->current_fbuf = info->display_fbuf = info->discard_fbuf = NULL; | |
12932 | 122 } |
123 | |
124 static void info_user_data (mpeg2dec_t * mpeg2dec) | |
125 { | |
126 if (mpeg2dec->user_data_len) { | |
127 mpeg2dec->info.user_data = mpeg2dec->chunk_buffer; | |
128 mpeg2dec->info.user_data_len = mpeg2dec->user_data_len - 3; | |
129 } | |
9852 | 130 } |
131 | |
132 int mpeg2_header_sequence (mpeg2dec_t * mpeg2dec) | |
133 { | |
134 uint8_t * buffer = mpeg2dec->chunk_start; | |
12932 | 135 mpeg2_sequence_t * sequence = &(mpeg2dec->new_sequence); |
136 static unsigned int frame_period[16] = { | |
137 0, 1126125, 1125000, 1080000, 900900, 900000, 540000, 450450, 450000, | |
138 /* unofficial: xing 15 fps */ | |
139 1800000, | |
140 /* unofficial: libmpeg3 "Unofficial economy rates" 5/10/12/15 fps */ | |
141 5400000, 2700000, 2250000, 1800000, 0, 0 | |
9852 | 142 }; |
1 | 143 int i; |
144 | |
9852 | 145 if ((buffer[6] & 0x20) != 0x20) /* missing marker_bit */ |
146 return 1; | |
1 | 147 |
9852 | 148 i = (buffer[0] << 16) | (buffer[1] << 8) | buffer[2]; |
12932 | 149 if (! (sequence->display_width = sequence->picture_width = i >> 12)) |
150 return 1; | |
151 if (! (sequence->display_height = sequence->picture_height = i & 0xfff)) | |
152 return 1; | |
153 sequence->width = (sequence->picture_width + 15) & ~15; | |
154 sequence->height = (sequence->picture_height + 15) & ~15; | |
155 sequence->chroma_width = sequence->width >> 1; | |
156 sequence->chroma_height = sequence->height >> 1; | |
12617 | 157 |
12932 | 158 sequence->flags = (SEQ_FLAG_PROGRESSIVE_SEQUENCE | |
159 SEQ_VIDEO_FORMAT_UNSPECIFIED); | |
9852 | 160 |
161 sequence->pixel_width = buffer[3] >> 4; /* aspect ratio */ | |
12932 | 162 sequence->frame_period = frame_period[buffer[3] & 15]; |
36 | 163 |
9852 | 164 sequence->byte_rate = (buffer[4]<<10) | (buffer[5]<<2) | (buffer[6]>>6); |
165 | |
166 sequence->vbv_buffer_size = ((buffer[6]<<16)|(buffer[7]<<8))&0x1ff800; | |
167 | |
168 if (buffer[7] & 4) | |
169 sequence->flags |= SEQ_FLAG_CONSTRAINED_PARAMETERS; | |
1 | 170 |
12932 | 171 mpeg2dec->copy_matrix = 3; |
1 | 172 if (buffer[7] & 2) { |
173 for (i = 0; i < 64; i++) | |
12932 | 174 mpeg2dec->new_quantizer_matrix[0][mpeg2_scan_norm[i]] = |
1 | 175 (buffer[i+7] << 7) | (buffer[i+8] >> 1); |
176 buffer += 64; | |
9852 | 177 } else |
178 for (i = 0; i < 64; i++) | |
12932 | 179 mpeg2dec->new_quantizer_matrix[0][mpeg2_scan_norm[i]] = |
180 default_intra_quantizer_matrix[i]; | |
9852 | 181 |
182 if (buffer[7] & 1) | |
183 for (i = 0; i < 64; i++) | |
12932 | 184 mpeg2dec->new_quantizer_matrix[1][mpeg2_scan_norm[i]] = |
9852 | 185 buffer[i+8]; |
186 else | |
12932 | 187 memset (mpeg2dec->new_quantizer_matrix[1], 16, 64); |
9852 | 188 |
189 sequence->profile_level_id = 0x80; | |
12932 | 190 sequence->colour_primaries = 0; |
191 sequence->transfer_characteristics = 0; | |
192 sequence->matrix_coefficients = 0; | |
9852 | 193 |
194 mpeg2dec->ext_state = SEQ_EXT; | |
195 mpeg2dec->state = STATE_SEQUENCE; | |
196 mpeg2dec->display_offset_x = mpeg2dec->display_offset_y = 0; | |
197 | |
198 return 0; | |
199 } | |
200 | |
201 static int sequence_ext (mpeg2dec_t * mpeg2dec) | |
202 { | |
203 uint8_t * buffer = mpeg2dec->chunk_start; | |
12932 | 204 mpeg2_sequence_t * sequence = &(mpeg2dec->new_sequence); |
9852 | 205 uint32_t flags; |
206 | |
207 if (!(buffer[3] & 1)) | |
208 return 1; | |
209 | |
210 sequence->profile_level_id = (buffer[0] << 4) | (buffer[1] >> 4); | |
211 | |
12932 | 212 sequence->display_width = sequence->picture_width += |
9852 | 213 ((buffer[1] << 13) | (buffer[2] << 5)) & 0x3000; |
12932 | 214 sequence->display_height = sequence->picture_height += |
9852 | 215 (buffer[2] << 7) & 0x3000; |
12932 | 216 sequence->width = (sequence->picture_width + 15) & ~15; |
217 sequence->height = (sequence->picture_height + 15) & ~15; | |
9852 | 218 flags = sequence->flags | SEQ_FLAG_MPEG2; |
219 if (!(buffer[1] & 8)) { | |
220 flags &= ~SEQ_FLAG_PROGRESSIVE_SEQUENCE; | |
12932 | 221 sequence->height = (sequence->height + 31) & ~31; |
9852 | 222 } |
223 if (buffer[5] & 0x80) | |
224 flags |= SEQ_FLAG_LOW_DELAY; | |
225 sequence->flags = flags; | |
12932 | 226 sequence->chroma_width = sequence->width; |
227 sequence->chroma_height = sequence->height; | |
9852 | 228 switch (buffer[1] & 6) { |
229 case 0: /* invalid */ | |
230 return 1; | |
231 case 2: /* 4:2:0 */ | |
12932 | 232 sequence->chroma_height >>= 1; |
9852 | 233 case 4: /* 4:2:2 */ |
12932 | 234 sequence->chroma_width >>= 1; |
9852 | 235 } |
236 | |
237 sequence->byte_rate += ((buffer[2]<<25) | (buffer[3]<<17)) & 0x3ffc0000; | |
238 | |
239 sequence->vbv_buffer_size |= buffer[4] << 21; | |
240 | |
241 sequence->frame_period = | |
27572 | 242 sequence->frame_period * ((buffer[5]&31)+1) / (((buffer[5]>>5)&3)+1); |
9852 | 243 |
244 mpeg2dec->ext_state = SEQ_DISPLAY_EXT; | |
245 | |
246 return 0; | |
247 } | |
248 | |
249 static int sequence_display_ext (mpeg2dec_t * mpeg2dec) | |
250 { | |
251 uint8_t * buffer = mpeg2dec->chunk_start; | |
12932 | 252 mpeg2_sequence_t * sequence = &(mpeg2dec->new_sequence); |
9852 | 253 |
27572 | 254 sequence->flags = ((sequence->flags & ~SEQ_MASK_VIDEO_FORMAT) | |
255 ((buffer[0]<<4) & SEQ_MASK_VIDEO_FORMAT)); | |
9852 | 256 if (buffer[0] & 1) { |
27572 | 257 sequence->flags |= SEQ_FLAG_COLOUR_DESCRIPTION; |
9852 | 258 sequence->colour_primaries = buffer[1]; |
259 sequence->transfer_characteristics = buffer[2]; | |
260 sequence->matrix_coefficients = buffer[3]; | |
261 buffer += 3; | |
1 | 262 } |
263 | |
9852 | 264 if (!(buffer[2] & 2)) /* missing marker_bit */ |
265 return 1; | |
1 | 266 |
21399
111bc7023189
ignore invalid values in the sequence display extension (backport from 0.4.1)
henry
parents:
18783
diff
changeset
|
267 if( (buffer[1] << 6) | (buffer[2] >> 2) ) |
111bc7023189
ignore invalid values in the sequence display extension (backport from 0.4.1)
henry
parents:
18783
diff
changeset
|
268 sequence->display_width = (buffer[1] << 6) | (buffer[2] >> 2); |
111bc7023189
ignore invalid values in the sequence display extension (backport from 0.4.1)
henry
parents:
18783
diff
changeset
|
269 if( ((buffer[2]& 1 ) << 13) | (buffer[3] << 5) | (buffer[4] >> 3) ) |
111bc7023189
ignore invalid values in the sequence display extension (backport from 0.4.1)
henry
parents:
18783
diff
changeset
|
270 sequence->display_height = |
111bc7023189
ignore invalid values in the sequence display extension (backport from 0.4.1)
henry
parents:
18783
diff
changeset
|
271 ((buffer[2]& 1 ) << 13) | (buffer[3] << 5) | (buffer[4] >> 3); |
1 | 272 |
273 return 0; | |
274 } | |
275 | |
27572 | 276 static inline void simplify (unsigned int * u, unsigned int * v) |
277 { | |
278 unsigned int a, b, tmp; | |
279 | |
280 a = *u; b = *v; | |
281 while (a) { /* find greatest common divisor */ | |
282 tmp = a; a = b % tmp; b = tmp; | |
283 } | |
284 *u /= b; *v /= b; | |
285 } | |
286 | |
12932 | 287 static inline void finalize_sequence (mpeg2_sequence_t * sequence) |
1 | 288 { |
9852 | 289 int width; |
290 int height; | |
291 | |
292 sequence->byte_rate *= 50; | |
293 | |
294 if (sequence->flags & SEQ_FLAG_MPEG2) { | |
295 switch (sequence->pixel_width) { | |
296 case 1: /* square pixels */ | |
297 sequence->pixel_width = sequence->pixel_height = 1; return; | |
298 case 2: /* 4:3 aspect ratio */ | |
299 width = 4; height = 3; break; | |
300 case 3: /* 16:9 aspect ratio */ | |
301 width = 16; height = 9; break; | |
302 case 4: /* 2.21:1 aspect ratio */ | |
303 width = 221; height = 100; break; | |
304 default: /* illegal */ | |
305 sequence->pixel_width = sequence->pixel_height = 0; return; | |
306 } | |
307 width *= sequence->display_height; | |
308 height *= sequence->display_width; | |
309 | |
310 } else { | |
29264
e83eef58b30a
Remove all kind of trailing whitespaces from all MPlayer's files.
bircoph
parents:
27572
diff
changeset
|
311 if (sequence->byte_rate == 50 * 0x3ffff) |
e83eef58b30a
Remove all kind of trailing whitespaces from all MPlayer's files.
bircoph
parents:
27572
diff
changeset
|
312 sequence->byte_rate = 0; /* mpeg-1 VBR */ |
1 | 313 |
9852 | 314 switch (sequence->pixel_width) { |
315 case 0: case 15: /* illegal */ | |
316 sequence->pixel_width = sequence->pixel_height = 0; return; | |
317 case 1: /* square pixels */ | |
318 sequence->pixel_width = sequence->pixel_height = 1; return; | |
319 case 3: /* 720x576 16:9 */ | |
320 sequence->pixel_width = 64; sequence->pixel_height = 45; return; | |
321 case 6: /* 720x480 16:9 */ | |
322 sequence->pixel_width = 32; sequence->pixel_height = 27; return; | |
27572 | 323 case 8: /* BT.601 625 lines 4:3 */ |
324 sequence->pixel_width = 59; sequence->pixel_height = 54; return; | |
325 case 12: /* BT.601 525 lines 4:3 */ | |
326 sequence->pixel_width = 10; sequence->pixel_height = 11; return; | |
9852 | 327 default: |
328 height = 88 * sequence->pixel_width + 1171; | |
329 width = 2000; | |
330 } | |
331 } | |
1 | 332 |
9852 | 333 sequence->pixel_width = width; |
334 sequence->pixel_height = height; | |
27572 | 335 simplify (&sequence->pixel_width, &sequence->pixel_height); |
9852 | 336 } |
337 | |
27572 | 338 int mpeg2_guess_aspect (const mpeg2_sequence_t * sequence, |
339 unsigned int * pixel_width, | |
340 unsigned int * pixel_height) | |
12932 | 341 { |
27572 | 342 static struct { |
343 unsigned int width, height; | |
344 } video_modes[] = { | |
345 {720, 576}, /* 625 lines, 13.5 MHz (D1, DV, DVB, DVD) */ | |
346 {704, 576}, /* 625 lines, 13.5 MHz (1/1 D1, DVB, DVD, 4CIF) */ | |
347 {544, 576}, /* 625 lines, 10.125 MHz (DVB, laserdisc) */ | |
348 {528, 576}, /* 625 lines, 10.125 MHz (3/4 D1, DVB, laserdisc) */ | |
349 {480, 576}, /* 625 lines, 9 MHz (2/3 D1, DVB, SVCD) */ | |
350 {352, 576}, /* 625 lines, 6.75 MHz (D2, 1/2 D1, CVD, DVB, DVD) */ | |
351 {352, 288}, /* 625 lines, 6.75 MHz, 1 field (D4, VCD, DVB, DVD, CIF) */ | |
352 {176, 144}, /* 625 lines, 3.375 MHz, half field (QCIF) */ | |
353 {720, 486}, /* 525 lines, 13.5 MHz (D1) */ | |
354 {704, 486}, /* 525 lines, 13.5 MHz */ | |
355 {720, 480}, /* 525 lines, 13.5 MHz (DV, DSS, DVD) */ | |
356 {704, 480}, /* 525 lines, 13.5 MHz (1/1 D1, ATSC, DVD) */ | |
357 {544, 480}, /* 525 lines. 10.125 MHz (DSS, laserdisc) */ | |
358 {528, 480}, /* 525 lines. 10.125 MHz (3/4 D1, laserdisc) */ | |
359 {480, 480}, /* 525 lines, 9 MHz (2/3 D1, SVCD) */ | |
360 {352, 480}, /* 525 lines, 6.75 MHz (D2, 1/2 D1, CVD, DVD) */ | |
361 {352, 240} /* 525 lines. 6.75 MHz, 1 field (D4, VCD, DSS, DVD) */ | |
362 }; | |
363 unsigned int width, height, pix_width, pix_height, i, DAR_16_9; | |
364 | |
365 *pixel_width = sequence->pixel_width; | |
366 *pixel_height = sequence->pixel_height; | |
367 width = sequence->picture_width; | |
368 height = sequence->picture_height; | |
369 for (i = 0; i < sizeof (video_modes) / sizeof (video_modes[0]); i++) | |
370 if (width == video_modes[i].width && height == video_modes[i].height) | |
371 break; | |
372 if (i == sizeof (video_modes) / sizeof (video_modes[0]) || | |
373 (sequence->pixel_width == 1 && sequence->pixel_height == 1) || | |
374 width != sequence->display_width || height != sequence->display_height) | |
375 return 0; | |
376 | |
377 for (pix_height = 1; height * pix_height < 480; pix_height <<= 1); | |
378 height *= pix_height; | |
379 for (pix_width = 1; width * pix_width <= 352; pix_width <<= 1); | |
380 width *= pix_width; | |
381 | |
382 if (! (sequence->flags & SEQ_FLAG_MPEG2)) { | |
383 static unsigned int mpeg1_check[2][2] = {{11, 54}, {27, 45}}; | |
384 DAR_16_9 = (sequence->pixel_height == 27 || | |
385 sequence->pixel_height == 45); | |
386 if (width < 704 || | |
387 sequence->pixel_height != mpeg1_check[DAR_16_9][height == 576]) | |
388 return 0; | |
389 } else { | |
390 DAR_16_9 = (3 * sequence->picture_width * sequence->pixel_width > | |
391 4 * sequence->picture_height * sequence->pixel_height); | |
392 switch (width) { | |
393 case 528: case 544: pix_width *= 4; pix_height *= 3; break; | |
394 case 480: pix_width *= 3; pix_height *= 2; break; | |
395 } | |
396 } | |
397 if (DAR_16_9) { | |
398 pix_width *= 4; pix_height *= 3; | |
399 } | |
400 if (height == 576) { | |
401 pix_width *= 59; pix_height *= 54; | |
402 } else { | |
403 pix_width *= 10; pix_height *= 11; | |
404 } | |
405 *pixel_width = pix_width; | |
406 *pixel_height = pix_height; | |
407 simplify (pixel_width, pixel_height); | |
408 return (height == 576) ? 1 : 2; | |
409 } | |
410 | |
411 static void copy_matrix (mpeg2dec_t * mpeg2dec, int idx) | |
412 { | |
413 if (memcmp (mpeg2dec->quantizer_matrix[idx], | |
414 mpeg2dec->new_quantizer_matrix[idx], 64)) { | |
415 memcpy (mpeg2dec->quantizer_matrix[idx], | |
416 mpeg2dec->new_quantizer_matrix[idx], 64); | |
417 mpeg2dec->scaled[idx] = -1; | |
12932 | 418 } |
419 } | |
420 | |
421 static void finalize_matrix (mpeg2dec_t * mpeg2dec) | |
422 { | |
423 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
424 int i; | |
425 | |
426 for (i = 0; i < 2; i++) { | |
427 if (mpeg2dec->copy_matrix & (1 << i)) | |
428 copy_matrix (mpeg2dec, i); | |
429 if ((mpeg2dec->copy_matrix & (4 << i)) && | |
430 memcmp (mpeg2dec->quantizer_matrix[i], | |
431 mpeg2dec->new_quantizer_matrix[i+2], 64)) { | |
432 copy_matrix (mpeg2dec, i + 2); | |
433 decoder->chroma_quantizer[i] = decoder->quantizer_prescale[i+2]; | |
434 } else if (mpeg2dec->copy_matrix & (5 << i)) | |
435 decoder->chroma_quantizer[i] = decoder->quantizer_prescale[i]; | |
436 } | |
437 } | |
438 | |
439 static mpeg2_state_t invalid_end_action (mpeg2dec_t * mpeg2dec) | |
440 { | |
441 mpeg2_reset_info (&(mpeg2dec->info)); | |
442 mpeg2dec->info.gop = NULL; | |
443 info_user_data (mpeg2dec); | |
444 mpeg2_header_state_init (mpeg2dec); | |
445 mpeg2dec->sequence = mpeg2dec->new_sequence; | |
446 mpeg2dec->action = mpeg2_seek_header; | |
447 mpeg2dec->state = STATE_SEQUENCE; | |
448 return STATE_SEQUENCE; | |
449 } | |
450 | |
9852 | 451 void mpeg2_header_sequence_finalize (mpeg2dec_t * mpeg2dec) |
452 { | |
12932 | 453 mpeg2_sequence_t * sequence = &(mpeg2dec->new_sequence); |
454 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
9852 | 455 |
456 finalize_sequence (sequence); | |
12932 | 457 finalize_matrix (mpeg2dec); |
36 | 458 |
12932 | 459 decoder->mpeg1 = !(sequence->flags & SEQ_FLAG_MPEG2); |
460 decoder->width = sequence->width; | |
461 decoder->height = sequence->height; | |
462 decoder->vertical_position_extension = (sequence->picture_height > 2800); | |
463 decoder->chroma_format = ((sequence->chroma_width == sequence->width) + | |
464 (sequence->chroma_height == sequence->height)); | |
465 | |
466 if (mpeg2dec->sequence.width != (unsigned)-1) { | |
467 /* | |
468 * According to 6.1.1.6, repeat sequence headers should be | |
27572 | 469 * identical to the original. However some encoders do not |
470 * respect that and change various fields (including bitrate | |
471 * and aspect ratio) in the repeat sequence headers. So we | |
472 * choose to be as conservative as possible and only restart | |
473 * the decoder if the width, height, chroma_width, | |
474 * chroma_height or low_delay flag are modified. | |
12932 | 475 */ |
27572 | 476 if (sequence->width != mpeg2dec->sequence.width || |
477 sequence->height != mpeg2dec->sequence.height || | |
478 sequence->chroma_width != mpeg2dec->sequence.chroma_width || | |
479 sequence->chroma_height != mpeg2dec->sequence.chroma_height || | |
480 ((sequence->flags ^ mpeg2dec->sequence.flags) & | |
481 SEQ_FLAG_LOW_DELAY)) { | |
12932 | 482 decoder->stride_frame = sequence->width; |
483 mpeg2_header_end (mpeg2dec); | |
484 mpeg2dec->action = invalid_end_action; | |
485 mpeg2dec->state = STATE_INVALID_END; | |
486 return; | |
487 } | |
27572 | 488 mpeg2dec->state = (memcmp (&(mpeg2dec->sequence), sequence, |
489 sizeof (mpeg2_sequence_t)) ? | |
490 STATE_SEQUENCE_MODIFIED : STATE_SEQUENCE_REPEATED); | |
12932 | 491 } else |
492 decoder->stride_frame = sequence->width; | |
9852 | 493 mpeg2dec->sequence = *sequence; |
12932 | 494 mpeg2_reset_info (&(mpeg2dec->info)); |
9852 | 495 mpeg2dec->info.sequence = &(mpeg2dec->sequence); |
12932 | 496 mpeg2dec->info.gop = NULL; |
497 info_user_data (mpeg2dec); | |
9852 | 498 } |
499 | |
500 int mpeg2_header_gop (mpeg2dec_t * mpeg2dec) | |
501 { | |
12932 | 502 uint8_t * buffer = mpeg2dec->chunk_start; |
503 mpeg2_gop_t * gop = &(mpeg2dec->new_gop); | |
504 | |
505 if (! (buffer[1] & 8)) | |
506 return 1; | |
507 gop->hours = (buffer[0] >> 2) & 31; | |
508 gop->minutes = ((buffer[0] << 4) | (buffer[1] >> 4)) & 63; | |
509 gop->seconds = ((buffer[1] << 3) | (buffer[2] >> 5)) & 63; | |
510 gop->pictures = ((buffer[2] << 1) | (buffer[3] >> 7)) & 63; | |
511 gop->flags = (buffer[0] >> 7) | ((buffer[3] >> 4) & 6); | |
9852 | 512 mpeg2dec->state = STATE_GOP; |
1 | 513 return 0; |
514 } | |
515 | |
12932 | 516 void mpeg2_header_gop_finalize (mpeg2dec_t * mpeg2dec) |
517 { | |
518 mpeg2dec->gop = mpeg2dec->new_gop; | |
519 mpeg2_reset_info (&(mpeg2dec->info)); | |
520 mpeg2dec->info.gop = &(mpeg2dec->gop); | |
521 info_user_data (mpeg2dec); | |
522 } | |
523 | |
524 void mpeg2_set_fbuf (mpeg2dec_t * mpeg2dec, int b_type) | |
1 | 525 { |
526 int i; | |
527 | |
9852 | 528 for (i = 0; i < 3; i++) |
529 if (mpeg2dec->fbuf[1] != &mpeg2dec->fbuf_alloc[i].fbuf && | |
530 mpeg2dec->fbuf[2] != &mpeg2dec->fbuf_alloc[i].fbuf) { | |
531 mpeg2dec->fbuf[0] = &mpeg2dec->fbuf_alloc[i].fbuf; | |
532 mpeg2dec->info.current_fbuf = mpeg2dec->fbuf[0]; | |
12932 | 533 if (b_type || (mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY)) { |
534 if (b_type || mpeg2dec->convert) | |
9852 | 535 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[0]; |
536 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[0]; | |
537 } | |
538 break; | |
539 } | |
540 } | |
541 | |
542 int mpeg2_header_picture (mpeg2dec_t * mpeg2dec) | |
543 { | |
544 uint8_t * buffer = mpeg2dec->chunk_start; | |
12932 | 545 mpeg2_picture_t * picture = &(mpeg2dec->new_picture); |
546 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
9852 | 547 int type; |
548 | |
27572 | 549 mpeg2dec->state = ((mpeg2dec->state != STATE_SLICE_1ST) ? |
550 STATE_PICTURE : STATE_PICTURE_2ND); | |
9852 | 551 mpeg2dec->ext_state = PIC_CODING_EXT; |
552 | |
553 picture->temporal_reference = (buffer[0] << 2) | (buffer[1] >> 6); | |
554 | |
27572 | 555 type = (buffer [1] >> 3) & 7; |
9852 | 556 if (type == PIC_FLAG_CODING_TYPE_P || type == PIC_FLAG_CODING_TYPE_B) { |
557 /* forward_f_code and backward_f_code - used in mpeg1 only */ | |
558 decoder->f_motion.f_code[1] = (buffer[3] >> 2) & 1; | |
559 decoder->f_motion.f_code[0] = | |
560 (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1; | |
561 decoder->b_motion.f_code[1] = (buffer[4] >> 6) & 1; | |
562 decoder->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1; | |
1 | 563 } |
564 | |
27572 | 565 picture->flags = PIC_FLAG_PROGRESSIVE_FRAME | type; |
566 picture->tag = picture->tag2 = 0; | |
567 if (mpeg2dec->num_tags) { | |
568 if (mpeg2dec->bytes_since_tag >= mpeg2dec->chunk_ptr - buffer + 4) { | |
569 mpeg2dec->num_tags = 0; | |
570 picture->tag = mpeg2dec->tag_current; | |
571 picture->tag2 = mpeg2dec->tag2_current; | |
572 picture->flags |= PIC_FLAG_TAGS; | |
573 } else if (mpeg2dec->num_tags > 1) { | |
574 mpeg2dec->num_tags = 1; | |
575 picture->tag = mpeg2dec->tag_previous; | |
576 picture->tag2 = mpeg2dec->tag2_previous; | |
577 picture->flags |= PIC_FLAG_TAGS; | |
578 } | |
579 } | |
580 picture->nb_fields = 2; | |
581 picture->display_offset[0].x = picture->display_offset[1].x = | |
582 picture->display_offset[2].x = mpeg2dec->display_offset_x; | |
583 picture->display_offset[0].y = picture->display_offset[1].y = | |
584 picture->display_offset[2].y = mpeg2dec->display_offset_y; | |
585 | |
9852 | 586 /* XXXXXX decode extra_information_picture as well */ |
587 | |
27572 | 588 decoder->q_scale_type = 0; |
12932 | 589 decoder->intra_dc_precision = 7; |
590 decoder->frame_pred_frame_dct = 1; | |
591 decoder->concealment_motion_vectors = 0; | |
592 decoder->scan = mpeg2_scan_norm; | |
593 decoder->picture_structure = FRAME_PICTURE; | |
594 mpeg2dec->copy_matrix = 0; | |
595 | |
1 | 596 return 0; |
597 } | |
598 | |
9852 | 599 static int picture_coding_ext (mpeg2dec_t * mpeg2dec) |
1 | 600 { |
9852 | 601 uint8_t * buffer = mpeg2dec->chunk_start; |
12932 | 602 mpeg2_picture_t * picture = &(mpeg2dec->new_picture); |
603 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
9852 | 604 uint32_t flags; |
1 | 605 |
9852 | 606 /* pre subtract 1 for use later in compute_motion_vector */ |
607 decoder->f_motion.f_code[0] = (buffer[0] & 15) - 1; | |
608 decoder->f_motion.f_code[1] = (buffer[1] >> 4) - 1; | |
609 decoder->b_motion.f_code[0] = (buffer[1] & 15) - 1; | |
610 decoder->b_motion.f_code[1] = (buffer[2] >> 4) - 1; | |
1 | 611 |
9852 | 612 flags = picture->flags; |
12932 | 613 decoder->intra_dc_precision = 7 - ((buffer[2] >> 2) & 3); |
9852 | 614 decoder->picture_structure = buffer[2] & 3; |
615 switch (decoder->picture_structure) { | |
616 case TOP_FIELD: | |
617 flags |= PIC_FLAG_TOP_FIELD_FIRST; | |
618 case BOTTOM_FIELD: | |
619 picture->nb_fields = 1; | |
620 break; | |
621 case FRAME_PICTURE: | |
622 if (!(mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE)) { | |
623 picture->nb_fields = (buffer[3] & 2) ? 3 : 2; | |
12932 | 624 flags |= (buffer[3] & 128) ? PIC_FLAG_TOP_FIELD_FIRST : 0; |
10509 | 625 flags |= (buffer[3] & 2) ? PIC_FLAG_REPEAT_FIRST_FIELD : 0; |
9852 | 626 } else |
627 picture->nb_fields = (buffer[3]&2) ? ((buffer[3]&128) ? 6 : 4) : 2; | |
628 break; | |
629 default: | |
630 return 1; | |
631 } | |
632 decoder->top_field_first = buffer[3] >> 7; | |
633 decoder->frame_pred_frame_dct = (buffer[3] >> 6) & 1; | |
634 decoder->concealment_motion_vectors = (buffer[3] >> 5) & 1; | |
27572 | 635 decoder->q_scale_type = buffer[3] & 16; |
9852 | 636 decoder->intra_vlc_format = (buffer[3] >> 3) & 1; |
637 decoder->scan = (buffer[3] & 4) ? mpeg2_scan_alt : mpeg2_scan_norm; | |
27572 | 638 if (!(buffer[4] & 0x80)) |
639 flags &= ~PIC_FLAG_PROGRESSIVE_FRAME; | |
9852 | 640 if (buffer[4] & 0x40) |
641 flags |= (((buffer[4]<<26) | (buffer[5]<<18) | (buffer[6]<<10)) & | |
642 PIC_MASK_COMPOSITE_DISPLAY) | PIC_FLAG_COMPOSITE_DISPLAY; | |
643 picture->flags = flags; | |
1 | 644 |
9852 | 645 mpeg2dec->ext_state = PIC_DISPLAY_EXT | COPYRIGHT_EXT | QUANT_MATRIX_EXT; |
646 | |
1 | 647 return 0; |
648 } | |
649 | |
9852 | 650 static int picture_display_ext (mpeg2dec_t * mpeg2dec) |
1 | 651 { |
9852 | 652 uint8_t * buffer = mpeg2dec->chunk_start; |
12932 | 653 mpeg2_picture_t * picture = &(mpeg2dec->new_picture); |
9852 | 654 int i, nb_pos; |
655 | |
656 nb_pos = picture->nb_fields; | |
657 if (mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE) | |
658 nb_pos >>= 1; | |
659 | |
660 for (i = 0; i < nb_pos; i++) { | |
661 int x, y; | |
1 | 662 |
9852 | 663 x = ((buffer[4*i] << 24) | (buffer[4*i+1] << 16) | |
664 (buffer[4*i+2] << 8) | buffer[4*i+3]) >> (11-2*i); | |
665 y = ((buffer[4*i+2] << 24) | (buffer[4*i+3] << 16) | | |
666 (buffer[4*i+4] << 8) | buffer[4*i+5]) >> (10-2*i); | |
667 if (! (x & y & 1)) | |
668 return 1; | |
669 picture->display_offset[i].x = mpeg2dec->display_offset_x = x >> 1; | |
670 picture->display_offset[i].y = mpeg2dec->display_offset_y = y >> 1; | |
671 } | |
672 for (; i < 3; i++) { | |
673 picture->display_offset[i].x = mpeg2dec->display_offset_x; | |
674 picture->display_offset[i].y = mpeg2dec->display_offset_y; | |
675 } | |
676 return 0; | |
677 } | |
1 | 678 |
12932 | 679 void mpeg2_header_picture_finalize (mpeg2dec_t * mpeg2dec, uint32_t accels) |
680 { | |
681 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
682 int old_type_b = (decoder->coding_type == B_TYPE); | |
683 int low_delay = mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY; | |
684 | |
685 finalize_matrix (mpeg2dec); | |
686 decoder->coding_type = mpeg2dec->new_picture.flags & PIC_MASK_CODING_TYPE; | |
687 | |
688 if (mpeg2dec->state == STATE_PICTURE) { | |
689 mpeg2_picture_t * picture; | |
690 mpeg2_picture_t * other; | |
691 | |
692 decoder->second_field = 0; | |
693 | |
694 picture = other = mpeg2dec->pictures; | |
695 if (old_type_b ^ (mpeg2dec->picture < mpeg2dec->pictures + 2)) | |
696 picture += 2; | |
697 else | |
698 other += 2; | |
699 mpeg2dec->picture = picture; | |
700 *picture = mpeg2dec->new_picture; | |
701 | |
702 if (!old_type_b) { | |
703 mpeg2dec->fbuf[2] = mpeg2dec->fbuf[1]; | |
704 mpeg2dec->fbuf[1] = mpeg2dec->fbuf[0]; | |
705 } | |
706 mpeg2dec->fbuf[0] = NULL; | |
707 mpeg2_reset_info (&(mpeg2dec->info)); | |
708 mpeg2dec->info.current_picture = picture; | |
709 mpeg2dec->info.display_picture = picture; | |
710 if (decoder->coding_type != B_TYPE) { | |
711 if (!low_delay) { | |
712 if (mpeg2dec->first) { | |
713 mpeg2dec->info.display_picture = NULL; | |
714 mpeg2dec->first = 0; | |
715 } else { | |
716 mpeg2dec->info.display_picture = other; | |
717 if (other->nb_fields == 1) | |
718 mpeg2dec->info.display_picture_2nd = other + 1; | |
719 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[1]; | |
720 } | |
721 } | |
722 if (!low_delay + !mpeg2dec->convert) | |
723 mpeg2dec->info.discard_fbuf = | |
724 mpeg2dec->fbuf[!low_delay + !mpeg2dec->convert]; | |
725 } | |
726 if (mpeg2dec->convert) { | |
727 mpeg2_convert_init_t convert_init; | |
728 if (!mpeg2dec->convert_start) { | |
729 int y_size, uv_size; | |
730 | |
731 mpeg2dec->decoder.convert_id = | |
732 mpeg2_malloc (mpeg2dec->convert_id_size, | |
733 MPEG2_ALLOC_CONVERT_ID); | |
734 mpeg2dec->convert (MPEG2_CONVERT_START, | |
735 mpeg2dec->decoder.convert_id, | |
736 &(mpeg2dec->sequence), | |
737 mpeg2dec->convert_stride, accels, | |
738 mpeg2dec->convert_arg, &convert_init); | |
739 mpeg2dec->convert_start = convert_init.start; | |
740 mpeg2dec->decoder.convert = convert_init.copy; | |
741 | |
742 y_size = decoder->stride_frame * mpeg2dec->sequence.height; | |
743 uv_size = y_size >> (2 - mpeg2dec->decoder.chroma_format); | |
744 mpeg2dec->yuv_buf[0][0] = | |
745 (uint8_t *) mpeg2_malloc (y_size, MPEG2_ALLOC_YUV); | |
746 mpeg2dec->yuv_buf[0][1] = | |
747 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV); | |
748 mpeg2dec->yuv_buf[0][2] = | |
749 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV); | |
750 mpeg2dec->yuv_buf[1][0] = | |
751 (uint8_t *) mpeg2_malloc (y_size, MPEG2_ALLOC_YUV); | |
752 mpeg2dec->yuv_buf[1][1] = | |
753 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV); | |
754 mpeg2dec->yuv_buf[1][2] = | |
755 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV); | |
756 y_size = decoder->stride_frame * 32; | |
757 uv_size = y_size >> (2 - mpeg2dec->decoder.chroma_format); | |
758 mpeg2dec->yuv_buf[2][0] = | |
759 (uint8_t *) mpeg2_malloc (y_size, MPEG2_ALLOC_YUV); | |
760 mpeg2dec->yuv_buf[2][1] = | |
761 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV); | |
762 mpeg2dec->yuv_buf[2][2] = | |
763 (uint8_t *) mpeg2_malloc (uv_size, MPEG2_ALLOC_YUV); | |
764 } | |
765 if (!mpeg2dec->custom_fbuf) { | |
766 while (mpeg2dec->alloc_index < 3) { | |
767 mpeg2_fbuf_t * fbuf; | |
768 | |
769 fbuf = &mpeg2dec->fbuf_alloc[mpeg2dec->alloc_index++].fbuf; | |
770 fbuf->id = NULL; | |
771 fbuf->buf[0] = | |
772 (uint8_t *) mpeg2_malloc (convert_init.buf_size[0], | |
773 MPEG2_ALLOC_CONVERTED); | |
774 fbuf->buf[1] = | |
775 (uint8_t *) mpeg2_malloc (convert_init.buf_size[1], | |
776 MPEG2_ALLOC_CONVERTED); | |
777 fbuf->buf[2] = | |
778 (uint8_t *) mpeg2_malloc (convert_init.buf_size[2], | |
779 MPEG2_ALLOC_CONVERTED); | |
780 } | |
781 mpeg2_set_fbuf (mpeg2dec, (decoder->coding_type == B_TYPE)); | |
782 } | |
783 } else if (!mpeg2dec->custom_fbuf) { | |
784 while (mpeg2dec->alloc_index < 3) { | |
785 mpeg2_fbuf_t * fbuf; | |
786 int y_size, uv_size; | |
787 | |
788 fbuf = &(mpeg2dec->fbuf_alloc[mpeg2dec->alloc_index++].fbuf); | |
789 fbuf->id = NULL; | |
790 y_size = decoder->stride_frame * mpeg2dec->sequence.height; | |
791 uv_size = y_size >> (2 - decoder->chroma_format); | |
792 fbuf->buf[0] = (uint8_t *) mpeg2_malloc (y_size, | |
793 MPEG2_ALLOC_YUV); | |
794 fbuf->buf[1] = (uint8_t *) mpeg2_malloc (uv_size, | |
795 MPEG2_ALLOC_YUV); | |
796 fbuf->buf[2] = (uint8_t *) mpeg2_malloc (uv_size, | |
797 MPEG2_ALLOC_YUV); | |
798 } | |
799 mpeg2_set_fbuf (mpeg2dec, (decoder->coding_type == B_TYPE)); | |
800 } | |
801 } else { | |
802 decoder->second_field = 1; | |
803 mpeg2dec->picture++; /* second field picture */ | |
804 *(mpeg2dec->picture) = mpeg2dec->new_picture; | |
805 mpeg2dec->info.current_picture_2nd = mpeg2dec->picture; | |
806 if (low_delay || decoder->coding_type == B_TYPE) | |
807 mpeg2dec->info.display_picture_2nd = mpeg2dec->picture; | |
808 } | |
809 | |
810 info_user_data (mpeg2dec); | |
811 } | |
812 | |
9852 | 813 static int copyright_ext (mpeg2dec_t * mpeg2dec) |
814 { | |
815 return 0; | |
816 } | |
817 | |
818 static int quant_matrix_ext (mpeg2dec_t * mpeg2dec) | |
819 { | |
820 uint8_t * buffer = mpeg2dec->chunk_start; | |
12932 | 821 int i, j; |
9852 | 822 |
12932 | 823 for (i = 0; i < 4; i++) |
824 if (buffer[0] & (8 >> i)) { | |
825 for (j = 0; j < 64; j++) | |
826 mpeg2dec->new_quantizer_matrix[i][mpeg2_scan_norm[j]] = | |
827 (buffer[j] << (i+5)) | (buffer[j+1] >> (3-i)); | |
828 mpeg2dec->copy_matrix |= 1 << i; | |
829 buffer += 64; | |
830 } | |
9852 | 831 |
1 | 832 return 0; |
833 } | |
834 | |
9852 | 835 int mpeg2_header_extension (mpeg2dec_t * mpeg2dec) |
1 | 836 { |
9852 | 837 static int (* parser[]) (mpeg2dec_t *) = { |
838 0, sequence_ext, sequence_display_ext, quant_matrix_ext, | |
839 copyright_ext, 0, 0, picture_display_ext, picture_coding_ext | |
840 }; | |
841 int ext, ext_bit; | |
842 | |
843 ext = mpeg2dec->chunk_start[0] >> 4; | |
844 ext_bit = 1 << ext; | |
1 | 845 |
9852 | 846 if (!(mpeg2dec->ext_state & ext_bit)) |
847 return 0; /* ignore illegal extensions */ | |
848 mpeg2dec->ext_state &= ~ext_bit; | |
849 return parser[ext] (mpeg2dec); | |
850 } | |
1 | 851 |
9852 | 852 int mpeg2_header_user_data (mpeg2dec_t * mpeg2dec) |
853 { | |
12932 | 854 mpeg2dec->user_data_len += mpeg2dec->chunk_ptr - 1 - mpeg2dec->chunk_start; |
9852 | 855 mpeg2dec->chunk_start = mpeg2dec->chunk_ptr - 1; |
29264
e83eef58b30a
Remove all kind of trailing whitespaces from all MPlayer's files.
bircoph
parents:
27572
diff
changeset
|
856 |
1 | 857 return 0; |
858 } | |
9852 | 859 |
27572 | 860 static void prescale (mpeg2dec_t * mpeg2dec, int idx) |
9852 | 861 { |
12932 | 862 static int non_linear_scale [] = { |
863 0, 1, 2, 3, 4, 5, 6, 7, | |
864 8, 10, 12, 14, 16, 18, 20, 22, | |
865 24, 28, 32, 36, 40, 44, 48, 52, | |
866 56, 64, 72, 80, 88, 96, 104, 112 | |
867 }; | |
868 int i, j, k; | |
869 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
870 | |
27572 | 871 if (mpeg2dec->scaled[idx] != decoder->q_scale_type) { |
872 mpeg2dec->scaled[idx] = decoder->q_scale_type; | |
12932 | 873 for (i = 0; i < 32; i++) { |
27572 | 874 k = decoder->q_scale_type ? non_linear_scale[i] : (i << 1); |
12932 | 875 for (j = 0; j < 64; j++) |
27572 | 876 decoder->quantizer_prescale[idx][i][j] = |
877 k * mpeg2dec->quantizer_matrix[idx][j]; | |
12932 | 878 } |
879 } | |
880 } | |
881 | |
882 mpeg2_state_t mpeg2_header_slice_start (mpeg2dec_t * mpeg2dec) | |
883 { | |
884 mpeg2_decoder_t * decoder = &(mpeg2dec->decoder); | |
885 | |
886 mpeg2dec->info.user_data = NULL; mpeg2dec->info.user_data_len = 0; | |
9852 | 887 mpeg2dec->state = ((mpeg2dec->picture->nb_fields > 1 || |
888 mpeg2dec->state == STATE_PICTURE_2ND) ? | |
889 STATE_SLICE : STATE_SLICE_1ST); | |
890 | |
12932 | 891 if (mpeg2dec->decoder.coding_type != D_TYPE) { |
892 prescale (mpeg2dec, 0); | |
893 if (decoder->chroma_quantizer[0] == decoder->quantizer_prescale[2]) | |
894 prescale (mpeg2dec, 2); | |
895 if (mpeg2dec->decoder.coding_type != I_TYPE) { | |
896 prescale (mpeg2dec, 1); | |
897 if (decoder->chroma_quantizer[1] == decoder->quantizer_prescale[3]) | |
898 prescale (mpeg2dec, 3); | |
899 } | |
900 } | |
901 | |
9852 | 902 if (!(mpeg2dec->nb_decode_slices)) |
903 mpeg2dec->picture->flags |= PIC_FLAG_SKIP; | |
904 else if (mpeg2dec->convert_start) { | |
12932 | 905 mpeg2dec->convert_start (decoder->convert_id, mpeg2dec->fbuf[0], |
906 mpeg2dec->picture, mpeg2dec->info.gop); | |
9852 | 907 |
908 if (mpeg2dec->decoder.coding_type == B_TYPE) | |
909 mpeg2_init_fbuf (&(mpeg2dec->decoder), mpeg2dec->yuv_buf[2], | |
910 mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1], | |
911 mpeg2dec->yuv_buf[mpeg2dec->yuv_index]); | |
912 else { | |
913 mpeg2_init_fbuf (&(mpeg2dec->decoder), | |
914 mpeg2dec->yuv_buf[mpeg2dec->yuv_index ^ 1], | |
915 mpeg2dec->yuv_buf[mpeg2dec->yuv_index], | |
916 mpeg2dec->yuv_buf[mpeg2dec->yuv_index]); | |
917 if (mpeg2dec->state == STATE_SLICE) | |
918 mpeg2dec->yuv_index ^= 1; | |
919 } | |
920 } else { | |
921 int b_type; | |
922 | |
923 b_type = (mpeg2dec->decoder.coding_type == B_TYPE); | |
924 mpeg2_init_fbuf (&(mpeg2dec->decoder), mpeg2dec->fbuf[0]->buf, | |
925 mpeg2dec->fbuf[b_type + 1]->buf, | |
926 mpeg2dec->fbuf[b_type]->buf); | |
927 } | |
928 mpeg2dec->action = NULL; | |
27572 | 929 return STATE_INTERNAL_NORETURN; |
9852 | 930 } |
931 | |
12932 | 932 static mpeg2_state_t seek_sequence (mpeg2dec_t * mpeg2dec) |
9852 | 933 { |
12932 | 934 mpeg2_reset_info (&(mpeg2dec->info)); |
935 mpeg2dec->info.sequence = NULL; | |
936 mpeg2dec->info.gop = NULL; | |
937 mpeg2_header_state_init (mpeg2dec); | |
938 mpeg2dec->action = mpeg2_seek_header; | |
939 return mpeg2_seek_header (mpeg2dec); | |
940 } | |
941 | |
942 mpeg2_state_t mpeg2_header_end (mpeg2dec_t * mpeg2dec) | |
943 { | |
944 mpeg2_picture_t * picture; | |
9852 | 945 int b_type; |
946 | |
12932 | 947 b_type = (mpeg2dec->decoder.coding_type == B_TYPE); |
9852 | 948 picture = mpeg2dec->pictures; |
12932 | 949 if ((mpeg2dec->picture >= picture + 2) ^ b_type) |
9852 | 950 picture = mpeg2dec->pictures + 2; |
951 | |
12932 | 952 mpeg2_reset_info (&(mpeg2dec->info)); |
9852 | 953 if (!(mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY)) { |
954 mpeg2dec->info.display_picture = picture; | |
955 if (picture->nb_fields == 1) | |
956 mpeg2dec->info.display_picture_2nd = picture + 1; | |
957 mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[b_type]; | |
12932 | 958 if (!mpeg2dec->convert) |
9852 | 959 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type + 1]; |
12932 | 960 } else if (!mpeg2dec->convert) |
9852 | 961 mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[b_type]; |
12932 | 962 mpeg2dec->action = seek_sequence; |
9852 | 963 return STATE_END; |
964 } |