3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 2003 Regis Duchesne <hpreg@zoy.org>
5 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
7 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
8 * See http://libmpeg2.sourceforge.net/ for updates.
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.
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.
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
27 #include "mpeg2dec_config.h"
29 extern struct plugin_api
* rb
;
32 #include "attributes.h"
33 #include "mpeg2_internal.h"
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
42 /* default intra quant matrix, in zig-zag order */
43 static const uint8_t default_intra_quantizer_matrix
[64] ICONST_ATTR
= {
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,
61 uint8_t mpeg2_scan_norm
[64] IDATA_ATTR
= {
62 /* Zig-Zag scan pattern */
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
69 uint8_t mpeg2_scan_alt
[64] IDATA_ATTR
= {
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
77 void mpeg2_header_state_init (mpeg2dec_t
* mpeg2dec
)
79 if (mpeg2dec
->sequence
.width
!= (unsigned)-1) {
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]);
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]);
96 if (mpeg2dec
->decoder
.convert_id
)
97 mpeg2_free (mpeg2dec
->decoder
.convert_id
);
99 mpeg2dec
->decoder
.coding_type
= I_TYPE
;
100 mpeg2dec
->decoder
.convert
= NULL
;
101 mpeg2dec
->decoder
.convert_id
= NULL
;
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
;
107 mpeg2dec
->alloc_index
= 0;
108 mpeg2dec
->alloc_index_user
= 0;
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;
117 void mpeg2_reset_info (mpeg2_info_t
* info
)
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
;
124 static void info_user_data (mpeg2dec_t
* mpeg2dec
)
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;
132 int mpeg2_header_sequence (mpeg2dec_t
* mpeg2dec
)
134 uint8_t * buffer
= mpeg2dec
->chunk_start
;
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 */
140 /* unofficial: libmpeg3 "Unofficial economy rates" 5/10/12/15 fps */
141 5400000, 2700000, 2250000, 1800000, 0, 0
145 if ((buffer
[6] & 0x20) != 0x20) /* missing marker_bit */
148 i
= (buffer
[0] << 16) | (buffer
[1] << 8) | buffer
[2];
149 if (! (sequence
->display_width
= sequence
->picture_width
= i
>> 12))
151 if (! (sequence
->display_height
= sequence
->picture_height
= i
& 0xfff))
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;
158 sequence
->flags
= (SEQ_FLAG_PROGRESSIVE_SEQUENCE
|
159 SEQ_VIDEO_FORMAT_UNSPECIFIED
);
161 sequence
->pixel_width
= buffer
[3] >> 4; /* aspect ratio */
162 sequence
->frame_period
= frame_period
[buffer
[3] & 15];
164 sequence
->byte_rate
= (buffer
[4]<<10) | (buffer
[5]<<2) | (buffer
[6]>>6);
166 sequence
->vbv_buffer_size
= ((buffer
[6]<<16)|(buffer
[7]<<8))&0x1ff800;
169 sequence
->flags
|= SEQ_FLAG_CONSTRAINED_PARAMETERS
;
171 mpeg2dec
->copy_matrix
= 3;
173 for (i
= 0; i
< 64; i
++)
174 mpeg2dec
->new_quantizer_matrix
[0][mpeg2_scan_norm
[i
]] =
175 (buffer
[i
+7] << 7) | (buffer
[i
+8] >> 1);
178 for (i
= 0; i
< 64; i
++)
179 mpeg2dec
->new_quantizer_matrix
[0][mpeg2_scan_norm
[i
]] =
180 default_intra_quantizer_matrix
[i
];
183 for (i
= 0; i
< 64; i
++)
184 mpeg2dec
->new_quantizer_matrix
[1][mpeg2_scan_norm
[i
]] =
187 rb
->memset (mpeg2dec
->new_quantizer_matrix
[1], 16, 64);
189 sequence
->profile_level_id
= 0x80;
190 sequence
->colour_primaries
= 0;
191 sequence
->transfer_characteristics
= 0;
192 sequence
->matrix_coefficients
= 0;
194 mpeg2dec
->ext_state
= SEQ_EXT
;
195 mpeg2dec
->state
= STATE_SEQUENCE
;
196 mpeg2dec
->display_offset_x
= mpeg2dec
->display_offset_y
= 0;
201 static int sequence_ext (mpeg2dec_t
* mpeg2dec
)
203 uint8_t * buffer
= mpeg2dec
->chunk_start
;
204 mpeg2_sequence_t
* sequence
= &(mpeg2dec
->new_sequence
);
207 if (!(buffer
[3] & 1))
210 sequence
->profile_level_id
= (buffer
[0] << 4) | (buffer
[1] >> 4);
212 sequence
->display_width
= sequence
->picture_width
+=
213 ((buffer
[1] << 13) | (buffer
[2] << 5)) & 0x3000;
214 sequence
->display_height
= sequence
->picture_height
+=
215 (buffer
[2] << 7) & 0x3000;
216 sequence
->width
= (sequence
->picture_width
+ 15) & ~15;
217 sequence
->height
= (sequence
->picture_height
+ 15) & ~15;
218 flags
= sequence
->flags
| SEQ_FLAG_MPEG2
;
219 if (!(buffer
[1] & 8)) {
220 flags
&= ~SEQ_FLAG_PROGRESSIVE_SEQUENCE
;
221 sequence
->height
= (sequence
->height
+ 31) & ~31;
223 if (buffer
[5] & 0x80)
224 flags
|= SEQ_FLAG_LOW_DELAY
;
225 sequence
->flags
= flags
;
226 sequence
->chroma_width
= sequence
->width
;
227 sequence
->chroma_height
= sequence
->height
;
228 switch (buffer
[1] & 6) {
229 case 0: /* invalid */
232 sequence
->chroma_height
>>= 1;
234 sequence
->chroma_width
>>= 1;
237 sequence
->byte_rate
+= ((buffer
[2]<<25) | (buffer
[3]<<17)) & 0x3ffc0000;
239 sequence
->vbv_buffer_size
|= buffer
[4] << 21;
241 sequence
->frame_period
=
242 sequence
->frame_period
* ((buffer
[5]&31)+1) / (((buffer
[5]>>2)&3)+1);
244 mpeg2dec
->ext_state
= SEQ_DISPLAY_EXT
;
249 static int sequence_display_ext (mpeg2dec_t
* mpeg2dec
)
251 uint8_t * buffer
= mpeg2dec
->chunk_start
;
252 mpeg2_sequence_t
* sequence
= &(mpeg2dec
->new_sequence
);
255 flags
= ((sequence
->flags
& ~SEQ_MASK_VIDEO_FORMAT
) |
256 ((buffer
[0]<<4) & SEQ_MASK_VIDEO_FORMAT
));
258 flags
|= SEQ_FLAG_COLOUR_DESCRIPTION
;
259 sequence
->colour_primaries
= buffer
[1];
260 sequence
->transfer_characteristics
= buffer
[2];
261 sequence
->matrix_coefficients
= buffer
[3];
265 if (!(buffer
[2] & 2)) /* missing marker_bit */
268 sequence
->display_width
= (buffer
[1] << 6) | (buffer
[2] >> 2);
269 sequence
->display_height
=
270 ((buffer
[2]& 1 ) << 13) | (buffer
[3] << 5) | (buffer
[4] >> 3);
275 static inline void finalize_sequence (mpeg2_sequence_t
* sequence
)
280 sequence
->byte_rate
*= 50;
282 if (sequence
->flags
& SEQ_FLAG_MPEG2
) {
283 switch (sequence
->pixel_width
) {
284 case 1: /* square pixels */
285 sequence
->pixel_width
= sequence
->pixel_height
= 1; return;
286 case 2: /* 4:3 aspect ratio */
287 width
= 4; height
= 3; break;
288 case 3: /* 16:9 aspect ratio */
289 width
= 16; height
= 9; break;
290 case 4: /* 2.21:1 aspect ratio */
291 width
= 221; height
= 100; break;
292 default: /* illegal */
293 sequence
->pixel_width
= sequence
->pixel_height
= 0; return;
295 width
*= sequence
->display_height
;
296 height
*= sequence
->display_width
;
299 if (sequence
->byte_rate
== 50 * 0x3ffff)
300 sequence
->byte_rate
= 0; /* mpeg-1 VBR */
302 switch (sequence
->pixel_width
) {
303 case 0: case 15: /* illegal */
304 sequence
->pixel_width
= sequence
->pixel_height
= 0; return;
305 case 1: /* square pixels */
306 sequence
->pixel_width
= sequence
->pixel_height
= 1; return;
307 case 3: /* 720x576 16:9 */
308 sequence
->pixel_width
= 64; sequence
->pixel_height
= 45; return;
309 case 6: /* 720x480 16:9 */
310 sequence
->pixel_width
= 32; sequence
->pixel_height
= 27; return;
311 case 12: /* 720*480 4:3 */
312 sequence
->pixel_width
= 8; sequence
->pixel_height
= 9; return;
314 height
= 88 * sequence
->pixel_width
+ 1171;
319 sequence
->pixel_width
= width
;
320 sequence
->pixel_height
= height
;
321 while (width
) { /* find greatest common divisor */
323 width
= height
% tmp
;
326 sequence
->pixel_width
/= height
;
327 sequence
->pixel_height
/= height
;
330 static void copy_matrix (mpeg2dec_t
* mpeg2dec
, int index
)
332 if (rb
->memcmp (mpeg2dec
->quantizer_matrix
[index
],
333 mpeg2dec
->new_quantizer_matrix
[index
], 64)) {
334 rb
->memcpy (mpeg2dec
->quantizer_matrix
[index
],
335 mpeg2dec
->new_quantizer_matrix
[index
], 64);
336 mpeg2dec
->scaled
[index
] = -1;
340 static void finalize_matrix (mpeg2dec_t
* mpeg2dec
)
342 mpeg2_decoder_t
* decoder
= &(mpeg2dec
->decoder
);
345 for (i
= 0; i
< 2; i
++) {
346 if (mpeg2dec
->copy_matrix
& (1 << i
))
347 copy_matrix (mpeg2dec
, i
);
348 if ((mpeg2dec
->copy_matrix
& (4 << i
)) &&
349 rb
->memcmp (mpeg2dec
->quantizer_matrix
[i
],
350 mpeg2dec
->new_quantizer_matrix
[i
+2], 64)) {
351 copy_matrix (mpeg2dec
, i
+ 2);
352 decoder
->chroma_quantizer
[i
] = decoder
->quantizer_prescale
[i
+2];
353 } else if (mpeg2dec
->copy_matrix
& (5 << i
))
354 decoder
->chroma_quantizer
[i
] = decoder
->quantizer_prescale
[i
];
358 static mpeg2_state_t
invalid_end_action (mpeg2dec_t
* mpeg2dec
)
360 mpeg2_reset_info (&(mpeg2dec
->info
));
361 mpeg2dec
->info
.gop
= NULL
;
362 info_user_data (mpeg2dec
);
363 mpeg2_header_state_init (mpeg2dec
);
364 mpeg2dec
->sequence
= mpeg2dec
->new_sequence
;
365 mpeg2dec
->action
= mpeg2_seek_header
;
366 mpeg2dec
->state
= STATE_SEQUENCE
;
367 return STATE_SEQUENCE
;
370 void mpeg2_header_sequence_finalize (mpeg2dec_t
* mpeg2dec
)
372 mpeg2_sequence_t
* sequence
= &(mpeg2dec
->new_sequence
);
373 mpeg2_decoder_t
* decoder
= &(mpeg2dec
->decoder
);
375 finalize_sequence (sequence
);
376 finalize_matrix (mpeg2dec
);
378 decoder
->mpeg1
= !(sequence
->flags
& SEQ_FLAG_MPEG2
);
379 decoder
->width
= sequence
->width
;
380 decoder
->height
= sequence
->height
;
381 decoder
->vertical_position_extension
= (sequence
->picture_height
> 2800);
382 decoder
->chroma_format
= ((sequence
->chroma_width
== sequence
->width
) +
383 (sequence
->chroma_height
== sequence
->height
));
385 if (mpeg2dec
->sequence
.width
!= (unsigned)-1) {
386 unsigned int new_byte_rate
;
389 * According to 6.1.1.6, repeat sequence headers should be
390 * identical to the original. However some DVDs dont respect
391 * that and have different bitrates in the repeat sequence
392 * headers. So we'll ignore that in the comparison and still
393 * consider these as repeat sequence headers.
395 * However, be careful not to alter the current sequence when
396 * returning STATE_INVALID_END.
398 new_byte_rate
= sequence
->byte_rate
;
399 sequence
->byte_rate
= mpeg2dec
->sequence
.byte_rate
;
400 if (rb
->memcmp (&(mpeg2dec
->sequence
), sequence
,
401 sizeof (mpeg2_sequence_t
))) {
402 decoder
->stride_frame
= sequence
->width
;
403 sequence
->byte_rate
= new_byte_rate
;
404 mpeg2_header_end (mpeg2dec
);
405 mpeg2dec
->action
= invalid_end_action
;
406 mpeg2dec
->state
= STATE_INVALID_END
;
409 sequence
->byte_rate
= new_byte_rate
;
410 mpeg2dec
->state
= STATE_SEQUENCE_REPEATED
;
412 decoder
->stride_frame
= sequence
->width
;
413 mpeg2dec
->sequence
= *sequence
;
414 mpeg2_reset_info (&(mpeg2dec
->info
));
415 mpeg2dec
->info
.sequence
= &(mpeg2dec
->sequence
);
416 mpeg2dec
->info
.gop
= NULL
;
417 info_user_data (mpeg2dec
);
420 int mpeg2_header_gop (mpeg2dec_t
* mpeg2dec
)
422 uint8_t * buffer
= mpeg2dec
->chunk_start
;
423 mpeg2_gop_t
* gop
= &(mpeg2dec
->new_gop
);
425 if (! (buffer
[1] & 8))
427 gop
->hours
= (buffer
[0] >> 2) & 31;
428 gop
->minutes
= ((buffer
[0] << 4) | (buffer
[1] >> 4)) & 63;
429 gop
->seconds
= ((buffer
[1] << 3) | (buffer
[2] >> 5)) & 63;
430 gop
->pictures
= ((buffer
[2] << 1) | (buffer
[3] >> 7)) & 63;
431 gop
->flags
= (buffer
[0] >> 7) | ((buffer
[3] >> 4) & 6);
432 mpeg2dec
->state
= STATE_GOP
;
436 void mpeg2_header_gop_finalize (mpeg2dec_t
* mpeg2dec
)
438 mpeg2dec
->gop
= mpeg2dec
->new_gop
;
439 mpeg2_reset_info (&(mpeg2dec
->info
));
440 mpeg2dec
->info
.gop
= &(mpeg2dec
->gop
);
441 info_user_data (mpeg2dec
);
444 void mpeg2_set_fbuf (mpeg2dec_t
* mpeg2dec
, int b_type
)
448 for (i
= 0; i
< 3; i
++)
449 if (mpeg2dec
->fbuf
[1] != &mpeg2dec
->fbuf_alloc
[i
].fbuf
&&
450 mpeg2dec
->fbuf
[2] != &mpeg2dec
->fbuf_alloc
[i
].fbuf
) {
451 mpeg2dec
->fbuf
[0] = &mpeg2dec
->fbuf_alloc
[i
].fbuf
;
452 mpeg2dec
->info
.current_fbuf
= mpeg2dec
->fbuf
[0];
453 if (b_type
|| (mpeg2dec
->sequence
.flags
& SEQ_FLAG_LOW_DELAY
)) {
454 if (b_type
|| mpeg2dec
->convert
)
455 mpeg2dec
->info
.discard_fbuf
= mpeg2dec
->fbuf
[0];
456 mpeg2dec
->info
.display_fbuf
= mpeg2dec
->fbuf
[0];
462 mpeg2_state_t
mpeg2_header_picture_start (mpeg2dec_t
* mpeg2dec
)
464 mpeg2_picture_t
* picture
= &(mpeg2dec
->new_picture
);
466 mpeg2dec
->state
= ((mpeg2dec
->state
!= STATE_SLICE_1ST
) ?
467 STATE_PICTURE
: STATE_PICTURE_2ND
);
469 picture
->tag
= picture
->tag2
= 0;
470 if (mpeg2dec
->num_tags
) {
471 if (mpeg2dec
->bytes_since_tag
>= 4) {
472 mpeg2dec
->num_tags
= 0;
473 picture
->tag
= mpeg2dec
->tag_current
;
474 picture
->tag2
= mpeg2dec
->tag2_current
;
475 picture
->flags
= PIC_FLAG_TAGS
;
476 } else if (mpeg2dec
->num_tags
> 1) {
477 mpeg2dec
->num_tags
= 1;
478 picture
->tag
= mpeg2dec
->tag_previous
;
479 picture
->tag2
= mpeg2dec
->tag2_previous
;
480 picture
->flags
= PIC_FLAG_TAGS
;
483 picture
->display_offset
[0].x
= picture
->display_offset
[1].x
=
484 picture
->display_offset
[2].x
= mpeg2dec
->display_offset_x
;
485 picture
->display_offset
[0].y
= picture
->display_offset
[1].y
=
486 picture
->display_offset
[2].y
= mpeg2dec
->display_offset_y
;
487 return mpeg2_parse_header (mpeg2dec
);
490 int mpeg2_header_picture (mpeg2dec_t
* mpeg2dec
)
492 uint8_t * buffer
= mpeg2dec
->chunk_start
;
493 mpeg2_picture_t
* picture
= &(mpeg2dec
->new_picture
);
494 mpeg2_decoder_t
* decoder
= &(mpeg2dec
->decoder
);
497 type
= (buffer
[1] >> 3) & 7;
498 mpeg2dec
->ext_state
= PIC_CODING_EXT
;
500 picture
->temporal_reference
= (buffer
[0] << 2) | (buffer
[1] >> 6);
502 picture
->flags
|= type
;
504 if (type
== PIC_FLAG_CODING_TYPE_P
|| type
== PIC_FLAG_CODING_TYPE_B
) {
505 /* forward_f_code and backward_f_code - used in mpeg1 only */
506 decoder
->f_motion
.f_code
[1] = (buffer
[3] >> 2) & 1;
507 decoder
->f_motion
.f_code
[0] =
508 (((buffer
[3] << 1) | (buffer
[4] >> 7)) & 7) - 1;
509 decoder
->b_motion
.f_code
[1] = (buffer
[4] >> 6) & 1;
510 decoder
->b_motion
.f_code
[0] = ((buffer
[4] >> 3) & 7) - 1;
513 /* XXXXXX decode extra_information_picture as well */
515 picture
->nb_fields
= 2;
517 mpeg2dec
->q_scale_type
= 0;
518 decoder
->intra_dc_precision
= 7;
519 decoder
->frame_pred_frame_dct
= 1;
520 decoder
->concealment_motion_vectors
= 0;
521 decoder
->scan
= mpeg2_scan_norm
;
522 decoder
->picture_structure
= FRAME_PICTURE
;
523 mpeg2dec
->copy_matrix
= 0;
528 static int picture_coding_ext (mpeg2dec_t
* mpeg2dec
)
530 uint8_t * buffer
= mpeg2dec
->chunk_start
;
531 mpeg2_picture_t
* picture
= &(mpeg2dec
->new_picture
);
532 mpeg2_decoder_t
* decoder
= &(mpeg2dec
->decoder
);
535 /* pre subtract 1 for use later in compute_motion_vector */
536 decoder
->f_motion
.f_code
[0] = (buffer
[0] & 15) - 1;
537 decoder
->f_motion
.f_code
[1] = (buffer
[1] >> 4) - 1;
538 decoder
->b_motion
.f_code
[0] = (buffer
[1] & 15) - 1;
539 decoder
->b_motion
.f_code
[1] = (buffer
[2] >> 4) - 1;
541 flags
= picture
->flags
;
542 decoder
->intra_dc_precision
= 7 - ((buffer
[2] >> 2) & 3);
543 decoder
->picture_structure
= buffer
[2] & 3;
544 switch (decoder
->picture_structure
) {
546 flags
|= PIC_FLAG_TOP_FIELD_FIRST
;
548 picture
->nb_fields
= 1;
551 if (!(mpeg2dec
->sequence
.flags
& SEQ_FLAG_PROGRESSIVE_SEQUENCE
)) {
552 picture
->nb_fields
= (buffer
[3] & 2) ? 3 : 2;
553 flags
|= (buffer
[3] & 128) ? PIC_FLAG_TOP_FIELD_FIRST
: 0;
555 picture
->nb_fields
= (buffer
[3]&2) ? ((buffer
[3]&128) ? 6 : 4) : 2;
560 decoder
->top_field_first
= buffer
[3] >> 7;
561 decoder
->frame_pred_frame_dct
= (buffer
[3] >> 6) & 1;
562 decoder
->concealment_motion_vectors
= (buffer
[3] >> 5) & 1;
563 mpeg2dec
->q_scale_type
= buffer
[3] & 16;
564 decoder
->intra_vlc_format
= (buffer
[3] >> 3) & 1;
565 decoder
->scan
= (buffer
[3] & 4) ? mpeg2_scan_alt
: mpeg2_scan_norm
;
566 flags
|= (buffer
[4] & 0x80) ? PIC_FLAG_PROGRESSIVE_FRAME
: 0;
567 if (buffer
[4] & 0x40)
568 flags
|= (((buffer
[4]<<26) | (buffer
[5]<<18) | (buffer
[6]<<10)) &
569 PIC_MASK_COMPOSITE_DISPLAY
) | PIC_FLAG_COMPOSITE_DISPLAY
;
570 picture
->flags
= flags
;
572 mpeg2dec
->ext_state
= PIC_DISPLAY_EXT
| COPYRIGHT_EXT
| QUANT_MATRIX_EXT
;
577 static int picture_display_ext (mpeg2dec_t
* mpeg2dec
)
579 uint8_t * buffer
= mpeg2dec
->chunk_start
;
580 mpeg2_picture_t
* picture
= &(mpeg2dec
->new_picture
);
583 nb_pos
= picture
->nb_fields
;
584 if (mpeg2dec
->sequence
.flags
& SEQ_FLAG_PROGRESSIVE_SEQUENCE
)
587 for (i
= 0; i
< nb_pos
; i
++) {
590 x
= ((buffer
[4*i
] << 24) | (buffer
[4*i
+1] << 16) |
591 (buffer
[4*i
+2] << 8) | buffer
[4*i
+3]) >> (11-2*i
);
592 y
= ((buffer
[4*i
+2] << 24) | (buffer
[4*i
+3] << 16) |
593 (buffer
[4*i
+4] << 8) | buffer
[4*i
+5]) >> (10-2*i
);
596 picture
->display_offset
[i
].x
= mpeg2dec
->display_offset_x
= x
>> 1;
597 picture
->display_offset
[i
].y
= mpeg2dec
->display_offset_y
= y
>> 1;
600 picture
->display_offset
[i
].x
= mpeg2dec
->display_offset_x
;
601 picture
->display_offset
[i
].y
= mpeg2dec
->display_offset_y
;
606 void mpeg2_header_picture_finalize (mpeg2dec_t
* mpeg2dec
)
608 mpeg2_decoder_t
* decoder
= &(mpeg2dec
->decoder
);
609 int old_type_b
= (decoder
->coding_type
== B_TYPE
);
610 int low_delay
= mpeg2dec
->sequence
.flags
& SEQ_FLAG_LOW_DELAY
;
612 finalize_matrix (mpeg2dec
);
613 decoder
->coding_type
= mpeg2dec
->new_picture
.flags
& PIC_MASK_CODING_TYPE
;
615 if (mpeg2dec
->state
== STATE_PICTURE
) {
616 mpeg2_picture_t
* picture
;
617 mpeg2_picture_t
* other
;
619 decoder
->second_field
= 0;
621 picture
= other
= mpeg2dec
->pictures
;
622 if (old_type_b
^ (mpeg2dec
->picture
< mpeg2dec
->pictures
+ 2))
626 mpeg2dec
->picture
= picture
;
627 *picture
= mpeg2dec
->new_picture
;
630 mpeg2dec
->fbuf
[2] = mpeg2dec
->fbuf
[1];
631 mpeg2dec
->fbuf
[1] = mpeg2dec
->fbuf
[0];
633 mpeg2dec
->fbuf
[0] = NULL
;
634 mpeg2_reset_info (&(mpeg2dec
->info
));
635 mpeg2dec
->info
.current_picture
= picture
;
636 mpeg2dec
->info
.display_picture
= picture
;
637 if (decoder
->coding_type
!= B_TYPE
) {
639 if (mpeg2dec
->first
) {
640 mpeg2dec
->info
.display_picture
= NULL
;
643 mpeg2dec
->info
.display_picture
= other
;
644 if (other
->nb_fields
== 1)
645 mpeg2dec
->info
.display_picture_2nd
= other
+ 1;
646 mpeg2dec
->info
.display_fbuf
= mpeg2dec
->fbuf
[1];
649 if (!low_delay
+ !mpeg2dec
->convert
)
650 mpeg2dec
->info
.discard_fbuf
=
651 mpeg2dec
->fbuf
[!low_delay
+ !mpeg2dec
->convert
];
653 if (mpeg2dec
->convert
) {
654 mpeg2_convert_init_t convert_init
;
655 if (!mpeg2dec
->convert_start
) {
658 mpeg2dec
->decoder
.convert_id
=
659 mpeg2_malloc (mpeg2dec
->convert_id_size
,
660 MPEG2_ALLOC_CONVERT_ID
);
661 mpeg2dec
->convert (MPEG2_CONVERT_START
,
662 mpeg2dec
->decoder
.convert_id
,
663 &(mpeg2dec
->sequence
),
664 mpeg2dec
->convert_stride
,
665 mpeg2dec
->convert_arg
, &convert_init
);
666 mpeg2dec
->convert_start
= convert_init
.start
;
667 mpeg2dec
->decoder
.convert
= convert_init
.copy
;
669 y_size
= decoder
->stride_frame
* mpeg2dec
->sequence
.height
;
670 uv_size
= y_size
>> (2 - mpeg2dec
->decoder
.chroma_format
);
671 mpeg2dec
->yuv_buf
[0][0] =
672 (uint8_t *) mpeg2_malloc (y_size
, MPEG2_ALLOC_YUV
);
673 mpeg2dec
->yuv_buf
[0][1] =
674 (uint8_t *) mpeg2_malloc (uv_size
, MPEG2_ALLOC_YUV
);
675 mpeg2dec
->yuv_buf
[0][2] =
676 (uint8_t *) mpeg2_malloc (uv_size
, MPEG2_ALLOC_YUV
);
677 mpeg2dec
->yuv_buf
[1][0] =
678 (uint8_t *) mpeg2_malloc (y_size
, MPEG2_ALLOC_YUV
);
679 mpeg2dec
->yuv_buf
[1][1] =
680 (uint8_t *) mpeg2_malloc (uv_size
, MPEG2_ALLOC_YUV
);
681 mpeg2dec
->yuv_buf
[1][2] =
682 (uint8_t *) mpeg2_malloc (uv_size
, MPEG2_ALLOC_YUV
);
683 y_size
= decoder
->stride_frame
* 32;
684 uv_size
= y_size
>> (2 - mpeg2dec
->decoder
.chroma_format
);
685 mpeg2dec
->yuv_buf
[2][0] =
686 (uint8_t *) mpeg2_malloc (y_size
, MPEG2_ALLOC_YUV
);
687 mpeg2dec
->yuv_buf
[2][1] =
688 (uint8_t *) mpeg2_malloc (uv_size
, MPEG2_ALLOC_YUV
);
689 mpeg2dec
->yuv_buf
[2][2] =
690 (uint8_t *) mpeg2_malloc (uv_size
, MPEG2_ALLOC_YUV
);
692 if (!mpeg2dec
->custom_fbuf
) {
693 while (mpeg2dec
->alloc_index
< 3) {
696 fbuf
= &mpeg2dec
->fbuf_alloc
[mpeg2dec
->alloc_index
++].fbuf
;
699 (uint8_t *) mpeg2_malloc (convert_init
.buf_size
[0],
700 MPEG2_ALLOC_CONVERTED
);
702 (uint8_t *) mpeg2_malloc (convert_init
.buf_size
[1],
703 MPEG2_ALLOC_CONVERTED
);
705 (uint8_t *) mpeg2_malloc (convert_init
.buf_size
[2],
706 MPEG2_ALLOC_CONVERTED
);
708 mpeg2_set_fbuf (mpeg2dec
, (decoder
->coding_type
== B_TYPE
));
710 } else if (!mpeg2dec
->custom_fbuf
) {
711 while (mpeg2dec
->alloc_index
< 3) {
715 fbuf
= &(mpeg2dec
->fbuf_alloc
[mpeg2dec
->alloc_index
++].fbuf
);
717 y_size
= decoder
->stride_frame
* mpeg2dec
->sequence
.height
;
718 uv_size
= y_size
>> (2 - decoder
->chroma_format
);
719 fbuf
->buf
[0] = (uint8_t *) mpeg2_malloc (y_size
,
721 fbuf
->buf
[1] = (uint8_t *) mpeg2_malloc (uv_size
,
723 fbuf
->buf
[2] = (uint8_t *) mpeg2_malloc (uv_size
,
726 mpeg2_set_fbuf (mpeg2dec
, (decoder
->coding_type
== B_TYPE
));
729 decoder
->second_field
= 1;
730 mpeg2dec
->picture
++; /* second field picture */
731 *(mpeg2dec
->picture
) = mpeg2dec
->new_picture
;
732 mpeg2dec
->info
.current_picture_2nd
= mpeg2dec
->picture
;
733 if (low_delay
|| decoder
->coding_type
== B_TYPE
)
734 mpeg2dec
->info
.display_picture_2nd
= mpeg2dec
->picture
;
737 info_user_data (mpeg2dec
);
740 static int copyright_ext (mpeg2dec_t
* mpeg2dec
)
746 static int quant_matrix_ext (mpeg2dec_t
* mpeg2dec
)
748 uint8_t * buffer
= mpeg2dec
->chunk_start
;
751 for (i
= 0; i
< 4; i
++)
752 if (buffer
[0] & (8 >> i
)) {
753 for (j
= 0; j
< 64; j
++)
754 mpeg2dec
->new_quantizer_matrix
[i
][mpeg2_scan_norm
[j
]] =
755 (buffer
[j
] << (i
+5)) | (buffer
[j
+1] >> (3-i
));
756 mpeg2dec
->copy_matrix
|= 1 << i
;
763 int mpeg2_header_extension (mpeg2dec_t
* mpeg2dec
)
765 static int (* parser
[]) (mpeg2dec_t
*) = {
766 0, sequence_ext
, sequence_display_ext
, quant_matrix_ext
,
767 copyright_ext
, 0, 0, picture_display_ext
, picture_coding_ext
771 ext
= mpeg2dec
->chunk_start
[0] >> 4;
774 if (!(mpeg2dec
->ext_state
& ext_bit
))
775 return 0; /* ignore illegal extensions */
776 mpeg2dec
->ext_state
&= ~ext_bit
;
777 return parser
[ext
] (mpeg2dec
);
780 int mpeg2_header_user_data (mpeg2dec_t
* mpeg2dec
)
782 mpeg2dec
->user_data_len
+= mpeg2dec
->chunk_ptr
- 1 - mpeg2dec
->chunk_start
;
783 mpeg2dec
->chunk_start
= mpeg2dec
->chunk_ptr
- 1;
788 static void prescale (mpeg2dec_t
* mpeg2dec
, int index
)
790 static int non_linear_scale
[] = {
791 0, 1, 2, 3, 4, 5, 6, 7,
792 8, 10, 12, 14, 16, 18, 20, 22,
793 24, 28, 32, 36, 40, 44, 48, 52,
794 56, 64, 72, 80, 88, 96, 104, 112
797 mpeg2_decoder_t
* decoder
= &(mpeg2dec
->decoder
);
799 if (mpeg2dec
->scaled
[index
] != mpeg2dec
->q_scale_type
) {
800 mpeg2dec
->scaled
[index
] = mpeg2dec
->q_scale_type
;
801 for (i
= 0; i
< 32; i
++) {
802 k
= mpeg2dec
->q_scale_type
? non_linear_scale
[i
] : (i
<< 1);
803 for (j
= 0; j
< 64; j
++)
804 decoder
->quantizer_prescale
[index
][i
][j
] =
805 k
* mpeg2dec
->quantizer_matrix
[index
][j
];
810 mpeg2_state_t
mpeg2_header_slice_start (mpeg2dec_t
* mpeg2dec
)
812 mpeg2_decoder_t
* decoder
= &(mpeg2dec
->decoder
);
814 mpeg2dec
->info
.user_data
= NULL
; mpeg2dec
->info
.user_data_len
= 0;
815 mpeg2dec
->state
= ((mpeg2dec
->picture
->nb_fields
> 1 ||
816 mpeg2dec
->state
== STATE_PICTURE_2ND
) ?
817 STATE_SLICE
: STATE_SLICE_1ST
);
819 if (mpeg2dec
->decoder
.coding_type
!= D_TYPE
) {
820 prescale (mpeg2dec
, 0);
821 if (decoder
->chroma_quantizer
[0] == decoder
->quantizer_prescale
[2])
822 prescale (mpeg2dec
, 2);
823 if (mpeg2dec
->decoder
.coding_type
!= I_TYPE
) {
824 prescale (mpeg2dec
, 1);
825 if (decoder
->chroma_quantizer
[1] == decoder
->quantizer_prescale
[3])
826 prescale (mpeg2dec
, 3);
830 if (!(mpeg2dec
->nb_decode_slices
))
831 mpeg2dec
->picture
->flags
|= PIC_FLAG_SKIP
;
832 else if (mpeg2dec
->convert_start
) {
833 mpeg2dec
->convert_start (decoder
->convert_id
, mpeg2dec
->fbuf
[0],
834 mpeg2dec
->picture
, mpeg2dec
->info
.gop
);
836 if (mpeg2dec
->decoder
.coding_type
== B_TYPE
)
837 mpeg2_init_fbuf (&(mpeg2dec
->decoder
), mpeg2dec
->yuv_buf
[2],
838 mpeg2dec
->yuv_buf
[mpeg2dec
->yuv_index
^ 1],
839 mpeg2dec
->yuv_buf
[mpeg2dec
->yuv_index
]);
841 mpeg2_init_fbuf (&(mpeg2dec
->decoder
),
842 mpeg2dec
->yuv_buf
[mpeg2dec
->yuv_index
^ 1],
843 mpeg2dec
->yuv_buf
[mpeg2dec
->yuv_index
],
844 mpeg2dec
->yuv_buf
[mpeg2dec
->yuv_index
]);
845 if (mpeg2dec
->state
== STATE_SLICE
)
846 mpeg2dec
->yuv_index
^= 1;
851 b_type
= (mpeg2dec
->decoder
.coding_type
== B_TYPE
);
852 mpeg2_init_fbuf (&(mpeg2dec
->decoder
), mpeg2dec
->fbuf
[0]->buf
,
853 mpeg2dec
->fbuf
[b_type
+ 1]->buf
,
854 mpeg2dec
->fbuf
[b_type
]->buf
);
856 mpeg2dec
->action
= NULL
;
857 return (mpeg2_state_t
)-1;
860 static mpeg2_state_t
seek_sequence (mpeg2dec_t
* mpeg2dec
)
862 mpeg2_reset_info (&(mpeg2dec
->info
));
863 mpeg2dec
->info
.sequence
= NULL
;
864 mpeg2dec
->info
.gop
= NULL
;
865 mpeg2_header_state_init (mpeg2dec
);
866 mpeg2dec
->action
= mpeg2_seek_header
;
867 return mpeg2_seek_header (mpeg2dec
);
870 mpeg2_state_t
mpeg2_header_end (mpeg2dec_t
* mpeg2dec
)
872 mpeg2_picture_t
* picture
;
875 b_type
= (mpeg2dec
->decoder
.coding_type
== B_TYPE
);
876 picture
= mpeg2dec
->pictures
;
877 if ((mpeg2dec
->picture
>= picture
+ 2) ^ b_type
)
878 picture
= mpeg2dec
->pictures
+ 2;
880 mpeg2_reset_info (&(mpeg2dec
->info
));
881 if (!(mpeg2dec
->sequence
.flags
& SEQ_FLAG_LOW_DELAY
)) {
882 mpeg2dec
->info
.display_picture
= picture
;
883 if (picture
->nb_fields
== 1)
884 mpeg2dec
->info
.display_picture_2nd
= picture
+ 1;
885 mpeg2dec
->info
.display_fbuf
= mpeg2dec
->fbuf
[b_type
];
886 if (!mpeg2dec
->convert
)
887 mpeg2dec
->info
.discard_fbuf
= mpeg2dec
->fbuf
[b_type
+ 1];
888 } else if (!mpeg2dec
->convert
)
889 mpeg2dec
->info
.discard_fbuf
= mpeg2dec
->fbuf
[b_type
];
890 mpeg2dec
->action
= seek_sequence
;