~ubuntu-branches/ubuntu/gutsy/vtk/gutsy

« back to all changes in this revision

Viewing changes to Utilities/vtkmpeg2encode/conform.c

  • Committer: Bazaar Package Importer
  • Author(s): Michele Angrisano
  • Date: 2007-06-30 22:39:48 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070630223948-6tn51upaurwfrcz8
Tags: 5.0.3-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add export SHELL=/bin/bash in debian/rules.
  - Update maintainer in field debian/control.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* conform.c, conformance checks                                            */
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
 
 
33
 
#include "mpeg2enc_config.h"
34
 
#include "mpeg2enc_global.h"
35
 
 
36
 
/* check for (level independent) parameter limits */
37
 
VTK_MPEG2ENC_EXPORT void MPEG2_range_checks(mpeg2_struct)
38
 
  struct MPEG2_structure *mpeg2_struct;
39
 
{
40
 
  int i;
41
 
 
42
 
  /* range and value checks */
43
 
 
44
 
  if (mpeg2_struct->horizontal_size<1 || mpeg2_struct->horizontal_size>16383)
45
 
    (*(mpeg2_struct->report_error))("horizontal_size must be between 1 and 16383");
46
 
  if (mpeg2_struct->mpeg1 && mpeg2_struct->horizontal_size>4095)
47
 
    (*(mpeg2_struct->report_error))("horizontal_size must be less than 4096 (MPEG-1)");
48
 
  if ((mpeg2_struct->horizontal_size&4095)==0)
49
 
    (*(mpeg2_struct->report_error))("horizontal_size must not be a multiple of 4096");
50
 
  if (mpeg2_struct->chroma_format!=CHROMA444 && mpeg2_struct->horizontal_size%2 != 0)
51
 
    (*(mpeg2_struct->report_error))("horizontal_size must be a even (4:2:0 / 4:2:2)");
52
 
 
53
 
  if (mpeg2_struct->vertical_size<1 || mpeg2_struct->vertical_size>16383)
54
 
    (*(mpeg2_struct->report_error))("vertical_size must be between 1 and 16383");
55
 
  if (mpeg2_struct->mpeg1 && mpeg2_struct->vertical_size>4095)
56
 
    (*(mpeg2_struct->report_error))("vertical size must be less than 4096 (MPEG-1)");
57
 
  if ((mpeg2_struct->vertical_size&4095)==0)
58
 
    (*(mpeg2_struct->report_error))("vertical_size must not be a multiple of 4096");
59
 
  if (mpeg2_struct->chroma_format==CHROMA420 && mpeg2_struct->vertical_size%2 != 0)
60
 
    (*(mpeg2_struct->report_error))("vertical_size must be a even (4:2:0)");
61
 
  if(mpeg2_struct->fieldpic)
62
 
  {
63
 
    if (mpeg2_struct->vertical_size%2 != 0)
64
 
      (*(mpeg2_struct->report_error))("vertical_size must be a even (field pictures)");
65
 
    if (mpeg2_struct->chroma_format==CHROMA420 && mpeg2_struct->vertical_size%4 != 0)
66
 
      (*(mpeg2_struct->report_error))("vertical_size must be a multiple of 4 (4:2:0 field pictures)");
67
 
  }
68
 
 
69
 
  if (mpeg2_struct->mpeg1)
70
 
  {
71
 
    if (mpeg2_struct->aspectratio<1 || mpeg2_struct->aspectratio>14)
72
 
      (*(mpeg2_struct->report_error))("pel_aspect_ratio must be between 1 and 14 (MPEG-1)");
73
 
  }
74
 
  else
75
 
  {
76
 
    if (mpeg2_struct->aspectratio<1 || mpeg2_struct->aspectratio>4)
77
 
      (*(mpeg2_struct->report_error))("aspect_ratio_information must be 1, 2, 3 or 4");
78
 
  }
79
 
 
80
 
  if (mpeg2_struct->frame_rate_code<1 || mpeg2_struct->frame_rate_code>8)
81
 
    (*(mpeg2_struct->report_error))("frame_rate code must be between 1 and 8");
82
 
 
83
 
  if (mpeg2_struct->bit_rate<=0.0)
84
 
    (*(mpeg2_struct->report_error))("bit_rate must be positive");
85
 
  if (mpeg2_struct->bit_rate > ((1<<30)-1)*400.0)
86
 
    (*(mpeg2_struct->report_error))("bit_rate must be less than 429 Gbit/s");
87
 
  if (mpeg2_struct->mpeg1 && mpeg2_struct->bit_rate > ((1<<18)-1)*400.0)
88
 
    (*(mpeg2_struct->report_error))("bit_rate must be less than 104 Mbit/s (MPEG-1)");
89
 
 
90
 
  if (mpeg2_struct->vbv_buffer_size<1 || mpeg2_struct->vbv_buffer_size>0x3ffff)
91
 
    (*(mpeg2_struct->report_error))("vbv_buffer_size must be in range 1..(2^18-1)");
92
 
  if (mpeg2_struct->mpeg1 && mpeg2_struct->vbv_buffer_size>=1024)
93
 
    (*(mpeg2_struct->report_error))("vbv_buffer_size must be less than 1024 (MPEG-1)");
94
 
 
95
 
  if (mpeg2_struct->chroma_format<CHROMA420 || mpeg2_struct->chroma_format>CHROMA444)
96
 
    (*(mpeg2_struct->report_error))("chroma_format must be in range 1...3");
97
 
 
98
 
  if (mpeg2_struct->video_format<0 || mpeg2_struct->video_format>4)
99
 
    (*(mpeg2_struct->report_error))("video_format must be in range 0...4");
100
 
 
101
 
  if (mpeg2_struct->color_primaries<1 || mpeg2_struct->color_primaries>7 || mpeg2_struct->color_primaries==3)
102
 
    (*(mpeg2_struct->report_error))("color_primaries must be in range 1...2 or 4...7");
103
 
 
104
 
  if (mpeg2_struct->transfer_characteristics<1 || mpeg2_struct->transfer_characteristics>7
105
 
      || mpeg2_struct->transfer_characteristics==3)
106
 
    (*(mpeg2_struct->report_error))("transfer_characteristics must be in range 1...2 or 4...7");
107
 
 
108
 
  if (mpeg2_struct->matrix_coefficients<1 || mpeg2_struct->matrix_coefficients>7 || mpeg2_struct->matrix_coefficients==3)
109
 
    (*(mpeg2_struct->report_error))("matrix_coefficients must be in range 1...2 or 4...7");
110
 
 
111
 
  if (mpeg2_struct->display_horizontal_size<0 || mpeg2_struct->display_horizontal_size>16383)
112
 
    (*(mpeg2_struct->report_error))("display_horizontal_size must be in range 0...16383");
113
 
  if (mpeg2_struct->display_vertical_size<0 || mpeg2_struct->display_vertical_size>16383)
114
 
    (*(mpeg2_struct->report_error))("display_vertical_size must be in range 0...16383");
115
 
 
116
 
  if (mpeg2_struct->dc_prec<0 || mpeg2_struct->dc_prec>3)
117
 
    (*(mpeg2_struct->report_error))("intra_dc_precision must be in range 0...3");
118
 
 
119
 
  for (i=0; i<mpeg2_struct->M_val; i++)
120
 
  {
121
 
    if (mpeg2_struct->motion_data[i].forw_hor_f_code<1 || mpeg2_struct->motion_data[i].forw_hor_f_code>9)
122
 
      (*(mpeg2_struct->report_error))("f_code must be between 1 and 9");
123
 
    if (mpeg2_struct->motion_data[i].forw_vert_f_code<1 || mpeg2_struct->motion_data[i].forw_vert_f_code>9)
124
 
      (*(mpeg2_struct->report_error))("f_code must be between 1 and 9");
125
 
    if (mpeg2_struct->mpeg1 && mpeg2_struct->motion_data[i].forw_hor_f_code>7)
126
 
      (*(mpeg2_struct->report_error))("f_code must be le less than 8");
127
 
    if (mpeg2_struct->mpeg1 && mpeg2_struct->motion_data[i].forw_vert_f_code>7)
128
 
      (*(mpeg2_struct->report_error))("f_code must be le less than 8");
129
 
    if (mpeg2_struct->motion_data[i].sxf<=0)
130
 
      (*(mpeg2_struct->report_error))("search window must be positive"); /* doesn't belong here */
131
 
    if (mpeg2_struct->motion_data[i].syf<=0)
132
 
      (*(mpeg2_struct->report_error))("search window must be positive");
133
 
    if (i!=0)
134
 
    {
135
 
      if (mpeg2_struct->motion_data[i].back_hor_f_code<1 || mpeg2_struct->motion_data[i].back_hor_f_code>9)
136
 
        (*(mpeg2_struct->report_error))("f_code must be between 1 and 9");
137
 
      if (mpeg2_struct->motion_data[i].back_vert_f_code<1 || mpeg2_struct->motion_data[i].back_vert_f_code>9)
138
 
        (*(mpeg2_struct->report_error))("f_code must be between 1 and 9");
139
 
      if (mpeg2_struct->mpeg1 && mpeg2_struct->motion_data[i].back_hor_f_code>7)
140
 
        (*(mpeg2_struct->report_error))("f_code must be le less than 8");
141
 
      if (mpeg2_struct->mpeg1 && mpeg2_struct->motion_data[i].back_vert_f_code>7)
142
 
        (*(mpeg2_struct->report_error))("f_code must be le less than 8");
143
 
      if (mpeg2_struct->motion_data[i].sxb<=0)
144
 
        (*(mpeg2_struct->report_error))("search window must be positive");
145
 
      if (mpeg2_struct->motion_data[i].syb<=0)
146
 
        (*(mpeg2_struct->report_error))("search window must be positive");
147
 
    }
148
 
  }
149
 
}
150
 
 
151
 
/* identifies valid profile / level combinations */
152
 
static char profile_level_defined[5][4] =
153
 
{
154
 
/* HL   H-14 ML   LL  */
155
 
  {1,   1,   1,   0},  /* HP   */
156
 
  {0,   1,   0,   0},  /* Spat */
157
 
  {0,   0,   1,   1},  /* SNR  */
158
 
  {1,   1,   1,   1},  /* MP   */
159
 
  {0,   0,   1,   0}   /* SP   */
160
 
};
161
 
 
162
 
static struct level_limits {
163
 
  int hor_f_code;
164
 
  int vert_f_code;
165
 
  int hor_size;
166
 
  int vert_size;
167
 
  int sample_rate;
168
 
  int bit_rate; /* Mbit/s */
169
 
  int vbv_buffer_size; /* 16384 bit steps */
170
 
} maxval_tab[4] =
171
 
{
172
 
  {9, 5, 1920, 1152, 62668800, 80, 597}, /* HL */
173
 
  {9, 5, 1440, 1152, 47001600, 60, 448}, /* H-14 */
174
 
  {8, 5,  720,  576, 10368000, 15, 112}, /* ML */
175
 
  {7, 4,  352,  288,  3041280,  4,  29}  /* LL */
176
 
};
177
 
 
178
 
#define SP   5
179
 
#define MP   4
180
 
#define SNR  3
181
 
#define SPAT 2
182
 
#define HP   1
183
 
 
184
 
#define LL  10
185
 
#define ML   8
186
 
#define H14  6
187
 
#define HL   4
188
 
 
189
 
VTK_MPEG2ENC_EXPORT void MPEG2_profile_and_level_checks(mpeg2_struct)
190
 
  struct MPEG2_structure *mpeg2_struct;
191
 
{
192
 
  int i;
193
 
  struct level_limits *maxval;
194
 
 
195
 
  if (mpeg2_struct->profile<0 || mpeg2_struct->profile>15)
196
 
    (*(mpeg2_struct->report_error))("profile must be between 0 and 15");
197
 
 
198
 
  if (mpeg2_struct->level<0 || mpeg2_struct->level>15)
199
 
    (*(mpeg2_struct->report_error))("level must be between 0 and 15");
200
 
 
201
 
  if (mpeg2_struct->profile>=8)
202
 
  {
203
 
    if (!mpeg2_struct->quiet)
204
 
      fprintf(stderr,"Warning: profile uses a reserved value, conformance checks skipped\n");
205
 
    return;
206
 
  }
207
 
 
208
 
  if (mpeg2_struct->profile<HP || mpeg2_struct->profile>SP)
209
 
    (*(mpeg2_struct->report_error))("undefined Profile");
210
 
 
211
 
  if (mpeg2_struct->profile==SNR || mpeg2_struct->profile==SPAT)
212
 
    (*(mpeg2_struct->report_error))("This encoder currently generates no scalable bitstreams");
213
 
 
214
 
  if (mpeg2_struct->level<HL || mpeg2_struct->level>LL || mpeg2_struct->level&1)
215
 
    (*(mpeg2_struct->report_error))("undefined Level");
216
 
 
217
 
  maxval = &maxval_tab[(mpeg2_struct->level-4) >> 1];
218
 
 
219
 
  /* check profile@level combination */
220
 
  if(!profile_level_defined[mpeg2_struct->profile-1][(mpeg2_struct->level-4) >> 1])
221
 
    (*(mpeg2_struct->report_error))("undefined profile@level combination");
222
 
  
223
 
 
224
 
  /* profile (syntax) constraints */
225
 
 
226
 
  if (mpeg2_struct->profile==SP && mpeg2_struct->M_val!=1)
227
 
    (*(mpeg2_struct->report_error))("Simple Profile does not allow B pictures");
228
 
 
229
 
  if (mpeg2_struct->profile!=HP && mpeg2_struct->chroma_format!=CHROMA420)
230
 
    (*(mpeg2_struct->report_error))("chroma format must be 4:2:0 in specified Profile");
231
 
 
232
 
  if (mpeg2_struct->profile==HP && mpeg2_struct->chroma_format==CHROMA444)
233
 
    (*(mpeg2_struct->report_error))("chroma format must be 4:2:0 or 4:2:2 in High Profile");
234
 
 
235
 
  if (mpeg2_struct->profile>=MP) /* SP, MP: constrained repeat_first_field */
236
 
  {
237
 
    if (mpeg2_struct->frame_rate_code<=2 && mpeg2_struct->repeatfirst)
238
 
      (*(mpeg2_struct->report_error))("repeat_first_first must be zero");
239
 
    if (mpeg2_struct->frame_rate_code<=6 && mpeg2_struct->prog_seq && mpeg2_struct->repeatfirst)
240
 
      (*(mpeg2_struct->report_error))("repeat_first_first must be zero");
241
 
  }
242
 
 
243
 
  if (mpeg2_struct->profile!=HP && mpeg2_struct->dc_prec==3)
244
 
    (*(mpeg2_struct->report_error))("11 bit DC precision only allowed in High Profile");
245
 
 
246
 
 
247
 
  /* level (parameter value) constraints */
248
 
 
249
 
  /* Table 8-8 */
250
 
  if (mpeg2_struct->frame_rate_code>5 && mpeg2_struct->level>=ML)
251
 
    (*(mpeg2_struct->report_error))("Picture rate greater than permitted in specified Level");
252
 
 
253
 
  for (i=0; i<mpeg2_struct->M_val; i++)
254
 
  {
255
 
    if (mpeg2_struct->motion_data[i].forw_hor_f_code > maxval->hor_f_code)
256
 
      (*(mpeg2_struct->report_error))("forward horizontal f_code greater than permitted in specified Level");
257
 
 
258
 
    if (mpeg2_struct->motion_data[i].forw_vert_f_code > maxval->vert_f_code)
259
 
      (*(mpeg2_struct->report_error))("forward vertical f_code greater than permitted in specified Level");
260
 
 
261
 
    if (i!=0)
262
 
    {
263
 
      if (mpeg2_struct->motion_data[i].back_hor_f_code > maxval->hor_f_code)
264
 
        (*(mpeg2_struct->report_error))("backward horizontal f_code greater than permitted in specified Level");
265
 
  
266
 
      if (mpeg2_struct->motion_data[i].back_vert_f_code > maxval->vert_f_code)
267
 
        (*(mpeg2_struct->report_error))("backward vertical f_code greater than permitted in specified Level");
268
 
    }
269
 
  }
270
 
 
271
 
  /* Table 8-10 */
272
 
  if (mpeg2_struct->horizontal_size > maxval->hor_size)
273
 
    (*(mpeg2_struct->report_error))("Horizontal size is greater than permitted in specified Level");
274
 
 
275
 
  if (mpeg2_struct->vertical_size > maxval->vert_size)
276
 
    (*(mpeg2_struct->report_error))("Horizontal size is greater than permitted in specified Level");
277
 
 
278
 
  /* Table 8-11 */
279
 
  if (mpeg2_struct->horizontal_size*mpeg2_struct->vertical_size*mpeg2_struct->frame_rate > maxval->sample_rate)
280
 
    (*(mpeg2_struct->report_error))("Sample rate is greater than permitted in specified Level");
281
 
 
282
 
  /* Table 8-12 */
283
 
  if (mpeg2_struct->bit_rate> 1.0e6 * maxval->bit_rate)
284
 
    (*(mpeg2_struct->report_error))("Bit rate is greater than permitted in specified Level");
285
 
 
286
 
  /* Table 8-13 */
287
 
  if (mpeg2_struct->vbv_buffer_size > maxval->vbv_buffer_size)
288
 
    (*(mpeg2_struct->report_error))("vbv_buffer_size exceeds High Level limit");
289
 
}