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 */
32 #include <string.h> /* for strcat */
37 /* private prototypes */
38 static void store_one _ANSI_ARGS_((char *outname, unsigned char *src[],
39 int offset, int incr, int height));
40 static void store_yuv _ANSI_ARGS_((char *outname, unsigned char *src[],
41 int offset, int incr, int height));
42 static void store_sif _ANSI_ARGS_((char *outname, unsigned char *src[],
43 int offset, int incr, int height));
44 static void store_ppm_tga _ANSI_ARGS_((char *outname, unsigned char *src[],
45 int offset, int incr, int height, int tgaflag));
46 static void store_yuv1 _ANSI_ARGS_((char *name, unsigned char *src,
47 int offset, int incr, int width, int height));
48 static void putbyte _ANSI_ARGS_((int c));
49 static void putword _ANSI_ARGS_((int w));
50 static void conv422to444 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
51 static void conv420to422 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
54 static unsigned char obfr[OBFRSIZE];
55 static unsigned char *optr;
59 * store a picture as either one frame or two fields
61 void Write_Frame(src,frame)
65 char outname[FILENAME_LENGTH];
67 if (progressive_sequence || progressive_frame || Frame_Store_Flag)
70 my_sprintf(outname,Output_Picture_Filename,frame,'f');
71 store_one(outname,src,0,Coded_Picture_Width,vertical_size);
76 my_sprintf(outname,Output_Picture_Filename,frame,'a');
77 store_one(outname,src,0,Coded_Picture_Width<<1,vertical_size>>1);
79 my_sprintf(outname,Output_Picture_Filename,frame,'b');
80 store_one(outname,src,
81 Coded_Picture_Width,Coded_Picture_Width<<1,vertical_size>>1);
86 * store one frame or one field
88 static void store_one(outname,src,offset,incr,height)
91 int offset, incr, height;
96 store_yuv(outname,src,offset,incr,height);
99 store_sif(outname,src,offset,incr,height);
102 store_ppm_tga(outname,src,offset,incr,height,1);
105 store_ppm_tga(outname,src,offset,incr,height,0);
117 /* separate headerless files for y, u and v */
118 static void store_yuv(outname,src,offset,incr,height)
120 unsigned char *src[];
121 int offset,incr,height;
124 char tmpname[FILENAME_LENGTH];
126 hsize = horizontal_size;
128 my_sprintf(tmpname,"%s.Y",outname);
129 store_yuv1(tmpname,src[0],offset,incr,hsize,height);
131 if (chroma_format!=CHROMA444)
133 offset>>=1; incr>>=1; hsize>>=1;
136 if (chroma_format==CHROMA420)
141 my_sprintf(tmpname,"%s.U",outname);
142 store_yuv1(tmpname,src[1],offset,incr,hsize,height);
144 my_sprintf(tmpname,"%s.V",outname);
145 store_yuv1(tmpname,src[2],offset,incr,hsize,height);
148 /* auxiliary routine */
149 static void store_yuv1(name,src,offset,incr,width,height)
152 int offset,incr,width,height;
158 my_fprintf("saving %s\n",name);
160 if ((outfile = open(name,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
162 my_sprintf(Error_Text,"Couldn't create %s\n",name);
168 for (i=0; i<height; i++)
170 p = src + offset + incr*i;
171 for (j=0; j<width; j++)
176 write(outfile,obfr,optr-obfr);
182 * store as headerless file in U,Y,V,Y format
184 static void store_sif (outname,src,offset,incr,height)
186 unsigned char *src[];
187 int offset, incr, height;
190 unsigned char *py, *pu, *pv;
191 static unsigned char *u422, *v422;
193 if (chroma_format==CHROMA444)
194 Error("4:4:4 not supported for SIF format");
196 if (chroma_format==CHROMA422)
205 if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
206 *Coded_Picture_Height)))
207 Error("malloc failed");
208 if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
209 *Coded_Picture_Height)))
210 Error("malloc failed");
213 conv420to422(src[1],u422);
214 conv420to422(src[2],v422);
217 strcat(outname,".SIF");
220 my_fprintf("saving %s\n",outname);
222 if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
224 my_sprintf(Error_Text,"Couldn't create %s\n",outname);
230 for (i=0; i<height; i++)
232 py = src[0] + offset + incr*i;
233 pu = u422 + (offset>>1) + (incr>>1)*i;
234 pv = v422 + (offset>>1) + (incr>>1)*i;
236 for (j=0; j<horizontal_size; j+=2)
246 write(outfile,obfr,optr-obfr);
252 * store as PPM (PBMPLUS) or uncompressed Truevision TGA ('Targa') file
254 static void store_ppm_tga(outname,src,offset,incr,height,tgaflag)
256 unsigned char *src[];
257 int offset, incr, height;
261 int y, u, v, r, g, b;
262 int crv, cbu, cgu, cgv;
263 unsigned char *py, *pu, *pv;
264 static unsigned char tga24[14] = {0,0,2,0,0,0,0, 0,0,0,0,0,24,32};
265 char header[FILENAME_LENGTH];
266 static unsigned char *u422, *v422, *u444, *v444;
268 if (chroma_format==CHROMA444)
277 if (chroma_format==CHROMA420)
279 if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
280 *Coded_Picture_Height)))
281 Error("malloc failed");
282 if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
283 *Coded_Picture_Height)))
284 Error("malloc failed");
287 if (!(u444 = (unsigned char *)malloc(Coded_Picture_Width
288 *Coded_Picture_Height)))
289 Error("malloc failed");
291 if (!(v444 = (unsigned char *)malloc(Coded_Picture_Width
292 *Coded_Picture_Height)))
293 Error("malloc failed");
296 if (chroma_format==CHROMA420)
298 conv420to422(src[1],u422);
299 conv420to422(src[2],v422);
300 conv422to444(u422,u444);
301 conv422to444(v422,v444);
305 conv422to444(src[1],u444);
306 conv422to444(src[2],v444);
310 strcat(outname,tgaflag ? ".tga" : ".ppm");
313 my_fprintf("saving %s\n",outname);
315 if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
317 my_sprintf(Error_Text,"Couldn't create %s\n",outname);
329 putword(horizontal_size); putword(height);
330 putbyte(tga24[12]); putbyte(tga24[13]);
335 my_sprintf(header,"P6\n%d %d\n255\n",horizontal_size,height);
337 for (i=0; header[i]!=0; i++)
341 /* matrix coefficients */
342 crv = Inverse_Table_6_9[matrix_coefficients][0];
343 cbu = Inverse_Table_6_9[matrix_coefficients][1];
344 cgu = Inverse_Table_6_9[matrix_coefficients][2];
345 cgv = Inverse_Table_6_9[matrix_coefficients][3];
347 for (i=0; i<height; i++)
349 py = src[0] + offset + incr*i;
350 pu = u444 + offset + incr*i;
351 pv = v444 + offset + incr*i;
353 for (j=0; j<horizontal_size; j++)
357 y = 76309 * (*py++ - 16); /* (255/219)*65536 */
358 r = Clip[(y + crv*v + 32768)>>16];
359 g = Clip[(y - cgu*u - cgv*v + 32768)>>16];
360 b = Clip[(y + cbu*u + 32786)>>16];
364 putbyte(b); putbyte(g); putbyte(r);
368 putbyte(r); putbyte(g); putbyte(b);
374 write(outfile,obfr,optr-obfr);
379 static void putbyte(c)
384 if (optr == obfr+OBFRSIZE)
386 write(outfile,obfr,OBFRSIZE);
391 static void putword(w)
394 putbyte(w); putbyte(w>>8);
397 /* horizontal 1:2 interpolation filter */
398 static void conv422to444(src,dst)
399 unsigned char *src,*dst;
401 int i, i2, w, j, im3, im2, im1, ip1, ip2, ip3;
403 w = Coded_Picture_Width>>1;
407 for (j=0; j<Coded_Picture_Height; j++)
412 im2 = (i<2) ? 0 : i-2;
413 im1 = (i<1) ? 0 : i-1;
414 ip1 = (i<w-1) ? i+1 : w-1;
415 ip2 = (i<w-2) ? i+2 : w-1;
416 ip3 = (i<w-3) ? i+3 : w-1;
418 /* FIR filter coefficients (*256): 21 0 -52 0 159 256 159 0 -52 0 21 */
419 /* even samples (0 0 256 0 0) */
422 /* odd samples (21 -52 159 159 -52 21) */
423 dst[i2+1] = Clip[(int)(21*(src[im2]+src[ip3])
424 -52*(src[im1]+src[ip2])
425 +159*(src[i]+src[ip1])+128)>>8];
428 dst+= Coded_Picture_Width;
433 for (j=0; j<Coded_Picture_Height; j++)
439 im3 = (i<3) ? 0 : i-3;
440 im2 = (i<2) ? 0 : i-2;
441 im1 = (i<1) ? 0 : i-1;
442 ip1 = (i<w-1) ? i+1 : w-1;
443 ip2 = (i<w-2) ? i+2 : w-1;
444 ip3 = (i<w-3) ? i+3 : w-1;
446 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
447 dst[i2] = Clip[(int)( 5*src[im3]
452 +11*src[ip2]+128)>>8];
454 dst[i2+1] = Clip[(int)( 5*src[ip3]
459 +11*src[im2]+128)>>8];
462 dst+= Coded_Picture_Width;
467 /* vertical 1:2 interpolation filter */
468 static void conv420to422(src,dst)
469 unsigned char *src,*dst;
472 int jm6, jm5, jm4, jm3, jm2, jm1, jp1, jp2, jp3, jp4, jp5, jp6, jp7;
474 w = Coded_Picture_Width>>1;
475 h = Coded_Picture_Height>>1;
477 if (progressive_frame)
485 jm3 = (j<3) ? 0 : j-3;
486 jm2 = (j<2) ? 0 : j-2;
487 jm1 = (j<1) ? 0 : j-1;
488 jp1 = (j<h-1) ? j+1 : h-1;
489 jp2 = (j<h-2) ? j+2 : h-1;
490 jp3 = (j<h-3) ? j+3 : h-1;
492 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
493 /* New FIR filter coefficients (*256): 3 -16 67 227 -32 7 */
494 dst[w*j2] = Clip[(int)( 3*src[w*jm3]
499 +7*src[w*jp2]+128)>>8];
501 dst[w*(j2+1)] = Clip[(int)( 3*src[w*jp3]
506 +7*src[w*jm2]+128)>>8];
522 jm6 = (j<6) ? 0 : j-6;
523 jm4 = (j<4) ? 0 : j-4;
524 jm2 = (j<2) ? 0 : j-2;
525 jp2 = (j<h-2) ? j+2 : h-2;
526 jp4 = (j<h-4) ? j+4 : h-2;
527 jp6 = (j<h-6) ? j+6 : h-2;
529 /* Polyphase FIR filter coefficients (*256): 2 -10 35 242 -18 5 */
530 /* New polyphase FIR filter coefficients (*256): 1 -7 30 248 -21 5 */
531 dst[w*j2] = Clip[(int)( 1*src[w*jm6]
536 +5*src[w*jp4]+128)>>8];
538 /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
539 /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
540 dst[w*(j2+2)] = Clip[(int)( 7*src[w*jm4]
545 +4*src[w*jp6]+128)>>8];
548 jm5 = (j<5) ? 1 : j-5;
549 jm3 = (j<3) ? 1 : j-3;
550 jm1 = (j<1) ? 1 : j-1;
551 jp1 = (j<h-1) ? j+1 : h-1;
552 jp3 = (j<h-3) ? j+3 : h-1;
553 jp5 = (j<h-5) ? j+5 : h-1;
554 jp7 = (j<h-7) ? j+7 : h-1;
556 /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
557 /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
558 dst[w*(j2+1)] = Clip[(int)( 7*src[w*jp5]
563 +4*src[w*jm5]+128)>>8];
565 dst[w*(j2+3)] = Clip[(int)( 1*src[w*jp7]
570 +5*src[w*jm3]+128)>>8];