Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / libmpeg3 / video / getpicture.c
blobdf62a7cd855b6bb397d7482180a8810248c9bbde
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 bzero(slice->block[comp], sizeof(short) * 64 * size);
48 // memset(slice->block[comp], 0, sizeof(short) * 64 * size);
49 return 0;
52 static inline int mpeg3video_getdclum(mpeg3_slice_buffer_t *slice_buffer)
54 int code, size, val;
55 /* decode length */
56 code = mpeg3slice_showbits5(slice_buffer);
58 if(code < 31)
60 size = mpeg3_DClumtab0[code].val;
61 mpeg3slice_flushbits(slice_buffer, mpeg3_DClumtab0[code].len);
63 else
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;
71 else
73 val = mpeg3slice_getbits(slice_buffer, size);
74 if((val & (1 << (size - 1))) == 0) val -= (1 << size) - 1;
77 return val;
81 int mpeg3video_getdcchrom(mpeg3_slice_buffer_t *slice_buffer)
83 int code, size, val;
85 /* decode length */
86 code = mpeg3slice_showbits5(slice_buffer);
88 if(code < 31)
90 size = mpeg3_DCchromtab0[code].val;
91 mpeg3slice_flushbits(slice_buffer, mpeg3_DCchromtab0[code].len);
93 else
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;
101 else
103 val = mpeg3slice_getbits(slice_buffer, size);
104 if((val & (1 << (size - 1))) == 0) val -= (1 << size) - 1;
107 return val;
111 /* decode one intra coded MPEG-1 block */
113 int mpeg3video_getintrablock(mpeg3_slice_t *slice,
114 mpeg3video_t *video,
115 int comp,
116 int dc_dct_pred[])
118 int val, i, j = 8, sign;
119 unsigned int code;
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 */
125 if(comp < 4)
126 bp[0] = (dc_dct_pred[0] += mpeg3video_getdclum(slice_buffer)) << 3;
127 else
128 if(comp == 4)
129 bp[0] = (dc_dct_pred[1] += mpeg3video_getdcchrom(slice_buffer)) << 3;
130 else
131 bp[0] = (dc_dct_pred[2] += mpeg3video_getdcchrom(slice_buffer)) << 3;
133 if(slice->fault) return 1;
135 /* decode AC coefficients */
136 for(i = 1; ; i++)
138 code = mpeg3slice_showbits16(slice_buffer);
139 if(code >= 16384)
140 tab = &mpeg3_DCTtabnext[(code >> 12) - 4];
141 else
142 if(code >= 1024) tab = &mpeg3_DCTtab0[(code >> 8) - 4];
143 else
144 if(code >= 512) tab = &mpeg3_DCTtab1[(code >> 6) - 8];
145 else
146 if(code >= 256) tab = &mpeg3_DCTtab2[(code >> 4) - 16];
147 else
148 if(code >= 128) tab = &mpeg3_DCTtab3[(code >> 3) - 16];
149 else
150 if(code >= 64) tab = &mpeg3_DCTtab4[(code >> 2) - 16];
151 else
152 if(code >= 32) tab = &mpeg3_DCTtab5[(code >> 1) - 16];
153 else
154 if(code >= 16) tab = &mpeg3_DCTtab6[code - 16];
155 else
157 /* fprintf(stderr, "mpeg3video_getintrablock: invalid Huffman code\n"); */
158 slice->fault = 1;
159 return 0;
162 mpeg3slice_flushbits(slice_buffer, tab->len);
164 if(tab->run == 64) break; /* end_of_block */
166 if(tab->run == 65)
168 /* escape */
169 i += mpeg3slice_getbits(slice_buffer, 6);
171 if((val = mpeg3slice_getbits(slice_buffer, 8)) == 0)
172 val = mpeg3slice_getbits(slice_buffer, 8);
173 else
174 if(val == 128)
175 val = mpeg3slice_getbits(slice_buffer, 8) - 256;
176 else
177 if(val > 128)
178 val -= 256;
180 if((sign = (val < 0)) != 0) val= -val;
182 else
184 i += tab->run;
185 val = tab->level;
186 sign = mpeg3slice_getbit(slice_buffer);
189 if(i < 64)
190 j = video->mpeg3_zigzag_scan_table[i];
191 else
193 slice->fault = 1;
194 return 0;
198 val = (val * slice->quant_scale * video->intra_quantizer_matrix[j]) >> 3;
199 val = (val - 1) | 1;
201 bp[j] = sign ? -val : val;
204 if(j != 0)
206 /* not a sparse matrix ! */
207 slice->sparse[comp] = 0;
209 return 0;
213 /* decode one non-intra coded MPEG-1 block */
215 int mpeg3video_getinterblock(mpeg3_slice_t *slice,
216 mpeg3video_t *video,
217 int comp)
219 int val, i, j, sign;
220 unsigned int code;
221 mpeg3_DCTtab_t *tab;
222 short *bp = slice->block[comp];
223 mpeg3_slice_buffer_t *slice_buffer = slice->slice_buffer;
225 /* decode AC coefficients */
226 for(i = 0; ; i++)
228 code = mpeg3slice_showbits16(slice_buffer);
229 if(code >= 16384)
231 if(i == 0)
232 tab = &mpeg3_DCTtabfirst[(code >> 12) - 4];
233 else
234 tab = &mpeg3_DCTtabnext[(code >> 12) - 4];
236 else
237 if(code >= 1024) tab = &mpeg3_DCTtab0[(code >> 8) - 4];
238 else
239 if(code >= 512) tab = &mpeg3_DCTtab1[(code >> 6) - 8];
240 else
241 if(code >= 256) tab = &mpeg3_DCTtab2[(code >> 4) - 16];
242 else
243 if(code >= 128) tab = &mpeg3_DCTtab3[(code >> 3) - 16];
244 else
245 if(code >= 64) tab = &mpeg3_DCTtab4[(code >> 2) - 16];
246 else
247 if(code >= 32) tab = &mpeg3_DCTtab5[(code >> 1) - 16];
248 else
249 if(code >= 16) tab = &mpeg3_DCTtab6[code - 16];
250 else
252 // invalid Huffman code
253 slice->fault = 1;
254 return 1;
257 mpeg3slice_flushbits(slice_buffer, tab->len);
259 /* end of block */
260 if(tab->run == 64)
261 break;
263 if(tab->run == 65)
265 /* escape */
266 i += mpeg3slice_getbits(slice_buffer, 6);
267 if((val = mpeg3slice_getbits(slice_buffer, 8)) == 0)
268 val = mpeg3slice_getbits(slice_buffer, 8);
269 else
270 if(val == 128)
271 val = mpeg3slice_getbits(slice_buffer, 8) - 256;
272 else
273 if(val > 128)
274 val -= 256;
276 if((sign = (val < 0)) != 0) val = -val;
278 else
280 i += tab->run;
281 val = tab->level;
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;
288 val = (val - 1) | 1;
290 bp[j] = sign ? -val : val;
293 if(j != 0)
295 /* not a sparse matrix ! */
296 slice->sparse[comp] = 0;
298 return 0;
302 /* decode one intra coded MPEG-2 block */
303 int mpeg3video_getmpg2intrablock(mpeg3_slice_t *slice,
304 mpeg3video_t *video,
305 int comp,
306 int dc_dct_pred[])
308 int val, i, j, sign, nc;
309 unsigned int code;
310 mpeg3_DCTtab_t *tab;
311 short *bp;
312 int *qmat;
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 */
323 if(comp < 4)
324 val = (dc_dct_pred[0] += mpeg3video_getdclum(slice_buffer));
325 else
326 if((comp & 1) == 0)
327 val = (dc_dct_pred[1] += mpeg3video_getdcchrom(slice_buffer));
328 else
329 val = (dc_dct_pred[2] += mpeg3video_getdcchrom(slice_buffer));
331 if(slice->fault) return 0;
332 bp[0] = val << (3 - video->dc_prec);
334 nc = 0;
336 /* decode AC coefficients */
337 for(i = 1; ; i++)
339 code = mpeg3slice_showbits16(slice_buffer);
341 if(code >= 16384 && !video->intravlc)
342 tab = &mpeg3_DCTtabnext[(code >> 12) - 4];
343 else
344 if(code >= 1024)
346 if(video->intravlc)
347 tab = &mpeg3_DCTtab0a[(code >> 8) - 4];
348 else
349 tab = &mpeg3_DCTtab0[(code >> 8) - 4];
351 else
352 if(code >= 512)
354 if(video->intravlc)
355 tab = &mpeg3_DCTtab1a[(code >> 6) - 8];
356 else
357 tab = &mpeg3_DCTtab1[(code >> 6) - 8];
359 else
360 if(code >= 256) tab = &mpeg3_DCTtab2[(code >> 4) - 16];
361 else
362 if(code >= 128) tab = &mpeg3_DCTtab3[(code >> 3) - 16];
363 else
364 if(code >= 64) tab = &mpeg3_DCTtab4[(code >> 2) - 16];
365 else
366 if(code >= 32) tab = &mpeg3_DCTtab5[(code >> 1) - 16];
367 else
368 if(code >= 16) tab = &mpeg3_DCTtab6[code - 16];
369 else
371 /* fprintf(stderr,"mpeg3video_getmpg2intrablock: invalid Huffman code\n"); */
372 slice->fault = 1;
373 return 1;
376 mpeg3slice_flushbits(slice_buffer, tab->len);
378 /* end_of_block */
379 if(tab->run == 64)
380 break;
382 if(tab->run == 65)
384 /* escape */
385 i += mpeg3slice_getbits(slice_buffer, 6);
387 val = mpeg3slice_getbits(slice_buffer, 12);
388 if((val & 2047) == 0)
390 // invalid signed_level (escape)
391 slice->fault = 1;
392 return 0;
394 if((sign = (val >= 2048)) != 0) val = 4096 - val;
396 else
398 i += tab->run;
399 val = tab->level;
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;
408 nc++;
411 if(j != 0)
413 /* not a sparse matrix ! */
414 slice->sparse[comp] = 0;
416 return 1;
420 /* decode one non-intra coded MPEG-2 block */
422 int mpeg3video_getmpg2interblock(mpeg3_slice_t *slice,
423 mpeg3video_t *video,
424 int comp)
426 int val, i, j, sign, nc;
427 unsigned int code;
428 mpeg3_DCTtab_t *tab;
429 short *bp;
430 int *qmat;
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;
440 nc = 0;
442 /* decode AC coefficients */
443 for(i = 0; ; i++)
445 code = mpeg3slice_showbits16(slice_buffer);
446 if(code >= 16384)
448 if(i == 0) tab = &mpeg3_DCTtabfirst[(code >> 12) - 4];
449 else tab = &mpeg3_DCTtabnext[(code >> 12) - 4];
451 else
452 if(code >= 1024) tab = &mpeg3_DCTtab0[(code >> 8) - 4];
453 else
454 if(code >= 512) tab = &mpeg3_DCTtab1[(code >> 6) - 8];
455 else
456 if(code >= 256) tab = &mpeg3_DCTtab2[(code >> 4) - 16];
457 else
458 if(code >= 128) tab = &mpeg3_DCTtab3[(code >> 3) - 16];
459 else
460 if(code >= 64) tab = &mpeg3_DCTtab4[(code >> 2) - 16];
461 else
462 if(code >= 32) tab = &mpeg3_DCTtab5[(code >> 1) - 16];
463 else
464 if(code >= 16) tab = &mpeg3_DCTtab6[code - 16];
465 else
467 // invalid Huffman code
468 slice->fault = 1;
469 return 0;
472 mpeg3slice_flushbits(slice_buffer, tab->len);
474 /* end_of_block */
475 if(tab->run == 64)
476 break;
478 if(tab->run == 65)
480 /* escape */
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"); */
486 slice->fault = 1;
487 return 1;
489 if((sign = (val >= 2048)) != 0) val = 4096 - val;
491 else
493 i += tab->run;
494 val = tab->level;
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 ;
503 nc++;
506 if(j != 0)
508 slice->sparse[comp] = 0;
510 return 0;
514 /* decode all macroblocks of the current picture */
515 int mpeg3video_get_macroblocks(mpeg3video_t *video, int framenum)
517 unsigned int code;
518 mpeg3_slice_buffer_t *slice_buffer; /* Buffer being loaded */
519 int i;
520 int current_buffer;
521 mpeg3_bits_t *vstream = video->vstream;
523 /* Load every slice into a buffer array */
524 video->total_slice_buffers = 0;
525 current_buffer = 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 */
544 /* Expand buffer */
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));
564 current_buffer++;
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);
581 else
582 if(i == 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;
588 else
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));
613 return 0;
616 int mpeg3video_allocate_decoders(mpeg3video_t *video, int decoder_count)
618 int i;
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;
636 return 0;
639 /* decode one frame or field picture */
641 int mpeg3video_getpicture(mpeg3video_t *video, int framenum)
643 int i, result = 0;
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;
652 if(!video->mpeg2)
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];
665 else
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 :
683 video->chrom_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];
712 else
714 video->output_src[0] = video->oldrefframe[0];
715 video->output_src[1] = video->oldrefframe[1];
716 video->output_src[2] = video->oldrefframe[2];
719 else
721 mpeg3video_display_second_field(video);
725 if(video->mpeg2)
727 video->current_repeat += 100;
730 if(video->pict_struct != FRAME_PICTURE)
731 video->secondfield = !video->secondfield;
732 return result;