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 bzero(slice
->block
[comp
], sizeof(short) * 64 * size
);
48 // memset(slice->block[comp], 0, sizeof(short) * 64 * size);
52 static inline int mpeg3video_getdclum(mpeg3_slice_buffer_t
*slice_buffer
)
56 code
= mpeg3slice_showbits5(slice_buffer
);
60 size
= mpeg3_DClumtab0
[code
].val
;
61 mpeg3slice_flushbits(slice_buffer
, mpeg3_DClumtab0
[code
].len
);
65 code
= mpeg3slice_showbits9(slice_buffer
) - 0x1f0;
66 size
= mpeg3_DClumtab1
[code
].val
;
67 mpeg3slice_flushbits(slice_buffer
, mpeg3_DClumtab1
[code
].len
);
70 if(size
== 0) val
= 0;
73 val
= mpeg3slice_getbits(slice_buffer
, size
);
74 if((val
& (1 << (size
- 1))) == 0) val
-= (1 << size
) - 1;
81 int mpeg3video_getdcchrom(mpeg3_slice_buffer_t
*slice_buffer
)
86 code
= mpeg3slice_showbits5(slice_buffer
);
90 size
= mpeg3_DCchromtab0
[code
].val
;
91 mpeg3slice_flushbits(slice_buffer
, mpeg3_DCchromtab0
[code
].len
);
95 code
= mpeg3slice_showbits(slice_buffer
, 10) - 0x3e0;
96 size
= mpeg3_DCchromtab1
[code
].val
;
97 mpeg3slice_flushbits(slice_buffer
, mpeg3_DCchromtab1
[code
].len
);
100 if(size
== 0) val
= 0;
103 val
= mpeg3slice_getbits(slice_buffer
, size
);
104 if((val
& (1 << (size
- 1))) == 0) val
-= (1 << size
) - 1;
111 /* decode one intra coded MPEG-1 block */
113 int mpeg3video_getintrablock(mpeg3_slice_t
*slice
,
118 int val
, i
, j
= 8, sign
;
120 mpeg3_DCTtab_t
*tab
= 0;
121 short *bp
= slice
->block
[comp
];
122 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
124 /* decode DC coefficients */
126 bp
[0] = (dc_dct_pred
[0] += mpeg3video_getdclum(slice_buffer
)) << 3;
129 bp
[0] = (dc_dct_pred
[1] += mpeg3video_getdcchrom(slice_buffer
)) << 3;
131 bp
[0] = (dc_dct_pred
[2] += mpeg3video_getdcchrom(slice_buffer
)) << 3;
133 if(slice
->fault
) return 1;
135 /* decode AC coefficients */
138 code
= mpeg3slice_showbits16(slice_buffer
);
140 tab
= &mpeg3_DCTtabnext
[(code
>> 12) - 4];
142 if(code
>= 1024) tab
= &mpeg3_DCTtab0
[(code
>> 8) - 4];
144 if(code
>= 512) tab
= &mpeg3_DCTtab1
[(code
>> 6) - 8];
146 if(code
>= 256) tab
= &mpeg3_DCTtab2
[(code
>> 4) - 16];
148 if(code
>= 128) tab
= &mpeg3_DCTtab3
[(code
>> 3) - 16];
150 if(code
>= 64) tab
= &mpeg3_DCTtab4
[(code
>> 2) - 16];
152 if(code
>= 32) tab
= &mpeg3_DCTtab5
[(code
>> 1) - 16];
154 if(code
>= 16) tab
= &mpeg3_DCTtab6
[code
- 16];
157 /* fprintf(stderr, "mpeg3video_getintrablock: invalid Huffman code\n"); */
162 mpeg3slice_flushbits(slice_buffer
, tab
->len
);
164 if(tab
->run
== 64) break; /* end_of_block */
169 i
+= mpeg3slice_getbits(slice_buffer
, 6);
171 if((val
= mpeg3slice_getbits(slice_buffer
, 8)) == 0)
172 val
= mpeg3slice_getbits(slice_buffer
, 8);
175 val
= mpeg3slice_getbits(slice_buffer
, 8) - 256;
180 if((sign
= (val
< 0)) != 0) val
= -val
;
186 sign
= mpeg3slice_getbit(slice_buffer
);
190 j
= video
->mpeg3_zigzag_scan_table
[i
];
198 val
= (val
* slice
->quant_scale
* video
->intra_quantizer_matrix
[j
]) >> 3;
201 bp
[j
] = sign
? -val
: val
;
206 /* not a sparse matrix ! */
207 slice
->sparse
[comp
] = 0;
213 /* decode one non-intra coded MPEG-1 block */
215 int mpeg3video_getinterblock(mpeg3_slice_t
*slice
,
222 short *bp
= slice
->block
[comp
];
223 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
225 /* decode AC coefficients */
228 code
= mpeg3slice_showbits16(slice_buffer
);
232 tab
= &mpeg3_DCTtabfirst
[(code
>> 12) - 4];
234 tab
= &mpeg3_DCTtabnext
[(code
>> 12) - 4];
237 if(code
>= 1024) tab
= &mpeg3_DCTtab0
[(code
>> 8) - 4];
239 if(code
>= 512) tab
= &mpeg3_DCTtab1
[(code
>> 6) - 8];
241 if(code
>= 256) tab
= &mpeg3_DCTtab2
[(code
>> 4) - 16];
243 if(code
>= 128) tab
= &mpeg3_DCTtab3
[(code
>> 3) - 16];
245 if(code
>= 64) tab
= &mpeg3_DCTtab4
[(code
>> 2) - 16];
247 if(code
>= 32) tab
= &mpeg3_DCTtab5
[(code
>> 1) - 16];
249 if(code
>= 16) tab
= &mpeg3_DCTtab6
[code
- 16];
252 // invalid Huffman code
257 mpeg3slice_flushbits(slice_buffer
, tab
->len
);
266 i
+= mpeg3slice_getbits(slice_buffer
, 6);
267 if((val
= mpeg3slice_getbits(slice_buffer
, 8)) == 0)
268 val
= mpeg3slice_getbits(slice_buffer
, 8);
271 val
= mpeg3slice_getbits(slice_buffer
, 8) - 256;
276 if((sign
= (val
< 0)) != 0) val
= -val
;
282 sign
= mpeg3slice_getbit(slice_buffer
);
285 j
= video
->mpeg3_zigzag_scan_table
[i
];
287 val
= (((val
<< 1)+1) * slice
->quant_scale
* video
->non_intra_quantizer_matrix
[j
]) >> 4;
290 bp
[j
] = sign
? -val
: val
;
295 /* not a sparse matrix ! */
296 slice
->sparse
[comp
] = 0;
302 /* decode one intra coded MPEG-2 block */
303 int mpeg3video_getmpg2intrablock(mpeg3_slice_t
*slice
,
308 int val
, i
, j
, sign
, nc
;
313 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
315 /* with data partitioning, data always goes to base layer */
316 bp
= slice
->block
[comp
];
318 qmat
= (comp
< 4 || video
->chroma_format
== CHROMA420
)
319 ? video
->intra_quantizer_matrix
320 : video
->chroma_intra_quantizer_matrix
;
322 /* decode DC coefficients */
324 val
= (dc_dct_pred
[0] += mpeg3video_getdclum(slice_buffer
));
327 val
= (dc_dct_pred
[1] += mpeg3video_getdcchrom(slice_buffer
));
329 val
= (dc_dct_pred
[2] += mpeg3video_getdcchrom(slice_buffer
));
331 if(slice
->fault
) return 0;
332 bp
[0] = val
<< (3 - video
->dc_prec
);
336 /* decode AC coefficients */
339 code
= mpeg3slice_showbits16(slice_buffer
);
341 if(code
>= 16384 && !video
->intravlc
)
342 tab
= &mpeg3_DCTtabnext
[(code
>> 12) - 4];
347 tab
= &mpeg3_DCTtab0a
[(code
>> 8) - 4];
349 tab
= &mpeg3_DCTtab0
[(code
>> 8) - 4];
355 tab
= &mpeg3_DCTtab1a
[(code
>> 6) - 8];
357 tab
= &mpeg3_DCTtab1
[(code
>> 6) - 8];
360 if(code
>= 256) tab
= &mpeg3_DCTtab2
[(code
>> 4) - 16];
362 if(code
>= 128) tab
= &mpeg3_DCTtab3
[(code
>> 3) - 16];
364 if(code
>= 64) tab
= &mpeg3_DCTtab4
[(code
>> 2) - 16];
366 if(code
>= 32) tab
= &mpeg3_DCTtab5
[(code
>> 1) - 16];
368 if(code
>= 16) tab
= &mpeg3_DCTtab6
[code
- 16];
371 /* fprintf(stderr,"mpeg3video_getmpg2intrablock: invalid Huffman code\n"); */
376 mpeg3slice_flushbits(slice_buffer
, tab
->len
);
385 i
+= mpeg3slice_getbits(slice_buffer
, 6);
387 val
= mpeg3slice_getbits(slice_buffer
, 12);
388 if((val
& 2047) == 0)
390 // invalid signed_level (escape)
394 if((sign
= (val
>= 2048)) != 0) val
= 4096 - val
;
400 sign
= mpeg3slice_getbit(slice_buffer
);
403 j
= (video
->altscan
? video
->mpeg3_alternate_scan_table
: video
->mpeg3_zigzag_scan_table
)[i
];
405 val
= (val
* slice
->quant_scale
* qmat
[j
]) >> 4;
407 bp
[j
] = sign
? -val
: val
;
413 /* not a sparse matrix ! */
414 slice
->sparse
[comp
] = 0;
420 /* decode one non-intra coded MPEG-2 block */
422 int mpeg3video_getmpg2interblock(mpeg3_slice_t
*slice
,
426 int val
, i
, j
, sign
, nc
;
431 mpeg3_slice_buffer_t
*slice_buffer
= slice
->slice_buffer
;
433 /* with data partitioning, data always goes to base layer */
434 bp
= slice
->block
[comp
];
436 qmat
= (comp
< 4 || video
->chroma_format
== CHROMA420
)
437 ? video
->non_intra_quantizer_matrix
438 : video
->chroma_non_intra_quantizer_matrix
;
442 /* decode AC coefficients */
445 code
= mpeg3slice_showbits16(slice_buffer
);
448 if(i
== 0) tab
= &mpeg3_DCTtabfirst
[(code
>> 12) - 4];
449 else tab
= &mpeg3_DCTtabnext
[(code
>> 12) - 4];
452 if(code
>= 1024) tab
= &mpeg3_DCTtab0
[(code
>> 8) - 4];
454 if(code
>= 512) tab
= &mpeg3_DCTtab1
[(code
>> 6) - 8];
456 if(code
>= 256) tab
= &mpeg3_DCTtab2
[(code
>> 4) - 16];
458 if(code
>= 128) tab
= &mpeg3_DCTtab3
[(code
>> 3) - 16];
460 if(code
>= 64) tab
= &mpeg3_DCTtab4
[(code
>> 2) - 16];
462 if(code
>= 32) tab
= &mpeg3_DCTtab5
[(code
>> 1) - 16];
464 if(code
>= 16) tab
= &mpeg3_DCTtab6
[code
- 16];
467 // invalid Huffman code
472 mpeg3slice_flushbits(slice_buffer
, tab
->len
);
481 i
+= mpeg3slice_getbits(slice_buffer
, 6);
482 val
= mpeg3slice_getbits(slice_buffer
, 12);
483 if((val
& 2047) == 0)
485 /* fprintf(stderr, "mpeg3video_getmpg2interblock: invalid signed_level (escape)\n"); */
489 if((sign
= (val
>= 2048)) != 0) val
= 4096 - val
;
495 sign
= mpeg3slice_getbit(slice_buffer
);
498 j
= (video
->altscan
? video
->mpeg3_alternate_scan_table
: video
->mpeg3_zigzag_scan_table
)[i
];
500 val
= (((val
<< 1)+1) * slice
->quant_scale
* qmat
[j
]) >> 5;
502 bp
[j
] = sign
? (-val
) : val
;
508 slice
->sparse
[comp
] = 0;
514 /* decode all macroblocks of the current picture */
515 int mpeg3video_get_macroblocks(mpeg3video_t
*video
, int framenum
)
518 mpeg3_slice_buffer_t
*slice_buffer
; /* Buffer being loaded */
521 mpeg3_bits_t
*vstream
= video
->vstream
;
523 /* Load every slice into a buffer array */
524 video
->total_slice_buffers
= 0;
527 while(!mpeg3bits_eof(vstream
) &&
528 mpeg3bits_showbits32_noptr(vstream
) >= MPEG3_SLICE_MIN_START
&&
529 mpeg3bits_showbits32_noptr(vstream
) <= MPEG3_SLICE_MAX_START
&&
530 video
->total_slice_buffers
< MPEG3_MAX_CPUS
)
532 /* Initialize the buffer */
533 if(current_buffer
>= video
->slice_buffers_initialized
)
534 mpeg3_new_slice_buffer(&(video
->slice_buffers
[video
->slice_buffers_initialized
++]));
535 slice_buffer
= &(video
->slice_buffers
[current_buffer
]);
536 slice_buffer
->buffer_size
= 0;
537 slice_buffer
->current_position
= 0;
538 slice_buffer
->bits_size
= 0;
539 slice_buffer
->done
= 0;
541 /* Read the slice into the buffer including the slice start code */
545 if(slice_buffer
->buffer_allocation
<= slice_buffer
->buffer_size
)
546 mpeg3_expand_slice_buffer(slice_buffer
);
548 /* Load 1 char into buffer */
549 slice_buffer
->data
[slice_buffer
->buffer_size
++] = mpeg3bits_getbyte_noptr(vstream
);
550 }while(!mpeg3bits_eof(vstream
) &&
551 mpeg3bits_showbits24_noptr(vstream
) != MPEG3_PACKET_START_CODE_PREFIX
);
553 /* Pad the buffer to get the last macroblock */
554 if(slice_buffer
->buffer_allocation
<= slice_buffer
->buffer_size
+ 4)
555 mpeg3_expand_slice_buffer(slice_buffer
);
557 slice_buffer
->data
[slice_buffer
->buffer_size
++] = 0;
558 slice_buffer
->data
[slice_buffer
->buffer_size
++] = 0;
559 slice_buffer
->data
[slice_buffer
->buffer_size
++] = 1;
560 slice_buffer
->data
[slice_buffer
->buffer_size
++] = 0;
561 slice_buffer
->bits_size
= 0;
563 pthread_mutex_lock(&(slice_buffer
->completion_lock
));
565 video
->total_slice_buffers
++;
570 /* Run the slice decoders */
571 if(video
->total_slice_buffers
> 0)
573 for(i
= 0; i
< video
->total_slice_decoders
; i
++)
575 if(i
== 0 && video
->total_slice_decoders
> 1)
577 video
->slice_decoders
[i
].current_buffer
= 0;
578 video
->slice_decoders
[i
].buffer_step
= 1;
579 video
->slice_decoders
[i
].last_buffer
= (video
->total_slice_buffers
- 1);
584 video
->slice_decoders
[i
].current_buffer
= video
->total_slice_buffers
- 1;
585 video
->slice_decoders
[i
].buffer_step
= -1;
586 video
->slice_decoders
[i
].last_buffer
= 0;
590 video
->slice_decoders
[i
].current_buffer
= i
;
591 video
->slice_decoders
[i
].buffer_step
= 1;
592 video
->slice_decoders
[i
].last_buffer
= video
->total_slice_buffers
- 1;
594 pthread_mutex_unlock(&(video
->slice_decoders
[i
].input_lock
));
598 /* Wait for the slice buffers to finish */
599 if(video
->total_slice_buffers
> 0)
601 for(i
= 0; i
< video
->total_slice_buffers
; i
++)
603 pthread_mutex_lock(&(video
->slice_buffers
[i
].completion_lock
));
604 pthread_mutex_unlock(&(video
->slice_buffers
[i
].completion_lock
));
607 /* Wait for decoders to finish so packages aren't overwritten */
608 for(i
= 0; i
< video
->total_slice_decoders
; i
++)
610 pthread_mutex_lock(&(video
->slice_decoders
[i
].completion_lock
));
616 int mpeg3video_allocate_decoders(mpeg3video_t
*video
, int decoder_count
)
619 mpeg3_t
*file
= video
->file
;
620 /* Get the slice decoders */
621 if(video
->total_slice_decoders
!= file
->cpus
)
623 for(i
= 0; i
< video
->total_slice_decoders
; i
++)
625 mpeg3_delete_slice_decoder(&(video
->slice_decoders
[i
]));
628 for(i
= 0; i
< file
->cpus
&& i
< MPEG3_MAX_CPUS
; i
++)
630 mpeg3_new_slice_decoder(video
, &(video
->slice_decoders
[i
]));
631 video
->slice_decoders
[i
].thread_number
= i
;
634 video
->total_slice_decoders
= file
->cpus
;
639 /* decode one frame or field picture */
641 int mpeg3video_getpicture(mpeg3video_t
*video
, int framenum
)
644 mpeg3_t
*file
= video
->file
;
646 if(video
->pict_struct
== FRAME_PICTURE
&& video
->secondfield
)
648 /* recover from illegal number of field pictures */
649 video
->secondfield
= 0;
654 video
->current_repeat
= video
->repeat_count
= 0;
657 mpeg3video_allocate_decoders(video
, file
->cpus
);
659 for(i
= 0; i
< 3; i
++)
661 if(video
->pict_type
== B_TYPE
)
663 video
->newframe
[i
] = video
->auxframe
[i
];
667 if(!video
->secondfield
&& !video
->current_repeat
)
669 /* Swap refframes for I frames */
670 unsigned char* tmp
= video
->oldrefframe
[i
];
671 video
->oldrefframe
[i
] = video
->refframe
[i
];
672 video
->refframe
[i
] = tmp
;
675 video
->newframe
[i
] = video
->refframe
[i
];
678 if(video
->pict_struct
== BOTTOM_FIELD
)
680 /* Only used if fields are in different pictures */
681 video
->newframe
[i
] += (i
== 0) ?
682 video
->coded_picture_width
:
688 /* The problem is when a B frame lands on the first repeat and is skipped, */
689 /* the second repeat goes for the same bitmap as the skipped repeat, */
690 /* so it picks up a frame from 3 frames back. */
691 /* The first repeat must consititutively read a B frame if its B frame is going to be */
692 /* used in a later repeat. */
693 if(!video
->current_repeat
)
694 if(!(video
->skip_bframes
&& video
->pict_type
== B_TYPE
) ||
695 (video
->repeat_count
>= 100 + 100 * video
->skip_bframes
))
696 result
= mpeg3video_get_macroblocks(video
, framenum
);
698 /* Set the frame to display */
699 video
->output_src
[0] = 0;
700 video
->output_src
[1] = 0;
701 video
->output_src
[2] = 0;
702 if(framenum
> -1 && !result
)
704 if(video
->pict_struct
== FRAME_PICTURE
|| video
->secondfield
)
706 if(video
->pict_type
== B_TYPE
)
708 video
->output_src
[0] = video
->auxframe
[0];
709 video
->output_src
[1] = video
->auxframe
[1];
710 video
->output_src
[2] = video
->auxframe
[2];
714 video
->output_src
[0] = video
->oldrefframe
[0];
715 video
->output_src
[1] = video
->oldrefframe
[1];
716 video
->output_src
[2] = video
->oldrefframe
[2];
721 mpeg3video_display_second_field(video
);
727 video
->current_repeat
+= 100;
730 if(video
->pict_struct
!= FRAME_PICTURE
)
731 video
->secondfield
= !video
->secondfield
;