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 <unistd.h> // for write
34 #include <string.h> // for strcat
39 /* private prototypes */
40 static void store_one _ANSI_ARGS_((char *outname, unsigned char *src[],
41 int offset, int incr, int height));
42 static void store_yuv _ANSI_ARGS_((char *outname, unsigned char *src[],
43 int offset, int incr, int height));
44 static void store_sif _ANSI_ARGS_((char *outname, unsigned char *src[],
45 int offset, int incr, int height));
46 static void store_ppm_tga _ANSI_ARGS_((char *outname, unsigned char *src[],
47 int offset, int incr, int height, int tgaflag));
48 static void store_yuv1 _ANSI_ARGS_((char *name, unsigned char *src,
49 int offset, int incr, int width, int height));
50 static void putbyte _ANSI_ARGS_((int c));
51 static void putword _ANSI_ARGS_((int w));
52 static void conv422to444 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
53 static void conv420to422 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
56 static unsigned char obfr[OBFRSIZE];
57 static unsigned char *optr;
61 * store a picture as either one frame or two fields
63 void Write_Frame(src,frame)
67 char outname[FILENAME_LENGTH];
69 if (progressive_sequence || progressive_frame || Frame_Store_Flag)
72 sprintf(outname,Output_Picture_Filename,frame,'f');
73 store_one(outname,src,0,Coded_Picture_Width,vertical_size);
78 sprintf(outname,Output_Picture_Filename,frame,'a');
79 store_one(outname,src,0,Coded_Picture_Width<<1,vertical_size>>1);
81 sprintf(outname,Output_Picture_Filename,frame,'b');
82 store_one(outname,src,
83 Coded_Picture_Width,Coded_Picture_Width<<1,vertical_size>>1);
88 * store one frame or one field
90 static void store_one(outname,src,offset,incr,height)
93 int offset, incr, height;
98 store_yuv(outname,src,offset,incr,height);
101 store_sif(outname,src,offset,incr,height);
104 store_ppm_tga(outname,src,offset,incr,height,1);
107 store_ppm_tga(outname,src,offset,incr,height,0);
119 /* separate headerless files for y, u and v */
120 static void store_yuv(outname,src,offset,incr,height)
122 unsigned char *src[];
123 int offset,incr,height;
126 char tmpname[FILENAME_LENGTH];
128 hsize = horizontal_size;
130 sprintf(tmpname,"%s.Y",outname);
131 store_yuv1(tmpname,src[0],offset,incr,hsize,height);
133 if (chroma_format!=CHROMA444)
135 offset>>=1; incr>>=1; hsize>>=1;
138 if (chroma_format==CHROMA420)
143 sprintf(tmpname,"%s.U",outname);
144 store_yuv1(tmpname,src[1],offset,incr,hsize,height);
146 sprintf(tmpname,"%s.V",outname);
147 store_yuv1(tmpname,src[2],offset,incr,hsize,height);
150 /* auxiliary routine */
151 static void store_yuv1(name,src,offset,incr,width,height)
154 int offset,incr,width,height;
160 fprintf(stderr,"saving %s\n",name);
162 if ((outfile = open(name,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
164 sprintf(Error_Text,"Couldn't create %s\n",name);
170 for (i=0; i<height; i++)
172 p = src + offset + incr*i;
173 for (j=0; j<width; j++)
178 write(outfile,obfr,optr-obfr);
184 * store as headerless file in U,Y,V,Y format
186 static void store_sif (outname,src,offset,incr,height)
188 unsigned char *src[];
189 int offset, incr, height;
192 unsigned char *py, *pu, *pv;
193 static unsigned char *u422, *v422;
195 if (chroma_format==CHROMA444)
196 Error("4:4:4 not supported for SIF format");
198 if (chroma_format==CHROMA422)
207 if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
208 *Coded_Picture_Height)))
209 Error("malloc failed");
210 if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
211 *Coded_Picture_Height)))
212 Error("malloc failed");
215 conv420to422(src[1],u422);
216 conv420to422(src[2],v422);
219 strcat(outname,".SIF");
222 fprintf(stderr,"saving %s\n",outname);
224 if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
226 sprintf(Error_Text,"Couldn't create %s\n",outname);
232 for (i=0; i<height; i++)
234 py = src[0] + offset + incr*i;
235 pu = u422 + (offset>>1) + (incr>>1)*i;
236 pv = v422 + (offset>>1) + (incr>>1)*i;
238 for (j=0; j<horizontal_size; j+=2)
248 write(outfile,obfr,optr-obfr);
254 * store as PPM (PBMPLUS) or uncompressed Truevision TGA ('Targa') file
256 static void store_ppm_tga(outname,src,offset,incr,height,tgaflag)
258 unsigned char *src[];
259 int offset, incr, height;
263 int y, u, v, r, g, b;
264 int crv, cbu, cgu, cgv;
265 unsigned char *py, *pu, *pv;
266 static unsigned char tga24[14] = {0,0,2,0,0,0,0, 0,0,0,0,0,24,32};
267 char header[FILENAME_LENGTH];
268 static unsigned char *u422, *v422, *u444, *v444;
270 if (chroma_format==CHROMA444)
279 if (chroma_format==CHROMA420)
281 if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
282 *Coded_Picture_Height)))
283 Error("malloc failed");
284 if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
285 *Coded_Picture_Height)))
286 Error("malloc failed");
289 if (!(u444 = (unsigned char *)malloc(Coded_Picture_Width
290 *Coded_Picture_Height)))
291 Error("malloc failed");
293 if (!(v444 = (unsigned char *)malloc(Coded_Picture_Width
294 *Coded_Picture_Height)))
295 Error("malloc failed");
298 if (chroma_format==CHROMA420)
300 conv420to422(src[1],u422);
301 conv420to422(src[2],v422);
302 conv422to444(u422,u444);
303 conv422to444(v422,v444);
307 conv422to444(src[1],u444);
308 conv422to444(src[2],v444);
312 strcat(outname,tgaflag ? ".tga" : ".ppm");
315 fprintf(stderr,"saving %s\n",outname);
317 if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
319 sprintf(Error_Text,"Couldn't create %s\n",outname);
331 putword(horizontal_size); putword(height);
332 putbyte(tga24[12]); putbyte(tga24[13]);
337 sprintf(header,"P6\n%d %d\n255\n",horizontal_size,height);
339 for (i=0; header[i]!=0; i++)
343 /* matrix coefficients */
344 crv = Inverse_Table_6_9[matrix_coefficients][0];
345 cbu = Inverse_Table_6_9[matrix_coefficients][1];
346 cgu = Inverse_Table_6_9[matrix_coefficients][2];
347 cgv = Inverse_Table_6_9[matrix_coefficients][3];
349 for (i=0; i<height; i++)
351 py = src[0] + offset + incr*i;
352 pu = u444 + offset + incr*i;
353 pv = v444 + offset + incr*i;
355 for (j=0; j<horizontal_size; j++)
359 y = 76309 * (*py++ - 16); /* (255/219)*65536 */
360 r = Clip[(y + crv*v + 32768)>>16];
361 g = Clip[(y - cgu*u - cgv*v + 32768)>>16];
362 b = Clip[(y + cbu*u + 32786)>>16];
366 putbyte(b); putbyte(g); putbyte(r);
370 putbyte(r); putbyte(g); putbyte(b);
376 write(outfile,obfr,optr-obfr);
381 static void putbyte(c)
386 if (optr == obfr+OBFRSIZE)
388 write(outfile,obfr,OBFRSIZE);
393 static void putword(w)
396 putbyte(w); putbyte(w>>8);
399 /* horizontal 1:2 interpolation filter */
400 static void conv422to444(src,dst)
401 unsigned char *src,*dst;
403 int i, i2, w, j, im3, im2, im1, ip1, ip2, ip3;
405 w = Coded_Picture_Width>>1;
409 for (j=0; j<Coded_Picture_Height; j++)
414 im2 = (i<2) ? 0 : i-2;
415 im1 = (i<1) ? 0 : i-1;
416 ip1 = (i<w-1) ? i+1 : w-1;
417 ip2 = (i<w-2) ? i+2 : w-1;
418 ip3 = (i<w-3) ? i+3 : w-1;
420 /* FIR filter coefficients (*256): 21 0 -52 0 159 256 159 0 -52 0 21 */
421 /* even samples (0 0 256 0 0) */
424 /* odd samples (21 -52 159 159 -52 21) */
425 dst[i2+1] = Clip[(int)(21*(src[im2]+src[ip3])
426 -52*(src[im1]+src[ip2])
427 +159*(src[i]+src[ip1])+128)>>8];
430 dst+= Coded_Picture_Width;
435 for (j=0; j<Coded_Picture_Height; j++)
441 im3 = (i<3) ? 0 : i-3;
442 im2 = (i<2) ? 0 : i-2;
443 im1 = (i<1) ? 0 : i-1;
444 ip1 = (i<w-1) ? i+1 : w-1;
445 ip2 = (i<w-2) ? i+2 : w-1;
446 ip3 = (i<w-3) ? i+3 : w-1;
448 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
449 dst[i2] = Clip[(int)( 5*src[im3]
454 +11*src[ip2]+128)>>8];
456 dst[i2+1] = Clip[(int)( 5*src[ip3]
461 +11*src[im2]+128)>>8];
464 dst+= Coded_Picture_Width;
469 /* vertical 1:2 interpolation filter */
470 static void conv420to422(src,dst)
471 unsigned char *src,*dst;
474 int jm6, jm5, jm4, jm3, jm2, jm1, jp1, jp2, jp3, jp4, jp5, jp6, jp7;
476 w = Coded_Picture_Width>>1;
477 h = Coded_Picture_Height>>1;
479 if (progressive_frame)
487 jm3 = (j<3) ? 0 : j-3;
488 jm2 = (j<2) ? 0 : j-2;
489 jm1 = (j<1) ? 0 : j-1;
490 jp1 = (j<h-1) ? j+1 : h-1;
491 jp2 = (j<h-2) ? j+2 : h-1;
492 jp3 = (j<h-3) ? j+3 : h-1;
494 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
495 /* New FIR filter coefficients (*256): 3 -16 67 227 -32 7 */
496 dst[w*j2] = Clip[(int)( 3*src[w*jm3]
501 +7*src[w*jp2]+128)>>8];
503 dst[w*(j2+1)] = Clip[(int)( 3*src[w*jp3]
508 +7*src[w*jm2]+128)>>8];
524 jm6 = (j<6) ? 0 : j-6;
525 jm4 = (j<4) ? 0 : j-4;
526 jm2 = (j<2) ? 0 : j-2;
527 jp2 = (j<h-2) ? j+2 : h-2;
528 jp4 = (j<h-4) ? j+4 : h-2;
529 jp6 = (j<h-6) ? j+6 : h-2;
531 /* Polyphase FIR filter coefficients (*256): 2 -10 35 242 -18 5 */
532 /* New polyphase FIR filter coefficients (*256): 1 -7 30 248 -21 5 */
533 dst[w*j2] = Clip[(int)( 1*src[w*jm6]
538 +5*src[w*jp4]+128)>>8];
540 /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
541 /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
542 dst[w*(j2+2)] = Clip[(int)( 7*src[w*jm4]
547 +4*src[w*jp6]+128)>>8];
550 jm5 = (j<5) ? 1 : j-5;
551 jm3 = (j<3) ? 1 : j-3;
552 jm1 = (j<1) ? 1 : j-1;
553 jp1 = (j<h-1) ? j+1 : h-1;
554 jp3 = (j<h-3) ? j+3 : h-1;
555 jp5 = (j<h-5) ? j+5 : h-1;
556 jp7 = (j<h-7) ? j+7 : h-1;
558 /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
559 /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
560 dst[w*(j2+1)] = Clip[(int)( 7*src[w*jp5]
565 +4*src[w*jm5]+128)>>8];
567 dst[w*(j2+3)] = Clip[(int)( 1*src[w*jp7]
572 +5*src[w*jm3]+128)>>8];