File: putmpg.c

package info (click to toggle)
vtk 5.0.2-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 51,080 kB
  • ctags: 67,442
  • sloc: cpp: 522,627; ansic: 221,292; tcl: 43,377; python: 14,072; perl: 3,102; java: 1,436; yacc: 1,033; sh: 469; lex: 248; makefile: 181; asm: 154
file content (144 lines) | stat: -rw-r--r-- 4,363 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* putmpg.c, block and motion vector encoding routines                      */

/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */

/*
 * Disclaimer of Warranty
 *
 * These software programs are available to the user without any license fee or
 * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
 * any and all warranties, whether express, implied, or statuary, including any
 * implied warranties or merchantability or of fitness for a particular
 * purpose.  In no event shall the copyright-holder be liable for any
 * incidental, punitive, or consequential damages of any kind whatsoever
 * arising from the use of these programs.
 *
 * This disclaimer of warranty extends to the user of these programs and user's
 * customers, employees, agents, transferees, successors, and assigns.
 *
 * The MPEG Software Simulation Group does not represent or warrant that the
 * programs furnished hereunder are free of infringement of any third-party
 * patents.
 *
 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
 * are subject to royalty fees to patent holders.  Many of these patents are
 * general enough such that they are unavoidable regardless of implementation
 * design.
 *
 */

#include <stdio.h>
#include "mpeg2enc_config.h"
#include "mpeg2enc_global.h"

/* generate variable length codes for an intra-coded block (6.2.6, 6.3.17) */
void MPEG2_putintrablk(blk,cc,mpeg2_struct)
short *blk;
int cc;
struct MPEG2_structure *mpeg2_struct;
{
  int n, dct_diff, run, signed_level;

  /* DC coefficient (7.2.1) */
  dct_diff = blk[0] - mpeg2_struct->dc_dct_pred[cc]; /* difference to previous block */
  mpeg2_struct->dc_dct_pred[cc] = blk[0];

  if (cc==0)
    MPEG2_putDClum(dct_diff,mpeg2_struct);
  else
    MPEG2_putDCchrom(dct_diff,mpeg2_struct);

  /* AC coefficients (7.2.2) */
  run = 0;
  for (n=1; n<64; n++)
  {
    /* use appropriate entropy scanning pattern */
    signed_level = blk[(mpeg2_struct->altscan ? MPEG2_alternate_scan : MPEG2_zig_zag_scan)[n]];
    if (signed_level!=0)
    {
      MPEG2_putAC(run,signed_level,mpeg2_struct->intravlc,mpeg2_struct);
      run = 0;
    }
    else
      run++; /* count zero coefficients */
  }

  /* End of Block -- normative block punctuation */
  if (mpeg2_struct->intravlc)
    MPEG2_putbits(6,4,mpeg2_struct); /* 0110 (Table B-15) */
  else
    MPEG2_putbits(2,2,mpeg2_struct); /* 10 (Table B-14) */
}

/* generate variable length codes for a non-intra-coded block (6.2.6, 6.3.17) */
void MPEG2_putnonintrablk(blk,mpeg2_struct)
short *blk;
struct MPEG2_structure *mpeg2_struct;
{
  int n, run, signed_level, first;

  run = 0;
  first = 1;

  for (n=0; n<64; n++)
  {
    /* use appropriate entropy scanning pattern */
    signed_level = blk[(mpeg2_struct->altscan ? MPEG2_alternate_scan : MPEG2_zig_zag_scan)[n]];

    if (signed_level!=0)
    {
      if (first)
      {
        /* first coefficient in non-intra block */
        MPEG2_putACfirst(run,signed_level,mpeg2_struct);
        first = 0;
      }
      else
        MPEG2_putAC(run,signed_level,0,mpeg2_struct);

      run = 0;
    }
    else
      run++; /* count zero coefficients */
  }

  /* End of Block -- normative block punctuation  */
  MPEG2_putbits(2,2,mpeg2_struct);
}

/* generate variable length code for a motion vector component (7.6.3.1) */
void MPEG2_putmv(dmv,f_code,mpeg2_struct)
int dmv,f_code;
struct MPEG2_structure *mpeg2_struct;
{
  int r_size, f, vmin, vmax, dv, temp, motion_code, motion_residual;

  r_size = f_code - 1; /* number of fixed length code ('residual') bits */
  f = 1<<r_size;
  vmin = -16*f; /* lower range limit */
  vmax = 16*f - 1; /* upper range limit */
  dv = 32*f;

  /* fold vector difference into [vmin...vmax] */
  if (dmv>vmax)
    dmv-= dv;
  else if (dmv<vmin)
    dmv+= dv;

  /* check value */
  if (dmv<vmin || dmv>vmax)
    if (!mpeg2_struct->quiet)
      fprintf(stderr,"invalid motion vector\n");

  /* split dmv into motion_code and motion_residual */
  temp = ((dmv<0) ? -dmv : dmv) + f - 1;
  motion_code = temp>>r_size;
  if (dmv<0)
    motion_code = -motion_code;
  motion_residual = temp & (f-1);

  MPEG2_putmotioncode(motion_code,mpeg2_struct); /* variable length code */

  if (r_size!=0 && motion_code!=0)
    MPEG2_putbits(motion_residual,r_size,mpeg2_struct); /* fixed length code */
}