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
33 #include <string.h> /* for strcat */
38 /* private prototypes */
39 static void store_one _ANSI_ARGS_((char *outname, unsigned char *src[],
40 int offset, int incr, int height));
41 static void store_yuv _ANSI_ARGS_((char *outname, unsigned char *src[],
42 int offset, int incr, int height));
43 static void store_sif _ANSI_ARGS_((char *outname, unsigned char *src[],
44 int offset, int incr, int height));
45 static void store_ppm_tga _ANSI_ARGS_((char *outname, unsigned char *src[],
46 int offset, int incr, int height, int tgaflag));
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 conv422to444 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
52 static void conv420to422 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
55 static unsigned char obfr[OBFRSIZE];
56 static unsigned char *optr;
60 * store a picture as either one frame or two fields
62 void Write_Frame(src,frame)
66 char outname[FILENAME_LENGTH];
68 if (progressive_sequence || progressive_frame || Frame_Store_Flag)
71 sprintf(outname,Output_Picture_Filename,frame,'f');
72 store_one(outname,src,0,Coded_Picture_Width,vertical_size);
77 sprintf(outname,Output_Picture_Filename,frame,'a');
78 store_one(outname,src,0,Coded_Picture_Width<<1,vertical_size>>1);
80 sprintf(outname,Output_Picture_Filename,frame,'b');
81 store_one(outname,src,
82 Coded_Picture_Width,Coded_Picture_Width<<1,vertical_size>>1);
87 * store one frame or one field
89 static void store_one(outname,src,offset,incr,height)
92 int offset, incr, height;
97 store_yuv(outname,src,offset,incr,height);
100 store_sif(outname,src,offset,incr,height);
103 store_ppm_tga(outname,src,offset,incr,height,1);
106 store_ppm_tga(outname,src,offset,incr,height,0);
118 /* separate headerless files for y, u and v */
119 static void store_yuv(outname,src,offset,incr,height)
121 unsigned char *src[];
122 int offset,incr,height;
125 char tmpname[FILENAME_LENGTH];
127 hsize = horizontal_size;
129 sprintf(tmpname,"%s.Y",outname);
130 store_yuv1(tmpname,src[0],offset,incr,hsize,height);
132 if (chroma_format!=CHROMA444)
134 offset>>=1; incr>>=1; hsize>>=1;
137 if (chroma_format==CHROMA420)
142 sprintf(tmpname,"%s.U",outname);
143 store_yuv1(tmpname,src[1],offset,incr,hsize,height);
145 sprintf(tmpname,"%s.V",outname);
146 store_yuv1(tmpname,src[2],offset,incr,hsize,height);
149 /* auxiliary routine */
150 static void store_yuv1(name,src,offset,incr,width,height)
153 int offset,incr,width,height;
159 fprintf(stderr,"saving %s\n",name);
161 if ((outfile = open(name,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
163 sprintf(Error_Text,"Couldn't create %s\n",name);
169 for (i=0; i<height; i++)
171 p = src + offset + incr*i;
172 for (j=0; j<width; j++)
177 write(outfile,obfr,optr-obfr);
183 * store as headerless file in U,Y,V,Y format
185 static void store_sif (outname,src,offset,incr,height)
187 unsigned char *src[];
188 int offset, incr, height;
191 unsigned char *py, *pu, *pv;
192 static unsigned char *u422, *v422;
194 if (chroma_format==CHROMA444)
195 Error("4:4:4 not supported for SIF format");
197 if (chroma_format==CHROMA422)
206 if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
207 *Coded_Picture_Height)))
208 Error("malloc failed");
209 if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
210 *Coded_Picture_Height)))
211 Error("malloc failed");
214 conv420to422(src[1],u422);
215 conv420to422(src[2],v422);
218 strcat(outname,".SIF");
221 fprintf(stderr,"saving %s\n",outname);
223 if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
225 sprintf(Error_Text,"Couldn't create %s\n",outname);
231 for (i=0; i<height; i++)
233 py = src[0] + offset + incr*i;
234 pu = u422 + (offset>>1) + (incr>>1)*i;
235 pv = v422 + (offset>>1) + (incr>>1)*i;
237 for (j=0; j<horizontal_size; j+=2)
247 write(outfile,obfr,optr-obfr);
253 * store as PPM (PBMPLUS) or uncompressed Truevision TGA ('Targa') file
255 static void store_ppm_tga(outname,src,offset,incr,height,tgaflag)
257 unsigned char *src[];
258 int offset, incr, height;
262 int y, u, v, r, g, b;
263 int crv, cbu, cgu, cgv;
264 unsigned char *py, *pu, *pv;
265 static unsigned char tga24[14] = {0,0,2,0,0,0,0, 0,0,0,0,0,24,32};
266 char header[FILENAME_LENGTH];
267 static unsigned char *u422, *v422, *u444, *v444;
269 if (chroma_format==CHROMA444)
278 if (chroma_format==CHROMA420)
280 if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
281 *Coded_Picture_Height)))
282 Error("malloc failed");
283 if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
284 *Coded_Picture_Height)))
285 Error("malloc failed");
288 if (!(u444 = (unsigned char *)malloc(Coded_Picture_Width
289 *Coded_Picture_Height)))
290 Error("malloc failed");
292 if (!(v444 = (unsigned char *)malloc(Coded_Picture_Width
293 *Coded_Picture_Height)))
294 Error("malloc failed");
297 if (chroma_format==CHROMA420)
299 conv420to422(src[1],u422);
300 conv420to422(src[2],v422);
301 conv422to444(u422,u444);
302 conv422to444(v422,v444);
306 conv422to444(src[1],u444);
307 conv422to444(src[2],v444);
311 strcat(outname,tgaflag ? ".tga" : ".ppm");
314 fprintf(stderr,"saving %s\n",outname);
316 if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
318 sprintf(Error_Text,"Couldn't create %s\n",outname);
330 putword(horizontal_size); putword(height);
331 putbyte(tga24[12]); putbyte(tga24[13]);
336 sprintf(header,"P6\n%d %d\n255\n",horizontal_size,height);
338 for (i=0; header[i]!=0; i++)
342 /* matrix coefficients */
343 crv = Inverse_Table_6_9[matrix_coefficients][0];
344 cbu = Inverse_Table_6_9[matrix_coefficients][1];
345 cgu = Inverse_Table_6_9[matrix_coefficients][2];
346 cgv = Inverse_Table_6_9[matrix_coefficients][3];
348 for (i=0; i<height; i++)
350 py = src[0] + offset + incr*i;
351 pu = u444 + offset + incr*i;
352 pv = v444 + offset + incr*i;
354 for (j=0; j<horizontal_size; j++)
358 y = 76309 * (*py++ - 16); /* (255/219)*65536 */
359 r = Clip[(y + crv*v + 32768)>>16];
360 g = Clip[(y - cgu*u - cgv*v + 32768)>>16];
361 b = Clip[(y + cbu*u + 32786)>>16];
365 putbyte(b); putbyte(g); putbyte(r);
369 putbyte(r); putbyte(g); putbyte(b);
375 write(outfile,obfr,optr-obfr);
380 static void putbyte(c)
385 if (optr == obfr+OBFRSIZE)
387 write(outfile,obfr,OBFRSIZE);
392 static void putword(w)
395 putbyte(w); putbyte(w>>8);
398 /* horizontal 1:2 interpolation filter */
399 static void conv422to444(src,dst)
400 unsigned char *src,*dst;
402 int i, i2, w, j, im3, im2, im1, ip1, ip2, ip3;
404 w = Coded_Picture_Width>>1;
408 for (j=0; j<Coded_Picture_Height; j++)
413 im2 = (i<2) ? 0 : i-2;
414 im1 = (i<1) ? 0 : i-1;
415 ip1 = (i<w-1) ? i+1 : w-1;
416 ip2 = (i<w-2) ? i+2 : w-1;
417 ip3 = (i<w-3) ? i+3 : w-1;
419 /* FIR filter coefficients (*256): 21 0 -52 0 159 256 159 0 -52 0 21 */
420 /* even samples (0 0 256 0 0) */
423 /* odd samples (21 -52 159 159 -52 21) */
424 dst[i2+1] = Clip[(int)(21*(src[im2]+src[ip3])
425 -52*(src[im1]+src[ip2])
426 +159*(src[i]+src[ip1])+128)>>8];
429 dst+= Coded_Picture_Width;
434 for (j=0; j<Coded_Picture_Height; j++)
440 im3 = (i<3) ? 0 : i-3;
441 im2 = (i<2) ? 0 : i-2;
442 im1 = (i<1) ? 0 : i-1;
443 ip1 = (i<w-1) ? i+1 : w-1;
444 ip2 = (i<w-2) ? i+2 : w-1;
445 ip3 = (i<w-3) ? i+3 : w-1;
447 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
448 dst[i2] = Clip[(int)( 5*src[im3]
453 +11*src[ip2]+128)>>8];
455 dst[i2+1] = Clip[(int)( 5*src[ip3]
460 +11*src[im2]+128)>>8];
463 dst+= Coded_Picture_Width;
468 /* vertical 1:2 interpolation filter */
469 static void conv420to422(src,dst)
470 unsigned char *src,*dst;
473 int jm6, jm5, jm4, jm3, jm2, jm1, jp1, jp2, jp3, jp4, jp5, jp6, jp7;
475 w = Coded_Picture_Width>>1;
476 h = Coded_Picture_Height>>1;
478 if (progressive_frame)
486 jm3 = (j<3) ? 0 : j-3;
487 jm2 = (j<2) ? 0 : j-2;
488 jm1 = (j<1) ? 0 : j-1;
489 jp1 = (j<h-1) ? j+1 : h-1;
490 jp2 = (j<h-2) ? j+2 : h-1;
491 jp3 = (j<h-3) ? j+3 : h-1;
493 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
494 /* New FIR filter coefficients (*256): 3 -16 67 227 -32 7 */
495 dst[w*j2] = Clip[(int)( 3*src[w*jm3]
500 +7*src[w*jp2]+128)>>8];
502 dst[w*(j2+1)] = Clip[(int)( 3*src[w*jp3]
507 +7*src[w*jm2]+128)>>8];
523 jm6 = (j<6) ? 0 : j-6;
524 jm4 = (j<4) ? 0 : j-4;
525 jm2 = (j<2) ? 0 : j-2;
526 jp2 = (j<h-2) ? j+2 : h-2;
527 jp4 = (j<h-4) ? j+4 : h-2;
528 jp6 = (j<h-6) ? j+6 : h-2;
530 /* Polyphase FIR filter coefficients (*256): 2 -10 35 242 -18 5 */
531 /* New polyphase FIR filter coefficients (*256): 1 -7 30 248 -21 5 */
532 dst[w*j2] = Clip[(int)( 1*src[w*jm6]
537 +5*src[w*jp4]+128)>>8];
539 /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
540 /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
541 dst[w*(j2+2)] = Clip[(int)( 7*src[w*jm4]
546 +4*src[w*jp6]+128)>>8];
549 jm5 = (j<5) ? 1 : j-5;
550 jm3 = (j<3) ? 1 : j-3;
551 jm1 = (j<1) ? 1 : j-1;
552 jp1 = (j<h-1) ? j+1 : h-1;
553 jp3 = (j<h-3) ? j+3 : h-1;
554 jp5 = (j<h-5) ? j+5 : h-1;
555 jp7 = (j<h-7) ? j+7 : h-1;
557 /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
558 /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
559 dst[w*(j2+1)] = Clip[(int)( 7*src[w*jp5]
564 +4*src[w*jm5]+128)>>8];
566 dst[w*(j2+3)] = Clip[(int)( 1*src[w*jp7]
571 +5*src[w*jm3]+128)>>8];