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;
58 * store a picture as either one frame or two fields
60 void Write_Frame(src,frame)
64 char outname[FILENAME_LENGTH];
66 if (progressive_sequence || progressive_frame || Frame_Store_Flag)
69 my_sprintf(outname,Output_Picture_Filename,frame,'f');
70 store_one(outname,src,0,Coded_Picture_Width,vertical_size);
75 my_sprintf(outname,Output_Picture_Filename,frame,'a');
76 store_one(outname,src,0,Coded_Picture_Width<<1,vertical_size>>1);
78 my_sprintf(outname,Output_Picture_Filename,frame,'b');
79 store_one(outname,src,
80 Coded_Picture_Width,Coded_Picture_Width<<1,vertical_size>>1);
85 * store one frame or one field
87 static void store_one(outname,src,offset,incr,height)
90 int offset, incr, height;
95 store_yuv(outname,src,offset,incr,height);
98 store_sif(outname,src,offset,incr,height);
101 store_ppm_tga(outname,src,offset,incr,height,1);
104 store_ppm_tga(outname,src,offset,incr,height,0);
116 /* separate headerless files for y, u and v */
117 static void store_yuv(outname,src,offset,incr,height)
119 unsigned char *src[];
120 int offset,incr,height;
123 char tmpname[FILENAME_LENGTH];
125 hsize = horizontal_size;
127 my_sprintf(tmpname,"%s.Y",outname);
128 store_yuv1(tmpname,src[0],offset,incr,hsize,height);
130 if (chroma_format!=CHROMA444)
132 offset>>=1; incr>>=1; hsize>>=1;
135 if (chroma_format==CHROMA420)
140 my_sprintf(tmpname,"%s.U",outname);
141 store_yuv1(tmpname,src[1],offset,incr,hsize,height);
143 my_sprintf(tmpname,"%s.V",outname);
144 store_yuv1(tmpname,src[2],offset,incr,hsize,height);
147 /* auxiliary routine */
148 static void store_yuv1(name,src,offset,incr,width,height)
151 int offset,incr,width,height;
157 my_fprintf("saving %s\n",name);
159 //if ((outfile = open(name,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
162 if(!my_fopen(name, "wb", outfile))
164 my_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 my_fwrite(obfr,optr-obfr,1,outfile);
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 my_fprintf("saving %s\n",outname);
224 //if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
227 if(!my_fopen(outname, "wb", outfile))
229 my_sprintf(Error_Text,"Couldn't create %s\n",outname);
235 for (i=0; i<height; i++)
237 py = src[0] + offset + incr*i;
238 pu = u422 + (offset>>1) + (incr>>1)*i;
239 pv = v422 + (offset>>1) + (incr>>1)*i;
241 for (j=0; j<horizontal_size; j+=2)
251 my_fwrite(obfr,optr-obfr,1,outfile);
257 * store as PPM (PBMPLUS) or uncompressed Truevision TGA ('Targa') file
259 static void store_ppm_tga(outname,src,offset,incr,height,tgaflag)
261 unsigned char *src[];
262 int offset, incr, height;
266 int y, u, v, r, g, b;
267 int crv, cbu, cgu, cgv;
268 unsigned char *py, *pu, *pv;
269 static unsigned char tga24[14] = {0,0,2,0,0,0,0, 0,0,0,0,0,24,32};
270 char header[FILENAME_LENGTH];
271 static unsigned char *u422, *v422, *u444, *v444;
273 if (chroma_format==CHROMA444)
282 if (chroma_format==CHROMA420)
284 if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
285 *Coded_Picture_Height)))
286 Error("malloc failed");
287 if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
288 *Coded_Picture_Height)))
289 Error("malloc failed");
292 if (!(u444 = (unsigned char *)malloc(Coded_Picture_Width
293 *Coded_Picture_Height)))
294 Error("malloc failed");
296 if (!(v444 = (unsigned char *)malloc(Coded_Picture_Width
297 *Coded_Picture_Height)))
298 Error("malloc failed");
301 if (chroma_format==CHROMA420)
303 conv420to422(src[1],u422);
304 conv420to422(src[2],v422);
305 conv422to444(u422,u444);
306 conv422to444(v422,v444);
310 conv422to444(src[1],u444);
311 conv422to444(src[2],v444);
315 strcat(outname,tgaflag ? ".tga" : ".ppm");
318 my_fprintf("saving %s\n",outname);
320 //if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
323 if(! my_fopen(outname, "wb", outfile))
325 my_sprintf(Error_Text,"Couldn't create %s\n",outname);
337 putword(horizontal_size); putword(height);
338 putbyte(tga24[12]); putbyte(tga24[13]);
343 my_sprintf(header,"P6\n%d %d\n255\n",horizontal_size,height);
345 for (i=0; header[i]!=0; i++)
349 /* matrix coefficients */
350 crv = Inverse_Table_6_9[matrix_coefficients][0];
351 cbu = Inverse_Table_6_9[matrix_coefficients][1];
352 cgu = Inverse_Table_6_9[matrix_coefficients][2];
353 cgv = Inverse_Table_6_9[matrix_coefficients][3];
355 for (i=0; i<height; i++)
357 py = src[0] + offset + incr*i;
358 pu = u444 + offset + incr*i;
359 pv = v444 + offset + incr*i;
361 for (j=0; j<horizontal_size; j++)
365 y = 76309 * (*py++ - 16); /* (255/219)*65536 */
366 r = Clip[(y + crv*v + 32768)>>16];
367 g = Clip[(y - cgu*u - cgv*v + 32768)>>16];
368 b = Clip[(y + cbu*u + 32786)>>16];
372 putbyte(b); putbyte(g); putbyte(r);
376 putbyte(r); putbyte(g); putbyte(b);
382 my_fwrite(obfr,optr-obfr,1,outfile);
387 static void putbyte(c)
392 if (optr == obfr+OBFRSIZE)
394 my_fwrite(obfr,OBFRSIZE,1,outfile);
399 static void putword(w)
402 putbyte(w); putbyte(w>>8);
405 /* horizontal 1:2 interpolation filter */
406 static void conv422to444(src,dst)
407 unsigned char *src,*dst;
409 int i, i2, w, j, im3, im2, im1, ip1, ip2, ip3;
411 w = Coded_Picture_Width>>1;
415 for (j=0; j<Coded_Picture_Height; j++)
420 im2 = (i<2) ? 0 : i-2;
421 im1 = (i<1) ? 0 : i-1;
422 ip1 = (i<w-1) ? i+1 : w-1;
423 ip2 = (i<w-2) ? i+2 : w-1;
424 ip3 = (i<w-3) ? i+3 : w-1;
426 /* FIR filter coefficients (*256): 21 0 -52 0 159 256 159 0 -52 0 21 */
427 /* even samples (0 0 256 0 0) */
430 /* odd samples (21 -52 159 159 -52 21) */
431 dst[i2+1] = Clip[(int)(21*(src[im2]+src[ip3])
432 -52*(src[im1]+src[ip2])
433 +159*(src[i]+src[ip1])+128)>>8];
436 dst+= Coded_Picture_Width;
441 for (j=0; j<Coded_Picture_Height; j++)
447 im3 = (i<3) ? 0 : i-3;
448 im2 = (i<2) ? 0 : i-2;
449 im1 = (i<1) ? 0 : i-1;
450 ip1 = (i<w-1) ? i+1 : w-1;
451 ip2 = (i<w-2) ? i+2 : w-1;
452 ip3 = (i<w-3) ? i+3 : w-1;
454 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
455 dst[i2] = Clip[(int)( 5*src[im3]
460 +11*src[ip2]+128)>>8];
462 dst[i2+1] = Clip[(int)( 5*src[ip3]
467 +11*src[im2]+128)>>8];
470 dst+= Coded_Picture_Width;
475 /* vertical 1:2 interpolation filter */
476 static void conv420to422(src,dst)
477 unsigned char *src,*dst;
480 int jm6, jm5, jm4, jm3, jm2, jm1, jp1, jp2, jp3, jp4, jp5, jp6, jp7;
482 w = Coded_Picture_Width>>1;
483 h = Coded_Picture_Height>>1;
485 if (progressive_frame)
493 jm3 = (j<3) ? 0 : j-3;
494 jm2 = (j<2) ? 0 : j-2;
495 jm1 = (j<1) ? 0 : j-1;
496 jp1 = (j<h-1) ? j+1 : h-1;
497 jp2 = (j<h-2) ? j+2 : h-1;
498 jp3 = (j<h-3) ? j+3 : h-1;
500 /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
501 /* New FIR filter coefficients (*256): 3 -16 67 227 -32 7 */
502 dst[w*j2] = Clip[(int)( 3*src[w*jm3]
507 +7*src[w*jp2]+128)>>8];
509 dst[w*(j2+1)] = Clip[(int)( 3*src[w*jp3]
514 +7*src[w*jm2]+128)>>8];
530 jm6 = (j<6) ? 0 : j-6;
531 jm4 = (j<4) ? 0 : j-4;
532 jm2 = (j<2) ? 0 : j-2;
533 jp2 = (j<h-2) ? j+2 : h-2;
534 jp4 = (j<h-4) ? j+4 : h-2;
535 jp6 = (j<h-6) ? j+6 : h-2;
537 /* Polyphase FIR filter coefficients (*256): 2 -10 35 242 -18 5 */
538 /* New polyphase FIR filter coefficients (*256): 1 -7 30 248 -21 5 */
539 dst[w*j2] = Clip[(int)( 1*src[w*jm6]
544 +5*src[w*jp4]+128)>>8];
546 /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
547 /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
548 dst[w*(j2+2)] = Clip[(int)( 7*src[w*jm4]
553 +4*src[w*jp6]+128)>>8];
556 jm5 = (j<5) ? 1 : j-5;
557 jm3 = (j<3) ? 1 : j-3;
558 jm1 = (j<1) ? 1 : j-1;
559 jp1 = (j<h-1) ? j+1 : h-1;
560 jp3 = (j<h-3) ? j+3 : h-1;
561 jp5 = (j<h-5) ? j+5 : h-1;
562 jp7 = (j<h-7) ? j+7 : h-1;
564 /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
565 /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
566 dst[w*(j2+1)] = Clip[(int)( 7*src[w*jp5]
571 +4*src[w*jm5]+128)>>8];
573 dst[w*(j2+3)] = Clip[(int)( 1*src[w*jp7]
578 +5*src[w*jm3]+128)>>8];