r123: Merged HEAD and TEST. New stuff shall be committed to HEAD from now on.
[cinelerra_cv/mob.git] / libmpeg3 / video / getpicture.c
blob2f26dc51d39c8326fcbc6389a03a4cad3c2f2329
1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
3 #include "vlc.h"
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 int mpeg3video_get_cbp(mpeg3_slice_t *slice)
11 int code;
12 mpeg3_slice_buffer_t *slice_buffer = slice->slice_buffer;
14 if((code = mpeg3slice_showbits9(slice_buffer)) >= 128)
16 code >>= 4;
17 mpeg3slice_flushbits(slice_buffer, mpeg3_CBPtab0[code].len);
18 return mpeg3_CBPtab0[code].val;
21 if(code >= 8)
23 code >>= 1;
24 mpeg3slice_flushbits(slice_buffer, mpeg3_CBPtab1[code].len);
25 return mpeg3_CBPtab1[code].val;
28 if(code < 1)
30 /* fprintf(stderr,"mpeg3video_get_cbp: invalid coded_block_pattern code\n"); */
31 slice->fault = 1;
32 return 0;
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);
48 return 0;
51 static inline int mpeg3video_getdclum(mpeg3_slice_buffer_t *slice_buffer)
53 int code, size, val;
54 /* decode length */
55 code = mpeg3slice_showbits5(slice_buffer);
57 if(code < 31)
59 size = mpeg3_DClumtab0[code].val;
60 mpeg3slice_flushbits(slice_buffer, mpeg3_DClumtab0[code].len);
62 else
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;
70 else
72 val = mpeg3slice_getbits(slice_buffer, size);
73 if((val & (1 << (size - 1))) == 0) val -= (1 << size) - 1;
76 return val;
80 int mpeg3video_getdcchrom(mpeg3_slice_buffer_t *slice_buffer)
82 int code, size, val;
84 /* decode length */
85 code = mpeg3slice_showbits5(slice_buffer);
87 if(code < 31)
89 size = mpeg3_DCchromtab0[code].val;
90 mpeg3slice_flushbits(slice_buffer, mpeg3_DCchromtab0[code].len);
92 else
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;
100 else
102 val = mpeg3slice_getbits(slice_buffer, size);
103 if((val & (1 << (size - 1))) == 0) val -= (1 << size) - 1;
106 return val;
110 /* decode one intra coded MPEG-1 block */
112 int mpeg3video_getintrablock(mpeg3_slice_t *slice,
113 mpeg3video_t *video,
114 int comp,
115 int dc_dct_pred[])
117 int val, i, j = 8, sign;
118 unsigned int code;
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 */
124 if(comp < 4)
125 bp[0] = (dc_dct_pred[0] += mpeg3video_getdclum(slice_buffer)) << 3;
126 else
127 if(comp == 4)
128 bp[0] = (dc_dct_pred[1] += mpeg3video_getdcchrom(slice_buffer)) << 3;
129 else
130 bp[0] = (dc_dct_pred[2] += mpeg3video_getdcchrom(slice_buffer)) << 3;
132 #ifdef HAVE_MMX
133 if(video->have_mmx)
134 bp[0] <<= 4;
135 #endif
137 if(slice->fault) return 1;
139 /* decode AC coefficients */
140 for(i = 1; ; i++)
142 code = mpeg3slice_showbits16(slice_buffer);
143 if(code >= 16384)
144 tab = &mpeg3_DCTtabnext[(code >> 12) - 4];
145 else
146 if(code >= 1024) tab = &mpeg3_DCTtab0[(code >> 8) - 4];
147 else
148 if(code >= 512) tab = &mpeg3_DCTtab1[(code >> 6) - 8];
149 else
150 if(code >= 256) tab = &mpeg3_DCTtab2[(code >> 4) - 16];
151 else
152 if(code >= 128) tab = &mpeg3_DCTtab3[(code >> 3) - 16];
153 else
154 if(code >= 64) tab = &mpeg3_DCTtab4[(code >> 2) - 16];
155 else
156 if(code >= 32) tab = &mpeg3_DCTtab5[(code >> 1) - 16];
157 else
158 if(code >= 16) tab = &mpeg3_DCTtab6[code - 16];
159 else
161 /* fprintf(stderr, "mpeg3video_getintrablock: invalid Huffman code\n"); */
162 slice->fault = 1;
163 return 0;
166 mpeg3slice_flushbits(slice_buffer, tab->len);
168 if(tab->run == 64) break; /* end_of_block */
170 if(tab->run == 65)
172 /* escape */
173 i += mpeg3slice_getbits(slice_buffer, 6);
175 if((val = mpeg3slice_getbits(slice_buffer, 8)) == 0)
176 val = mpeg3slice_getbits(slice_buffer, 8);
177 else
178 if(val == 128)
179 val = mpeg3slice_getbits(slice_buffer, 8) - 256;
180 else
181 if(val > 128)
182 val -= 256;
184 if((sign = (val < 0)) != 0) val= -val;
186 else
188 i += tab->run;
189 val = tab->level;
190 sign = mpeg3slice_getbit(slice_buffer);
193 if(i < 64)
194 j = video->mpeg3_zigzag_scan_table[i];
195 else
197 slice->fault = 1;
198 return 0;
202 #ifdef HAVE_MMX
203 if(video->have_mmx)
205 val = (val * slice->quant_scale * video->intra_quantizer_matrix[j]) << 1;
206 val = (val - 16) | 16;
208 else
209 #endif
211 val = (val * slice->quant_scale * video->intra_quantizer_matrix[j]) >> 3;
212 val = (val - 1) | 1;
215 bp[j] = sign ? -val : val;
218 if(j != 0)
220 /* not a sparse matrix ! */
221 slice->sparse[comp] = 0;
223 return 0;
227 /* decode one non-intra coded MPEG-1 block */
229 int mpeg3video_getinterblock(mpeg3_slice_t *slice,
230 mpeg3video_t *video,
231 int comp)
233 int val, i, j, sign;
234 unsigned int code;
235 mpeg3_DCTtab_t *tab;
236 short *bp = slice->block[comp];
237 mpeg3_slice_buffer_t *slice_buffer = slice->slice_buffer;
239 /* decode AC coefficients */
240 for(i = 0; ; i++)
242 code = mpeg3slice_showbits16(slice_buffer);
243 if(code >= 16384)
245 if(i == 0)
246 tab = &mpeg3_DCTtabfirst[(code >> 12) - 4];
247 else
248 tab = &mpeg3_DCTtabnext[(code >> 12) - 4];
250 else
251 if(code >= 1024) tab = &mpeg3_DCTtab0[(code >> 8) - 4];
252 else
253 if(code >= 512) tab = &mpeg3_DCTtab1[(code >> 6) - 8];
254 else
255 if(code >= 256) tab = &mpeg3_DCTtab2[(code >> 4) - 16];
256 else
257 if(code >= 128) tab = &mpeg3_DCTtab3[(code >> 3) - 16];
258 else
259 if(code >= 64) tab = &mpeg3_DCTtab4[(code >> 2) - 16];
260 else
261 if(code >= 32) tab = &mpeg3_DCTtab5[(code >> 1) - 16];
262 else
263 if(code >= 16) tab = &mpeg3_DCTtab6[code - 16];
264 else
266 // invalid Huffman code
267 slice->fault = 1;
268 return 1;
271 mpeg3slice_flushbits(slice_buffer, tab->len);
273 /* end of block */
274 if(tab->run == 64)
275 break;
277 if(tab->run == 65)
279 /* escape */
280 i += mpeg3slice_getbits(slice_buffer, 6);
281 if((val = mpeg3slice_getbits(slice_buffer, 8)) == 0)
282 val = mpeg3slice_getbits(slice_buffer, 8);
283 else
284 if(val == 128)
285 val = mpeg3slice_getbits(slice_buffer, 8) - 256;
286 else
287 if(val > 128)
288 val -= 256;
290 if((sign = (val < 0)) != 0) val = -val;
292 else
294 i += tab->run;
295 val = tab->level;
296 sign = mpeg3slice_getbit(slice_buffer);
299 j = video->mpeg3_zigzag_scan_table[i];
301 #ifdef HAVE_MMX
302 if(video->have_mmx)
304 val = (((val << 1)+1) * slice->quant_scale * video->non_intra_quantizer_matrix[j]);
305 val = (val - 16) | 16;
307 else
308 #endif
310 val = (((val << 1)+1) * slice->quant_scale * video->non_intra_quantizer_matrix[j]) >> 4;
311 val = (val - 1) | 1;
314 bp[j] = sign ? -val : val;
317 if(j != 0)
319 /* not a sparse matrix ! */
320 slice->sparse[comp] = 0;
322 return 0;
326 /* decode one intra coded MPEG-2 block */
327 int mpeg3video_getmpg2intrablock(mpeg3_slice_t *slice,
328 mpeg3video_t *video,
329 int comp,
330 int dc_dct_pred[])
332 int val, i, j, sign, nc;
333 unsigned int code;
334 mpeg3_DCTtab_t *tab;
335 short *bp;
336 int *qmat;
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 */
347 if(comp < 4)
348 val = (dc_dct_pred[0] += mpeg3video_getdclum(slice_buffer));
349 else
350 if((comp & 1) == 0)
351 val = (dc_dct_pred[1] += mpeg3video_getdcchrom(slice_buffer));
352 else
353 val = (dc_dct_pred[2] += mpeg3video_getdcchrom(slice_buffer));
355 if(slice->fault) return 0;
356 #ifdef HAVE_MMX
357 if(video->have_mmx)
358 bp[0] = val << (7 - video->dc_prec);
359 else
360 #endif
361 bp[0] = val << (3 - video->dc_prec);
363 nc = 0;
365 /* decode AC coefficients */
366 for(i = 1; ; i++)
368 code = mpeg3slice_showbits16(slice_buffer);
370 if(code >= 16384 && !video->intravlc)
371 tab = &mpeg3_DCTtabnext[(code >> 12) - 4];
372 else
373 if(code >= 1024)
375 if(video->intravlc)
376 tab = &mpeg3_DCTtab0a[(code >> 8) - 4];
377 else
378 tab = &mpeg3_DCTtab0[(code >> 8) - 4];
380 else
381 if(code >= 512)
383 if(video->intravlc)
384 tab = &mpeg3_DCTtab1a[(code >> 6) - 8];
385 else
386 tab = &mpeg3_DCTtab1[(code >> 6) - 8];
388 else
389 if(code >= 256) tab = &mpeg3_DCTtab2[(code >> 4) - 16];
390 else
391 if(code >= 128) tab = &mpeg3_DCTtab3[(code >> 3) - 16];
392 else
393 if(code >= 64) tab = &mpeg3_DCTtab4[(code >> 2) - 16];
394 else
395 if(code >= 32) tab = &mpeg3_DCTtab5[(code >> 1) - 16];
396 else
397 if(code >= 16) tab = &mpeg3_DCTtab6[code - 16];
398 else
400 /* fprintf(stderr,"mpeg3video_getmpg2intrablock: invalid Huffman code\n"); */
401 slice->fault = 1;
402 return 1;
405 mpeg3slice_flushbits(slice_buffer, tab->len);
407 /* end_of_block */
408 if(tab->run == 64)
409 break;
411 if(tab->run == 65)
413 /* escape */
414 i += mpeg3slice_getbits(slice_buffer, 6);
416 val = mpeg3slice_getbits(slice_buffer, 12);
417 if((val & 2047) == 0)
419 // invalid signed_level (escape)
420 slice->fault = 1;
421 return 0;
423 if((sign = (val >= 2048)) != 0) val = 4096 - val;
425 else
427 i += tab->run;
428 val = tab->level;
429 sign = mpeg3slice_getbit(slice_buffer);
432 j = (video->altscan ? video->mpeg3_alternate_scan_table : video->mpeg3_zigzag_scan_table)[i];
434 #ifdef HAVE_MMX
435 if(video->have_mmx)
436 val = (val * slice->quant_scale * qmat[j]);
437 else
438 #endif
439 val = (val * slice->quant_scale * qmat[j]) >> 4;
441 bp[j] = sign ? -val : val;
442 nc++;
445 if(j != 0)
447 /* not a sparse matrix ! */
448 slice->sparse[comp] = 0;
450 return 1;
454 /* decode one non-intra coded MPEG-2 block */
456 int mpeg3video_getmpg2interblock(mpeg3_slice_t *slice,
457 mpeg3video_t *video,
458 int comp)
460 int val, i, j, sign, nc;
461 unsigned int code;
462 mpeg3_DCTtab_t *tab;
463 short *bp;
464 int *qmat;
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;
474 nc = 0;
476 /* decode AC coefficients */
477 for(i = 0; ; i++)
479 code = mpeg3slice_showbits16(slice_buffer);
480 if(code >= 16384)
482 if(i == 0) tab = &mpeg3_DCTtabfirst[(code >> 12) - 4];
483 else tab = &mpeg3_DCTtabnext[(code >> 12) - 4];
485 else
486 if(code >= 1024) tab = &mpeg3_DCTtab0[(code >> 8) - 4];
487 else
488 if(code >= 512) tab = &mpeg3_DCTtab1[(code >> 6) - 8];
489 else
490 if(code >= 256) tab = &mpeg3_DCTtab2[(code >> 4) - 16];
491 else
492 if(code >= 128) tab = &mpeg3_DCTtab3[(code >> 3) - 16];
493 else
494 if(code >= 64) tab = &mpeg3_DCTtab4[(code >> 2) - 16];
495 else
496 if(code >= 32) tab = &mpeg3_DCTtab5[(code >> 1) - 16];
497 else
498 if(code >= 16) tab = &mpeg3_DCTtab6[code - 16];
499 else
501 // invalid Huffman code
502 slice->fault = 1;
503 return 0;
506 mpeg3slice_flushbits(slice_buffer, tab->len);
508 /* end_of_block */
509 if(tab->run == 64)
510 break;
512 if(tab->run == 65)
514 /* escape */
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"); */
520 slice->fault = 1;
521 return 1;
523 if((sign = (val >= 2048)) != 0) val = 4096 - val;
525 else
527 i += tab->run;
528 val = tab->level;
529 sign = mpeg3slice_getbit(slice_buffer);
532 j = (video->altscan ? video->mpeg3_alternate_scan_table : video->mpeg3_zigzag_scan_table)[i];
534 #ifdef HAVE_MMX
535 if(video->have_mmx)
536 val = (((val << 1)+1) * slice->quant_scale * qmat[j]) >> 1;
537 else
538 #endif
539 val = (((val << 1)+1) * slice->quant_scale * qmat[j]) >> 5;
541 bp[j] = sign ? (-val) : val ;
542 nc++;
545 if(j != 0)
547 slice->sparse[comp] = 0;
549 return 0;
553 /* decode all macroblocks of the current picture */
554 int mpeg3video_get_macroblocks(mpeg3video_t *video, int framenum)
556 unsigned int code;
557 mpeg3_slice_buffer_t *slice_buffer; /* Buffer being loaded */
558 int i;
559 int current_buffer;
560 mpeg3_bits_t *vstream = video->vstream;
562 /* Load every slice into a buffer array */
563 video->total_slice_buffers = 0;
564 current_buffer = 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 */
582 /* Expand buffer */
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);
602 current_buffer++;
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);
617 else
618 if(i == 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;
624 else
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));
649 return 0;
652 int mpeg3video_allocate_decoders(mpeg3video_t *video, int decoder_count)
654 int i;
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;
672 return 0;
675 /* decode one frame or field picture */
677 int mpeg3video_getpicture(mpeg3video_t *video, int framenum)
679 int i, result = 0;
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;
688 if(!video->mpeg2)
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];
701 else
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 :
719 video->chrom_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;
744 else
746 video->output_src = video->oldrefframe;
749 else
751 mpeg3video_display_second_field(video);
755 if(video->mpeg2)
757 video->current_repeat += 100;
760 if(video->pict_struct != FRAME_PICTURE)
761 video->secondfield = !video->secondfield;
762 return result;