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;
166 my_fprintf("saving %s\n",name);
169 if(!my_fopen(name, "wb", outfile))
171 my_sprintf(Error_Text,"Couldn't create %s\n",name);
177 for (i=0; i<height; i++)
179 p = src + offset + incr*i;
180 for (j=0; j<width; j++)
185 my_fwrite(obfr,optr-obfr,1,outfile);
191 * store as headerless file in U,Y,V,Y format
193 static void store_sif (outname,src,offset,incr,height)
195 unsigned char *src[];
196 int offset, incr, height;
199 unsigned char *py, *pu, *pv;
200 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);
235 if(!my_fopen(outname, "wb", outfile))
237 my_sprintf(Error_Text,"Couldn't create %s\n",outname);
243 for (i=0; i<height; i++)
245 py = src[0] + offset + incr*i;
246 pu = u422 + (offset>>1) + (incr>>1)*i;
247 pv = v422 + (offset>>1) + (incr>>1)*i;
249 for (j=0; j<horizontal_size; j+=2)
259 my_fwrite(obfr,optr-obfr,1,outfile);
265 * store as PPM (PBMPLUS) or uncompressed Truevision TGA ('Targa') file
267 static void store_ppm_tga(outname,src,offset,incr,height,tgaflag)
269 unsigned char *src[];
270 int offset, incr, height;
274 int y, u, v, r, g, b;
275 int crv, cbu, cgu, cgv;
276 unsigned char *py, *pu, *pv;
277 static unsigned char tga24[14] = {0,0,2,0,0,0,0, 0,0,0,0,0,24,32};
278 char header[FILENAME_LENGTH];
279 static unsigned char *u422, *v422, *u444, *v444;
282 if (chroma_format==CHROMA444)
291 if (chroma_format==CHROMA420)
293 if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
294 *Coded_Picture_Height)))
295 Error("malloc failed");
296 static_malloc[2] = u422;
297 if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
298 *Coded_Picture_Height)))
299 Error("malloc failed");
300 static_malloc[3] = v422;
303 if (!(u444 = (unsigned char *)malloc(Coded_Picture_Width
304 *Coded_Picture_Height)))
305 Error("malloc failed");
306 static_malloc[4] = u444;
308 if (!(v444 = (unsigned char *)malloc(Coded_Picture_Width
309 *Coded_Picture_Height)))
310 Error("malloc failed");
311 static_malloc[5] = v444;
314 if (chroma_format==CHROMA420)
316 conv420to422(src[1],u422);
317 conv420to422(src[2],v422);
318 conv422to444(u422,u444);
319 conv422to444(v422,v444);
323 conv422to444(src[1],u444);
324 conv422to444(src[2],v444);
328 strcat(outname,tgaflag ? ".tga" : ".ppm");
331 my_fprintf("saving %s\n",outname);
334 if(! my_fopen(outname, "wb", outfile))
336 my_sprintf(Error_Text,"Couldn't create %s\n",outname);
348 putword(horizontal_size); putword(height);
349 putbyte(tga24[12]); putbyte(tga24[13]);
354 my_sprintf(header,"P6\n%d %d\n255\n",horizontal_size,height);
356 for (i=0; header[i]!=0; i++)
360 /* matrix coefficients */
361 crv = Inverse_Table_6_9[matrix_coefficients][0];
362 cbu = Inverse_Table_6_9[matrix_coefficients][1];
363 cgu = Inverse_Table_6_9[matrix_coefficients][2];
364 cgv = Inverse_Table_6_9[matrix_coefficients][3];
366 for (i=0; i<height; i++)
368 py = src[0] + offset + incr*i;
369 pu = u444 + offset + incr*i;
370 pv = v444 + offset + incr*i;
372 for (j=0; j<horizontal_size; j++)
376 y = 76309 * (*py++ - 16); /* (255/219)*65536 */
377 r = Clip[(y + crv*v + 32768)>>16];
378 g = Clip[(y - cgu*u - cgv*v + 32768)>>16];
379 b = Clip[(y + cbu*u + 32786)>>16];
383 putbyte(b); putbyte(g); putbyte(r);
387 putbyte(r); putbyte(g); putbyte(b);
393 my_fwrite(obfr,optr-obfr,1,outfile);
398 static void putbyte(c)
403 if (optr == obfr+OBFRSIZE)
405 my_fwrite(obfr,OBFRSIZE,1,outfile);
410 static void putword(w)
413 putbyte(w); putbyte(w>>8);
416 /* horizontal 1:2 interpolation filter */
417 static void conv422to444(src,dst)
418 unsigned char *src,*dst;
420 int i, i2, w, j, im3, im2, im1, ip1, ip2, ip3;
422 w = Coded_Picture_Width>>1;
426 for (j=0; j<Coded_Picture_Height; j++)
431 im2 = (i<2) ? 0 : i-2;
432 im1 = (i<1) ? 0 : i-1;
433 ip1 = (i<w-1) ? i+1 : w-1;
434 ip2 = (i<w-2) ? i+2 : w-1;
435 ip3 = (i<w-3) ? i+3 : w-1;
437 /* FIR filter coefficients (*256): 21 0 -52 0 159 256 159 0 -52 0 21 */
438 /* even samples (0 0 256 0 0) */
441 /* odd samples (21 -52 159 159 -52 21) */
442 dst[i2+1] = Clip[(int)(21*(src[im2]+src[ip3])
443 -52*(src[im1]+src[ip2])
444 +159*(src[i]+src[ip1])+128)>>8];
447 dst+= Coded_Picture_Width;
452 for (j=0; j<Coded_Picture_Height; j++)
458 im3 = (i<3) ? 0 : i-3;
459 im2 = (i<2) ? 0 : i-2;
460 im1 = (i<1) ? 0 : i-1;
461 ip1 = (i<w-1) ? i+1 : w-1;
462 ip2 = (i<w-2) ? i+2 : w-1;
463 ip3 = (i<w-3) ? i+3 : w-1;
465 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
466 dst[i2] = Clip[(int)( 5*src[im3]
471 +11*src[ip2]+128)>>8];
473 dst[i2+1] = Clip[(int)( 5*src[ip3]
478 +11*src[im2]+128)>>8];
481 dst+= Coded_Picture_Width;
486 /* vertical 1:2 interpolation filter */
487 static void conv420to422(src,dst)
488 unsigned char *src,*dst;
491 int jm6, jm5, jm4, jm3, jm2, jm1, jp1, jp2, jp3, jp4, jp5, jp6, jp7;
493 w = Coded_Picture_Width>>1;
494 h = Coded_Picture_Height>>1;
496 if (progressive_frame)
504 jm3 = (j<3) ? 0 : j-3;
505 jm2 = (j<2) ? 0 : j-2;
506 jm1 = (j<1) ? 0 : j-1;
507 jp1 = (j<h-1) ? j+1 : h-1;
508 jp2 = (j<h-2) ? j+2 : h-1;
509 jp3 = (j<h-3) ? j+3 : h-1;
511 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
512 /* New FIR filter coefficients (*256): 3 -16 67 227 -32 7 */
513 dst[w*j2] = Clip[(int)( 3*src[w*jm3]
518 +7*src[w*jp2]+128)>>8];
520 dst[w*(j2+1)] = Clip[(int)( 3*src[w*jp3]
525 +7*src[w*jm2]+128)>>8];
541 jm6 = (j<6) ? 0 : j-6;
542 jm4 = (j<4) ? 0 : j-4;
543 jm2 = (j<2) ? 0 : j-2;
544 jp2 = (j<h-2) ? j+2 : h-2;
545 jp4 = (j<h-4) ? j+4 : h-2;
546 jp6 = (j<h-6) ? j+6 : h-2;
548 /* Polyphase FIR filter coefficients (*256): 2 -10 35 242 -18 5 */
549 /* New polyphase FIR filter coefficients (*256): 1 -7 30 248 -21 5 */
550 dst[w*j2] = Clip[(int)( 1*src[w*jm6]
555 +5*src[w*jp4]+128)>>8];
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+2)] = Clip[(int)( 7*src[w*jm4]
564 +4*src[w*jp6]+128)>>8];
567 jm5 = (j<5) ? 1 : j-5;
568 jm3 = (j<3) ? 1 : j-3;
569 jm1 = (j<1) ? 1 : j-1;
570 jp1 = (j<h-1) ? j+1 : h-1;
571 jp3 = (j<h-3) ? j+3 : h-1;
572 jp5 = (j<h-5) ? j+5 : h-1;
573 jp7 = (j<h-7) ? j+7 : h-1;
575 /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
576 /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
577 dst[w*(j2+1)] = Clip[(int)( 7*src[w*jp5]
582 +4*src[w*jm5]+128)>>8];
584 dst[w*(j2+3)] = Clip[(int)( 1*src[w*jp7]
589 +5*src[w*jm3]+128)>>8];