1 #include "../libzmpeg3.h"
10 #if defined(__x86_64__)
11 #define m_(v) (*(mmx_t*)(v))
13 #define m_(v) (*(char*)(v))
22 data = new uint8_t[buffer_allocation=4096];
35 uint8_t *zslice_buffer_t::
36 expand_buffer(int bfrsz)
38 long new_allocation = 2*buffer_allocation;
39 uint8_t *new_buffer = new uint8_t[new_allocation];
40 if( bfrsz > 0 ) memcpy(new_buffer,data,bfrsz);
43 buffer_allocation = new_allocation;
44 buffer_size = buffer_allocation-bfrsz-4;
49 void zslice_buffer_t::
50 fill_buffer(zbits_t *vstream)
53 vstream->next_byte_align();
54 demuxer_t *demux = vstream->demuxer;
55 /* sync stream to zcode and access demuxer */
56 uint32_t zcode = vstream->show_bits32_noptr();
57 for( int i=32; i>0; ) *sbp++ = (zcode>>(i-=8));
59 buffer_size = buffer_allocation-(4+4); /* sizeof(zcode)+padding */
61 /* Read the slice into the buffer including the slice start code */
62 while( !vstream->eof() ) {
63 if( buffer_size <= 0 ) sbp = expand_buffer(sbp - data);
64 if( (zcode&0xff) != 0 ) {
66 zcode = (zcode<<8) | (*sbp++ = demux->read_char());
67 zcode = (zcode<<8) | (*sbp++ = demux->read_char());
71 zcode = (zcode<<8) | (*sbp++ = demux->read_char());
72 if( (zcode&0xffffff) == PACKET_START_CODE_PREFIX ) break;
75 /* finish trailing code as picture start code (0x100) */
77 buffer_size = sbp - data;
78 /* reload stream bfr, position to start code */
79 vstream->reset(zcode);
80 vstream->get_byte_noptr();
83 /* limit coefficients to -2048..2047 */
85 /* move/add 8x8-Block from block[comp] to refframe */
87 int zslice_decoder_t::
88 add_block(int comp, int bx, int by, int dct_type, int addflag)
93 /* color component index */
94 cc = (comp < 4) ? 0 : (comp & 1) + 1;
98 if( video->pict_struct == video_t::pics_FRAME_PICTURE ) {
100 /* field DCT coding */
101 rfp = video->newframe[0] + video->coded_picture_width *
102 (by + ((comp & 2) >> 1)) + bx + ((comp & 1) << 3);
103 iincr = (video->coded_picture_width << 1);
106 /* frame DCT coding */
107 rfp = video->newframe[0] + video->coded_picture_width *
108 (by + ((comp & 2) << 2)) + bx + ((comp & 1) << 3);
109 iincr = video->coded_picture_width;
114 rfp = video->newframe[0] + (video->coded_picture_width << 1) *
115 (by + ((comp & 2) << 2)) + bx + ((comp & 1) << 3);
116 iincr = (video->coded_picture_width << 1);
122 /* scale coordinates */
123 if( video->chroma_format != video_t::cfmt_444 ) bx >>= 1;
124 if( video->chroma_format == video_t::cfmt_420 ) by >>= 1;
125 if( video->pict_struct == video_t::pics_FRAME_PICTURE ) {
126 if( dct_type && (video->chroma_format != video_t::cfmt_420) ) {
127 /* field DCT coding */
128 rfp = video->newframe[cc] + video->chrom_width *
129 (by + ((comp & 2) >> 1)) + bx + (comp & 8);
130 iincr = (video->chrom_width << 1);
133 /* frame DCT coding */
134 rfp = video->newframe[cc] + video->chrom_width *
135 (by + ((comp & 2) << 2)) + bx + (comp & 8);
136 iincr = video->chrom_width;
141 rfp = video->newframe[cc] + (video->chrom_width << 1) *
142 (by + ((comp & 2) << 2)) + bx + (comp & 8);
143 iincr = (video->chrom_width << 1);
150 for( i=0; i<8; ++i ) {
152 rfp[0] = clip(bp[0] + rfp[0]);
153 rfp[1] = clip(bp[1] + rfp[1]);
154 rfp[2] = clip(bp[2] + rfp[2]);
155 rfp[3] = clip(bp[3] + rfp[3]);
156 rfp[4] = clip(bp[4] + rfp[4]);
157 rfp[5] = clip(bp[5] + rfp[5]);
158 rfp[6] = clip(bp[6] + rfp[6]);
159 rfp[7] = clip(bp[7] + rfp[7]);
161 movq_m2r(m_(rfp),mm1); /* rfp[0..7] */
162 pxor_r2r(mm3,mm3); /* zero */
163 pxor_r2r(mm4,mm4); /* zero */
164 movq_m2r(m_(bp+0),mm5); /* bp[0..3] */
165 movq_r2r(mm1,mm2); /* copy rfp[0..7] */
166 movq_m2r(m_(bp+4),mm6); /* bp[4..7] */
167 punpcklbw_r2r(mm3,mm1); /* rfp[0..3] */
168 punpckhbw_r2r(mm3,mm2); /* rfp[4..7] */
169 paddsw_r2r(mm5,mm1); /* + bp[0..3] */
170 paddsw_r2r(mm6,mm2); /* + bp[4..7] */
171 pcmpgtw_r2r(mm1,mm3); /* 1s to fields < 0 */
172 pcmpgtw_r2r(mm2,mm4); /* 1s to fields < 0 */
173 pandn_r2r(mm1,mm3); /* clip <0 = zero */
174 pandn_r2r(mm2,mm4); /* clip <0 = zero */
175 packuswb_r2r(mm4,mm3); /* pack/clip >255 = 255 */
176 movq_r2m(mm3,m_(rfp)); /* store rfp[0..7] */
183 for( i=0; i<8; ++i ) {
185 rfp[0] = clip(bp[0] + 128);
186 rfp[1] = clip(bp[1] + 128);
187 rfp[2] = clip(bp[2] + 128);
188 rfp[3] = clip(bp[3] + 128);
189 rfp[4] = clip(bp[4] + 128);
190 rfp[5] = clip(bp[5] + 128);
191 rfp[6] = clip(bp[6] + 128);
192 rfp[7] = clip(bp[7] + 128);
194 static short i128[4] = { 128, 128, 128, 128 };
195 movq_m2r(m_(i128),mm1); /* 128,,128 */
196 pxor_r2r(mm3,mm3); /* zero */
197 pxor_r2r(mm4,mm4); /* zero */
198 movq_m2r(m_(bp+0),mm5); /* bp[0..3] */
199 movq_r2r(mm1,mm2); /* 128,,128 */
200 movq_m2r(m_(bp+4),mm6); /* bp[4..7] */
201 paddsw_r2r(mm5,mm1); /* + bp[0..3] */
202 paddsw_r2r(mm6,mm2); /* + bp[4..7] */
203 pcmpgtw_r2r(mm1,mm3); /* 1s to fields < 0 */
204 pcmpgtw_r2r(mm2,mm4); /* 1s to fields < 0 */
205 pandn_r2r(mm1,mm3); /* clip <0 = zero */
206 pandn_r2r(mm2,mm4); /* clip <0 = zero */
207 packuswb_r2r(mm4,mm3); /* pack/clip >255 = 255 */
208 movq_r2m(mm3,m_(rfp)); /* store rfp[0..7] */
221 uint8_t zslice_decoder_t::
222 non_linear_mquant_table[32] = {
223 0, 1, 2, 3, 4, 5, 6, 7,
224 8, 10, 12, 14, 16, 18, 20, 22,
225 24, 28, 32, 36, 40, 44, 48, 52,
226 56, 64, 72, 80, 88, 96,104,112,
229 int zslice_decoder_t::
233 int mb_type, cbp, motion_type = 0, dct_type;
234 int macroblock_address, mba_inc, mba_max;
235 int slice_vert_pos_ext;
238 int mv_count, mv_format, mvscale;
239 int pmv[2][2][2], mv_field_sel[2][2];
240 int dmv, dmvector[2];
242 int stwtype, stwclass;
245 /* number of macroblocks per picture */
246 mba_max = video->mb_width * video->mb_height;
248 /* field picture has half as many macroblocks as frame */
249 if( video->pict_struct != video_t::pics_FRAME_PICTURE )
251 macroblock_address = 0;
252 /* first macroblock in slice is not skipped */
255 zcode = slice_buffer->get_bits(32);
256 /* decode slice header (may change quant_scale) */
257 slice_vert_pos_ext = get_slice_hdr();
258 /* reset all DC coefficient and motion vector predictors */
259 dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0;
260 pmv[0][0][0] = pmv[0][0][1] = pmv[1][0][0] = pmv[1][0][1] = 0;
261 pmv[0][1][0] = pmv[0][1][1] = pmv[1][1][0] = pmv[1][1][1] = 0;
263 for( i=0; !slice_buffer->eob(); ++i ) {
265 if( !slice_buffer->show_bits(23) ) break; /* Done */
266 /* decode macroblock address increment */
267 mba_inc = get_macroblock_address();
268 if( fault ) return 1;
270 /* Get the macroblock_address */
271 int mline = (slice_vert_pos_ext << 7) + (zcode&0xff) - 1;
272 macroblock_address = mline * video->mb_width + mba_inc - 1;
273 /* first macroblock in slice: not skipped */
278 if( fault ) return 1;
279 if( macroblock_address >= mba_max ) {
280 /* mba_inc points beyond picture dimensions */
281 //zerr("too many macroblocks in picture\n"); */
287 macroblock_modes(&mb_type, &stwtype, &stwclass, &motion_type,
288 &mv_count, &mv_format, &dmv, &mvscale, &dct_type);
289 if( fault ) return 1;
291 if( mb_type & mb_QUANT ) {
292 qs = slice_buffer->get_bits(5);
294 quant_scale = video->qscale_type ?
295 non_linear_mquant_table[qs] : (qs << 1);
299 /* make sure quant_scale is valid */
300 if( video->scalable_mode == sc_DP )
301 quant_scale = quant_scale; /*???*/
306 /* decode forward motion vectors */
307 if( (mb_type & mb_FORWARD) ||
308 ((mb_type & mb_INTRA) && video->conceal_mv) ) {
310 motion_vectors(pmv, dmvector, mv_field_sel, 0, mv_count, mv_format,
311 video->h_forw_r_size, video->v_forw_r_size, dmv, mvscale);
313 motion_vector(pmv[0][0], dmvector,
314 video->forw_r_size, video->forw_r_size, 0, 0, video->full_forw);
316 if( fault ) return 1;
318 /* decode backward motion vectors */
319 if( mb_type & mb_BACKWARD ) {
321 motion_vectors(pmv, dmvector, mv_field_sel, 1, mv_count, mv_format,
322 video->h_back_r_size, video->v_back_r_size, 0, mvscale);
324 motion_vector(pmv[0][1], dmvector,
325 video->back_r_size, video->back_r_size, 0, 0, video->full_back);
328 if( fault ) return 1;
330 /* remove marker_bit */
331 if( (mb_type & mb_INTRA) && video->conceal_mv )
332 slice_buffer->flush_bit();
334 /* macroblock_pattern */
335 if( mb_type & mb_PATTERN ) {
337 if( video->chroma_format == video_t::cfmt_422 ) {
338 /* coded_block_pattern_1 */
339 cbp = (cbp << 2) | slice_buffer->get_bits(2);
341 else if( video->chroma_format == video_t::cfmt_444 ) {
342 /* coded_block_pattern_2 */
343 cbp = (cbp << 6) | slice_buffer->get_bits(6);
347 cbp = (mb_type & mb_INTRA) ? ((1 << video->blk_cnt) - 1) : 0;
348 if( fault ) return 1;
351 clear_block(0, video->blk_cnt);
352 for( comp=0; comp<video->blk_cnt; ++comp ) {
353 if( cbp & (1 << (video->blk_cnt-comp-1)) ) {
354 if( mb_type & mb_INTRA ) {
356 get_mpg2_intra_block(comp, dc_dct_pred);
358 get_intra_block(comp, dc_dct_pred);
362 get_mpg2_inter_block(comp);
364 get_inter_block(comp);
366 if( fault ) return 1;
370 /* reset intra_dc predictors */
371 if( !(mb_type & mb_INTRA) )
372 dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0;
374 /* reset motion vector predictors */
375 if( (mb_type & mb_INTRA) && !video->conceal_mv ) {
376 /* intra mb without concealment motion vectors */
377 pmv[0][0][0] = pmv[0][0][1] = pmv[1][0][0] = pmv[1][0][1] = 0;
378 pmv[0][1][0] = pmv[0][1][1] = pmv[1][1][0] = pmv[1][1][1] = 0;
381 if( (video->pict_type == video_t::pic_type_P) &&
382 !(mb_type & (mb_FORWARD | mb_INTRA)) ) {
383 /* non-intra mb without forward mv in a P picture */
384 pmv[0][0][0] = pmv[0][0][1] = pmv[1][0][0] = pmv[1][0][1] = 0;
386 /* derive motion_type */
387 if( video->pict_struct == video_t::pics_FRAME_PICTURE )
388 motion_type = mc_FRAME;
390 motion_type = mc_FIELD;
391 /* predict from field of same parity */
392 mv_field_sel[0][0] = video->pict_struct == video_t::pics_BOTTOM_FIELD ? 1 : 0;
396 if( stwclass == 4 ) {
397 /* purely spatially predicted macroblock */
398 pmv[0][0][0] = pmv[0][0][1] = pmv[1][0][0] = pmv[1][0][1] = 0;
399 pmv[0][1][0] = pmv[0][1][1] = pmv[1][1][0] = pmv[1][1][1] = 0;
403 /* mba_inc!=1: skipped macroblock */
404 clear_block(0, video->blk_cnt);
406 /* reset intra_dc predictors */
407 dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0;
409 /* reset motion vector predictors */
410 if( video->pict_type == video_t::pic_type_P )
411 pmv[0][0][0] = pmv[0][0][1] = pmv[1][0][0] = pmv[1][0][1] = 0;
413 /* derive motion_type */
414 if( video->pict_struct == video_t::pics_FRAME_PICTURE )
415 motion_type = mc_FRAME;
417 motion_type = mc_FIELD;
418 /* predict from field of same parity */
419 mv_field_sel[0][0] = mv_field_sel[0][1] =
420 (video->pict_struct == video_t::pics_BOTTOM_FIELD) ? 1 : 0;
423 /* skipped I are spatial-only predicted, */
424 /* skipped P and B are temporal-only predicted */
425 stwtype = video->pict_type == video_t::pic_type_I ? 8 : 0;
426 mb_type &= ~mb_INTRA; /* clear mb_INTRA */
427 cbp = 0; /* no block data */
432 /* pixel coordinates of top left corner of current macroblock */
433 int mx = macroblock_address % video->mb_width;
434 int my = macroblock_address / video->mb_width;
437 if( video->thumb && video->pict_type == video_t::pic_type_I ) {
438 uint8_t *ap = video->tdat + 4*my*video->mb_width + 2*mx;
439 uint8_t *bp = ap + 2*video->mb_width;
440 if( (mb_type&mb_INTRA) != 0 ) {
441 *ap = clip(128 + block[0][0]/8); ++ap;
442 *ap = clip(128 + block[1][0]/8);
443 *bp = clip(128 + block[2][0]/8); ++bp;
444 *bp = clip(128 + block[3][0]/8);
447 *ap = clip(*ap + block[0][0]/8); ++ap;
448 *ap = clip(*ap + block[1][0]/8);
449 *bp = clip(*bp + block[2][0]/8); ++bp;
450 *bp = clip(*bp + block[3][0]/8);
455 int bx = 16*mx, by = 16*my;
456 /* motion compensation */
457 if( !(mb_type & mb_INTRA) )
458 video->reconstruct( bx, by, mb_type, motion_type, pmv,
459 mv_field_sel, dmvector, stwtype);
461 /* copy or add block data into picture */
462 for( comp=0; comp<video->blk_cnt; ++comp ) {
463 if( (cbp | snr_cbp) & (1 << (video->blk_cnt-1-comp)) ) {
464 idct_conversion(block[comp]);
465 add_block(comp, bx, by, dct_type, !(mb_type & mb_INTRA) ? 1 : 0);
470 /* advance to next macroblock */
471 ++macroblock_address;
478 void zslice_decoder_t::
485 video->put_slice_buffer(slice_buffer);
486 if( get_active_slice_buffer() ) break;
491 void *zslice_decoder_t::
492 the_slice_loop(void *the_slice_decoder)
494 ((slice_decoder_t *)the_slice_decoder)->slice_loop();
501 owner = pthread_self();
506 pthread_create(&tid,0,the_slice_loop, this);
514 pthread_join(tid, 0);
518 int zslice_decoder_t::
522 if( (zcode=slice_buffer->show_bits(9)) >= 128 ) {
524 slice_buffer->flush_bits(CBPtab0[zcode].len);
525 return CBPtab0[zcode].val;
530 slice_buffer->flush_bits(CBPtab1[zcode].len);
531 return CBPtab1[zcode].val;
535 //zerr("invalid coded_block_pattern code\n");
540 slice_buffer->flush_bits(CBPtab2[zcode].len);
541 return CBPtab2[zcode].val;
545 /* set block to zero */
546 int zslice_decoder_t::
547 clear_block(int comp, int size)
550 memset(block[comp], 0, size*sizeof(block[0]));
554 int zslice_buffer_t::
557 int zcode, size, val;
558 zcode = show_bits(5); /* decode length */
560 size = DClumtab0[zcode].val;
561 flush_bits(DClumtab0[zcode].len);
564 zcode = show_bits(9) - 0x1f0;
565 size = DClumtab1[zcode].val;
566 flush_bits(DClumtab1[zcode].len);
570 val = get_bits(size);
571 if( (val & (1 << (size-1))) == 0 )
572 val -= (1 << size)-1;
581 int zslice_buffer_t::
584 int zcode, size, val;
585 zcode = show_bits(5); /* decode length */
587 size = DCchromtab0[zcode].val;
588 flush_bits(DCchromtab0[zcode].len);
591 zcode = show_bits(10) - 0x3e0;
592 size = DCchromtab1[zcode].val;
593 flush_bits(DCchromtab1[zcode].len);
597 val = get_bits(size);
598 if( (val & (1 << (size-1))) == 0 )
599 val -= (1 << size)-1;
607 uint16_t *zslice_decoder_t::DCTlutab[3] = { 0, };
609 void zslice_decoder_t::
610 init_lut(uint16_t *&lutbl, DCTtab_t *tabn, DCTtab_t *tab0, DCTtab_t *tab1)
613 uint16_t *lut = new uint16_t[0x10000];
614 while( i < 0x0010 ) { lut[i] = 0; ++i; }
615 while( i < 0x0020 ) { lut[i] = lu_pack(&DCTtab6[i - 16]); ++i; }
616 while( i < 0x0040 ) { lut[i] = lu_pack(&DCTtab5[(i >> 1) - 16]); ++i; }
617 while( i < 0x0080 ) { lut[i] = lu_pack(&DCTtab4[(i >> 2) - 16]); ++i; }
618 while( i < 0x0100 ) { lut[i] = lu_pack(&DCTtab3[(i >> 3) - 16]); ++i; }
619 while( i < 0x0200 ) { lut[i] = lu_pack(&DCTtab2[(i >> 4) - 16]); ++i; }
620 while( i < 0x0400 ) { lut[i] = lu_pack( &tab1[(i >> 6) - 8]); ++i; }
621 int tblsz = !tabn ? 0x10000 : 0x4000;
622 while( i < tblsz ) { lut[i] = lu_pack( &tab0[(i >> 8) - 4]); ++i; }
623 while( i <0x10000 ) { lut[i] = lu_pack( &tabn[(i >>12) - 4]); ++i; }
627 void zslice_decoder_t::
630 if( DCTlutab[0] ) return;
631 init_lut(DCTlutab[0], DCTtabfirst, DCTtab0, DCTtab1);
632 init_lut(DCTlutab[1], DCTtabnext , DCTtab0, DCTtab1);
633 init_lut(DCTlutab[2], 0 , DCTtab0a, DCTtab1a);
636 /* decode one MPEG-1 coef */
638 inline int zslice_decoder_t::
639 get_coef(uint16_t *lut)
642 uint16_t zcode = slice_buffer->show_bits(16);
643 uint16_t lu = lut[zcode];
644 if( !lu ) return fault = 1;
645 int len = lu_len(lu);
646 slice_buffer->flush_bits(len);
647 int run = lu_run(lu);
648 if( run >= 32 ) { /* escape */
649 if( run == 32 ) return -1; /* end of block */
650 i += slice_buffer->get_bits(6);
651 if( (v = slice_buffer->get_bits(8)) < 128 ) {
653 if( !v ) v = slice_buffer->get_bits(8);
656 if( v == 128 ) v = slice_buffer->get_bits(8);
663 s = slice_buffer->get_bit();
665 if( i >= 64 ) return fault = 1;
666 val = v; sign = s; idx = i;
670 /* decode one MPEG-2 coef */
672 inline int zslice_decoder_t::
673 get_mpg2_coef(uint16_t *lut)
676 uint16_t zcode = slice_buffer->show_bits(16);
677 uint16_t lu = lut[zcode];
678 if( !lu ) return fault = 1;
679 int len = lu_len(lu);
680 slice_buffer->flush_bits(len);
681 int run = lu_run(lu);
682 if( run >= 32 ) { /* escape */
683 if( run == 32 ) return -1; /* end of block */
684 i += slice_buffer->get_bits(6);
685 v = slice_buffer->get_bits(12);
686 if( (v & 2047) == 0 ) return fault = 1;
687 s = (v >= 2048) ? 1 : 0;
688 if( s != 0 ) v = 4096 - v;
693 s = slice_buffer->get_bit();
695 if( i >= 64 ) return fault = 1;
696 val = v; sign = s; idx = i;
701 /* decode one intra coded MPEG-1 block */
703 void zslice_decoder_t::
704 get_intra_block(int comp, int dc_dct_pred[])
706 int dc_coef = /* decode DC coefficients */
707 comp < 4 ? (dc_dct_pred[0] += slice_buffer->get_dc_lum()) :
708 comp == 4 ? (dc_dct_pred[1] += slice_buffer->get_dc_chrom()) :
709 (dc_dct_pred[2] += slice_buffer->get_dc_chrom()) ;
710 short *bp = block[comp];
711 bp[0] = dc_coef << 3;
713 /* decode AC coefficients */
714 int *qmat = video->intra_quantizer_matrix;
717 while( !get_coef(DCTlutab[1]) ) {
718 j = video->zigzag_scan_table[idx++];
719 int v = (((val * quant_scale*qmat[j])>>3)-1) | 1;
720 bp[j] = sign ? -v : v;
723 if( j > 0 ) /* not a sparse matrix ! */
727 /* decode one intra coded MPEG-2 block */
728 void zslice_decoder_t::
729 get_mpg2_intra_block(int comp, int dc_dct_pred[])
731 int *qmat = (comp < 4 || video->chroma_format == video_t::cfmt_420) ?
732 video->intra_quantizer_matrix : video->chroma_intra_quantizer_matrix;
733 int dc_coef = /* decode DC coefficients */
734 comp < 4 ? (dc_dct_pred[0] += slice_buffer->get_dc_lum()) :
735 !(comp&1) ? (dc_dct_pred[1] += slice_buffer->get_dc_chrom()) :
736 (dc_dct_pred[2] += slice_buffer->get_dc_chrom()) ;
737 /* with data partitioning, data always goes to base layer */
738 short *bp = block[comp];
739 bp[0] = dc_coef << (3 - video->dc_prec);
741 uint8_t *scan_table = video->altscan ?
742 video->alternate_scan_table : video->zigzag_scan_table;
743 uint16_t *lutbl = !video->intravlc ? DCTlutab[1] : DCTlutab[2];
747 while( !get_mpg2_coef(lutbl) ) { /* decode AC coefficients */
748 j = scan_table[idx++];
749 int v = (val * quant_scale*qmat[j]) >> 4;
750 bp[j] = sign ? -v : v;
753 if( j > 0 ) /* not a sparse matrix ! */
758 /* decode one non-intra coded MPEG-1 block */
760 void zslice_decoder_t::
761 get_inter_block(int comp)
763 short *bp = block[comp];
766 if( get_coef(DCTlutab[0])) return;
767 int j = video->zigzag_scan_table[idx++];
768 int *qmat = video->non_intra_quantizer_matrix;
769 int v = (((((val<<1) + 1) * quant_scale*qmat[j])>>4)-1) | 1;
770 bp[j] = sign ? -v : v;
772 /* decode AC coefficients */
773 while( !get_coef(DCTlutab[1]) ) {
774 j = video->zigzag_scan_table[idx++];
775 v = (((((val<<1) + 1) * quant_scale*qmat[j])>>4)-1) | 1;
776 bp[j] = sign ? -v : v;
779 if( j > 0 ) /* not a sparse matrix ! */
784 /* decode one non-intra coded MPEG-2 block */
786 void zslice_decoder_t::
787 get_mpg2_inter_block(int comp)
789 /* with data partitioning, data always goes to base layer */
790 short *bp = block[comp];
791 int *qmat = (comp < 4 || video->chroma_format == video_t::cfmt_420) ?
792 video->non_intra_quantizer_matrix : video->chroma_non_intra_quantizer_matrix;
793 uint8_t *scan_table = video->altscan ?
794 video->alternate_scan_table : video->zigzag_scan_table;
797 if( get_mpg2_coef(DCTlutab[0]) ) return;
798 int j = scan_table[idx++];
799 int v = (((val << 1)+1) * quant_scale*qmat[j]) >> 5;
800 bp[j] = sign ? -v : v ;
802 /* decode AC coefficients */
803 while( !get_mpg2_coef(DCTlutab[1]) ) {
804 j = scan_table[idx++];
805 int v = (((val << 1)+1) * quant_scale*qmat[j]) >> 5;
806 bp[j] = sign ? -v : v ;
809 if( j > 0 ) /* not a sparse matrix ! */
815 decode_slice(zslice_buffer_t *buffer)
818 if( avail_slice_decoders ) {
819 zslice_decoder_t *decoder = avail_slice_decoders;
820 avail_slice_decoders = decoder->next;
821 decoder->slice_buffer = buffer;
822 decoder->video = buffer->video;
823 if( !decoder_active_locked++ )
824 decoder_active.lock();
825 decoder->input_lock.unlock();
828 buffer->next = active_slice_buffers;
829 active_slice_buffers = buffer;
831 decoder_lock.unlock();
834 int zslice_decoder_t::
835 get_active_slice_buffer()
838 src->decoder_lock.lock();
839 zslice_buffer_t *buffer = src->active_slice_buffers;
841 next = src->avail_slice_decoders;
842 src->avail_slice_decoders = this;
843 if( !--src->decoder_active_locked )
844 src->decoder_active.unlock();
850 src->active_slice_buffers = buffer->next;
851 slice_buffer = buffer;
852 video = buffer->video;
855 src->decoder_lock.unlock();
860 allocate_slice_decoders()
862 if( slice_decoders ) return;
864 if( count > MAX_CPUS ) count = MAX_CPUS;
865 slice_decoders = new slice_decoder_t[count];
866 slice_decoder_t *decoder = &slice_decoders[0];
867 avail_slice_decoders = 0;
868 for( int i=count; --i>=0; ++decoder ) {
870 decoder->next = avail_slice_decoders;
871 avail_slice_decoders = decoder;
873 total_slice_decoders = count;
877 delete_slice_decoders()
879 if( !slice_decoders ) return;
880 delete [] slice_decoders;
882 total_slice_decoders = 0;
886 reallocate_slice_decoders()
888 decoder_active.lock();
889 decoder_active.unlock();
891 if( !decoder_active_locked ) {
892 delete_slice_decoders();
893 allocate_slice_decoders();
895 decoder_lock.unlock();
899 allocate_slice_buffers()
901 if( slice_buffers ) return;
902 int count = 2*src->cpus;
903 if( count > 2*MAX_CPUS ) count = 2*MAX_CPUS;
904 slice_buffers = new slice_buffer_t[count];
905 slice_buffer_t *buffer = &slice_buffers[0];
906 avail_slice_buffers = 0;
907 for( int i=count; --i>=0; ++buffer ) {
908 buffer->next = avail_slice_buffers;
909 avail_slice_buffers = buffer;
911 total_slice_buffers = count;
915 delete_slice_buffers()
917 if( !slice_buffers ) return;
918 delete [] slice_buffers;
920 total_slice_buffers = 0;
924 reallocate_slice_buffers()
927 slice_active.unlock();
929 if( !slice_active_locked ) {
930 delete_slice_buffers();
931 allocate_slice_buffers();