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_yuv1 _ANSI_ARGS_((char *name, unsigned char *src,
46 int offset, int incr, int width, int height));
47 static void putbyte _ANSI_ARGS_((int c));
48 static void putword _ANSI_ARGS_((int w));
49 static void conv422to444 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
50 static void conv420to422 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
53 static unsigned char obfr[OBFRSIZE];
54 static unsigned char *optr;
55 static ostream *outfile;
56 unsigned char *static_malloc[6] = {0,0,0,0,0,0}; //worse case there is 6 buffer in this impl unit.
58 void FreeStaticBuffer()
62 free(static_malloc[i]);
66 * store a picture as either one frame or two fields
68 void Write_Frame(src,frame)
72 char outname[FILENAME_LENGTH];
74 if (progressive_sequence || progressive_frame || Frame_Store_Flag)
77 my_sprintf(outname,Output_Picture_Filename,frame,'f');
78 store_one(outname,src,0,Coded_Picture_Width,vertical_size);
83 my_sprintf(outname,Output_Picture_Filename,frame,'a');
84 store_one(outname,src,0,Coded_Picture_Width<<1,vertical_size>>1);
86 my_sprintf(outname,Output_Picture_Filename,frame,'b');
87 store_one(outname,src,
88 Coded_Picture_Width,Coded_Picture_Width<<1,vertical_size>>1);
93 * store one frame or one field
95 static void store_one(outname,src,offset,incr,height)
98 int offset, incr, height;
103 store_yuv(outname,src,offset,incr,height);
106 store_sif(outname,src,offset,incr,height);
109 store_ppm_tga(outname,src,offset,incr,height,1);
112 store_ppm_tga(outname,src,offset,incr,height,0);
124 /* separate headerless files for y, u and v */
125 static void store_yuv(outname,src,offset,incr,height)
127 unsigned char *src[];
128 int offset,incr,height;
131 char tmpname[FILENAME_LENGTH];
133 hsize = horizontal_size;
135 my_sprintf(tmpname,"%s.Y",outname);
136 store_yuv1(tmpname,src[0],offset,incr,hsize,height);
138 if (chroma_format!=CHROMA444)
140 offset>>=1; incr>>=1; hsize>>=1;
143 if (chroma_format==CHROMA420)
148 my_sprintf(tmpname,"%s.U",outname);
149 store_yuv1(tmpname,src[1],offset,incr,hsize,height);
151 my_sprintf(tmpname,"%s.V",outname);
152 store_yuv1(tmpname,src[2],offset,incr,hsize,height);
155 /* auxiliary routine */
156 static void store_yuv1(name,src,offset,incr,width,height)
159 int offset,incr,width,height;
165 my_fprintf("saving %s\n",name);
167 //if ((outfile = open(name,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
170 if(!my_fopen(name, "wb", outfile))
172 my_sprintf(Error_Text,"Couldn't create %s\n",name);
178 for (i=0; i<height; i++)
180 p = src + offset + incr*i;
181 for (j=0; j<width; j++)
186 my_fwrite(obfr,optr-obfr,1,outfile);
192 * store as headerless file in U,Y,V,Y format
194 static void store_sif (outname,src,offset,incr,height)
196 unsigned char *src[];
197 int offset, incr, height;
200 unsigned char *py, *pu, *pv;
201 static unsigned char *u422, *v422;
203 if (chroma_format==CHROMA444)
204 Error("4:4:4 not supported for SIF format");
206 if (chroma_format==CHROMA422)
215 if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
216 *Coded_Picture_Height)))
217 Error("malloc failed");
218 static_malloc[0] = u422;
219 if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
220 *Coded_Picture_Height)))
221 Error("malloc failed");
222 static_malloc[1] = v422;
225 conv420to422(src[1],u422);
226 conv420to422(src[2],v422);
229 strcat(outname,".SIF");
232 my_fprintf("saving %s\n",outname);
234 //if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
237 if(!my_fopen(outname, "wb", outfile))
239 my_sprintf(Error_Text,"Couldn't create %s\n",outname);
245 for (i=0; i<height; i++)
247 py = src[0] + offset + incr*i;
248 pu = u422 + (offset>>1) + (incr>>1)*i;
249 pv = v422 + (offset>>1) + (incr>>1)*i;
251 for (j=0; j<horizontal_size; j+=2)
261 my_fwrite(obfr,optr-obfr,1,outfile);
267 * store as PPM (PBMPLUS) or uncompressed Truevision TGA ('Targa') file
269 static void store_ppm_tga(outname,src,offset,incr,height,tgaflag)
271 unsigned char *src[];
272 int offset, incr, height;
276 int y, u, v, r, g, b;
277 int crv, cbu, cgu, cgv;
278 unsigned char *py, *pu, *pv;
279 static unsigned char tga24[14] = {0,0,2,0,0,0,0, 0,0,0,0,0,24,32};
280 char header[FILENAME_LENGTH];
281 static unsigned char *u422, *v422, *u444, *v444;
283 if (chroma_format==CHROMA444)
292 if (chroma_format==CHROMA420)
294 if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
295 *Coded_Picture_Height)))
296 Error("malloc failed");
297 static_malloc[2] = u422;
298 if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
299 *Coded_Picture_Height)))
300 Error("malloc failed");
301 static_malloc[3] = v422;
304 if (!(u444 = (unsigned char *)malloc(Coded_Picture_Width
305 *Coded_Picture_Height)))
306 Error("malloc failed");
307 static_malloc[4] = u444;
309 if (!(v444 = (unsigned char *)malloc(Coded_Picture_Width
310 *Coded_Picture_Height)))
311 Error("malloc failed");
312 static_malloc[5] = v444;
315 if (chroma_format==CHROMA420)
317 conv420to422(src[1],u422);
318 conv420to422(src[2],v422);
319 conv422to444(u422,u444);
320 conv422to444(v422,v444);
324 conv422to444(src[1],u444);
325 conv422to444(src[2],v444);
329 strcat(outname,tgaflag ? ".tga" : ".ppm");
332 my_fprintf("saving %s\n",outname);
334 //if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
337 if(! my_fopen(outname, "wb", outfile))
339 my_sprintf(Error_Text,"Couldn't create %s\n",outname);
351 putword(horizontal_size); putword(height);
352 putbyte(tga24[12]); putbyte(tga24[13]);
357 my_sprintf(header,"P6\n%d %d\n255\n",horizontal_size,height);
359 for (i=0; header[i]!=0; i++)
363 /* matrix coefficients */
364 crv = Inverse_Table_6_9[matrix_coefficients][0];
365 cbu = Inverse_Table_6_9[matrix_coefficients][1];
366 cgu = Inverse_Table_6_9[matrix_coefficients][2];
367 cgv = Inverse_Table_6_9[matrix_coefficients][3];
369 for (i=0; i<height; i++)
371 py = src[0] + offset + incr*i;
372 pu = u444 + offset + incr*i;
373 pv = v444 + offset + incr*i;
375 for (j=0; j<horizontal_size; j++)
379 y = 76309 * (*py++ - 16); /* (255/219)*65536 */
380 r = Clip[(y + crv*v + 32768)>>16];
381 g = Clip[(y - cgu*u - cgv*v + 32768)>>16];
382 b = Clip[(y + cbu*u + 32786)>>16];
386 putbyte(b); putbyte(g); putbyte(r);
390 putbyte(r); putbyte(g); putbyte(b);
396 my_fwrite(obfr,optr-obfr,1,outfile);
401 static void putbyte(c)
406 if (optr == obfr+OBFRSIZE)
408 my_fwrite(obfr,OBFRSIZE,1,outfile);
413 static void putword(w)
416 putbyte(w); putbyte(w>>8);
419 /* horizontal 1:2 interpolation filter */
420 static void conv422to444(src,dst)
421 unsigned char *src,*dst;
423 int i, i2, w, j, im3, im2, im1, ip1, ip2, ip3;
425 w = Coded_Picture_Width>>1;
429 for (j=0; j<Coded_Picture_Height; j++)
434 im2 = (i<2) ? 0 : i-2;
435 im1 = (i<1) ? 0 : i-1;
436 ip1 = (i<w-1) ? i+1 : w-1;
437 ip2 = (i<w-2) ? i+2 : w-1;
438 ip3 = (i<w-3) ? i+3 : w-1;
440 /* FIR filter coefficients (*256): 21 0 -52 0 159 256 159 0 -52 0 21 */
441 /* even samples (0 0 256 0 0) */
444 /* odd samples (21 -52 159 159 -52 21) */
445 dst[i2+1] = Clip[(int)(21*(src[im2]+src[ip3])
446 -52*(src[im1]+src[ip2])
447 +159*(src[i]+src[ip1])+128)>>8];
450 dst+= Coded_Picture_Width;
455 for (j=0; j<Coded_Picture_Height; j++)
461 im3 = (i<3) ? 0 : i-3;
462 im2 = (i<2) ? 0 : i-2;
463 im1 = (i<1) ? 0 : i-1;
464 ip1 = (i<w-1) ? i+1 : w-1;
465 ip2 = (i<w-2) ? i+2 : w-1;
466 ip3 = (i<w-3) ? i+3 : w-1;
468 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
469 dst[i2] = Clip[(int)( 5*src[im3]
474 +11*src[ip2]+128)>>8];
476 dst[i2+1] = Clip[(int)( 5*src[ip3]
481 +11*src[im2]+128)>>8];
484 dst+= Coded_Picture_Width;
489 /* vertical 1:2 interpolation filter */
490 static void conv420to422(src,dst)
491 unsigned char *src,*dst;
494 int jm6, jm5, jm4, jm3, jm2, jm1, jp1, jp2, jp3, jp4, jp5, jp6, jp7;
496 w = Coded_Picture_Width>>1;
497 h = Coded_Picture_Height>>1;
499 if (progressive_frame)
507 jm3 = (j<3) ? 0 : j-3;
508 jm2 = (j<2) ? 0 : j-2;
509 jm1 = (j<1) ? 0 : j-1;
510 jp1 = (j<h-1) ? j+1 : h-1;
511 jp2 = (j<h-2) ? j+2 : h-1;
512 jp3 = (j<h-3) ? j+3 : h-1;
514 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
515 /* New FIR filter coefficients (*256): 3 -16 67 227 -32 7 */
516 dst[w*j2] = Clip[(int)( 3*src[w*jm3]
521 +7*src[w*jp2]+128)>>8];
523 dst[w*(j2+1)] = Clip[(int)( 3*src[w*jp3]
528 +7*src[w*jm2]+128)>>8];
544 jm6 = (j<6) ? 0 : j-6;
545 jm4 = (j<4) ? 0 : j-4;
546 jm2 = (j<2) ? 0 : j-2;
547 jp2 = (j<h-2) ? j+2 : h-2;
548 jp4 = (j<h-4) ? j+4 : h-2;
549 jp6 = (j<h-6) ? j+6 : h-2;
551 /* Polyphase FIR filter coefficients (*256): 2 -10 35 242 -18 5 */
552 /* New polyphase FIR filter coefficients (*256): 1 -7 30 248 -21 5 */
553 dst[w*j2] = Clip[(int)( 1*src[w*jm6]
558 +5*src[w*jp4]+128)>>8];
560 /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
561 /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
562 dst[w*(j2+2)] = Clip[(int)( 7*src[w*jm4]
567 +4*src[w*jp6]+128)>>8];
570 jm5 = (j<5) ? 1 : j-5;
571 jm3 = (j<3) ? 1 : j-3;
572 jm1 = (j<1) ? 1 : j-1;
573 jp1 = (j<h-1) ? j+1 : h-1;
574 jp3 = (j<h-3) ? j+3 : h-1;
575 jp5 = (j<h-5) ? j+5 : h-1;
576 jp7 = (j<h-7) ? j+7 : h-1;
578 /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
579 /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
580 dst[w*(j2+1)] = Clip[(int)( 7*src[w*jp5]
585 +4*src[w*jm5]+128)>>8];
587 dst[w*(j2+3)] = Clip[(int)( 1*src[w*jp7]
592 +5*src[w*jm3]+128)>>8];