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