1 /* ***** BEGIN LICENSE BLOCK *****
2 *
3 * $Id: video_format_defaults.cpp,v 1.36 2008/10/20 04:21:45 asuraparaju Exp $ $Name: Dirac_1_0_2 $
4 *
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 *
7 * The contents of this file are subject to the Mozilla Public License
8 * Version 1.1 (the "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14 * the specific language governing rights and limitations under the License.
15 *
16 * The Original Code is BBC Research and Development code.
17 *
18 * The Initial Developer of the Original Code is the British Broadcasting
19 * Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2004.
21 * All Rights Reserved.
22 *
23 * Contributor(s): Andrew Kennedy (Original Author).
24 *                 Anuradha Suraparaju
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
28 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
29 * the GPL or the LGPL are applicable instead of those above. If you wish to
30 * allow use of your version of this file only under the terms of the either
31 * the GPL or LGPL and not to allow others to use your version of this file
32 * under the MPL, indicate your decision by deleting the provisions above
33 * and replace them with the notice and other provisions required by the GPL
34 * or LGPL. If you do not delete the provisions above, a recipient may use
35 * your version of this file under the terms of any one of the MPL, the GPL
36 * or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
38 
39 #include <sstream>
40 #include <libdirac_common/video_format_defaults.h>
41 #include <libdirac_common/dirac_exception.h>
42 
43 using namespace dirac;
44 
45 namespace dirac
46 {
SetDefaultCodecParameters(CodecParams & cparams,PictureType ptype,unsigned int num_refs)47 void SetDefaultCodecParameters(CodecParams &cparams,
48                                PictureType ptype,
49                                unsigned int num_refs)
50 {
51     std::ostringstream errstr;
52     // Transform parameters
53     cparams.SetZeroTransform(false);
54     cparams.SetTransformDepth(4);
55     WltFilter wf;
56     SetDefaultTransformFilter(ptype, cparams.GetVideoFormat(), wf);
57     cparams.SetTransformFilter(wf);
58     cparams.SetCodeBlockMode(QUANT_SINGLE);
59     cparams.SetSpatialPartition(false);
60 
61     // Default is set to progressive specified irrespective
62     // of whether the source material is interlaced or progressive.
63     // Overridden from command line of encoder or in bytestream for decoder.
64     cparams.SetPictureCodingMode(0);
65     cparams.SetTopFieldFirst(true);
66     switch (cparams.GetVideoFormat())
67     {
68     case VIDEO_FORMAT_QSIF525:
69     case VIDEO_FORMAT_QCIF:
70     case VIDEO_FORMAT_CUSTOM:
71     case VIDEO_FORMAT_SIF525:
72     case VIDEO_FORMAT_CIF:
73     case VIDEO_FORMAT_4CIF:
74     case VIDEO_FORMAT_4SIF525:
75     case VIDEO_FORMAT_SD_480I60:
76     case VIDEO_FORMAT_SD_576I50:
77     case VIDEO_FORMAT_HD_720P60:
78     case VIDEO_FORMAT_HD_720P50:
79     case VIDEO_FORMAT_HD_1080I60:
80     case VIDEO_FORMAT_HD_1080I50:
81     case VIDEO_FORMAT_HD_1080P60:
82     case VIDEO_FORMAT_HD_1080P50:
83     case VIDEO_FORMAT_DIGI_CINEMA_2K24:
84     case VIDEO_FORMAT_DIGI_CINEMA_4K24:
85     case VIDEO_FORMAT_UHDTV_4K60:
86     case VIDEO_FORMAT_UHDTV_4K50:
87     case VIDEO_FORMAT_UHDTV_8K60:
88     case VIDEO_FORMAT_UHDTV_8K50:
89         cparams.SetSpatialPartition(true);
90         break;
91     default:
92         errstr << "Unsupported video format " << cparams.GetVideoFormat()
93                << std::endl;
94         DIRAC_THROW_EXCEPTION(
95             ERR_INVALID_VIDEO_FORMAT,
96             errstr.str(),
97             SEVERITY_PICTURE_ERROR);
98         break;
99     }
100 
101     if (ptype == INTER_PICTURE)
102     {
103         ASSERTM (num_refs > 0 && num_refs < 3, "Number of reference frames should be 1 or 2 fo INTER frames" );
104         OLBParams bparams;
105 	PicturePredParams& predparams = cparams.GetPicPredParams();
106         predparams.SetUsingGlobalMotion(false);
107         SetDefaultBlockParameters(bparams, cparams.GetVideoFormat());
108         predparams.SetLumaBlockParams(bparams);
109         predparams.SetMVPrecision(MV_PRECISION_HALF_PIXEL);
110         // NOTE: FIXME - need to add global motion params here
111         predparams.SetPictureWeightsPrecision(1);
112         predparams.SetRef1Weight(1);
113         predparams.SetRef2Weight(1);
114         cparams.SetPictureCodingMode(0);
115     }
116 }
117 
SetDefaultSourceParameters(const VideoFormat & vf,SourceParams & sparams)118 void SetDefaultSourceParameters(const VideoFormat &vf, SourceParams& sparams)
119 {
120     std::ostringstream errstr;
121     sparams.SetVideoFormat(vf);
122     sparams.SetCFormat(format420);
123     sparams.SetSourceSampling(0);
124     sparams.SetTopFieldFirst(true);
125     sparams.SetPixelAspectRatio(PIXEL_ASPECT_RATIO_1_1);
126     sparams.SetSignalRange(SIGNAL_RANGE_8BIT_FULL);
127     sparams.SetLeftOffset(0);
128     sparams.SetTopOffset(0);
129     sparams.SetColourSpecification(1);
130 
131     switch (vf)
132     {
133     case VIDEO_FORMAT_CUSTOM:
134         sparams.SetXl(640);
135         sparams.SetYl(480);
136         sparams.SetTopFieldFirst(false);
137         sparams.SetFrameRate(FRAMERATE_23p97_FPS);
138         sparams.SetCleanWidth(640);
139         sparams.SetCleanHeight(480);
140         sparams.SetColourSpecification(0);
141         break;
142     case VIDEO_FORMAT_QSIF525:
143         sparams.SetXl(176);
144         sparams.SetYl(120);
145         sparams.SetTopFieldFirst(false);
146         sparams.SetFrameRate(FRAMERATE_14p98_FPS);
147         sparams.SetPixelAspectRatio(PIXEL_ASPECT_RATIO_10_11);
148         sparams.SetCleanWidth(176);
149         sparams.SetCleanHeight(120);
150         sparams.SetColourSpecification(1);
151         break;
152     case VIDEO_FORMAT_QCIF:
153         sparams.SetXl(176);
154         sparams.SetYl(144);
155         sparams.SetFrameRate(FRAMERATE_12p5_FPS);
156         sparams.SetPixelAspectRatio(PIXEL_ASPECT_RATIO_12_11);
157         sparams.SetCleanWidth(176);
158         sparams.SetCleanHeight(144);
159         sparams.SetColourSpecification(2);
160         break;
161     case VIDEO_FORMAT_SIF525:
162         sparams.SetXl(352);
163         sparams.SetYl(240);
164         sparams.SetTopFieldFirst(false);
165         sparams.SetFrameRate(FRAMERATE_14p98_FPS);
166         sparams.SetPixelAspectRatio(PIXEL_ASPECT_RATIO_10_11);
167         sparams.SetCleanWidth(352);
168         sparams.SetCleanHeight(240);
169         sparams.SetColourSpecification(1);
170         break;
171     case VIDEO_FORMAT_CIF:
172         sparams.SetXl(352);
173         sparams.SetYl(288);
174         sparams.SetFrameRate(FRAMERATE_12p5_FPS);
175         sparams.SetPixelAspectRatio(PIXEL_ASPECT_RATIO_12_11);
176         sparams.SetCleanWidth(352);
177         sparams.SetCleanHeight(288);
178         sparams.SetColourSpecification(2);
179         break;
180     case VIDEO_FORMAT_4SIF525:
181         sparams.SetXl(704);
182         sparams.SetYl(480);
183         sparams.SetTopFieldFirst(false);
184         sparams.SetFrameRate(FRAMERATE_14p98_FPS);
185         sparams.SetPixelAspectRatio(PIXEL_ASPECT_RATIO_10_11);
186         sparams.SetCleanWidth(704);
187         sparams.SetCleanHeight(480);
188         sparams.SetColourSpecification(1);
189         break;
190     case VIDEO_FORMAT_4CIF:
191         sparams.SetXl(704);
192         sparams.SetYl(576);
193         sparams.SetFrameRate(FRAMERATE_12p5_FPS);
194         sparams.SetPixelAspectRatio(PIXEL_ASPECT_RATIO_12_11);
195         sparams.SetCleanWidth(704);
196         sparams.SetCleanHeight(576);
197         sparams.SetColourSpecification(2);
198         break;
199     case VIDEO_FORMAT_SD_480I60:
200         sparams.SetXl(720);
201         sparams.SetYl(480);
202         sparams.SetCFormat(format422);
203         sparams.SetSourceSampling(1);
204         sparams.SetTopFieldFirst(false);
205         sparams.SetFrameRate(FRAMERATE_29p97_FPS);
206         sparams.SetPixelAspectRatio(PIXEL_ASPECT_RATIO_10_11);
207         sparams.SetCleanWidth(704);
208         sparams.SetCleanHeight(480);
209         sparams.SetLeftOffset(8);
210         sparams.SetSignalRange(SIGNAL_RANGE_10BIT_VIDEO);
211         sparams.SetColourSpecification(1);
212         break;
213     case VIDEO_FORMAT_SD_576I50:
214         sparams.SetXl(720);
215         sparams.SetYl(576);
216         sparams.SetCFormat(format422);
217         sparams.SetSourceSampling(1);
218         sparams.SetFrameRate(FRAMERATE_25_FPS);
219         sparams.SetPixelAspectRatio(PIXEL_ASPECT_RATIO_12_11);
220         sparams.SetCleanWidth(704);
221         sparams.SetCleanHeight(576);
222         sparams.SetLeftOffset(8);
223         sparams.SetSignalRange(SIGNAL_RANGE_10BIT_VIDEO);
224         sparams.SetColourSpecification(2);
225         break;
226     case VIDEO_FORMAT_HD_720P50:
227     case VIDEO_FORMAT_HD_720P60:
228         sparams.SetXl(1280);
229         sparams.SetYl(720);
230         sparams.SetCFormat(format422);
231         if (vf == VIDEO_FORMAT_HD_720P50)
232             sparams.SetFrameRate(FRAMERATE_50_FPS);
233         else
234             sparams.SetFrameRate(FRAMERATE_59p94_FPS);
235         sparams.SetCleanWidth(1280);
236         sparams.SetCleanHeight(720);
237         sparams.SetSignalRange(SIGNAL_RANGE_10BIT_VIDEO);
238         sparams.SetColourSpecification(3);
239         break;
240     case VIDEO_FORMAT_HD_1080I60:
241     case VIDEO_FORMAT_HD_1080I50:
242     case VIDEO_FORMAT_HD_1080P60:
243     case VIDEO_FORMAT_HD_1080P50:
244         sparams.SetXl(1920);
245         sparams.SetYl(1080);
246         sparams.SetCFormat(format422);
247         switch (vf)
248         {
249         case VIDEO_FORMAT_HD_1080I60:
250             sparams.SetSourceSampling(1);
251             sparams.SetFrameRate(FRAMERATE_29p97_FPS);
252             break;
253         case VIDEO_FORMAT_HD_1080I50:
254             sparams.SetSourceSampling(1);
255             sparams.SetFrameRate(FRAMERATE_25_FPS);
256             break;
257         case VIDEO_FORMAT_HD_1080P60:
258             sparams.SetFrameRate(FRAMERATE_59p94_FPS);
259             break;
260         case VIDEO_FORMAT_HD_1080P50:
261             sparams.SetFrameRate(FRAMERATE_50_FPS);
262             break;
263         default:
264             break;
265         }
266         sparams.SetSignalRange(SIGNAL_RANGE_10BIT_VIDEO);
267         sparams.SetCleanWidth(1920);
268         sparams.SetCleanHeight(1080);
269         sparams.SetColourSpecification(3);
270         break;
271     case VIDEO_FORMAT_DIGI_CINEMA_2K24:
272         sparams.SetXl(2048);
273         sparams.SetYl(1080);
274         sparams.SetCFormat(format444);
275         sparams.SetFrameRate(FRAMERATE_24_FPS);
276         sparams.SetCleanWidth(2048);
277         sparams.SetCleanHeight(1080);
278         sparams.SetSignalRange(SIGNAL_RANGE_12BIT_VIDEO);
279         sparams.SetColourSpecification(4);
280         break;
281     case VIDEO_FORMAT_DIGI_CINEMA_4K24:
282         sparams.SetXl(4096);
283         sparams.SetYl(2160);
284         sparams.SetCFormat(format444);
285         sparams.SetFrameRate(FRAMERATE_24_FPS);
286         sparams.SetCleanWidth(4096);
287         sparams.SetCleanHeight(2160);
288         sparams.SetSignalRange(SIGNAL_RANGE_12BIT_VIDEO);
289         sparams.SetColourSpecification(4);
290         break;
291     case VIDEO_FORMAT_UHDTV_4K60:
292     case VIDEO_FORMAT_UHDTV_4K50:
293         sparams.SetXl(3840);
294         sparams.SetYl(2160);
295         sparams.SetCFormat(format422);
296         sparams.SetSourceSampling(0);
297         switch (vf)
298         {
299         case VIDEO_FORMAT_UHDTV_4K60:
300             sparams.SetFrameRate(FRAMERATE_59p94_FPS);
301             break;
302         case VIDEO_FORMAT_UHDTV_4K50:
303             sparams.SetFrameRate(FRAMERATE_50_FPS);
304             break;
305         default:
306             break;
307         }
308         sparams.SetSignalRange(SIGNAL_RANGE_10BIT_VIDEO);
309         sparams.SetCleanWidth(3840);
310         sparams.SetCleanHeight(2160);
311         sparams.SetColourSpecification(3);
312         break;
313     case VIDEO_FORMAT_UHDTV_8K60:
314     case VIDEO_FORMAT_UHDTV_8K50:
315         sparams.SetXl(7680);
316         sparams.SetYl(4320);
317         sparams.SetCFormat(format422);
318         sparams.SetSourceSampling(0);
319         switch (vf)
320         {
321         case VIDEO_FORMAT_UHDTV_8K60:
322             sparams.SetFrameRate(FRAMERATE_59p94_FPS);
323             break;
324         case VIDEO_FORMAT_UHDTV_8K50:
325             sparams.SetFrameRate(FRAMERATE_50_FPS);
326             break;
327         default:
328             break;
329         }
330         sparams.SetSignalRange(SIGNAL_RANGE_10BIT_VIDEO);
331         sparams.SetCleanWidth(7680);
332         sparams.SetCleanHeight(4320);
333         sparams.SetColourSpecification(3);
334         break;
335    default:
336         errstr << "Unsupported video format " << sparams.GetVideoFormat()
337                << std::endl;
338         DIRAC_THROW_EXCEPTION(
339             ERR_INVALID_VIDEO_FORMAT,
340             errstr.str(),
341             SEVERITY_PICTURE_ERROR);
342         break;
343     }
344 }
345 
SetDefaultEncoderParameters(EncoderParams & encparams)346 void SetDefaultEncoderParameters(EncoderParams& encparams)
347 {
348     encparams.SetLossless(false);
349     encparams.SetQf(5.5f);
350     encparams.GetPicPredParams().SetMVPrecision(MV_PRECISION_HALF_PIXEL);
351     encparams.SetUsingAC(true);
352 
353     switch (encparams.GetVideoFormat())
354     {
355     case VIDEO_FORMAT_4SIF525:
356     case VIDEO_FORMAT_4CIF:
357     case VIDEO_FORMAT_SD_480I60:
358     case VIDEO_FORMAT_SD_576I50:
359         encparams.SetL1Sep(3);
360         encparams.SetNumL1(7);
361         encparams.SetCPD(32.0f);
362         break;
363 
364     case VIDEO_FORMAT_HD_720P60:
365     case VIDEO_FORMAT_HD_720P50:
366         encparams.SetL1Sep(3);
367         encparams.SetNumL1(15);
368         encparams.SetCPD(20.0f);
369         break;
370 
371     case VIDEO_FORMAT_HD_1080I60:
372     case VIDEO_FORMAT_HD_1080I50:
373     case VIDEO_FORMAT_HD_1080P60:
374     case VIDEO_FORMAT_HD_1080P50:
375         encparams.SetL1Sep(3);
376         encparams.SetNumL1(7);
377         encparams.SetCPD(32.0f);
378         break;
379     case VIDEO_FORMAT_UHDTV_4K60:
380     case VIDEO_FORMAT_UHDTV_4K50:
381     case VIDEO_FORMAT_UHDTV_8K60:
382     case VIDEO_FORMAT_UHDTV_8K50:
383         encparams.SetL1Sep(6);
384         encparams.SetNumL1(7);
385         encparams.SetCPD(48.0f);
386         break;
387     case VIDEO_FORMAT_CIF:
388     default:
389         encparams.SetL1Sep(3);
390         encparams.SetNumL1(19);
391         encparams.SetCPD(20.0f);
392         break;
393     }
394 
395 }
396 
SetDefaultBlockParameters(OLBParams & bparams,const VideoFormat & video_format)397 void SetDefaultBlockParameters(OLBParams& bparams,
398                                const VideoFormat& video_format)
399 {
400     switch (video_format)
401     {
402     case VIDEO_FORMAT_QCIF:
403     case VIDEO_FORMAT_QSIF525:
404     case VIDEO_FORMAT_CUSTOM:
405     case VIDEO_FORMAT_SIF525:
406     case VIDEO_FORMAT_CIF:
407     case VIDEO_FORMAT_4SIF525:
408     case VIDEO_FORMAT_4CIF:
409     case VIDEO_FORMAT_SD_480I60:
410     case VIDEO_FORMAT_SD_576I50:
411         bparams.SetXblen(12);
412         bparams.SetYblen(12);
413         bparams.SetXbsep(8);
414         bparams.SetYbsep(8);
415         break;
416 
417     case VIDEO_FORMAT_HD_720P60:
418     case VIDEO_FORMAT_HD_720P50:
419         bparams.SetXblen(16);
420         bparams.SetYblen(16);
421         bparams.SetXbsep(12);
422         bparams.SetYbsep(12);
423         break;
424 
425     case VIDEO_FORMAT_HD_1080I60:
426     case VIDEO_FORMAT_HD_1080I50:
427     case VIDEO_FORMAT_HD_1080P60:
428     case VIDEO_FORMAT_HD_1080P50:
429     case VIDEO_FORMAT_DIGI_CINEMA_2K24:
430     case VIDEO_FORMAT_DIGI_CINEMA_4K24:
431         bparams.SetXblen(24);
432         bparams.SetYblen(24);
433         bparams.SetXbsep(16);
434         bparams.SetYbsep(16);
435         break;
436     case VIDEO_FORMAT_UHDTV_4K60:
437     case VIDEO_FORMAT_UHDTV_4K50:
438     case VIDEO_FORMAT_UHDTV_8K60:
439     case VIDEO_FORMAT_UHDTV_8K50:
440         bparams.SetXblen(36);
441         bparams.SetYblen(36);
442         bparams.SetXbsep(24);
443         bparams.SetYbsep(24);
444         break;
445     default:
446         bparams.SetXblen(12);
447         bparams.SetYblen(12);
448         bparams.SetXbsep(8);
449         bparams.SetYbsep(8);
450         break;
451     }
452 }
453 
SetDefaultBlockParameters(OLBParams & bparams,int pidx)454 void SetDefaultBlockParameters(OLBParams& bparams, int pidx)
455 {
456     switch (pidx)
457     {
458     case 0: // custom - so undefined values
459         return;
460     case 1:
461         bparams =  OLBParams(8, 8, 4, 4);
462         break;
463     case 2:
464         bparams =  OLBParams(12, 12, 8, 8);
465         break;
466     case 3:
467         bparams =  OLBParams(16, 16, 12, 12);
468         break;
469     case 4:
470         bparams =  OLBParams(24, 24, 16, 16);
471         break;
472     default:
473         DIRAC_THROW_EXCEPTION(
474             ERR_UNSUPPORTED_STREAM_DATA,
475             "Block params index out of range [0-4]",
476             SEVERITY_PICTURE_ERROR);
477         break;
478     }
479 }
480 
BlockParametersIndex(const OLBParams & bparams)481 unsigned int BlockParametersIndex (const OLBParams& bparams)
482 {
483     OLBParams bparams_1(8, 8, 4, 4);
484     OLBParams bparams_2(12, 12, 8, 8);
485     OLBParams bparams_3(16, 16, 12, 12);
486     OLBParams bparams_4(24, 24, 16, 16);
487 
488     if (bparams == bparams_1)
489         return 1;
490     else if (bparams == bparams_2)
491         return 2;
492     else if (bparams == bparams_3)
493         return 3;
494     else if (bparams == bparams_4)
495         return 4;
496     else
497         return 0;
498 }
499 
SetDefaultTransformFilter(const PictureType ptype,const VideoFormat video_format,WltFilter & wf)500 void SetDefaultTransformFilter(const PictureType ptype, const VideoFormat video_format,
501                                WltFilter &wf)
502 {
503     switch (video_format)
504     {
505     case VIDEO_FORMAT_QCIF:
506     case VIDEO_FORMAT_QSIF525:
507     case VIDEO_FORMAT_CUSTOM:
508     case VIDEO_FORMAT_SIF525:
509     case VIDEO_FORMAT_CIF:
510     case VIDEO_FORMAT_4SIF525:
511     case VIDEO_FORMAT_4CIF:
512     case VIDEO_FORMAT_SD_480I60:
513     case VIDEO_FORMAT_SD_576I50:
514     case VIDEO_FORMAT_HD_720P60:
515     case VIDEO_FORMAT_HD_720P50:
516     case VIDEO_FORMAT_HD_1080I60:
517     case VIDEO_FORMAT_HD_1080I50:
518     case VIDEO_FORMAT_HD_1080P60:
519     case VIDEO_FORMAT_HD_1080P50:
520     case VIDEO_FORMAT_DIGI_CINEMA_2K24:
521     case VIDEO_FORMAT_DIGI_CINEMA_4K24:
522     case VIDEO_FORMAT_UHDTV_4K60:
523     case VIDEO_FORMAT_UHDTV_4K50:
524     case VIDEO_FORMAT_UHDTV_8K60:
525     case VIDEO_FORMAT_UHDTV_8K50:
526     default:
527         if (ptype == INTRA_PICTURE)
528             wf = DD13_7;
529         else
530             wf = DD13_7;
531         break;
532     }
533 }
534 }
535