OpenCores
URL https://opencores.org/ocsvn/mpeg2fpga/mpeg2fpga/trunk

Subversion Repositories mpeg2fpga

[/] [mpeg2fpga/] [trunk/] [tools/] [mpeg2dec/] [store.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 kdv
/* store.c, picture output routines                                         */
2
 
3
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
4
 
5
/*
6
 * Disclaimer of Warranty
7
 *
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.
15
 *
16
 * This disclaimer of warranty extends to the user of these programs and user's
17
 * customers, employees, agents, transferees, successors, and assigns.
18
 *
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
21
 * patents.
22
 *
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
26
 * design.
27
 *
28
 */
29
 
30
#include <stdio.h>
31
#include <stdlib.h>
32
#include <fcntl.h>
33
 
34
#include "config.h"
35
#include "global.h"
36
 
37
/* also used by X display */
38
void conv422to444 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
39
void conv420to422 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
40
/* private prototypes */
41
static void store_one _ANSI_ARGS_((char *outname, unsigned char *src[],
42
  int offset, int incr, int height));
43
static void store_yuv _ANSI_ARGS_((char *outname, unsigned char *src[],
44
  int offset, int incr, int height));
45
static void store_sif _ANSI_ARGS_((char *outname, unsigned char *src[],
46
  int offset, int incr, int height));
47
static void store_ppm_tga _ANSI_ARGS_((char *outname, unsigned char *src[],
48
  int offset, int incr, int height, int tgaflag));
49
static void store_yuv1 _ANSI_ARGS_((char *name, unsigned char *src,
50
  int offset, int incr, int width, int height));
51
static void putbyte _ANSI_ARGS_((int c));
52
static void putword _ANSI_ARGS_((int w));
53
 
54
#define OBFRSIZE 4096
55
static unsigned char obfr[OBFRSIZE];
56
static unsigned char *optr;
57
static int outfile;
58
 
59
/*
60
 * store a picture as either one frame or two fields
61
 */
62
void Write_Frame(src,frame)
63
unsigned char *src[];
64
int frame;
65
{
66
  char outname[FILENAME_LENGTH];
67
 
68
  if (progressive_sequence || progressive_frame || Frame_Store_Flag)
69
  {
70
    /* progressive */
71
    sprintf(outname,Output_Picture_Filename,frame,'f');
72
    store_one(outname,src,0,Coded_Picture_Width,vertical_size);
73
  }
74
  else
75
  {
76
    /* interlaced */
77
    sprintf(outname,Output_Picture_Filename,frame,'a');
78
    store_one(outname,src,0,Coded_Picture_Width<<1,vertical_size>>1);
79
 
80
    sprintf(outname,Output_Picture_Filename,frame,'b');
81
    store_one(outname,src,
82
      Coded_Picture_Width,Coded_Picture_Width<<1,vertical_size>>1);
83
  }
84
    sprintf(outname,"frame_%02d_out_",frame);
85
    store_one(outname,src,0,Coded_Picture_Width,vertical_size);
86
    sprintf(outname,"frame_%02d_fwd_",frame);
87
    store_one(outname,forward_reference_frame,0,Coded_Picture_Width,vertical_size);
88
    sprintf(outname,"frame_%02d_bwd_",frame);
89
    store_one(outname,backward_reference_frame,0,Coded_Picture_Width,vertical_size);
90
    sprintf(outname,"frame_%02d_aux_",frame);
91
    store_one(outname,auxframe,0,Coded_Picture_Width,vertical_size);
92
}
93
 
94
/*
95
 * store one frame or one field
96
 */
97
static void store_one(outname,src,offset,incr,height)
98
char *outname;
99
unsigned char *src[];
100
int offset, incr, height;
101
{
102
  switch (Output_Type)
103
  {
104
  case T_YUV:
105
    store_yuv(outname,src,offset,incr,height);
106
    break;
107
  case T_SIF:
108
    store_sif(outname,src,offset,incr,height);
109
    break;
110
  case T_TGA:
111
    store_ppm_tga(outname,src,offset,incr,height,1);
112
    break;
113
  case T_PPM:
114
    store_ppm_tga(outname,src,offset,incr,height,0);
115
    break;
116
#ifdef DISPLAY
117
  case T_X11:
118
    do_display(src);
119
    break;
120
#endif
121
  default:
122
    break;
123
  }
124
}
125
 
126
/* separate headerless files for y, u and v */
127
static void store_yuv(outname,src,offset,incr,height)
128
char *outname;
129
unsigned char *src[];
130
int offset,incr,height;
131
{
132
  int hsize;
133
  char tmpname[FILENAME_LENGTH];
134
 
135
  hsize = horizontal_size;
136
 
137
  sprintf(tmpname,"%s.y.ppm",outname);
138
  store_yuv1(tmpname,src[0],offset,incr,hsize,height);
139
 
140
  if (chroma_format!=CHROMA444)
141
  {
142
    offset>>=1; incr>>=1; hsize>>=1;
143
  }
144
 
145
  if (chroma_format==CHROMA420)
146
  {
147
    height>>=1;
148
  }
149
 
150
  sprintf(tmpname,"%s.u.ppm",outname);
151
  store_yuv1(tmpname,src[1],offset,incr,hsize,height);
152
 
153
  sprintf(tmpname,"%s.v.ppm",outname);
154
  store_yuv1(tmpname,src[2],offset,incr,hsize,height);
155
}
156
 
157
/* auxiliary routine */
158
static void store_yuv1(name,src,offset,incr,width,height)
159
char *name;
160
unsigned char *src;
161
int offset,incr,width,height;
162
{
163
  int i, j, pixel;
164
  unsigned char *p;
165
  FILE *ofil;
166
 
167
  if (!Quiet_Flag)
168
    fprintf(stderr,"saving %s\n",name);
169
 
170
  if ((ofil = fopen(name,"w"))==-1)
171
  {
172
    sprintf(Error_Text,"Couldn't create %s\n",name);
173
    Error(Error_Text);
174
  }
175
  fprintf(ofil,"P2\n%d %d\n255\n",width,height);
176
 
177
  for (i=0; i<height; i++)
178
  {
179
    p = src + offset + incr*i;
180
    for (j=0; j<width; j++) {
181
      pixel = *p++;
182
      fprintf(ofil, "%d ", pixel);
183
      }
184
    fprintf(ofil, "\n");
185
  }
186
  fclose(ofil);
187
}
188
 
189
/*
190
 * store as headerless file in U,Y,V,Y format
191
 */
192
static void store_sif (outname,src,offset,incr,height)
193
char *outname;
194
unsigned char *src[];
195
int offset, incr, height;
196
{
197
  int i,j;
198
  unsigned char *py, *pu, *pv;
199
  static unsigned char *u422, *v422;
200
 
201
  if (chroma_format==CHROMA444)
202
    Error("4:4:4 not supported for SIF format");
203
 
204
  if (chroma_format==CHROMA422)
205
  {
206
    u422 = src[1];
207
    v422 = src[2];
208
  }
209
  else
210
  {
211
    if (!u422)
212
    {
213
      if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
214
                                           *Coded_Picture_Height)))
215
        Error("malloc failed");
216
      if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
217
                                           *Coded_Picture_Height)))
218
        Error("malloc failed");
219
    }
220
 
221
    conv420to422(src[1],u422);
222
    conv420to422(src[2],v422);
223
  }
224
 
225
  strcat(outname,".SIF");
226
 
227
  if (!Quiet_Flag)
228
    fprintf(stderr,"saving %s\n",outname);
229
 
230
  if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
231
  {
232
    sprintf(Error_Text,"Couldn't create %s\n",outname);
233
    Error(Error_Text);
234
  }
235
 
236
  optr = obfr;
237
 
238
  for (i=0; i<height; i++)
239
  {
240
    py = src[0] + offset + incr*i;
241
    pu = u422 + (offset>>1) + (incr>>1)*i;
242
    pv = v422 + (offset>>1) + (incr>>1)*i;
243
 
244
    for (j=0; j<horizontal_size; j+=2)
245
    {
246
      putbyte(*pu++);
247
      putbyte(*py++);
248
      putbyte(*pv++);
249
      putbyte(*py++);
250
    }
251
  }
252
 
253
  if (optr!=obfr)
254
    write(outfile,obfr,optr-obfr);
255
 
256
  close(outfile);
257
}
258
 
259
/*
260
 * store as PPM (PBMPLUS) or uncompressed Truevision TGA ('Targa') file
261
 */
262
static void store_ppm_tga(outname,src,offset,incr,height,tgaflag)
263
char *outname;
264
unsigned char *src[];
265
int offset, incr, height;
266
int tgaflag;
267
{
268
  int i, j, k;
269
  int y, u, v, r, g, b;
270
  int crv, cbu, cgu, cgv;
271
  unsigned char *py, *pu, *pv;
272
  static unsigned char tga24[14] = {0,0,2,0,0,0,0, 0,0,0,0,0,24,32};
273
  char header[FILENAME_LENGTH];
274
  static unsigned char *u422, *v422, *u444, *v444;
275
 
276
  if (chroma_format==CHROMA444)
277
  {
278
    u444 = src[1];
279
    v444 = src[2];
280
  }
281
  else
282
  {
283
    if (!u444)
284
    {
285
      if (chroma_format==CHROMA420)
286
      {
287
        if (!(u422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
288
                                             *Coded_Picture_Height)))
289
          Error("malloc failed");
290
        if (!(v422 = (unsigned char *)malloc((Coded_Picture_Width>>1)
291
                                             *Coded_Picture_Height)))
292
          Error("malloc failed");
293
      }
294
 
295
      if (!(u444 = (unsigned char *)malloc(Coded_Picture_Width
296
                                           *Coded_Picture_Height)))
297
        Error("malloc failed");
298
 
299
      if (!(v444 = (unsigned char *)malloc(Coded_Picture_Width
300
                                           *Coded_Picture_Height)))
301
        Error("malloc failed");
302
    }
303
 
304
    if (chroma_format==CHROMA420)
305
    {
306
      conv420to422(src[1],u422);
307
      conv420to422(src[2],v422);
308
      conv422to444(u422,u444);
309
      conv422to444(v422,v444);
310
    }
311
    else
312
    {
313
      conv422to444(src[1],u444);
314
      conv422to444(src[2],v444);
315
    }
316
  }
317
 
318
 
319
  strcat(outname,tgaflag ? ".tga" : ".ppm");
320
 
321
  if (!Quiet_Flag)
322
    fprintf(stderr,"saving %s\n",outname);
323
 
324
 
325
  if ((outfile = open(outname,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))==-1)
326
  {
327
    sprintf(Error_Text,"Couldn't create %s\n",outname);
328
    Error(Error_Text);
329
  }
330
 
331
  optr = obfr;
332
 
333
  if (tgaflag)
334
  {
335
    /* TGA header */
336
    for (i=0; i<12; i++)
337
      putbyte(tga24[i]);
338
 
339
    putword(horizontal_size); putword(height);
340
    putbyte(tga24[12]); putbyte(tga24[13]);
341
  }
342
  else
343
  {
344
    /* PPM header */
345
    sprintf(header,"P3\n%d %d\n255\n",horizontal_size,height);
346
 
347
    for (k=0; header[k]!=0; k++)
348
      putbyte(header[k]);
349
  }
350
 
351
  /* matrix coefficients */
352
  crv = Inverse_Table_6_9[matrix_coefficients][0];
353
  cbu = Inverse_Table_6_9[matrix_coefficients][1];
354
  cgu = Inverse_Table_6_9[matrix_coefficients][2];
355
  cgv = Inverse_Table_6_9[matrix_coefficients][3];
356
 
357
  for (i=0; i<height; i++)
358
  {
359
    py = src[0] + offset + incr*i;
360
    pu = u444 + offset + incr*i;
361
    pv = v444 + offset + incr*i;
362
 
363
    for (j=0; j<horizontal_size; j++)
364
    {
365
#ifdef TRACE
366
          if (Trace_Flag)
367
          {
368
            unsigned int qu, qv, qy;
369
            qu = *pu; qv = *pv; qy = *py;
370
            printf("yuv[ %i %i ] = %u %u %u\n", i, j, qy, qu, qv);
371
          }
372
#endif /* TRACE */
373
 
374
      u = *pu++ - 128;
375
      v = *pv++ - 128;
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];
380
 
381
#ifdef TRACE
382
          if (Trace_Flag)
383
          {
384
            printf("rgb[ %i %i ] = %i %i %i\n", i, j, r, g, b);
385
          }
386
#endif /* TRACE */
387
 
388
      if (tgaflag)
389
      {
390
        putbyte(b); putbyte(g); putbyte(r);
391
      }
392
      else
393
      {
394
        sprintf(header,"%i %i %i ",r, g, b);
395
 
396
        for (k=0; header[k]!=0; k++)
397
          putbyte(header[k]);
398
      }
399
    }
400
    if (tgaflag)
401
    {
402
      for (j = 0; j < 1024 - horizontal_size; j++)
403
        { putword(0); putword(0); }
404
    }
405
      else
406
    {
407
      sprintf(header,"\n");
408
 
409
      for (k=0; header[k]!=0; k++)
410
        putbyte(header[k]);
411
    }
412
  }
413
 
414
#if 0
415
  if (optr!=obfr)
416
    write(outfile,obfr,optr-obfr);
417
#endif
418
 
419
  close(outfile);
420
}
421
 
422
static void putbyte(c)
423
int c;
424
{
425
  *optr++ = c;
426
 
427
  if (optr == obfr+OBFRSIZE)
428
  {
429
    write(outfile,obfr,OBFRSIZE);
430
    optr = obfr;
431
  }
432
}
433
 
434
static void putword(w)
435
int w;
436
{
437
  putbyte(w); putbyte(w>>8);
438
}
439
 
440
/* horizontal 1:2 interpolation filter */
441
void conv422to444(src,dst)
442
unsigned char *src,*dst;
443
{
444
  int i, i2, w, j, im3, im2, im1, ip1, ip2, ip3;
445
 
446
  w = Coded_Picture_Width>>1;
447
 
448
  if (base.MPEG2_Flag)
449
  {
450
    for (j=0; j<Coded_Picture_Height; j++)
451
    {
452
      for (i=0; i<w; i++)
453
      {
454
        i2 = i<<1;
455
        im2 = (i<2) ? 0 : i-2;
456
        im1 = (i<1) ? 0 : i-1;
457
        ip1 = (i<w-1) ? i+1 : w-1;
458
        ip2 = (i<w-2) ? i+2 : w-1;
459
        ip3 = (i<w-3) ? i+3 : w-1;
460
 
461
        /* FIR filter coefficients (*256): 21 0 -52 0 159 256 159 0 -52 0 21 */
462
        /* even samples (0 0 256 0 0) */
463
        dst[i2] = src[i];
464
 
465
        /* odd samples (21 -52 159 159 -52 21) */
466
        dst[i2+1] = Clip[(int)(21*(src[im2]+src[ip3])
467
                        -52*(src[im1]+src[ip2])
468
                       +159*(src[i]+src[ip1])+128)>>8];
469
      }
470
      src+= w;
471
      dst+= Coded_Picture_Width;
472
    }
473
  }
474
  else
475
  {
476
    for (j=0; j<Coded_Picture_Height; j++)
477
    {
478
      for (i=0; i<w; i++)
479
      {
480
 
481
        i2 = i<<1;
482
        im3 = (i<3) ? 0 : i-3;
483
        im2 = (i<2) ? 0 : i-2;
484
        im1 = (i<1) ? 0 : i-1;
485
        ip1 = (i<w-1) ? i+1 : w-1;
486
        ip2 = (i<w-2) ? i+2 : w-1;
487
        ip3 = (i<w-3) ? i+3 : w-1;
488
 
489
        /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
490
        dst[i2] =   Clip[(int)(  5*src[im3]
491
                         -21*src[im2]
492
                         +70*src[im1]
493
                        +228*src[i]
494
                         -37*src[ip1]
495
                         +11*src[ip2]+128)>>8];
496
 
497
       dst[i2+1] = Clip[(int)(  5*src[ip3]
498
                         -21*src[ip2]
499
                         +70*src[ip1]
500
                        +228*src[i]
501
                         -37*src[im1]
502
                         +11*src[im2]+128)>>8];
503
      }
504
      src+= w;
505
      dst+= Coded_Picture_Width;
506
    }
507
  }
508
}
509
 
510
/* vertical 1:2 interpolation filter */
511
void conv420to422(src,dst)
512
unsigned char *src,*dst;
513
{
514
  int w, h, i, j, j2;
515
  int jm6, jm5, jm4, jm3, jm2, jm1, jp1, jp2, jp3, jp4, jp5, jp6, jp7;
516
 
517
  w = Coded_Picture_Width>>1;
518
  h = Coded_Picture_Height>>1;
519
 
520
  if (progressive_frame)
521
  {
522
    /* intra frame */
523
    for (i=0; i<w; i++)
524
    {
525
      for (j=0; j<h; j++)
526
      {
527
        j2 = j<<1;
528
        jm3 = (j<3) ? 0 : j-3;
529
        jm2 = (j<2) ? 0 : j-2;
530
        jm1 = (j<1) ? 0 : j-1;
531
        jp1 = (j<h-1) ? j+1 : h-1;
532
        jp2 = (j<h-2) ? j+2 : h-1;
533
        jp3 = (j<h-3) ? j+3 : h-1;
534
 
535
        /* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
536
        /* New FIR filter coefficients (*256): 3 -16 67 227 -32 7 */
537
        dst[w*j2] =     Clip[(int)(  3*src[w*jm3]
538
                             -16*src[w*jm2]
539
                             +67*src[w*jm1]
540
                            +227*src[w*j]
541
                             -32*src[w*jp1]
542
                             +7*src[w*jp2]+128)>>8];
543
 
544
        dst[w*(j2+1)] = Clip[(int)(  3*src[w*jp3]
545
                             -16*src[w*jp2]
546
                             +67*src[w*jp1]
547
                            +227*src[w*j]
548
                             -32*src[w*jm1]
549
                             +7*src[w*jm2]+128)>>8];
550
      }
551
      src++;
552
      dst++;
553
    }
554
  }
555
  else
556
  {
557
    /* intra field */
558
    for (i=0; i<w; i++)
559
    {
560
      for (j=0; j<h; j+=2)
561
      {
562
        j2 = j<<1;
563
 
564
        /* top field */
565
        jm6 = (j<6) ? 0 : j-6;
566
        jm4 = (j<4) ? 0 : j-4;
567
        jm2 = (j<2) ? 0 : j-2;
568
        jp2 = (j<h-2) ? j+2 : h-2;
569
        jp4 = (j<h-4) ? j+4 : h-2;
570
        jp6 = (j<h-6) ? j+6 : h-2;
571
 
572
        /* Polyphase FIR filter coefficients (*256): 2 -10 35 242 -18 5 */
573
        /* New polyphase FIR filter coefficients (*256): 1 -7 30 248 -21 5 */
574
        dst[w*j2] = Clip[(int)(  1*src[w*jm6]
575
                         -7*src[w*jm4]
576
                         +30*src[w*jm2]
577
                        +248*src[w*j]
578
                         -21*src[w*jp2]
579
                          +5*src[w*jp4]+128)>>8];
580
 
581
        /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
582
        /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
583
        dst[w*(j2+2)] = Clip[(int)( 7*src[w*jm4]
584
                             -35*src[w*jm2]
585
                            +194*src[w*j]
586
                            +110*src[w*jp2]
587
                             -24*src[w*jp4]
588
                              +4*src[w*jp6]+128)>>8];
589
 
590
        /* bottom field */
591
        jm5 = (j<5) ? 1 : j-5;
592
        jm3 = (j<3) ? 1 : j-3;
593
        jm1 = (j<1) ? 1 : j-1;
594
        jp1 = (j<h-1) ? j+1 : h-1;
595
        jp3 = (j<h-3) ? j+3 : h-1;
596
        jp5 = (j<h-5) ? j+5 : h-1;
597
        jp7 = (j<h-7) ? j+7 : h-1;
598
 
599
        /* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
600
        /* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
601
        dst[w*(j2+1)] = Clip[(int)( 7*src[w*jp5]
602
                             -35*src[w*jp3]
603
                            +194*src[w*jp1]
604
                            +110*src[w*jm1]
605
                             -24*src[w*jm3]
606
                              +4*src[w*jm5]+128)>>8];
607
 
608
        dst[w*(j2+3)] = Clip[(int)(  1*src[w*jp7]
609
                             -7*src[w*jp5]
610
                             +30*src[w*jp3]
611
                            +248*src[w*jp1]
612
                             -21*src[w*jm1]
613
                              +5*src[w*jm3]+128)>>8];
614
      }
615
      src++;
616
      dst++;
617
    }
618
  }
619
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.