1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
|
/*************************************************************
Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved.
PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research
Group. If you use this software, you agree to the following: This
program package is purely experimental, and is licensed "as is".
Permission is granted to use, modify, and distribute this program
without charge for any purpose, provided this license/ disclaimer
notice appears in the copies. No warranty or maintenance is given,
either expressed or implied. In no event shall the author(s) be
liable to you or a third party for any special, incidental,
consequential, or other damages, arising out of the use or inability
to use the program for any purpose (or the loss of data), even if we
have been advised of such possibilities. Any public reference or
advertisement of this source code should refer to it as the Portable
Video Research Group (PVRG) code, and not by any author(s) (or
Stanford University) name.
*************************************************************/
/*
************************************************************
marker.c
This file contains the Marker library which uses the direct buffer
access routines bgetc...
************************************************************
*/
/*LABEL marker.c */
/*Include files */
#include "globals.h"
#include "stream.h"
#include "marker.h"
#ifdef SYSV
#include <sys/fcntl.h>
#endif
#include <stdlib.h> /* exit */
/*PUBLIC*/
extern void WriteSoi();
extern void WriteEoi();
extern void WriteJfif();
extern void WriteSof();
extern void WriteDri();
extern void WriteDqt();
extern void WriteSos();
extern void WriteDht();
extern void ReadSof();
extern void ReadDqt();
extern void ReadDht();
extern void ReadDri();
extern void ReadDnl();
extern int CheckMarker();
extern void CheckScan();
extern void ReadSos();
extern void MakeConsistentFrameSize();
/*PRIVATE*/
/* External marker definition */
extern FRAME *CFrame;
extern IMAGE *CImage;
extern SCAN *CScan;
extern int NumberMDU;
extern int Loud;
extern int izigzag_index[];
#define Zigzag(i) izigzag_index[i]
/*START*/
/*BFUNC
WriteSoi() puts an SOI marker onto the stream.
EFUNC*/
void WriteSoi()
{
BEGIN("WriteSoi")
swbytealign();
bputc(MARKER_MARKER);
bputc(MARKER_SOI);
}
/*BFUNC
WriteEoi() puts an EOI marker onto the stream.
EFUNC*/
void WriteEoi()
{
BEGIN("WriteEoi")
swbytealign();
bputc(MARKER_MARKER);
bputc(MARKER_EOI);
}
/*BFUNC
WriteJfif() puts an JFIF APP0 marker onto the stream. This is a
generic 1x1 aspect ratio, no thumbnail specification.
EFUNC*/
void WriteJfif()
{
BEGIN("WriteJfif")
int Start,End;
swbytealign();
bputc(MARKER_MARKER);
bputc(MARKER_APP);
Start = swtell(); /* Find out the start of position. */
bputw(0); /* Put a 0 down onto the stream. */
bputc(0x4a); bputc(0x46); bputc(0x49); bputc(0x46); bputc(0x00);
bputc(0x01); bputc(0x02); /*Version 1.02*/
bputc(0x00); /* No absolute DPI */
bputw(1); /* Aspect ratio */
bputw(1);
bputc(0x00); /* No thumbnails */
bputc(0x00);
End = swtell(); /* Find out end of the marker. */
swseek(Start); /* Rewind */
bputw((End-Start) >> 3); /* Put marker there. */
if ((Start-End) & 0x7) /* if not byte flush, then problems. */
{
WHEREAMI();
printf("Bad frame marker, not byte flush.\n");
}
swseek(End);
}
/*BFUNC
WriteSof() puts an SOF marker onto the stream.
EFUNC*/
void WriteSof()
{
BEGIN("WriteSof")
int i,j,Start,End;
swbytealign();
bputc(MARKER_MARKER);
bputc(MARKER_SOF|(CFrame->Type&0xf));
Start = swtell(); /* Find out the start of position. */
bputw(0); /* Put a 0 down onto the stream. */
bputc(CFrame->DataPrecision);
if (!CFrame->InsertDnl) {bputw(CFrame->GlobalHeight);}
else {bputw(0);}
bputw(CFrame->GlobalWidth);
bputc(CFrame->GlobalNumberComponents);
for(i=0;i<CFrame->GlobalNumberComponents;i++)
{
bputc(j=CFrame->cn[i]); /* Store off in index */
bputn(CFrame->hf[j],CFrame->vf[j]);
bputc(CFrame->tq[j]);
}
End = swtell(); /* Find out end of the marker. */
swseek(Start); /* Rewind */
bputw((End-Start) >> 3); /* Put marker there. */
if ((Start-End) & 0x7) /* if not byte flush, then problems. */
{
WHEREAMI();
printf("Bad frame marker, not byte flush.\n");
}
swseek(End);
}
/*BFUNC
WriteDri() writes out a resync (or restart) interval out to the
stream. If unspecified, resync is not enabled.
EFUNC*/
void WriteDri()
{
BEGIN("WriteDri")
swbytealign();
bputc(MARKER_MARKER);
bputc(MARKER_DRI);
bputw(4); /* Constant length of 4 */
bputw(CFrame->ResyncInterval);
}
/*BFUNC
WriteDnl() writes out a number of line marker out to the stream. Note
that we must have defined number of lines before as 0.
EFUNC*/
void WriteDnl()
{
BEGIN("WriteDnl")
swbytealign();
bputc(MARKER_MARKER);
bputc(MARKER_DNL);
bputw(4); /* Constant length of 4 */
bputw(CFrame->GlobalHeight);
}
/*BFUNC
WriteDqt() writes out the quantization matrices in the CImage
structure.
EFUNC*/
void WriteDqt()
{
BEGIN("WriteDqt")
int i,j,bignum_p,Start,End,*qmatrix;
if (!(CScan->NumberQTablesSend))
return; /* No tables to transmit, then ignore. */
swbytealign();
bputc(MARKER_MARKER);
bputc(MARKER_DQT);
Start = swtell();
bputw(0);
for(i=0;i<CScan->NumberQTablesSend;i++)
{
qmatrix = CImage->QuantizationMatrices[CScan->sq[i]];
for(bignum_p=0,j=63;j>=0;j--)
{
if(qmatrix[j]>255)
{
bignum_p=0x10;
break;
}
}
bputc((bignum_p|CScan->sq[i])); /* Precision defined for big numbers */
if (bignum_p)
{
for(j=0;j<64;j++)
bputw(qmatrix[Zigzag(j)]);
}
else
{
for(j=0;j<64;j++)
bputc(qmatrix[Zigzag(j)]);
}
}
CScan->NumberQTablesSend=0; /* Clear out queue */
End = swtell(); /* Assume a marker code will follow.*/
swseek(Start); /* bputc(END_QUANTIZATION_TABLE);*/
bputw((End-Start) >> 3);
if ((Start-End) & 0x7)
{
WHEREAMI();
printf("DQT marker not byte flush.\n");
}
swseek(End);
}
/*BFUNC
WriteSos() writes a start of scan marker.
EFUNC*/
void WriteSos()
{
BEGIN("WriteSos")
int i,Start,End;
swbytealign();
bputc(MARKER_MARKER);
bputc(MARKER_SOS);
Start = swtell();
bputw(0);
bputc(CScan->NumberComponents);
for(i=0;i<CScan->NumberComponents;i++)
{
bputc(CScan->ci[i]);
bputn(CScan->td[i],CScan->ta[i]);
}
bputc(CScan->SSS);
bputc(CScan->SSE);
bputn(CScan->SAH,CScan->SAL);
End = swtell();
swseek(Start);
bputw((End-Start) >> 3);
if ((Start-End) & 0x7)
{
WHEREAMI();
printf("Bad scan marker not byte flush.\n");
}
swseek(End);
}
/*BFUNC
WriteDht() writes out the Huffman tables to send.
EFUNC*/
void WriteDht()
{
BEGIN("WriteDht")
int i,Start,End;
if (!(CScan->NumberDCTablesSend) && !(CScan->NumberACTablesSend))
return; /* No tables to transmit, then ignore. */
swbytealign();
bputc(MARKER_MARKER);
bputc(MARKER_DHT);
Start = swtell();
bputw(0);
for(i=0;i<CScan->NumberDCTablesSend;i++)
{
bputc(CScan->sd[i]);
UseDCHuffman(CScan->sd[i]);
WriteHuffman();
}
for(i=0;i<CScan->NumberACTablesSend;i++)
{
bputc(CScan->sa[i]|0x10);
UseACHuffman(CScan->sa[i]);
WriteHuffman();
}
CScan->NumberDCTablesSend=0; /* Clear out send queue */
CScan->NumberACTablesSend=0;
/*
We end on a new marker... so an end of code table is unnecessary.
bputc(END_CODE_TABLE);
*/
End = swtell();
swseek(Start);
bputw((End-Start) >> 3);
if ((Start-End) & 0x7)
{
WHEREAMI();
printf("Bad scan marker not byte flush.\n");
}
swseek(End);
}
/*BFUNC
ReadSof() reads a start of frame marker from the stream. We assume that
the first two bytes (marker prefix) have already been stripped.
EFUNC*/
void ReadSof(Type)
int Type;
{
BEGIN("ReadSof")
int i,j,Length,Start,End,rb;
Start = srtell();
Length = bgetw();
if (Loud > MUTE)
printf("Frame Length %d\n",Length);
CFrame->Type=Type;
CFrame->DataPrecision = bgetc();
CFrame->GlobalHeight = bgetw();
CFrame->GlobalWidth = bgetw();
for(i=0;i<MAXIMUM_COMPONENTS;i++)
CFrame->hf[i]=CFrame->vf[i]=CFrame->tq[i]=0;
CFrame->GlobalNumberComponents = bgetc();
for(i=0;i<CFrame->GlobalNumberComponents;i++)
{
j = bgetc();
rb = bgetc();
CFrame->cn[i] = j;
CFrame->hf[j] = hinyb(rb);
CFrame->vf[j] = lonyb(rb);
CFrame->tq[j] = bgetc();
}
MakeConsistentFrameSize();
End = srtell();
if ((End-Start) != (Length<<3))
{
WHEREAMI();
printf("Bad read frame length.\n");
}
if (Loud > MUTE)
{
PrintImage();
PrintFrame();
}
}
/*BFUNC
ReadDqt() reads a quantization table marker from the stream.
The first two bytes have been stripped off.
EFUNC*/
void ReadDqt()
{
BEGIN("ReadDqt")
int i,Length,Qget,Index,Precision,Start,End;
Start = srtell();
Length = bgetw();
if (Loud > MUTE)
printf("Quantization Length %d\n",Length);
while((Qget=bgetc()) != END_QUANTIZATION_TABLE)
{
Index = Qget & 0xf;
Precision = (Qget >> 4)&0xf;
if (Precision > 1)
{
printf("Bad Precision: %d in Quantization Download\n",
Precision);
printf("*** Dumping Image ***\n");
PrintImage();
printf("*** Dumping Frame ***\n");
PrintFrame();
exit(ERROR_MARKER);
} /* Load in q-matrices */
CImage->QuantizationMatrices[Index] = (int *) calloc(65,sizeof(int));
if (Precision) /* If precision then word quantization*/
{
for(i=0;i<64;i++)
{
if (!(CImage->QuantizationMatrices[Index][Zigzag(i)]=bgetw()))
{
printf("marker.c:ReadDqt: Quantization value of zero.\n");
if (i)
{
printf("marker.c:ReadDqt: Changing to i-1.\n");
CImage->QuantizationMatrices[Index][Zigzag(i)]=
CImage->QuantizationMatrices[Index][Zigzag(i-1)];
}
else
{
printf("marker.c:ReadDqt: Changing to 16.\n");
CImage->QuantizationMatrices[Index][Zigzag(i)]=16;
}
}
}
}
else /* Otherwise byte quantization */
{
for(i=0;i<64;i++)
{
if (!(CImage->QuantizationMatrices[Index][Zigzag(i)]=bgetc()))
{
printf("marker.c:ReadDqt: Quantization value of zero.\n");
if (i)
{
printf("marker.c:ReadDqt: Changing to i-1.\n");
CImage->QuantizationMatrices[Index][Zigzag(i)]=
CImage->QuantizationMatrices[Index][Zigzag(i-1)];
}
else
{
printf("marker.c:ReadDqt: Changing to 16.\n");
CImage->QuantizationMatrices[Index][Zigzag(i)]=16;
}
}
}
}
}
bpushc(END_QUANTIZATION_TABLE);
End = srtell();
if ((End-Start) != (Length<<3))
{
WHEREAMI();
printf("Bad DQT read length.\n");
}
if (Loud > MUTE)
{
PrintImage();
PrintFrame();
}
}
/*BFUNC
ReadDht() reads a Huffman marker from the stream. We assume that the
first two bytes have been stripped off.
EFUNC*/
void ReadDht()
{
BEGIN("ReadDht")
int Index,Where,Length,Start,End;
Start = srtell();
Length = bgetw();
if (Loud > MUTE)
printf("Define Huffman length %d\n",Length);
while((Index = bgetc()) != END_CODE_TABLE)
{
Where = (Index >> 4) & 0x0f; /* Find location to place it in */
Index = Index & 0x0f;
MakeXhuff(); /* Make Huffman table */
MakeDhuff();
ReadHuffman();
if (Where)
{
SetACHuffman(Index); /* Set current Huffman limit */
CImage->NumberACTables = MAX(CImage->NumberACTables,(Index+1));
}
else
{
SetDCHuffman(Index);
CImage->NumberDCTables = MAX(CImage->NumberDCTables,(Index+1));
}
}
bpushc(END_CODE_TABLE);
End = srtell();
if ((End-Start) != (Length<<3))
{
WHEREAMI();
printf("Bad DHT length.\n");
}
if (Loud > MUTE)
PrintImage();
}
/*BFUNC
ReadDri() reads a resync interval marker from the stream. We assume
the first two bytes are stripped off.
EFUNC*/
void ReadDri()
{
BEGIN("ReadDri")
int Length;
if ((Length=bgetw())!=4) /* Constant length of 4 */
{
WHEREAMI();
printf("Bad length %d, should be 4.\n",Length);
}
CFrame->ResyncInterval = bgetw();
}
/*BFUNC
ReadDnl() reads a number of lines marker from the stream. The first
two bytes should be stripped off.
EFUNC*/
void ReadDnl()
{
BEGIN("ReadDnl")
int Length;
if ((Length=bgetw())!=4) /* Constant length of 4 */
printf("marker.c:ReadDnl: Bad length %d, should be 4.\n",Length);
CFrame->GlobalHeight = bgetw();
if (CScan->NumberComponents)
{
MakeConsistentFrameSize();
CheckScan();
ResizeIob();
if (CFrame->GlobalHeight)
{
InstallIob(0);
if (CFrame->Type==3)
NumberMDU = CScan->MDUWide*CScan->MDUHigh;
else
NumberMDU = CScan->MDUWide*CScan->MDUHigh;
}
else
NumberMDU = -1;
}
}
/*BFUNC
CheckMarker() checks to see if there is a marker in the stream ahead.
This function presumes that ungetc is not allowed to push more than
one byte back.
EFUNC*/
int CheckMarker()
{
BEGIN("CheckMarker")
int Length;
int v1;
Length = brtell();
v1=bgetw();
if (v1>=0xffc0)
{
brseek(Length,0L);
return(v1&0xff);
}
brseek(Length,0L);
return(0);
}
/*BFUNC
ReadSos() reads in a start of scan from the stream. The first two
bytes should have been stripped off.
EFUNC*/
void ReadSos()
{
BEGIN("ReadSos")
int i,Length,Start,End,rb;
Start = srtell();
Length = bgetw();
if (Loud > MUTE)
{
WHEREAMI();
printf("Scan length %d\n",Length);
}
CScan->NumberComponents = bgetc();
for(i=0;i<CScan->NumberComponents;i++)
{
CScan->ci[i] = bgetc();
rb = bgetc();
CScan->td[i] = hinyb(rb);
CScan->ta[i] = lonyb(rb);
}
CScan->SSS = bgetc();
CScan->SSE = bgetc();
rb = bgetc();
CScan->SAH = hinyb(rb);
CScan->SAL = lonyb(rb);
End = srtell();
if ((End-Start) != (Length<<3))
{
WHEREAMI();
printf("Bad scan length.\n");
}
if (Loud > MUTE)
PrintScan();
MakeConsistentFileNames(); /* A Scan marker always makes new files */
CheckValidity();
CheckBaseline();
CheckScan();
/* Create the io buffer structure */
if (CFrame->Type==3)
{
MakeIob(IOB_LINE,O_RDWR | O_CREAT,
((CFrame->DataPrecision>8)?2:1));
if (CFrame->GlobalHeight)
{
InstallIob(0);
NumberMDU = CScan->MDUWide*CScan->MDUHigh;
}
else NumberMDU = -1;
}
else
{
MakeIob(IOB_BLOCK,O_RDWR | O_CREAT | O_TRUNC,
((CFrame->DataPrecision>8)?2:1));
if (CFrame->GlobalHeight)
{
InstallIob(0);
NumberMDU = CScan->MDUWide*CScan->MDUHigh;
}
else NumberMDU = -1;
}
/* Sometimes rewinding is necessary */
/* for(i=0;i<CScan->NumberComponents;i++)
{
InstallIob(i);
RewindIob();
} */
ResetCodec(); /* Reset codec for information */
}
/*BFUNC
CheckScan() sets the MDU dimensions for the CScan structure.
EFUNC*/
void CheckScan()
{
int i;
if (CScan->NumberComponents==1)
{
i = (((CFrame->GlobalWidth*CFrame->hf[CScan->ci[0]])-1)/CFrame->Maxh)+1;
if (CFrame->Type!=3)
i = ((i-1)/8)+1;
CScan->MDUWide = i;
i = (((CFrame->GlobalHeight*CFrame->vf[CScan->ci[0]])-1)/CFrame->Maxv)+1;
if (CFrame->Type!=3)
i = ((i-1)/8)+1;
CScan->MDUHigh = i;
}
else
{
CScan->MDUWide=CFrame->MDUWide;
CScan->MDUHigh=CFrame->MDUHigh;
}
}
/*BFUNC
MakeConsistentFrameSize() makes a consistent frame size for all of the
horizontal and vertical frequencies read.
EFUNC*/
void MakeConsistentFrameSize()
{
BEGIN("MakeConsistentFrameSize")
int i,Maxh,Maxv;
int TestWide, TestHigh;
Maxv = Maxh = 1;
for(i=0;i<MAXIMUM_COMPONENTS;i++)
{
if (CFrame->vf[i] > Maxv)
Maxv = CFrame->vf[i];
if (CFrame->hf[i] > Maxh)
Maxh = CFrame->hf[i];
}
for(i=0;i<MAXIMUM_COMPONENTS;i++) /* Define estimated actual width */
{ /* ignoring replications */
if (CFrame->hf[i])
{
if (!CFrame->Width[i])
CFrame->Width[i] =
(((CFrame->GlobalWidth*CFrame->hf[i])-1)/Maxh)+1;
if (!CFrame->Height[i])
CFrame->Height[i] =
(((CFrame->GlobalHeight*CFrame->vf[i])-1)/Maxv)+1;
}
}
CFrame->Maxv = Maxv; CFrame->Maxh = Maxh;
CFrame->MDUWide = (CFrame->GlobalWidth-1)/Maxh +1;
if (CFrame->GlobalHeight)
CFrame->MDUHigh = (CFrame->GlobalHeight-1)/Maxv +1;
else
CFrame->MDUHigh = 0;
if (CFrame->Type!=3)
{
CFrame->MDUWide= (CFrame->MDUWide-1)/8 +1;
if (CFrame->MDUHigh)
CFrame->MDUHigh= (CFrame->MDUHigh-1)/8 +1;
}
for(i=0;i<MAXIMUM_COMPONENTS;i++)
{
if (CFrame->hf[i])
{
TestWide = ((CFrame->Width[i]-1)/(CFrame->hf[i]))+1;
if (CFrame->Type!=3) TestWide= (TestWide-1)/8 +1;
if (CFrame->MDUWide!=TestWide)
{
WHEREAMI();
printf("Inconsistent frame width.\n");
printf("Component[%dx%d]\n",
CFrame->Width[i],CFrame->Height[i]);
printf("MDU Wide: Image, Component %d!= %d.\n",
CFrame->MDUWide,TestWide);
}
if (CFrame->MDUHigh)
{
TestHigh = ((CFrame->Height[i]-1)/(CFrame->vf[i]))+1;
if (CFrame->Type!=3) TestHigh= (TestHigh-1)/8 +1;
if (CFrame->MDUHigh!=TestHigh)
{
WHEREAMI();
printf("Inconsistent frame height.\n");
printf("Component[%dx%d]\n",
CFrame->Width[i],CFrame->Height[i]);
printf("MDU High: Image, Component %d!= %d.\n",
CFrame->MDUHigh,TestHigh);
}
}
}
}
}
/*END*/
|