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