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