1 #include "../libzmpeg3.h"
8 #if defined(__x86_64__)
9 #define m_(v) (*(mmx_t*)(v))
11 #define m_(v) (*(char*)(v))
20 data = new uint8_t[buffer_allocation=4096];
33 uint8_t *zslice_buffer_t::
34 expand_buffer(int bfrsz)
36 long new_allocation = 2*buffer_allocation;
37 uint8_t *new_buffer = new uint8_t[new_allocation];
38 if( bfrsz > 0 ) memcpy(new_buffer,data,bfrsz);
41 buffer_allocation = new_allocation;
42 buffer_size = buffer_allocation-bfrsz-4;
47 void zslice_buffer_t::
48 fill_buffer(zbits_t *vstream)
51 vstream->next_byte_align();
52 demuxer_t *demux = vstream->demuxer;
53 /* sync stream to zcode and access demuxer */
54 uint32_t zcode = vstream->show_bits32_noptr();
55 for( int i=32; i>0; ) *sbp++ = (zcode>>(i-=8));
57 buffer_size = buffer_allocation-(4+4); /* sizeof(zcode)+padding */
59 /* Read the slice into the buffer including the slice start code */
60 while( !vstream->eof() ) {
61 if( buffer_size <= 0 ) sbp = expand_buffer(sbp - data);
62 if( (zcode&0xff) != 0 ) {
64 zcode = (zcode<<8) | (*sbp++ = demux->read_char());
65 zcode = (zcode<<8) | (*sbp++ = demux->read_char());
69 zcode = (zcode<<8) | (*sbp++ = demux->read_char());
70 if( (zcode&0xffffff) == PACKET_START_CODE_PREFIX ) break;
73 /* finish trailing code as picture start code (0x100) */
75 buffer_size = sbp - data;
76 /* reload stream bfr, position to start code */
77 vstream->reset(zcode);
78 vstream->get_byte_noptr();
81 /* limit coefficients to -2048..2047 */
83 /* move/add 8x8-Block from block[comp] to refframe */
85 int zslice_decoder_t::
86 add_block(int comp, int bx, int by, int dct_type, int addflag)
91 /* color component index */
92 cc = (comp < 4) ? 0 : (comp & 1) + 1;
96 if( video->pict_struct == video_t::pics_FRAME_PICTURE ) {
98 /* field DCT coding */
99 rfp = video->newframe[0] + video->coded_picture_width *
100 (by + ((comp & 2) >> 1)) + bx + ((comp & 1) << 3);
101 iincr = (video->coded_picture_width << 1);
104 /* frame DCT coding */
105 rfp = video->newframe[0] + video->coded_picture_width *
106 (by + ((comp & 2) << 2)) + bx + ((comp & 1) << 3);
107 iincr = video->coded_picture_width;
112 rfp = video->newframe[0] + (video->coded_picture_width << 1) *
113 (by + ((comp & 2) << 2)) + bx + ((comp & 1) << 3);
114 iincr = (video->coded_picture_width << 1);
120 /* scale coordinates */
121 if( video->chroma_format != video_t::cfmt_444 ) bx >>= 1;
122 if( video->chroma_format == video_t::cfmt_420 ) by >>= 1;
123 if( video->pict_struct == video_t::pics_FRAME_PICTURE ) {
124 if( dct_type && (video->chroma_format != video_t::cfmt_420) ) {
125 /* field DCT coding */
126 rfp = video->newframe[cc] + video->chrom_width *
127 (by + ((comp & 2) >> 1)) + bx + (comp & 8);
128 iincr = (video->chrom_width << 1);
131 /* frame DCT coding */
132 rfp = video->newframe[cc] + video->chrom_width *
133 (by + ((comp & 2) << 2)) + bx + (comp & 8);
134 iincr = video->chrom_width;
139 rfp = video->newframe[cc] + (video->chrom_width << 1) *
140 (by + ((comp & 2) << 2)) + bx + (comp & 8);
141 iincr = (video->chrom_width << 1);
148 for( i=0; i<8; ++i ) {
150 rfp[0] = clip(bp[0] + rfp[0]);
151 rfp[1] = clip(bp[1] + rfp[1]);
152 rfp[2] = clip(bp[2] + rfp[2]);
153 rfp[3] = clip(bp[3] + rfp[3]);
154 rfp[4] = clip(bp[4] + rfp[4]);
155 rfp[5] = clip(bp[5] + rfp[5]);
156 rfp[6] = clip(bp[6] + rfp[6]);
157 rfp[7] = clip(bp[7] + rfp[7]);
159 movq_m2r(m_(rfp),mm1); /* rfp[0..7] */
160 pxor_r2r(mm3,mm3); /* zero */
161 pxor_r2r(mm4,mm4); /* zero */
162 movq_m2r(m_(bp+0),mm5); /* bp[0..3] */
163 movq_r2r(mm1,mm2); /* copy rfp[0..7] */
164 movq_m2r(m_(bp+4),mm6); /* bp[4..7] */
165 punpcklbw_r2r(mm3,mm1); /* rfp[0..3] */
166 punpckhbw_r2r(mm3,mm2); /* rfp[4..7] */
167 paddsw_r2r(mm5,mm1); /* + bp[0..3] */
168 paddsw_r2r(mm6,mm2); /* + bp[4..7] */
169 pcmpgtw_r2r(mm1,mm3); /* 1s to fields < 0 */
170 pcmpgtw_r2r(mm2,mm4); /* 1s to fields < 0 */
171 pandn_r2r(mm1,mm3); /* clip <0 = zero */
172 pandn_r2r(mm2,mm4); /* clip <0 = zero */
173 packuswb_r2r(mm4,mm3); /* pack/clip >255 = 255 */
174 movq_r2m(mm3,m_(rfp)); /* store rfp[0..7] */
181 for( i=0; i<8; ++i ) {
183 rfp[0] = clip(bp[0] + 128);
184 rfp[1] = clip(bp[1] + 128);
185 rfp[2] = clip(bp[2] + 128);
186 rfp[3] = clip(bp[3] + 128);
187 rfp[4] = clip(bp[4] + 128);
188 rfp[5] = clip(bp[5] + 128);
189 rfp[6] = clip(bp[6] + 128);
190 rfp[7] = clip(bp[7] + 128);
192 static short i128[4] = { 128, 128, 128, 128 };
193 movq_m2r(m_(i128),mm1); /* 128,,128 */
194 pxor_r2r(mm3,mm3); /* zero */
195 pxor_r2r(mm4,mm4); /* zero */
196 movq_m2r(m_(bp+0),mm5); /* bp[0..3] */
197 movq_r2r(mm1,mm2); /* 128,,128 */
198 movq_m2r(m_(bp+4),mm6); /* bp[4..7] */
199 paddsw_r2r(mm5,mm1); /* + bp[0..3] */
200 paddsw_r2r(mm6,mm2); /* + bp[4..7] */
201 pcmpgtw_r2r(mm1,mm3); /* 1s to fields < 0 */
202 pcmpgtw_r2r(mm2,mm4); /* 1s to fields < 0 */
203 pandn_r2r(mm1,mm3); /* clip <0 = zero */
204 pandn_r2r(mm2,mm4); /* clip <0 = zero */
205 packuswb_r2r(mm4,mm3); /* pack/clip >255 = 255 */
206 movq_r2m(mm3,m_(rfp)); /* store rfp[0..7] */
219 uint8_t zslice_decoder_t::
220 non_linear_mquant_table[32] = {
221 0, 1, 2, 3, 4, 5, 6, 7,
222 8, 10, 12, 14, 16, 18, 20, 22,
223 24, 28, 32, 36, 40, 44, 48, 52,
224 56, 64, 72, 80, 88, 96,104,112,
227 int zslice_decoder_t::
231 int mb_type, cbp, motion_type = 0, dct_type;
232 int macroblock_address, mba_inc, mba_max;
233 int slice_vert_pos_ext;
236 int mv_count, mv_format, mvscale;
237 int pmv[2][2][2], mv_field_sel[2][2];
238 int dmv, dmvector[2];
240 int stwtype, stwclass;
243 /* number of macroblocks per picture */
244 mba_max = video->mb_width * video->mb_height;
246 /* field picture has half as many macroblocks as frame */
247 if( video->pict_struct != video_t::pics_FRAME_PICTURE )
249 macroblock_address = 0;
250 /* first macroblock in slice is not skipped */
253 zcode = slice_buffer->get_bits(32);
254 /* decode slice header (may change quant_scale) */
255 slice_vert_pos_ext = get_slice_hdr();
256 /* reset all DC coefficient and motion vector predictors */
257 dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0;
258 pmv[0][0][0] = pmv[0][0][1] = pmv[1][0][0] = pmv[1][0][1] = 0;
259 pmv[0][1][0] = pmv[0][1][1] = pmv[1][1][0] = pmv[1][1][1] = 0;
261 for( i=0; !slice_buffer->eob(); ++i ) {
263 if( !slice_buffer->show_bits(23) ) break; /* Done */
264 /* decode macroblock address increment */
265 mba_inc = get_macroblock_address();
266 if( fault ) return 1;
268 /* Get the macroblock_address */
269 int mline = (slice_vert_pos_ext << 7) + (zcode&0xff) - 1;
270 macroblock_address = mline * video->mb_width + mba_inc - 1;
271 /* first macroblock in slice: not skipped */
276 if( fault ) return 1;
277 if( macroblock_address >= mba_max ) {
278 /* mba_inc points beyond picture dimensions */
279 //zerr("too many macroblocks in picture\n"); */
285 macroblock_modes(&mb_type, &stwtype, &stwclass, &motion_type,
286 &mv_count, &mv_format, &dmv, &mvscale, &dct_type);
287 if( fault ) return 1;
289 if( mb_type & mb_QUANT ) {
290 qs = slice_buffer->get_bits(5);
292 quant_scale = video->qscale_type ?
293 non_linear_mquant_table[qs] : (qs << 1);
297 /* make sure quant_scale is valid */
298 if( video->scalable_mode == sc_DP )
299 quant_scale = quant_scale; /*???*/
304 /* decode forward motion vectors */
305 if( (mb_type & mb_FORWARD) ||
306 ((mb_type & mb_INTRA) && video->conceal_mv) ) {
308 motion_vectors(pmv, dmvector, mv_field_sel, 0, mv_count, mv_format,
309 video->h_forw_r_size, video->v_forw_r_size, dmv, mvscale);
311 motion_vector(pmv[0][0], dmvector,
312 video->forw_r_size, video->forw_r_size, 0, 0, video->full_forw);
314 if( fault ) return 1;
316 /* decode backward motion vectors */
317 if( mb_type & mb_BACKWARD ) {
319 motion_vectors(pmv, dmvector, mv_field_sel, 1, mv_count, mv_format,
320 video->h_back_r_size, video->v_back_r_size, 0, mvscale);
322 motion_vector(pmv[0][1], dmvector,
323 video->back_r_size, video->back_r_size, 0, 0, video->full_back);
326 if( fault ) return 1;
328 /* remove marker_bit */
329 if( (mb_type & mb_INTRA) && video->conceal_mv )
330 slice_buffer->flush_bit();
332 /* macroblock_pattern */
333 if( mb_type & mb_PATTERN ) {
335 if( video->chroma_format == video_t::cfmt_422 ) {
336 /* coded_block_pattern_1 */
337 cbp = (cbp << 2) | slice_buffer->get_bits(2);
339 else if( video->chroma_format == video_t::cfmt_444 ) {
340 /* coded_block_pattern_2 */
341 cbp = (cbp << 6) | slice_buffer->get_bits(6);
345 cbp = (mb_type & mb_INTRA) ? ((1 << video->blk_cnt) - 1) : 0;
346 if( fault ) return 1;
349 clear_block(0, video->blk_cnt);
350 for( comp=0; comp<video->blk_cnt; ++comp ) {
351 if( cbp & (1 << (video->blk_cnt-comp-1)) ) {
352 if( mb_type & mb_INTRA ) {
354 get_mpg2_intra_block(comp, dc_dct_pred);
356 get_intra_block(comp, dc_dct_pred);
360 get_mpg2_inter_block(comp);
362 get_inter_block(comp);
364 if( fault ) return 1;
368 /* reset intra_dc predictors */
369 if( !(mb_type & mb_INTRA) )
370 dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0;
372 /* reset motion vector predictors */
373 if( (mb_type & mb_INTRA) && !video->conceal_mv ) {
374 /* intra mb without concealment motion vectors */
375 pmv[0][0][0] = pmv[0][0][1] = pmv[1][0][0] = pmv[1][0][1] = 0;
376 pmv[0][1][0] = pmv[0][1][1] = pmv[1][1][0] = pmv[1][1][1] = 0;
379 if( (video->pict_type == video_t::pic_type_P) &&
380 !(mb_type & (mb_FORWARD | mb_INTRA)) ) {
381 /* non-intra mb without forward mv in a P picture */
382 pmv[0][0][0] = pmv[0][0][1] = pmv[1][0][0] = pmv[1][0][1] = 0;
384 /* derive motion_type */
385 if( video->pict_struct == video_t::pics_FRAME_PICTURE )
386 motion_type = mc_FRAME;
388 motion_type = mc_FIELD;
389 /* predict from field of same parity */
390 mv_field_sel[0][0] = video->pict_struct == video_t::pics_BOTTOM_FIELD ? 1 : 0;
394 if( stwclass == 4 ) {
395 /* purely spatially predicted macroblock */
396 pmv[0][0][0] = pmv[0][0][1] = pmv[1][0][0] = pmv[1][0][1] = 0;
397 pmv[0][1][0] = pmv[0][1][1] = pmv[1][1][0] = pmv[1][1][1] = 0;
401 /* mba_inc!=1: skipped macroblock */
402 clear_block(0, video->blk_cnt);
404 /* reset intra_dc predictors */
405 dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0;
407 /* reset motion vector predictors */
408 if( video->pict_type == video_t::pic_type_P )
409 pmv[0][0][0] = pmv[0][0][1] = pmv[1][0][0] = pmv[1][0][1] = 0;
411 /* derive motion_type */
412 if( video->pict_struct == video_t::pics_FRAME_PICTURE )
413 motion_type = mc_FRAME;
415 motion_type = mc_FIELD;
416 /* predict from field of same parity */
417 mv_field_sel[0][0] = mv_field_sel[0][1] =
418 (video->pict_struct == video_t::pics_BOTTOM_FIELD) ? 1 : 0;
421 /* skipped I are spatial-only predicted, */
422 /* skipped P and B are temporal-only predicted */
423 stwtype = video->pict_type == video_t::pic_type_I ? 8 : 0;
424 mb_type &= ~mb_INTRA; /* clear mb_INTRA */
425 cbp = 0; /* no block data */
430 /* pixel coordinates of top left corner of current macroblock */
431 int mx = macroblock_address % video->mb_width;
432 int my = macroblock_address / video->mb_width;
435 if( video->thumb && video->pict_type == video_t::pic_type_I ) {
436 uint8_t *ap = video->tdat + 4*my*video->mb_width + 2*mx;
437 uint8_t *bp = ap + 2*video->mb_width;
438 if( (mb_type&mb_INTRA) != 0 ) {
439 *ap = clip(128 + block[0][0]/8); ++ap;
440 *ap = clip(128 + block[1][0]/8);
441 *bp = clip(128 + block[2][0]/8); ++bp;
442 *bp = clip(128 + block[3][0]/8);
445 *ap = clip(*ap + block[0][0]/8); ++ap;
446 *ap = clip(*ap + block[1][0]/8);
447 *bp = clip(*bp + block[2][0]/8); ++bp;
448 *bp = clip(*bp + block[3][0]/8);
453 int bx = 16*mx, by = 16*my;
454 /* motion compensation */
455 if( !(mb_type & mb_INTRA) )
456 video->reconstruct( bx, by, mb_type, motion_type, pmv,
457 mv_field_sel, dmvector, stwtype);
459 /* copy or add block data into picture */
460 for( comp=0; comp<video->blk_cnt; ++comp ) {
461 if( (cbp | snr_cbp) & (1 << (video->blk_cnt-1-comp)) ) {
462 idct_conversion(block[comp]);
463 add_block(comp, bx, by, dct_type, !(mb_type & mb_INTRA) ? 1 : 0);
468 /* advance to next macroblock */
469 ++macroblock_address;
476 void zslice_decoder_t::
483 video->put_slice_buffer(slice_buffer);
484 if( get_active_slice_buffer() ) break;
489 void *zslice_decoder_t::
490 the_slice_loop(void *the_slice_decoder)
492 ((slice_decoder_t *)the_slice_decoder)->slice_loop();
499 owner = pthread_self();
504 pthread_create(&tid,0,the_slice_loop, this);
512 pthread_join(tid, 0);
516 int zslice_decoder_t::
520 if( (zcode=slice_buffer->show_bits(9)) >= 128 ) {
522 slice_buffer->flush_bits(CBPtab0[zcode].len);
523 return CBPtab0[zcode].val;
528 slice_buffer->flush_bits(CBPtab1[zcode].len);
529 return CBPtab1[zcode].val;
533 //zerr("invalid coded_block_pattern code\n");
538 slice_buffer->flush_bits(CBPtab2[zcode].len);
539 return CBPtab2[zcode].val;
543 /* set block to zero */
544 int zslice_decoder_t::
545 clear_block(int comp, int size)
548 memset(block[comp], 0, size*sizeof(block[0]));
552 int zslice_buffer_t::
555 int zcode, size, val;
556 zcode = show_bits(5); /* decode length */
558 size = DClumtab0[zcode].val;
559 flush_bits(DClumtab0[zcode].len);
562 zcode = show_bits(9) - 0x1f0;
563 size = DClumtab1[zcode].val;
564 flush_bits(DClumtab1[zcode].len);
568 val = get_bits(size);
569 if( (val & (1 << (size-1))) == 0 )
570 val -= (1 << size)-1;
579 int zslice_buffer_t::
582 int zcode, size, val;
583 zcode = show_bits(5); /* decode length */
585 size = DCchromtab0[zcode].val;
586 flush_bits(DCchromtab0[zcode].len);
589 zcode = show_bits(10) - 0x3e0;
590 size = DCchromtab1[zcode].val;
591 flush_bits(DCchromtab1[zcode].len);
595 val = get_bits(size);
596 if( (val & (1 << (size-1))) == 0 )
597 val -= (1 << size)-1;
605 uint16_t *zslice_decoder_t::DCTlutab[3] = { 0, };
607 void zslice_decoder_t::
608 init_lut(uint16_t *&lutbl, DCTtab_t *tabn, DCTtab_t *tab0, DCTtab_t *tab1)
611 uint16_t *lut = new uint16_t[0x10000];
612 while( i < 0x0010 ) { lut[i] = 0; ++i; }
613 while( i < 0x0020 ) { lut[i] = lu_pack(&DCTtab6[i - 16]); ++i; }
614 while( i < 0x0040 ) { lut[i] = lu_pack(&DCTtab5[(i >> 1) - 16]); ++i; }
615 while( i < 0x0080 ) { lut[i] = lu_pack(&DCTtab4[(i >> 2) - 16]); ++i; }
616 while( i < 0x0100 ) { lut[i] = lu_pack(&DCTtab3[(i >> 3) - 16]); ++i; }
617 while( i < 0x0200 ) { lut[i] = lu_pack(&DCTtab2[(i >> 4) - 16]); ++i; }
618 while( i < 0x0400 ) { lut[i] = lu_pack( &tab1[(i >> 6) - 8]); ++i; }
619 int tblsz = !tabn ? 0x10000 : 0x4000;
620 while( i < tblsz ) { lut[i] = lu_pack( &tab0[(i >> 8) - 4]); ++i; }
621 while( i <0x10000 ) { lut[i] = lu_pack( &tabn[(i >>12) - 4]); ++i; }
625 void zslice_decoder_t::
628 if( DCTlutab[0] ) return;
629 init_lut(DCTlutab[0], DCTtabfirst, DCTtab0, DCTtab1);
630 init_lut(DCTlutab[1], DCTtabnext , DCTtab0, DCTtab1);
631 init_lut(DCTlutab[2], 0 , DCTtab0a, DCTtab1a);
634 /* decode one MPEG-1 coef */
636 inline int zslice_decoder_t::
637 get_coef(uint16_t *lut)
640 uint16_t zcode = slice_buffer->show_bits(16);
641 uint16_t lu = lut[zcode];
642 if( !lu ) return fault = 1;
643 int len = lu_len(lu);
644 slice_buffer->flush_bits(len);
645 int run = lu_run(lu);
646 if( run >= 32 ) { /* escape */
647 if( run == 32 ) return -1; /* end of block */
648 i += slice_buffer->get_bits(6);
649 if( (v = slice_buffer->get_bits(8)) < 128 ) {
651 if( !v ) v = slice_buffer->get_bits(8);
654 if( v == 128 ) v = slice_buffer->get_bits(8);
661 s = slice_buffer->get_bit();
663 if( i >= 64 ) return fault = 1;
664 val = v; sign = s; idx = i;
668 /* decode one MPEG-2 coef */
670 inline int zslice_decoder_t::
671 get_mpg2_coef(uint16_t *lut)
674 uint16_t zcode = slice_buffer->show_bits(16);
675 uint16_t lu = lut[zcode];
676 if( !lu ) return fault = 1;
677 int len = lu_len(lu);
678 slice_buffer->flush_bits(len);
679 int run = lu_run(lu);
680 if( run >= 32 ) { /* escape */
681 if( run == 32 ) return -1; /* end of block */
682 i += slice_buffer->get_bits(6);
683 v = slice_buffer->get_bits(12);
684 if( (v & 2047) == 0 ) return fault = 1;
685 s = (v >= 2048) ? 1 : 0;
686 if( s != 0 ) v = 4096 - v;
691 s = slice_buffer->get_bit();
693 if( i >= 64 ) return fault = 1;
694 val = v; sign = s; idx = i;
699 /* decode one intra coded MPEG-1 block */
701 void zslice_decoder_t::
702 get_intra_block(int comp, int dc_dct_pred[])
704 int dc_coef = /* decode DC coefficients */
705 comp < 4 ? (dc_dct_pred[0] += slice_buffer->get_dc_lum()) :
706 comp == 4 ? (dc_dct_pred[1] += slice_buffer->get_dc_chrom()) :
707 (dc_dct_pred[2] += slice_buffer->get_dc_chrom()) ;
708 short *bp = block[comp];
709 bp[0] = dc_coef << 3;
711 /* decode AC coefficients */
712 int *qmat = video->intra_quantizer_matrix;
715 while( !get_coef(DCTlutab[1]) ) {
716 j = video->zigzag_scan_table[idx++];
717 int v = (((val * quant_scale*qmat[j])>>3)-1) | 1;
718 bp[j] = sign ? -v : v;
721 if( j > 0 ) /* not a sparse matrix ! */
725 /* decode one intra coded MPEG-2 block */
726 void zslice_decoder_t::
727 get_mpg2_intra_block(int comp, int dc_dct_pred[])
729 int *qmat = (comp < 4 || video->chroma_format == video_t::cfmt_420) ?
730 video->intra_quantizer_matrix : video->chroma_intra_quantizer_matrix;
731 int dc_coef = /* decode DC coefficients */
732 comp < 4 ? (dc_dct_pred[0] += slice_buffer->get_dc_lum()) :
733 !(comp&1) ? (dc_dct_pred[1] += slice_buffer->get_dc_chrom()) :
734 (dc_dct_pred[2] += slice_buffer->get_dc_chrom()) ;
735 /* with data partitioning, data always goes to base layer */
736 short *bp = block[comp];
737 bp[0] = dc_coef << (3 - video->dc_prec);
739 uint8_t *scan_table = video->altscan ?
740 video->alternate_scan_table : video->zigzag_scan_table;
741 uint16_t *lutbl = !video->intravlc ? DCTlutab[1] : DCTlutab[2];
745 while( !get_mpg2_coef(lutbl) ) { /* decode AC coefficients */
746 j = scan_table[idx++];
747 int v = (val * quant_scale*qmat[j]) >> 4;
748 bp[j] = sign ? -v : v;
751 if( j > 0 ) /* not a sparse matrix ! */
756 /* decode one non-intra coded MPEG-1 block */
758 void zslice_decoder_t::
759 get_inter_block(int comp)
761 short *bp = block[comp];
764 if( get_coef(DCTlutab[0])) return;
765 int j = video->zigzag_scan_table[idx++];
766 int *qmat = video->non_intra_quantizer_matrix;
767 int v = (((((val<<1) + 1) * quant_scale*qmat[j])>>4)-1) | 1;
768 bp[j] = sign ? -v : v;
770 /* decode AC coefficients */
771 while( !get_coef(DCTlutab[1]) ) {
772 j = video->zigzag_scan_table[idx++];
773 v = (((((val<<1) + 1) * quant_scale*qmat[j])>>4)-1) | 1;
774 bp[j] = sign ? -v : v;
777 if( j > 0 ) /* not a sparse matrix ! */
782 /* decode one non-intra coded MPEG-2 block */
784 void zslice_decoder_t::
785 get_mpg2_inter_block(int comp)
787 /* with data partitioning, data always goes to base layer */
788 short *bp = block[comp];
789 int *qmat = (comp < 4 || video->chroma_format == video_t::cfmt_420) ?
790 video->non_intra_quantizer_matrix : video->chroma_non_intra_quantizer_matrix;
791 uint8_t *scan_table = video->altscan ?
792 video->alternate_scan_table : video->zigzag_scan_table;
795 if( get_mpg2_coef(DCTlutab[0]) ) return;
796 int j = scan_table[idx++];
797 int v = (((val << 1)+1) * quant_scale*qmat[j]) >> 5;
798 bp[j] = sign ? -v : v ;
800 /* decode AC coefficients */
801 while( !get_mpg2_coef(DCTlutab[1]) ) {
802 j = scan_table[idx++];
803 int v = (((val << 1)+1) * quant_scale*qmat[j]) >> 5;
804 bp[j] = sign ? -v : v ;
807 if( j > 0 ) /* not a sparse matrix ! */
813 decode_slice(zslice_buffer_t *buffer)
816 if( avail_slice_decoders ) {
817 zslice_decoder_t *decoder = avail_slice_decoders;
818 avail_slice_decoders = decoder->next;
819 decoder->slice_buffer = buffer;
820 decoder->video = buffer->video;
821 if( !decoder_active_locked++ )
822 decoder_active.lock();
823 decoder->input_lock.unlock();
826 buffer->next = active_slice_buffers;
827 active_slice_buffers = buffer;
829 decoder_lock.unlock();
832 int zslice_decoder_t::
833 get_active_slice_buffer()
836 src->decoder_lock.lock();
837 zslice_buffer_t *buffer = src->active_slice_buffers;
839 next = src->avail_slice_decoders;
840 src->avail_slice_decoders = this;
841 if( !--src->decoder_active_locked )
842 src->decoder_active.unlock();
848 src->active_slice_buffers = buffer->next;
849 slice_buffer = buffer;
850 video = buffer->video;
853 src->decoder_lock.unlock();
858 allocate_slice_decoders()
860 if( slice_decoders ) return;
862 if( count > MAX_CPUS ) count = MAX_CPUS;
863 slice_decoders = new slice_decoder_t[count];
864 slice_decoder_t *decoder = &slice_decoders[0];
865 avail_slice_decoders = 0;
866 for( int i=count; --i>=0; ++decoder ) {
868 decoder->next = avail_slice_decoders;
869 avail_slice_decoders = decoder;
871 total_slice_decoders = count;
875 delete_slice_decoders()
877 if( !slice_decoders ) return;
878 delete [] slice_decoders;
880 total_slice_decoders = 0;
884 reallocate_slice_decoders()
886 decoder_active.lock();
887 decoder_active.unlock();
889 if( !decoder_active_locked ) {
890 delete_slice_decoders();
891 allocate_slice_decoders();
893 decoder_lock.unlock();
897 allocate_slice_buffers()
899 if( slice_buffers ) return;
900 int count = 2*src->cpus;
901 if( count > 2*MAX_CPUS ) count = 2*MAX_CPUS;
902 slice_buffers = new slice_buffer_t[count];
903 slice_buffer_t *buffer = &slice_buffers[0];
904 avail_slice_buffers = 0;
905 for( int i=count; --i>=0; ++buffer ) {
906 buffer->next = avail_slice_buffers;
907 avail_slice_buffers = buffer;
909 total_slice_buffers = count;
913 delete_slice_buffers()
915 if( !slice_buffers ) return;
916 delete [] slice_buffers;
918 total_slice_buffers = 0;
922 reallocate_slice_buffers()
925 slice_active.unlock();
927 if( !slice_active_locked ) {
928 delete_slice_buffers();
929 allocate_slice_buffers();