1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
9 int mpeg3video_get_cbp(mpeg3_slice_t
*slice
)
12 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
14 if((code
= mpeg3slice_showbits9(slice_buffer
)) >= 128)
17 mpeg3slice_flushbits(slice_buffer
, mpeg3_CBPtab0
[code
].len
);
18 return mpeg3_CBPtab0
[code
].val
;
24 mpeg3slice_flushbits(slice_buffer
, mpeg3_CBPtab1
[code
].len
);
25 return mpeg3_CBPtab1
[code
].val
;
30 /* fprintf(stderr,"mpeg3video_get_cbp: invalid coded_block_pattern code\n"); */
35 mpeg3slice_flushbits(slice_buffer
, mpeg3_CBPtab2
[code
].len
);
36 return mpeg3_CBPtab2
[code
].val
;
40 /* set block to zero */
41 int mpeg3video_clearblock(mpeg3_slice_t
*slice
, int comp
, int size
)
43 slice
->sparse
[comp
] = 1;
45 /* Compiler error with 2.95 required hard coding the size to 6 */
47 memset(slice
->block
[comp
], 0, sizeof(short) * 64 * size
);
51 static inline int mpeg3video_getdclum(mpeg3_slice_buffer_t
*slice_buffer
)
55 code
= mpeg3slice_showbits5(slice_buffer
);
59 size
= mpeg3_DClumtab0
[code
].val
;
60 mpeg3slice_flushbits(slice_buffer
, mpeg3_DClumtab0
[code
].len
);
64 code
= mpeg3slice_showbits9(slice_buffer
) - 0x1f0;
65 size
= mpeg3_DClumtab1
[code
].val
;
66 mpeg3slice_flushbits(slice_buffer
, mpeg3_DClumtab1
[code
].len
);
69 if(size
== 0) val
= 0;
72 val
= mpeg3slice_getbits(slice_buffer
, size
);
73 if((val
& (1 << (size
- 1))) == 0) val
-= (1 << size
) - 1;
80 int mpeg3video_getdcchrom(mpeg3_slice_buffer_t
*slice_buffer
)
85 code
= mpeg3slice_showbits5(slice_buffer
);
89 size
= mpeg3_DCchromtab0
[code
].val
;
90 mpeg3slice_flushbits(slice_buffer
, mpeg3_DCchromtab0
[code
].len
);
94 code
= mpeg3slice_showbits(slice_buffer
, 10) - 0x3e0;
95 size
= mpeg3_DCchromtab1
[code
].val
;
96 mpeg3slice_flushbits(slice_buffer
, mpeg3_DCchromtab1
[code
].len
);
99 if(size
== 0) val
= 0;
102 val
= mpeg3slice_getbits(slice_buffer
, size
);
103 if((val
& (1 << (size
- 1))) == 0) val
-= (1 << size
) - 1;
110 /* decode one intra coded MPEG-1 block */
112 int mpeg3video_getintrablock(mpeg3_slice_t
*slice
,
117 int val
, i
, j
= 8, sign
;
119 mpeg3_DCTtab_t
*tab
= 0;
120 short *bp
= slice
->block
[comp
];
121 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
123 /* decode DC coefficients */
125 bp
[0] = (dc_dct_pred
[0] += mpeg3video_getdclum(slice_buffer
)) << 3;
128 bp
[0] = (dc_dct_pred
[1] += mpeg3video_getdcchrom(slice_buffer
)) << 3;
130 bp
[0] = (dc_dct_pred
[2] += mpeg3video_getdcchrom(slice_buffer
)) << 3;
137 if(slice
->fault
) return 1;
139 /* decode AC coefficients */
142 code
= mpeg3slice_showbits16(slice_buffer
);
144 tab
= &mpeg3_DCTtabnext
[(code
>> 12) - 4];
146 if(code
>= 1024) tab
= &mpeg3_DCTtab0
[(code
>> 8) - 4];
148 if(code
>= 512) tab
= &mpeg3_DCTtab1
[(code
>> 6) - 8];
150 if(code
>= 256) tab
= &mpeg3_DCTtab2
[(code
>> 4) - 16];
152 if(code
>= 128) tab
= &mpeg3_DCTtab3
[(code
>> 3) - 16];
154 if(code
>= 64) tab
= &mpeg3_DCTtab4
[(code
>> 2) - 16];
156 if(code
>= 32) tab
= &mpeg3_DCTtab5
[(code
>> 1) - 16];
158 if(code
>= 16) tab
= &mpeg3_DCTtab6
[code
- 16];
161 /* fprintf(stderr, "mpeg3video_getintrablock: invalid Huffman code\n"); */
166 mpeg3slice_flushbits(slice_buffer
, tab
->len
);
168 if(tab
->run
== 64) break; /* end_of_block */
173 i
+= mpeg3slice_getbits(slice_buffer
, 6);
175 if((val
= mpeg3slice_getbits(slice_buffer
, 8)) == 0)
176 val
= mpeg3slice_getbits(slice_buffer
, 8);
179 val
= mpeg3slice_getbits(slice_buffer
, 8) - 256;
184 if((sign
= (val
< 0)) != 0) val
= -val
;
190 sign
= mpeg3slice_getbit(slice_buffer
);
194 j
= video
->mpeg3_zigzag_scan_table
[i
];
205 val
= (val
* slice
->quant_scale
* video
->intra_quantizer_matrix
[j
]) << 1;
206 val
= (val
- 16) | 16;
211 val
= (val
* slice
->quant_scale
* video
->intra_quantizer_matrix
[j
]) >> 3;
215 bp
[j
] = sign
? -val
: val
;
220 /* not a sparse matrix ! */
221 slice
->sparse
[comp
] = 0;
227 /* decode one non-intra coded MPEG-1 block */
229 int mpeg3video_getinterblock(mpeg3_slice_t
*slice
,
236 short *bp
= slice
->block
[comp
];
237 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
239 /* decode AC coefficients */
242 code
= mpeg3slice_showbits16(slice_buffer
);
246 tab
= &mpeg3_DCTtabfirst
[(code
>> 12) - 4];
248 tab
= &mpeg3_DCTtabnext
[(code
>> 12) - 4];
251 if(code
>= 1024) tab
= &mpeg3_DCTtab0
[(code
>> 8) - 4];
253 if(code
>= 512) tab
= &mpeg3_DCTtab1
[(code
>> 6) - 8];
255 if(code
>= 256) tab
= &mpeg3_DCTtab2
[(code
>> 4) - 16];
257 if(code
>= 128) tab
= &mpeg3_DCTtab3
[(code
>> 3) - 16];
259 if(code
>= 64) tab
= &mpeg3_DCTtab4
[(code
>> 2) - 16];
261 if(code
>= 32) tab
= &mpeg3_DCTtab5
[(code
>> 1) - 16];
263 if(code
>= 16) tab
= &mpeg3_DCTtab6
[code
- 16];
266 // invalid Huffman code
271 mpeg3slice_flushbits(slice_buffer
, tab
->len
);
280 i
+= mpeg3slice_getbits(slice_buffer
, 6);
281 if((val
= mpeg3slice_getbits(slice_buffer
, 8)) == 0)
282 val
= mpeg3slice_getbits(slice_buffer
, 8);
285 val
= mpeg3slice_getbits(slice_buffer
, 8) - 256;
290 if((sign
= (val
< 0)) != 0) val
= -val
;
296 sign
= mpeg3slice_getbit(slice_buffer
);
299 j
= video
->mpeg3_zigzag_scan_table
[i
];
304 val
= (((val
<< 1)+1) * slice
->quant_scale
* video
->non_intra_quantizer_matrix
[j
]);
305 val
= (val
- 16) | 16;
310 val
= (((val
<< 1)+1) * slice
->quant_scale
* video
->non_intra_quantizer_matrix
[j
]) >> 4;
314 bp
[j
] = sign
? -val
: val
;
319 /* not a sparse matrix ! */
320 slice
->sparse
[comp
] = 0;
326 /* decode one intra coded MPEG-2 block */
327 int mpeg3video_getmpg2intrablock(mpeg3_slice_t
*slice
,
332 int val
, i
, j
, sign
, nc
;
337 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
339 /* with data partitioning, data always goes to base layer */
340 bp
= slice
->block
[comp
];
342 qmat
= (comp
< 4 || video
->chroma_format
== CHROMA420
)
343 ? video
->intra_quantizer_matrix
344 : video
->chroma_intra_quantizer_matrix
;
346 /* decode DC coefficients */
348 val
= (dc_dct_pred
[0] += mpeg3video_getdclum(slice_buffer
));
351 val
= (dc_dct_pred
[1] += mpeg3video_getdcchrom(slice_buffer
));
353 val
= (dc_dct_pred
[2] += mpeg3video_getdcchrom(slice_buffer
));
355 if(slice
->fault
) return 0;
358 bp
[0] = val
<< (7 - video
->dc_prec
);
361 bp
[0] = val
<< (3 - video
->dc_prec
);
365 /* decode AC coefficients */
368 code
= mpeg3slice_showbits16(slice_buffer
);
370 if(code
>= 16384 && !video
->intravlc
)
371 tab
= &mpeg3_DCTtabnext
[(code
>> 12) - 4];
376 tab
= &mpeg3_DCTtab0a
[(code
>> 8) - 4];
378 tab
= &mpeg3_DCTtab0
[(code
>> 8) - 4];
384 tab
= &mpeg3_DCTtab1a
[(code
>> 6) - 8];
386 tab
= &mpeg3_DCTtab1
[(code
>> 6) - 8];
389 if(code
>= 256) tab
= &mpeg3_DCTtab2
[(code
>> 4) - 16];
391 if(code
>= 128) tab
= &mpeg3_DCTtab3
[(code
>> 3) - 16];
393 if(code
>= 64) tab
= &mpeg3_DCTtab4
[(code
>> 2) - 16];
395 if(code
>= 32) tab
= &mpeg3_DCTtab5
[(code
>> 1) - 16];
397 if(code
>= 16) tab
= &mpeg3_DCTtab6
[code
- 16];
400 /* fprintf(stderr,"mpeg3video_getmpg2intrablock: invalid Huffman code\n"); */
405 mpeg3slice_flushbits(slice_buffer
, tab
->len
);
414 i
+= mpeg3slice_getbits(slice_buffer
, 6);
416 val
= mpeg3slice_getbits(slice_buffer
, 12);
417 if((val
& 2047) == 0)
419 // invalid signed_level (escape)
423 if((sign
= (val
>= 2048)) != 0) val
= 4096 - val
;
429 sign
= mpeg3slice_getbit(slice_buffer
);
432 j
= (video
->altscan
? video
->mpeg3_alternate_scan_table
: video
->mpeg3_zigzag_scan_table
)[i
];
436 val
= (val
* slice
->quant_scale
* qmat
[j
]);
439 val
= (val
* slice
->quant_scale
* qmat
[j
]) >> 4;
441 bp
[j
] = sign
? -val
: val
;
447 /* not a sparse matrix ! */
448 slice
->sparse
[comp
] = 0;
454 /* decode one non-intra coded MPEG-2 block */
456 int mpeg3video_getmpg2interblock(mpeg3_slice_t
*slice
,
460 int val
, i
, j
, sign
, nc
;
465 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
467 /* with data partitioning, data always goes to base layer */
468 bp
= slice
->block
[comp
];
470 qmat
= (comp
< 4 || video
->chroma_format
== CHROMA420
)
471 ? video
->non_intra_quantizer_matrix
472 : video
->chroma_non_intra_quantizer_matrix
;
476 /* decode AC coefficients */
479 code
= mpeg3slice_showbits16(slice_buffer
);
482 if(i
== 0) tab
= &mpeg3_DCTtabfirst
[(code
>> 12) - 4];
483 else tab
= &mpeg3_DCTtabnext
[(code
>> 12) - 4];
486 if(code
>= 1024) tab
= &mpeg3_DCTtab0
[(code
>> 8) - 4];
488 if(code
>= 512) tab
= &mpeg3_DCTtab1
[(code
>> 6) - 8];
490 if(code
>= 256) tab
= &mpeg3_DCTtab2
[(code
>> 4) - 16];
492 if(code
>= 128) tab
= &mpeg3_DCTtab3
[(code
>> 3) - 16];
494 if(code
>= 64) tab
= &mpeg3_DCTtab4
[(code
>> 2) - 16];
496 if(code
>= 32) tab
= &mpeg3_DCTtab5
[(code
>> 1) - 16];
498 if(code
>= 16) tab
= &mpeg3_DCTtab6
[code
- 16];
501 // invalid Huffman code
506 mpeg3slice_flushbits(slice_buffer
, tab
->len
);
515 i
+= mpeg3slice_getbits(slice_buffer
, 6);
516 val
= mpeg3slice_getbits(slice_buffer
, 12);
517 if((val
& 2047) == 0)
519 /* fprintf(stderr, "mpeg3video_getmpg2interblock: invalid signed_level (escape)\n"); */
523 if((sign
= (val
>= 2048)) != 0) val
= 4096 - val
;
529 sign
= mpeg3slice_getbit(slice_buffer
);
532 j
= (video
->altscan
? video
->mpeg3_alternate_scan_table
: video
->mpeg3_zigzag_scan_table
)[i
];
536 val
= (((val
<< 1)+1) * slice
->quant_scale
* qmat
[j
]) >> 1;
539 val
= (((val
<< 1)+1) * slice
->quant_scale
* qmat
[j
]) >> 5;
541 bp
[j
] = sign
? (-val
) : val
;
547 slice
->sparse
[comp
] = 0;
553 /* decode all macroblocks of the current picture */
554 int mpeg3video_get_macroblocks(mpeg3video_t
*video
, int framenum
)
557 mpeg3_slice_buffer_t
*slice_buffer
; /* Buffer being loaded */
560 mpeg3_bits_t
*vstream
= video
->vstream
;
562 /* Load every slice into a buffer array */
563 video
->total_slice_buffers
= 0;
565 while(!mpeg3bits_eof(vstream
) &&
566 mpeg3bits_showbits32_noptr(vstream
) >= MPEG3_SLICE_MIN_START
&&
567 mpeg3bits_showbits32_noptr(vstream
) <= MPEG3_SLICE_MAX_START
&&
568 video
->total_slice_buffers
< MPEG3_MAX_CPUS
)
570 /* Initialize the buffer */
571 if(current_buffer
>= video
->slice_buffers_initialized
)
572 mpeg3_new_slice_buffer(&(video
->slice_buffers
[video
->slice_buffers_initialized
++]));
573 slice_buffer
= &(video
->slice_buffers
[current_buffer
]);
574 slice_buffer
->buffer_size
= 0;
575 slice_buffer
->current_position
= 0;
576 slice_buffer
->bits_size
= 0;
577 slice_buffer
->done
= 0;
579 /* Read the slice into the buffer including the slice start code */
583 if(slice_buffer
->buffer_allocation
<= slice_buffer
->buffer_size
)
584 mpeg3_expand_slice_buffer(slice_buffer
);
586 /* Load 1 char into buffer */
587 slice_buffer
->data
[slice_buffer
->buffer_size
++] = mpeg3bits_getbyte_noptr(vstream
);
588 }while(!mpeg3bits_eof(vstream
) &&
589 mpeg3bits_showbits24_noptr(vstream
) != MPEG3_PACKET_START_CODE_PREFIX
);
591 /* Pad the buffer to get the last macroblock */
592 if(slice_buffer
->buffer_allocation
<= slice_buffer
->buffer_size
+ 4)
593 mpeg3_expand_slice_buffer(slice_buffer
);
595 slice_buffer
->data
[slice_buffer
->buffer_size
++] = 0;
596 slice_buffer
->data
[slice_buffer
->buffer_size
++] = 0;
597 slice_buffer
->data
[slice_buffer
->buffer_size
++] = 1;
598 slice_buffer
->data
[slice_buffer
->buffer_size
++] = 0;
599 slice_buffer
->bits_size
= 0;
601 pthread_mutex_lock(&(slice_buffer
->completion_lock
)); fflush(stdout
);
603 video
->total_slice_buffers
++;
606 /* Run the slice decoders */
607 if(video
->total_slice_buffers
> 0)
609 for(i
= 0; i
< video
->total_slice_decoders
; i
++)
611 if(i
== 0 && video
->total_slice_decoders
> 1)
613 video
->slice_decoders
[i
].current_buffer
= 0;
614 video
->slice_decoders
[i
].buffer_step
= 1;
615 video
->slice_decoders
[i
].last_buffer
= (video
->total_slice_buffers
- 1);
620 video
->slice_decoders
[i
].current_buffer
= video
->total_slice_buffers
- 1;
621 video
->slice_decoders
[i
].buffer_step
= -1;
622 video
->slice_decoders
[i
].last_buffer
= 0;
626 video
->slice_decoders
[i
].current_buffer
= i
;
627 video
->slice_decoders
[i
].buffer_step
= 1;
628 video
->slice_decoders
[i
].last_buffer
= video
->total_slice_buffers
- 1;
630 pthread_mutex_unlock(&(video
->slice_decoders
[i
].input_lock
));
634 /* Wait for the slice buffers to finish */
635 if(video
->total_slice_buffers
> 0)
637 for(i
= 0; i
< video
->total_slice_buffers
; i
++)
639 pthread_mutex_lock(&(video
->slice_buffers
[i
].completion_lock
));
640 pthread_mutex_unlock(&(video
->slice_buffers
[i
].completion_lock
));
643 /* Wait for decoders to finish so packages aren't overwritten */
644 for(i
= 0; i
< video
->total_slice_decoders
; i
++)
646 pthread_mutex_lock(&(video
->slice_decoders
[i
].completion_lock
));
652 int mpeg3video_allocate_decoders(mpeg3video_t
*video
, int decoder_count
)
655 mpeg3_t
*file
= video
->file
;
656 /* Get the slice decoders */
657 if(video
->total_slice_decoders
!= file
->cpus
)
659 for(i
= 0; i
< video
->total_slice_decoders
; i
++)
661 mpeg3_delete_slice_decoder(&(video
->slice_decoders
[i
]));
664 for(i
= 0; i
< file
->cpus
&& i
< MPEG3_MAX_CPUS
; i
++)
666 mpeg3_new_slice_decoder(video
, &(video
->slice_decoders
[i
]));
667 video
->slice_decoders
[i
].thread_number
= i
;
670 video
->total_slice_decoders
= file
->cpus
;
675 /* decode one frame or field picture */
677 int mpeg3video_getpicture(mpeg3video_t
*video
, int framenum
)
680 mpeg3_t
*file
= video
->file
;
682 if(video
->pict_struct
== FRAME_PICTURE
&& video
->secondfield
)
684 /* recover from illegal number of field pictures */
685 video
->secondfield
= 0;
690 video
->current_repeat
= video
->repeat_count
= 0;
693 mpeg3video_allocate_decoders(video
, file
->cpus
);
695 for(i
= 0; i
< 3; i
++)
697 if(video
->pict_type
== B_TYPE
)
699 video
->newframe
[i
] = video
->auxframe
[i
];
703 if(!video
->secondfield
&& !video
->current_repeat
)
705 /* Swap refframes for I frames */
706 unsigned char* tmp
= video
->oldrefframe
[i
];
707 video
->oldrefframe
[i
] = video
->refframe
[i
];
708 video
->refframe
[i
] = tmp
;
711 video
->newframe
[i
] = video
->refframe
[i
];
714 if(video
->pict_struct
== BOTTOM_FIELD
)
716 /* Only used if fields are in different pictures */
717 video
->newframe
[i
] += (i
== 0) ?
718 video
->coded_picture_width
:
724 /* The problem is when a B frame lands on the first repeat and is skipped, */
725 /* the second repeat goes for the same bitmap as the skipped repeat, */
726 /* so it picks up a frame from 3 frames back. */
727 /* The first repeat must consititutively read a B frame if its B frame is going to be */
728 /* used in a later repeat. */
729 if(!video
->current_repeat
)
730 if(!(video
->skip_bframes
&& video
->pict_type
== B_TYPE
) ||
731 (video
->repeat_count
>= 100 + 100 * video
->skip_bframes
))
732 result
= mpeg3video_get_macroblocks(video
, framenum
);
734 /* Set the frame to display */
735 video
->output_src
= 0;
736 if(framenum
> -1 && !result
)
738 if(video
->pict_struct
== FRAME_PICTURE
|| video
->secondfield
)
740 if(video
->pict_type
== B_TYPE
)
742 video
->output_src
= video
->auxframe
;
746 video
->output_src
= video
->oldrefframe
;
751 mpeg3video_display_second_field(video
);
757 video
->current_repeat
+= 100;
760 if(video
->pict_struct
!= FRAME_PICTURE
)
761 video
->secondfield
= !video
->secondfield
;