1 /* store.c, picture output routines */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
6 * Disclaimer of Warranty
8 * These software programs are available to the user without any license fee or
9 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
10 * any and all warranties, whether express, implied, or statuary, including any
11 * implied warranties or merchantability or of fitness for a particular
12 * purpose. In no event shall the copyright-holder be liable for any
13 * incidental, punitive, or consequential damages of any kind whatsoever
14 * arising from the use of these programs.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
19 * The MPEG Software Simulation Group does not represent or warrant that the
20 * programs furnished hereunder are free of infringement of any third-party
23 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24 * are subject to royalty fees to patent holders. Many of these patents are
25 * general enough such that they are unavoidable regardless of implementation
30 #include <stdlib.h> /* for malloc */
31 #include <string.h> /* for strcat */
36 /* private prototypes */
37 static void store_one _ANSI_ARGS_((char *outname, unsigned char *src[],
38 int offset, int incr, int height));
39 static void store_yuv _ANSI_ARGS_((char *outname, unsigned char *src[],
40 int offset, int incr, int height));
41 static void store_sif _ANSI_ARGS_((char *outname, unsigned char *src[],
42 int offset, int incr, int height));
43 static void store_ppm_tga _ANSI_ARGS_((char *outname, unsigned char *src[],
44 int offset, int incr, int height, int tgaflag));
45 static void store_in_mem _ANSI_ARGS_((char *outmem, unsigned char *src[],
46 int offset, int incr, int height));
47 static void store_yuv1 _ANSI_ARGS_((char *name, unsigned char *src,
48 int offset, int incr, int width, int height));
49 static void putbyte _ANSI_ARGS_((int c));
50 static void putword _ANSI_ARGS_((int w));
51 static void mem_putbyte _ANSI_ARGS_((int c));
52 /*static void mem_putword _ANSI_ARGS_((int w));*/
53 static void conv422to444 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
54 static void conv420to422 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
57 static unsigned char obfr[OBFRSIZE];
58 static unsigned char *optr;
59 static ostream *outfile;
60 unsigned char *static_malloc[6] = {0,0,0,0,0,0}; /*worse case there is 6 buffer in this impl unit.*/
62 void FreeStaticBuffer()
66 free(static_malloc[i]);
70 * store a picture as either one frame or two fields
72 void Write_Frame(src,frame)
76 char outname[FILENAME_LENGTH];
78 if (progressive_sequence || progressive_frame || Frame_Store_Flag)
81 my_sprintf(outname,Output_Picture_Filename,frame,'f');
82 store_one(outname,src,0,Coded_Picture_Width,vertical_size);
87 my_sprintf(outname,Output_Picture_Filename,frame,'a');
88 store_one(outname,src,0,Coded_Picture_Width<<1,vertical_size>>1);
90 my_sprintf(outname,Output_Picture_Filename,frame,'b');
91 store_one(outname,src,
92 Coded_Picture_Width,Coded_Picture_Width<<1,vertical_size>>1);
97 * store one frame or one field
99 static void store_one(outname,src,offset,incr,height)
101 unsigned char *src[];
102 int offset, incr, height;
107 store_yuv(outname,src,offset,incr,height);
110 store_sif(outname,src,offset,incr,height);
113 store_ppm_tga(outname,src,offset,incr,height,1);
116 store_ppm_tga(outname,src,offset,incr,height,0);
119 store_in_mem(OUTMEM,src,offset,incr,height);
131 /* separate headerless files for y, u and v */
132 static void store_yuv(outname,src,offset,incr,height)
134 unsigned char *src[];
135 int offset,incr,height;
138 char tmpname[FILENAME_LENGTH];
140 hsize = horizontal_size;
142 my_sprintf(tmpname,"%s.Y",outname);
143 store_yuv1(tmpname,src[0],offset,incr,hsize,height);
145 if (chroma_format!=CHROMA444)
147 offset>>=1; incr>>=1; hsize>>=1;
150 if (chroma_format==CHROMA420)
155 my_sprintf(tmpname,"%s.U",outname);
156 store_yuv1(tmpname,src[1],offset,incr,hsize,height);
158 my_sprintf(tmpname,"%s.V",outname);
159 store_yuv1(tmpname,src[2],offset,incr,hsize,height);
162 /* auxiliary routine */
163 static void store_yuv1(name,src,offset,incr,width,height)
166 int offset,incr,width,height;
173 my_fprintf("saving %s\n",name);
176 if(!my_fopen(name, "wb", outfile))
178 my_sprintf(Error_Text,"Couldn't create %s\n",name);
184 for (i=0; i<height; i++)
186 p = src + offset + incr*i;
187 for (j=0; j<width; j++)
192 my_fwrite(obfr,optr-obfr,1,outfile);
198 * store as headerless file in U,Y,V,Y format
200 static void store_sif (outname,src,offset,incr,height)
202 unsigned char *src[];
203 int offset, incr, height;
206 unsigned char *py, *pu, *pv;
207 static unsigned char *u422, *v422;
210 if (chroma_format==CHROMA444)
211 Error("4:4:4 not supported for SIF format");
213 if (chroma_format==CHROMA422)
222 if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
223 *Coded_Picture_Height)))
224 Error("malloc failed");
225 static_malloc[0] = u422;
226 if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
227 *Coded_Picture_Height)))
228 Error("malloc failed");
229 static_malloc[1] = v422;
232 conv420to422(src[1],u422);
233 conv420to422(src[2],v422);
236 strcat(outname,".SIF");
239 my_fprintf("saving %s\n",outname);
242 if(!my_fopen(outname, "wb", outfile))
244 my_sprintf(Error_Text,"Couldn't create %s\n",outname);
250 for (i=0; i<height; i++)
252 py = src[0] + offset + incr*i;
253 pu = u422 + (offset>>1) + (incr>>1)*i;
254 pv = v422 + (offset>>1) + (incr>>1)*i;
256 for (j=0; j<horizontal_size; j+=2)
266 my_fwrite(obfr,optr-obfr,1,outfile);
272 * store as PPM (PBMPLUS) or uncompressed Truevision TGA ('Targa') file
274 static void store_ppm_tga(outname,src,offset,incr,height,tgaflag)
276 unsigned char *src[];
277 int offset, incr, height;
281 int y, u, v, r, g, b;
282 int crv, cbu, cgu, cgv;
283 unsigned char *py, *pu, *pv;
284 static unsigned char tga24[14] = {0,0,2,0,0,0,0, 0,0,0,0,0,24,32};
285 char header[FILENAME_LENGTH];
286 static unsigned char *u422, *v422, *u444, *v444;
289 if (chroma_format==CHROMA444)
298 if (chroma_format==CHROMA420)
300 if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
301 *Coded_Picture_Height)))
302 Error("malloc failed");
303 static_malloc[2] = u422;
304 if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
305 *Coded_Picture_Height)))
306 Error("malloc failed");
307 static_malloc[3] = v422;
310 if (!(u444 = (unsigned char *)malloc(Coded_Picture_Width
311 *Coded_Picture_Height)))
312 Error("malloc failed");
313 static_malloc[4] = u444;
315 if (!(v444 = (unsigned char *)malloc(Coded_Picture_Width
316 *Coded_Picture_Height)))
317 Error("malloc failed");
318 static_malloc[5] = v444;
321 if (chroma_format==CHROMA420)
323 conv420to422(src[1],u422);
324 conv420to422(src[2],v422);
325 conv422to444(u422,u444);
326 conv422to444(v422,v444);
330 conv422to444(src[1],u444);
331 conv422to444(src[2],v444);
335 strcat(outname,tgaflag ? ".tga" : ".ppm");
338 my_fprintf("saving %s\n",outname);
341 if(! my_fopen(outname, "wb", outfile))
343 my_sprintf(Error_Text,"Couldn't create %s\n",outname);
355 putword(horizontal_size); putword(height);
356 putbyte(tga24[12]); putbyte(tga24[13]);
361 my_sprintf(header,"P6\n%d %d\n255\n",horizontal_size,height);
363 for (i=0; header[i]!=0; i++)
367 /* matrix coefficients */
368 crv = Inverse_Table_6_9[matrix_coefficients][0];
369 cbu = Inverse_Table_6_9[matrix_coefficients][1];
370 cgu = Inverse_Table_6_9[matrix_coefficients][2];
371 cgv = Inverse_Table_6_9[matrix_coefficients][3];
373 for (i=0; i<height; i++)
375 py = src[0] + offset + incr*i;
376 pu = u444 + offset + incr*i;
377 pv = v444 + offset + incr*i;
379 for (j=0; j<horizontal_size; j++)
383 y = 76309 * (*py++ - 16); /* (255/219)*65536 */
384 r = Clip[(y + crv*v + 32768)>>16];
385 g = Clip[(y - cgu*u - cgv*v + 32768)>>16];
386 b = Clip[(y + cbu*u + 32786)>>16];
390 putbyte(b); putbyte(g); putbyte(r);
394 putbyte(r); putbyte(g); putbyte(b);
400 my_fwrite(obfr,optr-obfr,1,outfile);
406 * store as PPM (PBMPLUS) or uncompressed Truevision TGA ('Targa') file
408 static void store_in_mem(outmem,src,offset,incr,height)
410 unsigned char *src[];
411 int offset, incr, height;
414 int y, u, v, r, g, b;
415 int crv, cbu, cgu, cgv;
416 unsigned char *py, *pu, *pv;
417 /*static unsigned char tga24[14] = {0,0,2,0,0,0,0, 0,0,0,0,0,24,32};*/
418 /*char header[FILENAME_LENGTH];*/
419 static unsigned char *u422, *v422, *u444, *v444;
423 if (chroma_format==CHROMA444)
432 if (chroma_format==CHROMA420)
434 if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
435 *Coded_Picture_Height)))
436 Error("malloc failed");
437 static_malloc[2] = u422;
438 if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
439 *Coded_Picture_Height)))
440 Error("malloc failed");
441 static_malloc[3] = v422;
444 if (!(u444 = (unsigned char *)malloc(Coded_Picture_Width
445 *Coded_Picture_Height)))
446 Error("malloc failed");
447 static_malloc[4] = u444;
449 if (!(v444 = (unsigned char *)malloc(Coded_Picture_Width
450 *Coded_Picture_Height)))
451 Error("malloc failed");
452 static_malloc[5] = v444;
455 if (chroma_format==CHROMA420)
457 conv420to422(src[1],u422);
458 conv420to422(src[2],v422);
459 conv422to444(u422,u444);
460 conv422to444(v422,v444);
464 conv422to444(src[1],u444);
465 conv422to444(src[2],v444);
469 /*strcat(outname,tgaflag ? ".tga" : ".ppm");*/
472 my_fprintf("saving %s\n",outname);*/
475 if(! my_fopen(outname, "wb", outfile))
477 my_sprintf(Error_Text,"Couldn't create %s\n",outname);
490 putword(horizontal_size); putword(height);
491 putbyte(tga24[12]); putbyte(tga24[13]);
496 my_sprintf(header,"P6\n%d %d\n255\n",horizontal_size,height);
498 for (i=0; header[i]!=0; i++)
503 /* matrix coefficients */
504 crv = Inverse_Table_6_9[matrix_coefficients][0];
505 cbu = Inverse_Table_6_9[matrix_coefficients][1];
506 cgu = Inverse_Table_6_9[matrix_coefficients][2];
507 cgv = Inverse_Table_6_9[matrix_coefficients][3];
509 for (i=0; i<height; i++)
511 py = src[0] + offset + incr*i;
512 pu = u444 + offset + incr*i;
513 pv = v444 + offset + incr*i;
515 for (j=0; j<horizontal_size; j++)
519 y = 76309 * (*py++ - 16); /* (255/219)*65536 */
520 r = Clip[(y + crv*v + 32768)>>16];
521 g = Clip[(y - cgu*u - cgv*v + 32768)>>16];
522 b = Clip[(y + cbu*u + 32786)>>16];
528 putbyte(b); putbyte(g); putbyte(r);
532 putbyte(r); putbyte(g); putbyte(b);
535 mem_putbyte(r); mem_putbyte(g); mem_putbyte(b);
542 /*my_fwrite(obfr,optr-obfr,1,outfile);*/
543 memcpy(OUTMEM,obfr,optr-obfr); OUTMEM+=(optr-obfr);
546 /*my_fclose(outfile);*/
549 static void putbyte(c)
554 if (optr == obfr+OBFRSIZE)
556 my_fwrite(obfr,OBFRSIZE,1,outfile);
561 static void putword(w)
564 putbyte(w); putbyte(w>>8);
567 static void mem_putbyte(c)
572 if (optr == obfr+OBFRSIZE)
574 /*my_fwrite(obfr,OBFRSIZE,1,outfile);*/
575 memcpy(OUTMEM,obfr,OBFRSIZE);OUTMEM+=OBFRSIZE;
580 /*static void mem_putword(w)
583 mem_putbyte(w); mem_putbyte(w>>8);
586 /* horizontal 1:2 interpolation filter */
587 static void conv422to444(src,dst)
588 unsigned char *src,*dst;
590 int i, i2, w, j, im3, im2, im1, ip1, ip2, ip3;
592 w = Coded_Picture_Width>>1;
596 for (j=0; j<Coded_Picture_Height; j++)
601 im2 = (i<2) ? 0 : i-2;
602 im1 = (i<1) ? 0 : i-1;
603 ip1 = (i<w-1) ? i+1 : w-1;
604 ip2 = (i<w-2) ? i+2 : w-1;
605 ip3 = (i<w-3) ? i+3 : w-1;
607 /* FIR filter coefficients (*256): 21 0 -52 0 159 256 159 0 -52 0 21 */
608 /* even samples (0 0 256 0 0) */
611 /* odd samples (21 -52 159 159 -52 21) */
612 dst[i2+1] = Clip[(int)(21*(src[im2]+src[ip3])
613 -52*(src[im1]+src[ip2])
614 +159*(src[i]+src[ip1])+128)>>8];
617 dst+= Coded_Picture_Width;
622 for (j=0; j<Coded_Picture_Height; j++)
628 im3 = (i<3) ? 0 : i-3;
629 im2 = (i<2) ? 0 : i-2;
630 im1 = (i<1) ? 0 : i-1;
631 ip1 = (i<w-1) ? i+1 : w-1;
632 ip2 = (i<w-2) ? i+2 : w-1;
633 ip3 = (i<w-3) ? i+3 : w-1;
635 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
636 dst[i2] = Clip[(int)( 5*src[im3]
641 +11*src[ip2]+128)>>8];
643 dst[i2+1] = Clip[(int)( 5*src[ip3]
648 +11*src[im2]+128)>>8];
651 dst+= Coded_Picture_Width;
656 /* vertical 1:2 interpolation filter */
657 static void conv420to422(src,dst)
658 unsigned char *src,*dst;
661 int jm6, jm5, jm4, jm3, jm2, jm1, jp1, jp2, jp3, jp4, jp5, jp6, jp7;
663 w = Coded_Picture_Width>>1;
664 h = Coded_Picture_Height>>1;
666 if (progressive_frame)
674 jm3 = (j<3) ? 0 : j-3;
675 jm2 = (j<2) ? 0 : j-2;
676 jm1 = (j<1) ? 0 : j-1;
677 jp1 = (j<h-1) ? j+1 : h-1;
678 jp2 = (j<h-2) ? j+2 : h-1;
679 jp3 = (j<h-3) ? j+3 : h-1;
681 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
682 /* New FIR filter coefficients (*256): 3 -16 67 227 -32 7 */
683 dst[w*j2] = Clip[(int)( 3*src[w*jm3]
688 +7*src[w*jp2]+128)>>8];
690 dst[w*(j2+1)] = Clip[(int)( 3*src[w*jp3]
695 +7*src[w*jm2]+128)>>8];
711 jm6 = (j<6) ? 0 : j-6;
712 jm4 = (j<4) ? 0 : j-4;
713 jm2 = (j<2) ? 0 : j-2;
714 jp2 = (j<h-2) ? j+2 : h-2;
715 jp4 = (j<h-4) ? j+4 : h-2;
716 jp6 = (j<h-6) ? j+6 : h-2;
718 /* Polyphase FIR filter coefficients (*256): 2 -10 35 242 -18 5 */
719 /* New polyphase FIR filter coefficients (*256): 1 -7 30 248 -21 5 */
720 dst[w*j2] = Clip[(int)( 1*src[w*jm6]
725 +5*src[w*jp4]+128)>>8];
727 /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
728 /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
729 dst[w*(j2+2)] = Clip[(int)( 7*src[w*jm4]
734 +4*src[w*jp6]+128)>>8];
737 jm5 = (j<5) ? 1 : j-5;
738 jm3 = (j<3) ? 1 : j-3;
739 jm1 = (j<1) ? 1 : j-1;
740 jp1 = (j<h-1) ? j+1 : h-1;
741 jp3 = (j<h-3) ? j+3 : h-1;
742 jp5 = (j<h-5) ? j+5 : h-1;
743 jp7 = (j<h-7) ? j+7 : h-1;
745 /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
746 /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
747 dst[w*(j2+1)] = Clip[(int)( 7*src[w*jp5]
752 +4*src[w*jm5]+128)>>8];
754 dst[w*(j2+3)] = Clip[(int)( 1*src[w*jp7]
759 +5*src[w*jm3]+128)>>8];