"Fossies" - the Fresh Open Source Software Archive

Member "MediaInfo_CLI_GNU_FromSource/MediaInfoLib/Source/MediaInfo/Video/File_Avc.cpp" (10 Sep 2019, 214862 Bytes) of package /linux/misc/MediaInfo_CLI_19.09_GNU_FromSource.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "File_Avc.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 19.07_vs_19.09.

    1 /*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
    2  *
    3  *  Use of this source code is governed by a BSD-style license that can
    4  *  be found in the License.html file in the root of the source tree.
    5  */
    6 
    7 //---------------------------------------------------------------------------
    8 // Pre-compilation
    9 #include "MediaInfo/PreComp.h"
   10 #ifdef __BORLANDC__
   11     #pragma hdrstop
   12 #endif
   13 //---------------------------------------------------------------------------
   14 
   15 //---------------------------------------------------------------------------
   16 #include "MediaInfo/Setup.h"
   17 //---------------------------------------------------------------------------
   18 
   19 //***************************************************************************
   20 // Constants
   21 //***************************************************************************
   22 
   23 //---------------------------------------------------------------------------
   24 #if defined(MEDIAINFO_AVC_YES) || defined(MEDIAINFO_MPEGPS_YES) || defined(MEDIAINFO_MPEGTS_YES)
   25 //---------------------------------------------------------------------------
   26 
   27 namespace MediaInfoLib
   28 {
   29 
   30 #include "ZenLib/Conf.h"
   31 using namespace ZenLib;
   32 
   33 //---------------------------------------------------------------------------
   34 const char* Avc_profile_idc(int8u profile_idc)
   35 {
   36     switch (profile_idc)
   37     {
   38         case  44 : return "CAVLC 4:4:4 Intra";
   39         case  66 : return "Baseline";
   40         case  77 : return "Main";
   41         case  83 : return "Scalable Baseline";
   42         case  86 : return "Scalable High";
   43         case  88 : return "Extended";
   44         case 100 : return "High";
   45         case 110 : return "High 10";
   46         case 118 : return "Multiview High";
   47         case 122 : return "High 4:2:2";
   48         case 128 : return "Stereo High";
   49         case 138 : return "Multiview Depth High";
   50         case 144 : return "High 4:4:4";
   51         case 244 : return "High 4:4:4 Predictive";
   52         default  : return "";
   53     }
   54 }
   55 
   56 //---------------------------------------------------------------------------
   57 } //NameSpace
   58 
   59 //---------------------------------------------------------------------------
   60 #endif //...
   61 //---------------------------------------------------------------------------
   62 
   63 //***************************************************************************
   64 //
   65 //***************************************************************************
   66 
   67 //---------------------------------------------------------------------------
   68 #if defined(MEDIAINFO_AVC_YES)
   69 //---------------------------------------------------------------------------
   70 
   71 //---------------------------------------------------------------------------
   72 #include "MediaInfo/Video/File_Avc.h"
   73 #if defined(MEDIAINFO_DTVCCTRANSPORT_YES)
   74     #include "MediaInfo/Text/File_DtvccTransport.h"
   75 #endif //defined(MEDIAINFO_DTVCCTRANSPORT_YES)
   76 #if MEDIAINFO_ADVANCED2
   77     #include "ThirdParty/base64/base64.h"
   78 #endif //MEDIAINFO_ADVANCED2
   79 #include "MediaInfo/MediaInfo_Config_MediaInfo.h"
   80 #if MEDIAINFO_EVENTS
   81     #include "MediaInfo/MediaInfo_Config_PerPackage.h"
   82     #include "MediaInfo/MediaInfo_Events.h"
   83     #include "MediaInfo/MediaInfo_Events_Internal.h"
   84 #endif //MEDIAINFO_EVENTS
   85 #include <cstring>
   86 #include <algorithm>
   87 using namespace std;
   88 using namespace ZenLib;
   89 //---------------------------------------------------------------------------
   90 
   91 namespace MediaInfoLib
   92 {
   93 
   94 //***************************************************************************
   95 // Infos
   96 //***************************************************************************
   97 
   98 //---------------------------------------------------------------------------
   99 const size_t Avc_Errors_MaxCount=32;
  100 
  101 //---------------------------------------------------------------------------
  102 extern const int8u Avc_PixelAspectRatio_Size=17;
  103 extern const float32 Avc_PixelAspectRatio[Avc_PixelAspectRatio_Size]=
  104 {
  105     (float32)1, //Reserved
  106     (float32)1,
  107     (float32)12/(float32)11,
  108     (float32)10/(float32)11,
  109     (float32)16/(float32)11,
  110     (float32)40/(float32)33,
  111     (float32)24/(float32)11,
  112     (float32)20/(float32)11,
  113     (float32)32/(float32)11,
  114     (float32)80/(float32)33,
  115     (float32)18/(float32)11,
  116     (float32)15/(float32)11,
  117     (float32)64/(float32)33,
  118     (float32)160/(float32)99,
  119     (float32)4/(float32)3,
  120     (float32)3/(float32)2,
  121     (float32)2,
  122 };
  123 
  124 //---------------------------------------------------------------------------
  125 const char* Avc_video_format[]=
  126 {
  127     "Component",
  128     "PAL",
  129     "NTSC",
  130     "SECAM",
  131     "MAC",
  132     "",
  133     "Reserved",
  134     "Reserved",
  135 };
  136 
  137 //---------------------------------------------------------------------------
  138 const char* Avc_video_full_range[]=
  139 {
  140     "Limited",
  141     "Full",
  142 };
  143 
  144 //---------------------------------------------------------------------------
  145 static const char* Avc_primary_pic_type[]=
  146 {
  147     "I",
  148     "I, P",
  149     "I, P, B",
  150     "SI",
  151     "SI, SP",
  152     "I, SI",
  153     "I, SI, P, SP",
  154     "I, SI, P, SP, B",
  155 };
  156 
  157 //---------------------------------------------------------------------------
  158 static const char* Avc_slice_type[]=
  159 {
  160     "P",
  161     "B",
  162     "I",
  163     "SP",
  164     "SI",
  165     "P",
  166     "B",
  167     "I",
  168     "SP",
  169     "SI",
  170 };
  171 
  172 //---------------------------------------------------------------------------
  173 const int8u Avc_pic_struct_Size=9;
  174 static const char* Avc_pic_struct[]=
  175 {
  176     "frame",
  177     "top field",
  178     "bottom field",
  179     "top field, bottom field",
  180     "bottom field, top field",
  181     "top field, bottom field, top field repeated",
  182     "bottom field, top field, bottom field repeated",
  183     "frame doubling",
  184     "frame tripling",
  185 };
  186 
  187 //---------------------------------------------------------------------------
  188 static const int8u Avc_NumClockTS[]=
  189 {
  190     1,
  191     1,
  192     1,
  193     2,
  194     2,
  195     3,
  196     3,
  197     2,
  198     3,
  199 };
  200 
  201 //---------------------------------------------------------------------------
  202 static const char* Avc_ct_type[]=
  203 {
  204     "Progressive",
  205     "Interlaced",
  206     "Unknown",
  207     "Reserved",
  208 };
  209 
  210 //---------------------------------------------------------------------------
  211 static const int8u Avc_SubWidthC[]=
  212 {
  213     1,
  214     2,
  215     2,
  216     1,
  217 };
  218 
  219 //---------------------------------------------------------------------------
  220 static const int8u Avc_SubHeightC[]=
  221 {
  222     1,
  223     2,
  224     1,
  225     1,
  226 };
  227 
  228 //---------------------------------------------------------------------------
  229 const char* Avc_user_data_DTG1_active_format[]=
  230 {
  231     //1st value is for 4:3, 2nd is for 16:9
  232     "", //Undefined
  233     "Reserved",
  234     "Not recommended",
  235     "Not recommended",
  236     "Aspect ratio greater than 16:9", //Use GA94
  237     "Reserved",
  238     "Reserved",
  239     "Reserved",
  240     "4:3 full frame image / 16:9 full frame image",
  241     "4:3 full frame image / 4:3 pillarbox image",
  242     "16:9 letterbox image / 16:9 full frame image",
  243     "14:9 letterbox image / 14:9 pillarbox image",
  244     "Reserved",
  245     "4:3 full frame image, alternative 14:9 center / 4:3 pillarbox image, alternative 14:9 center",
  246     "16:9 letterbox image, alternative 14:9 center / 16:9 full frame image, alternative 14:9 center",
  247     "16:9 letterbox image, alternative 4:3 center / 16:9 full frame image, alternative 4:3 center",
  248 };
  249 
  250 //---------------------------------------------------------------------------
  251 const char* Mpegv_colour_primaries(int8u colour_primaries);
  252 const char* Mpegv_transfer_characteristics(int8u transfer_characteristics);
  253 const char* Mpegv_matrix_coefficients(int8u matrix_coefficients);
  254 const char* Mpegv_matrix_coefficients_ColorSpace(int8u matrix_coefficients);
  255 
  256 //---------------------------------------------------------------------------
  257 static const char* Avc_ChromaSubsampling_format_idc(int8u chroma_format_idc)
  258 {
  259     switch (chroma_format_idc)
  260     {
  261         case 1: return "4:2:0";
  262         case 2: return "4:2:2";
  263         case 3: return "4:4:4";
  264         default: return "";
  265     }
  266 }
  267 
  268 //---------------------------------------------------------------------------
  269 static const char* Avc_ChromaSubsampling_format_idc_ColorSpace(int8u chroma_format_idc)
  270 {
  271     switch (chroma_format_idc)
  272     {
  273         case 0: return "Y";
  274         case 1: return "YUV";
  275         case 2: return "YUV";
  276         default: return "";
  277     }
  278 }
  279 
  280 //---------------------------------------------------------------------------
  281 const char* Avc_user_data_GA94_cc_type(int8u cc_type)
  282 {
  283     switch (cc_type)
  284     {
  285         case  0 : return "CEA-608 line 21 field 1 closed captions"; //closed caption 3 if this is second field
  286         case  1 : return "CEA-608 line 21 field 2 closed captions"; //closed caption 4 if this is second field
  287         case  2 : return "DTVCC Channel Packet Data";
  288         case  3 : return "DTVCC Channel Packet Start";
  289         default : return "";
  290     }
  291 }
  292 
  293 //---------------------------------------------------------------------------
  294 int32u Avc_MaxDpbMbs(int8u level)
  295 {
  296     switch (level)
  297     {
  298         case  10 : return    1485;
  299         case  11 : return    3000;
  300         case  12 : return    6000;
  301         case  13 :
  302         case  20 : return   11880;
  303         case  21 : return   19800;
  304         case  22 : return   20250;
  305         case  30 : return   40500;
  306         case  31 : return  108000;
  307         case  32 : return  216000;
  308         case  40 :
  309         case  41 : return  245760;
  310         case  42 : return  522240;
  311         case  50 : return  589824;
  312         case  51 : return  983040;
  313         default  : return       0;
  314     }
  315 }
  316 
  317 //---------------------------------------------------------------------------
  318 namespace AVC_Intra_Headers
  319 {
  320     //720p50
  321     static const size_t __720p50__50_Size = 4 * 0x10 + 0xA;
  322     static const int8u  __720p50__50[__720p50__50_Size] = { 0x00, 0x00, 0x01, 0x67, 0x6E, 0x10, 0x20, 0xA6, 0xD4, 0x20, 0x32, 0x33, 0x0C, 0x71, 0x18, 0x88,
  323                                                             0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8C, 0x44, 0x30, 0x21, 0x02, 0x56, 0x4E, 0x6F, 0x37, 0xCD,
  324                                                             0xF9, 0xBF, 0x81, 0x6B, 0xF3, 0x7C, 0xDE, 0x6E, 0x6C, 0xD3, 0x3C, 0x0F, 0x01, 0x6E, 0xFF, 0xC0,
  325                                                             0x00, 0xC0, 0x01, 0x38, 0xC0, 0x40, 0x40, 0x50, 0x00, 0x00, 0x03, 0x00, 0x10, 0x00, 0x00, 0x06,
  326                                                             0x48, 0x40, 0x00, 0x00, 0x01, 0x68, 0xEE, 0x31, 0x12, 0x11 };
  327     static const size_t __720p50_100_Size = 5 * 0x10 + 0x2;
  328     static const int8u  __720p50_100[__720p50_100_Size] = { 0x00, 0x00, 0x01, 0x67, 0x7A, 0x10, 0x29, 0xB6, 0xD4, 0x20, 0x2A, 0x33, 0x1D, 0xC7, 0x62, 0xA1,
  329                                                             0x08, 0x40, 0x54, 0x66, 0x3B, 0x8E, 0xC5, 0x42, 0x02, 0x10, 0x25, 0x64, 0x2C, 0x89, 0xE8, 0x85,
  330                                                             0xE4, 0x21, 0x4B, 0x90, 0x83, 0x06, 0x95, 0xD1, 0x06, 0x46, 0x97, 0x20, 0xC8, 0xD7, 0x43, 0x08,
  331                                                             0x11, 0xC2, 0x1E, 0x4C, 0x91, 0x0F, 0x01, 0x40, 0x16, 0xEC, 0x07, 0x8C, 0x04, 0x04, 0x05, 0x00,
  332                                                             0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x03, 0x00, 0x64, 0x84, 0x00, 0x00, 0x01, 0x68, 0xCE, 0x31,
  333                                                             0x12, 0x11 };
  334     //720p60
  335     static const size_t __720p60__50_Size = 4 * 0x10 + 0xB;
  336     static const int8u  __720p60__50[__720p60__50_Size] = { 0x00, 0x00, 0x01, 0x67, 0x6E, 0x10, 0x20, 0xA6, 0xD4, 0x20, 0x32, 0x33, 0x0C, 0x71, 0x18, 0x88,
  337                                                             0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8C, 0x44, 0x30, 0x21, 0x02, 0x56, 0x4E, 0x6F, 0x37, 0xCD,
  338                                                             0xF9, 0xBF, 0x81, 0x6B, 0xF3, 0x7C, 0xDE, 0x6E, 0x6C, 0xD3, 0x3C, 0x0F, 0x01, 0x6E, 0xFF, 0xC0,
  339                                                             0x00, 0xC0, 0x01, 0x38, 0xC0, 0x40, 0x40, 0x50, 0x00, 0x00, 0x3E, 0x90, 0x00, 0x1D, 0x4C, 0x08,
  340                                                             0x40, 0x00, 0x00, 0x00, 0x00, 0x01, 0x68, 0xEE, 0x31, 0x12, 0x11 };
  341     static const size_t __720p60_100_Size = 5 * 0x10 + 0x1;
  342     static const int8u  __720p60_100[__720p60_100_Size] = { 0x00, 0x00, 0x01, 0x67, 0x7A, 0x10, 0x29, 0xB6, 0xD4, 0x20, 0x2A, 0x33, 0x1D, 0xC7, 0x62, 0xA1,
  343                                                             0x08, 0x40, 0x54, 0x66, 0x3B, 0x8E, 0xC5, 0x42, 0x02, 0x10, 0x25, 0x64, 0x2C, 0x89, 0xE8, 0x85,
  344                                                             0xE4, 0x21, 0x4B, 0x90, 0x83, 0x06, 0x95, 0xD1, 0x06, 0x46, 0x97, 0x20, 0xC8, 0xD7, 0x43, 0x08,
  345                                                             0x11, 0xC2, 0x1E, 0x4C, 0x91, 0x0F, 0x01, 0x40, 0x16, 0xEC, 0x07, 0x8C, 0x04, 0x04, 0x05, 0x00,
  346                                                             0x00, 0x03, 0x03, 0xE9, 0x00, 0x01, 0xD4, 0xC0, 0x84, 0x00, 0x00, 0x01, 0x68, 0xCE, 0x31, 0x12,
  347                                                             0x11 };
  348 
  349     //1080i50
  350     static const size_t _1080i50__50_Size = 5 * 0x10 + 0xE;
  351     static const int8u  _1080i50__50[_1080i50__50_Size] = { 0x00, 0x00, 0x01, 0x67, 0x6E, 0x10, 0x28, 0xA6, 0xD4, 0x20, 0x32, 0x33, 0x0C, 0x71, 0x18, 0x88,
  352                                                             0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8C, 0x44, 0x30, 0x21, 0x02, 0x56, 0x4E, 0x6E, 0x61, 0x87,
  353                                                             0x3E, 0x73, 0x4D, 0x98, 0x0C, 0x03, 0x06, 0x9C, 0x0B, 0x73, 0xE6, 0xC0, 0xB5, 0x18, 0x63, 0x0D,
  354                                                             0x39, 0xE0, 0x5B, 0x02, 0xD4, 0xC6, 0x19, 0x1A, 0x79, 0x8C, 0x32, 0x34, 0x24, 0xF0, 0x16, 0x81,
  355                                                             0x13, 0xF7, 0xFF, 0x80, 0x01, 0x80, 0x02, 0x71, 0x80, 0x80, 0x80, 0xA0, 0x00, 0x00, 0x03, 0x00,
  356                                                             0x20, 0x00, 0x00, 0x06, 0x50, 0x80, 0x00, 0x00, 0x01, 0x68, 0xEE, 0x31, 0x12, 0x11 };
  357     static const size_t _1080i50_100_Size = 5 * 0x10 + 0xF;
  358     static const int8u  _1080i50_100[_1080i50_100_Size] = { 0x00, 0x00, 0x01, 0x67, 0x7A, 0x10, 0x29, 0xB6, 0xD4, 0x20, 0x22, 0x33, 0x19, 0xC6, 0x63, 0x23,
  359                                                             0x21, 0x01, 0x11, 0x98, 0xCE, 0x33, 0x19, 0x18, 0x21, 0x03, 0x3A, 0x46, 0x65, 0x6A, 0x65, 0x24,
  360                                                             0xAD, 0xE9, 0x12, 0x32, 0x14, 0x1A, 0x26, 0x34, 0xAD, 0xA4, 0x41, 0x82, 0x23, 0x01, 0x50, 0x2B,
  361                                                             0x1A, 0x24, 0x69, 0x48, 0x30, 0x40, 0x2E, 0x11, 0x12, 0x08, 0xC6, 0x8C, 0x04, 0x41, 0x28, 0x4C,
  362                                                             0x34, 0xF0, 0x1E, 0x01, 0x13, 0xF2, 0xE0, 0x3C, 0x60, 0x20, 0x20, 0x28, 0x00, 0x00, 0x03, 0x00,
  363                                                             0x08, 0x00, 0x00, 0x03, 0x01, 0x94, 0x20, 0x00, 0x00, 0x01, 0x68, 0xCE, 0x33, 0x48, 0xD0 };
  364     //1080i60
  365     static const size_t _1080i60__50_Size = 5 * 0x10 + 0xD;
  366     static const int8u  _1080i60__50[_1080i60__50_Size] = { 0x00, 0x00, 0x01, 0x67, 0x6E, 0x10, 0x28, 0xA6, 0xD4, 0x20, 0x32, 0x33, 0x0C, 0x71, 0x18, 0x88,
  367                                                             0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8C, 0x44, 0x30, 0x21, 0x02, 0x56, 0x4E, 0x6E, 0x61, 0x87,
  368                                                             0x3E, 0x73, 0x4D, 0x98, 0x0C, 0x03, 0x06, 0x9C, 0x0B, 0x73, 0xE6, 0xC0, 0xB5, 0x18, 0x63, 0x0D,
  369                                                             0x39, 0xE0, 0x5B, 0x02, 0xD4, 0xC6, 0x19, 0x1A, 0x79, 0x8C, 0x32, 0x34, 0x24, 0xF0, 0x16, 0x81,
  370                                                             0x13, 0xF7, 0xFF, 0x80, 0x01, 0x80, 0x02, 0x71, 0x80, 0x80, 0x80, 0xA0, 0x00, 0x00, 0x7D, 0x20,
  371                                                             0x00, 0x1D, 0x4C, 0x10, 0x80, 0x00, 0x00, 0x01, 0x68, 0xEE, 0x31, 0x12, 0x11 };
  372     static const size_t _1080i60_100_Size = 5 * 0x10 + 0xD;
  373     static const int8u  _1080i60_100[_1080i60_100_Size] = { 0x00, 0x00, 0x01, 0x67, 0x7A, 0x10, 0x29, 0xB6, 0xD4, 0x20, 0x22, 0x33, 0x19, 0xC6, 0x63, 0x23,
  374                                                             0x21, 0x01, 0x11, 0x98, 0xCE, 0x33, 0x19, 0x18, 0x21, 0x03, 0x3A, 0x46, 0x65, 0x6A, 0x65, 0x24,
  375                                                             0xAD, 0xE9, 0x12, 0x32, 0x14, 0x1A, 0x26, 0x34, 0xAD, 0xA4, 0x41, 0x82, 0x23, 0x01, 0x50, 0x2B,
  376                                                             0x1A, 0x24, 0x69, 0x48, 0x30, 0x40, 0x2E, 0x11, 0x12, 0x08, 0xC6, 0x8C, 0x04, 0x41, 0x28, 0x4C,
  377                                                             0x34, 0xF0, 0x1E, 0x01, 0x13, 0xF2, 0xE0, 0x3C, 0x60, 0x20, 0x20, 0x28, 0x00, 0x00, 0x1F, 0x48,
  378                                                             0x00, 0x07, 0x53, 0x04, 0x20, 0x00, 0x00, 0x01, 0x68, 0xCE, 0x33, 0x48, 0xD0 };
  379 
  380     //1080p50
  381     static const size_t _1080p50_100_Size = 4 * 0x10 + 0xA;
  382     static const int8u  _1080p50_100[_1080p50_100_Size] = { 0x00, 0x00, 0x01, 0x67, 0x7A, 0x10, 0x2A, 0xB6, 0xD4, 0x20, 0x22, 0x33, 0x19, 0xC6, 0x63, 0x23,
  383                                                             0x21, 0x01, 0x11, 0x98, 0xCE, 0x33, 0x19, 0x18, 0x21, 0x02, 0x56, 0xB9, 0x3D, 0x7D, 0x7E, 0x4F,
  384                                                             0xE3, 0x3F, 0x11, 0xF1, 0x9E, 0x08, 0xB8, 0x8C, 0x54, 0x43, 0xC0, 0x78, 0x02, 0x27, 0xE2, 0x70,
  385                                                             0x1E, 0x30, 0x10, 0x10, 0x14, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x01, 0x92, 0x10,
  386                                                             0x00, 0x00, 0x00, 0x00, 0x01, 0x68, 0xCE, 0x33, 0x48, 0xD0 };
  387     //1080p60
  388     static const size_t _1080p60_100_Size = 4 * 0x10 + 0x8;
  389     static const int8u  _1080p60_100[_1080p60_100_Size] = { 0x00, 0x00, 0x01, 0x67, 0x7A, 0x10, 0x2A, 0xB6, 0xD4, 0x20, 0x22, 0x33, 0x19, 0xC6, 0x63, 0x23,
  390                                                             0x21, 0x01, 0x11, 0x98, 0xCE, 0x33, 0x19, 0x18, 0x21, 0x02, 0x56, 0xB9, 0x3D, 0x7D, 0x7E, 0x4F,
  391                                                             0xE3, 0x3F, 0x11, 0xF1, 0x9E, 0x08, 0xB8, 0x8C, 0x54, 0x43, 0xC0, 0x78, 0x02, 0x27, 0xE2, 0x70,
  392                                                             0x1E, 0x30, 0x10, 0x10, 0x14, 0x00, 0x00, 0x0F, 0xA4, 0x00, 0x07, 0x53, 0x02, 0x10, 0x00, 0x00,
  393                                                             0x00, 0x00, 0x01, 0x68, 0xCE, 0x33, 0x48, 0xD0 };
  394 };
  395 
  396 
  397 //---------------------------------------------------------------------------
  398 // Some DV Metadata info: http://www.freepatentsonline.com/20050076039.pdf
  399 static const char* MDPM(int8u ID)
  400 {
  401     switch (ID)
  402     {
  403         case 0x18: return "Date/Time";                          // Is "Text header" in doc?
  404         case 0x19: return "Date/Time (continue 1)";             // Is "Text" in doc?
  405         case 0x70: return "Consumer Camera 1";
  406         case 0x71: return "Consumer Camera 2";
  407         case 0x73: return "Lens";
  408         case 0x7F: return "Camera shutter";
  409         case 0xA1: return "Iris (F)";
  410         case 0xE0: return "Make Model";
  411         case 0xE1: return "Rec Info";
  412         case 0xE4: return "Model Name";
  413         case 0xE5: return "Model Name (continue 1)";
  414         case 0xE6: return "Model Name (continue 1)";
  415         default  : return "";
  416     }
  417 }
  418 
  419 //---------------------------------------------------------------------------
  420 static const char* MDPM_MakeName(int16u Value)
  421 {
  422     switch (Value)
  423     {
  424         case 0x0103: return "Panasonic";
  425         case 0x0108: return "Sony";
  426         case 0x1011: return "Canon";
  427         case 0x1104: return "JVC";
  428         default    : return "";
  429     }
  430 }
  431 
  432 //---------------------------------------------------------------------------
  433 // From DV
  434 extern const char*  Dv_consumer_camera_1_ae_mode[];
  435 extern const char*  Dv_consumer_camera_1_wb_mode[];
  436 extern const char*  Dv_consumer_camera_1_white_balance(int8u white_balance);
  437 extern const char*  Dv_consumer_camera_1_fcm[];
  438 
  439 //***************************************************************************
  440 // Constructor/Destructor
  441 //***************************************************************************
  442 
  443 //---------------------------------------------------------------------------
  444 File_Avc::File_Avc()
  445 #if MEDIAINFO_DUPLICATE
  446 :File__Duplicate()
  447 #endif //MEDIAINFO_DUPLICATE
  448 {
  449     //Config
  450     #if MEDIAINFO_EVENTS
  451         ParserIDs[0]=MediaInfo_Parser_Avc;
  452         StreamIDs_Width[0]=0;
  453     #endif //MEDIAINFO_EVENTS
  454     #if MEDIAINFO_TRACE
  455         Trace_Layers_Update(8); //Stream
  456     #endif //MEDIAINFO_TRACE
  457     MustSynchronize=true;
  458     Buffer_TotalBytes_FirstSynched_Max=64*1024;
  459     PTS_DTS_Needed=true;
  460     StreamSource=IsStream;
  461     Frame_Count_NotParsedIncluded=0;
  462 
  463     //In
  464     Frame_Count_Valid=0;
  465     FrameIsAlwaysComplete=false;
  466     MustParse_SPS_PPS=false;
  467     SizedBlocks=false;
  468 
  469     //Temporal references
  470     TemporalReferences_DelayedElement=NULL;
  471 
  472     //Temp
  473     preferred_transfer_characteristics=2;
  474 
  475     //Text
  476     #if defined(MEDIAINFO_DTVCCTRANSPORT_YES)
  477         GA94_03_Parser=NULL;
  478     #endif //defined(MEDIAINFO_DTVCCTRANSPORT_YES)
  479 }
  480 
  481 //---------------------------------------------------------------------------
  482 File_Avc::~File_Avc()
  483 {
  484     Clean_Temp_References();
  485     #if defined(MEDIAINFO_DTVCCTRANSPORT_YES)
  486         delete GA94_03_Parser; //GA94_03_Parser=NULL;
  487     #endif //defined(MEDIAINFO_DTVCCTRANSPORT_YES)
  488      Clean_Seq_Parameter();
  489 }
  490 
  491 //---------------------------------------------------------------------------
  492 void File_Avc::Clean_Temp_References()
  493 {
  494     for (size_t Pos = 0; Pos<TemporalReferences.size(); Pos++)
  495         delete TemporalReferences[Pos]; //TemporalReferences[Pos]=NULL;
  496     TemporalReferences.clear();
  497 }
  498 //---------------------------------------------------------------------------
  499 void File_Avc::Clean_Seq_Parameter()
  500 {
  501     for (size_t Pos = 0; Pos<seq_parameter_sets.size(); Pos++)
  502         delete seq_parameter_sets[Pos]; //TemporalReferences[Pos]=NULL;
  503     seq_parameter_sets.clear();
  504     for (size_t Pos = 0; Pos<subset_seq_parameter_sets.size(); Pos++)
  505         delete subset_seq_parameter_sets[Pos]; //subset_seq_parameter_sets[Pos]=NULL;
  506     subset_seq_parameter_sets.clear();
  507     for (size_t Pos = 0; Pos<pic_parameter_sets.size(); Pos++)
  508         delete pic_parameter_sets[Pos]; //pic_parameter_sets[Pos]=NULL;
  509     pic_parameter_sets.clear();
  510 }
  511 //***************************************************************************
  512 // AVC-Intra hardcoded headers
  513 //***************************************************************************
  514 
  515 //---------------------------------------------------------------------------
  516 File_Avc::avcintra_header File_Avc::AVC_Intra_Headers_Data(int32u CodecID)
  517 {
  518     switch (CodecID)
  519     {
  520         case  0x61693132: //ai12
  521         case  0x61693232: //ai22
  522                             return avcintra_header(AVC_Intra_Headers::_1080p50_100, AVC_Intra_Headers::_1080p50_100_Size);  // AVC-Intra 1080p50 class 100/200
  523         case  0x61693133: //ai13
  524         case  0x61693233: //ai23
  525                             return avcintra_header(AVC_Intra_Headers::_1080p60_100, AVC_Intra_Headers::_1080p60_100_Size);  // AVC-Intra 1080p60 class 100/200
  526         case  0x61693135: //ai15
  527         case  0x61693235: //ai25
  528                             return avcintra_header(AVC_Intra_Headers::_1080i50_100, AVC_Intra_Headers::_1080i50_100_Size);  // AVC-Intra 1080i50 class 100/200
  529         case  0x61693136: //ai16
  530         case  0x61693236: //ai26
  531                             return avcintra_header(AVC_Intra_Headers::_1080i60_100, AVC_Intra_Headers::_1080i60_100_Size);  // AVC-Intra 1080i60 class 100/200
  532         case  0x61693170: //ai1p
  533         case  0x61693270: //ai2p
  534                             return avcintra_header(AVC_Intra_Headers::__720p60_100, AVC_Intra_Headers::__720p60_100_Size);  // AVC-Intra  720p60 class 100/200
  535         case  0x61693171: //ai1q
  536         case  0x61693271: //ai2q
  537                             return avcintra_header(AVC_Intra_Headers::__720p50_100, AVC_Intra_Headers::__720p50_100_Size);  // AVC-Intra  720p50 class 100/200
  538       //case  0x61693532: //ai52
  539       //                    return avcintra_header(NULL, 0);                                                                // AVC-Intra 1080p25 class  50 (not supported)
  540       //case  0x61693533: //ai53
  541       //                    return avcintra_header(NULL, 0);                                                                // AVC-Intra 1080p30 class  50 (not supported)
  542         case  0x61693535: //ai55
  543                             return avcintra_header(AVC_Intra_Headers::_1080i50__50, AVC_Intra_Headers::_1080i50__50_Size);  // AVC-Intra 1080i50 class  50
  544         case  0x61693536: //ai56
  545                             return avcintra_header(AVC_Intra_Headers::_1080i60__50, AVC_Intra_Headers::_1080i60__50_Size);  // AVC-Intra 1080i60 class  50
  546         case  0x61693570: //ai5p
  547                             return avcintra_header(AVC_Intra_Headers::__720p60__50, AVC_Intra_Headers::__720p60__50_Size);  // AVC-Intra  720p60 class  50
  548         case  0x61693571: //ai5q
  549                             return avcintra_header(AVC_Intra_Headers::__720p50__50, AVC_Intra_Headers::__720p50__50_Size);  // AVC-Intra  720p50 class  50
  550         default       :
  551                             return avcintra_header(NULL, 0);
  552     }
  553 }
  554 
  555 //---------------------------------------------------------------------------
  556 int32u File_Avc::AVC_Intra_CodecID_FromMeta(int32u Height, int32u Fields, int32u SampleDuration, int32u TimeScale, int32u SizePerFrame)
  557 {
  558     // Computing bitrate
  559     int64u BitRate=((int64u)SizePerFrame)*8*TimeScale/SampleDuration;
  560     int64u SampleRate=float64_int64s(((float64)TimeScale)/SampleDuration);
  561     int32u Class=BitRate<=75000000?50:100; //Arbitrary choosen. TODO: check real maximumm bitrate, check class 200
  562     switch (Class)
  563     {
  564         case 100 : 
  565                     switch (Height)
  566                     {
  567                         case 1080 :
  568                                     switch (Fields)
  569                                     {
  570                                         case 1: 
  571                                                     switch (SampleRate)
  572                                                     {
  573                                                         case 50: return 0x61693132; //ai12
  574                                                         case 60: return 0x61693133; //ai13
  575                                                         default: return 0x4156696E; //AVin (neutral)
  576                                                     }
  577                                         case 2: 
  578                                                     switch (SampleRate)
  579                                                     {
  580                                                         case 25: return 0x61693135; //ai15 //TODO: check more files in order to know if it should be 1 or 2 fields per sample
  581                                                         case 30: return 0x61693136; //ai16 //TODO: check more files in order to know if it should be 1 or 2 fields per sample
  582                                                         case 50: return 0x61693135; //ai15 //TODO: check more files in order to know if it should be 1 or 2 fields per sample
  583                                                         case 60: return 0x61693136; //ai16 //TODO: check more files in order to know if it should be 1 or 2 fields per sample
  584                                                         default: return 0x4156696E; //AVin (neutral)
  585                                                     }
  586                                         default:    return 0x4156696E; //AVin (neutral)
  587                                     }
  588                         case  720 :
  589                                     switch (Fields)
  590                                     {
  591                                         case 1: 
  592                                                     switch (SampleRate)
  593                                                     {
  594                                                         case 50: return 0x61693170; //ai1p
  595                                                         case 60: return 0x61693171; //ai1q
  596                                                         default: return 0x4156696E; //AVin (neutral)
  597                                                     }
  598                                         default:    return 0x4156696E; //AVin (neutral)
  599                                     }
  600                         default   : return 0x4156696E; //AVin (neutral)
  601                     }
  602         case  50 : 
  603                     switch (Height)
  604                     {
  605                         case 1080 :
  606                                     switch (Fields)
  607                                     {
  608                                         case 1: 
  609                                                     switch (SampleRate)
  610                                                     {
  611                                                         case 25: return 0x61693532; //ai52
  612                                                         case 30: return 0x61693533; //ai53
  613                                                         default: return 0x4156696E; //AVin (neutral)
  614                                                     }
  615                                         case 2: 
  616                                                     switch (SampleRate)
  617                                                     {
  618                                                         case 25: return 0x61693535; //ai55 //TODO: check more files in order to know if it should be 1 or 2 fields per sample
  619                                                         case 30: return 0x61693536; //ai56 //TODO: check more files in order to know if it should be 1 or 2 fields per sample
  620                                                         case 50: return 0x61693535; //ai55 //TODO: check more files in order to know if it should be 1 or 2 fields per sample
  621                                                         case 60: return 0x61693536; //ai56 //TODO: check more files in order to know if it should be 1 or 2 fields per sample
  622                                                         default: return 0x4156696E; //AVin (neutral)
  623                                                     }
  624                                         default:    return 0x4156696E; //AVin (neutral)
  625                                     }
  626                         case  720 :
  627                                     switch (Fields)
  628                                     {
  629                                         case 1: 
  630                                                     switch (SampleRate)
  631                                                     {
  632                                                         case 50: return 0x61693570; //ai5p
  633                                                         case 60: return 0x61693571; //ai5q
  634                                                         default: return 0x4156696E; //AVin (neutral)
  635                                                     }
  636                                         default:    return 0x4156696E; //AVin (neutral)
  637                                     }
  638                         default   : return 0x4156696E; //AVin (neutral)
  639                     }
  640         default: return 0x4156696E; //AVin (neutral)
  641     }
  642 }
  643 
  644 //***************************************************************************
  645 // Streams management
  646 //***************************************************************************
  647 
  648 //---------------------------------------------------------------------------
  649 void File_Avc::Streams_Fill()
  650 {
  651     for (std::vector<seq_parameter_set_struct*>::iterator seq_parameter_set_Item=seq_parameter_sets.begin(); seq_parameter_set_Item!=seq_parameter_sets.end(); ++seq_parameter_set_Item)
  652         if ((*seq_parameter_set_Item))
  653             Streams_Fill(seq_parameter_set_Item);
  654     for (std::vector<seq_parameter_set_struct*>::iterator subset_seq_parameter_set_Item=subset_seq_parameter_sets.begin(); subset_seq_parameter_set_Item!=subset_seq_parameter_sets.end(); ++subset_seq_parameter_set_Item)
  655         if ((*subset_seq_parameter_set_Item))
  656         {
  657             if (seq_parameter_sets.empty())
  658                 Streams_Fill(subset_seq_parameter_set_Item);
  659             else
  660                 Streams_Fill_subset(subset_seq_parameter_set_Item);
  661             Fill(Stream_Video, 0, Video_MultiView_Count, (*subset_seq_parameter_set_Item)->num_views_minus1+1);
  662         }
  663 
  664     #if MEDIAINFO_ADVANCED2
  665         for (size_t Pos = 0; Pos<Dump_SPS.size(); Pos++)
  666             Fill(Stream_Video, 0, "Dump_seq_parameter_set", Dump_SPS[Pos].c_str());
  667         for (size_t Pos = 0; Pos<Dump_PPS.size(); Pos++)
  668             Fill(Stream_Video, 0, "Dump_pic_parameter_set", Dump_PPS[Pos].c_str());
  669     #endif //MEDIAINFO_ADVANCED2
  670 }
  671 
  672 //---------------------------------------------------------------------------
  673 void File_Avc::Streams_Fill(std::vector<seq_parameter_set_struct*>::iterator seq_parameter_set_Item)
  674 {
  675     //Calculating - Pixels
  676     int32u Width =((*seq_parameter_set_Item)->pic_width_in_mbs_minus1       +1)*16;
  677     int32u Height=((*seq_parameter_set_Item)->pic_height_in_map_units_minus1+1)*16*(2-(*seq_parameter_set_Item)->frame_mbs_only_flag);
  678     int8u chromaArrayType = (*seq_parameter_set_Item)->ChromaArrayType();
  679     if (chromaArrayType >= 4)
  680         chromaArrayType = 0;
  681     int32u CropUnitX=Avc_SubWidthC [chromaArrayType];
  682     int32u CropUnitY=Avc_SubHeightC[chromaArrayType]*(2-(*seq_parameter_set_Item)->frame_mbs_only_flag);
  683     Width -=((*seq_parameter_set_Item)->frame_crop_left_offset+(*seq_parameter_set_Item)->frame_crop_right_offset )*CropUnitX;
  684     Height-=((*seq_parameter_set_Item)->frame_crop_top_offset +(*seq_parameter_set_Item)->frame_crop_bottom_offset)*CropUnitY;
  685 
  686     //From vui_parameters
  687     float64 PixelAspectRatio=1;
  688     if ((*seq_parameter_set_Item)->vui_parameters)
  689     {
  690         if ((*seq_parameter_set_Item)->vui_parameters->aspect_ratio_info_present_flag)
  691         {
  692             if ((*seq_parameter_set_Item)->vui_parameters->aspect_ratio_idc<Avc_PixelAspectRatio_Size)
  693                 PixelAspectRatio=Avc_PixelAspectRatio[(*seq_parameter_set_Item)->vui_parameters->aspect_ratio_idc];
  694             else if ((*seq_parameter_set_Item)->vui_parameters->aspect_ratio_idc==0xFF && (*seq_parameter_set_Item)->vui_parameters->sar_height)
  695                 PixelAspectRatio=((float64)(*seq_parameter_set_Item)->vui_parameters->sar_width)/(*seq_parameter_set_Item)->vui_parameters->sar_height;
  696         }
  697         if ((*seq_parameter_set_Item)->vui_parameters->timing_info_present_flag)
  698         {
  699             if (!(*seq_parameter_set_Item)->vui_parameters->fixed_frame_rate_flag)
  700                 Fill(Stream_Video, StreamPos_Last, Video_FrameRate_Mode, "VFR");
  701             else if ((*seq_parameter_set_Item)->vui_parameters->timing_info_present_flag && (*seq_parameter_set_Item)->vui_parameters->time_scale && (*seq_parameter_set_Item)->vui_parameters->num_units_in_tick)
  702                 Fill(Stream_Video, StreamPos_Last, Video_FrameRate, (float64)(*seq_parameter_set_Item)->vui_parameters->time_scale/(*seq_parameter_set_Item)->vui_parameters->num_units_in_tick/((*seq_parameter_set_Item)->frame_mbs_only_flag?2:((*seq_parameter_set_Item)->pic_order_cnt_type==2?1:2))/FrameRate_Divider);
  703         }
  704 
  705         //Colour description
  706         if (preferred_transfer_characteristics!=2)
  707             Fill(Stream_Video, 0, Video_transfer_characteristics, Mpegv_transfer_characteristics(preferred_transfer_characteristics));
  708         if ((*seq_parameter_set_Item)->vui_parameters->video_signal_type_present_flag)
  709         {
  710             Fill(Stream_Video, 0, Video_Standard, Avc_video_format[(*seq_parameter_set_Item)->vui_parameters->video_format]);
  711             Fill(Stream_Video, 0, Video_colour_range, Avc_video_full_range[(*seq_parameter_set_Item)->vui_parameters->video_full_range_flag]);
  712             if ((*seq_parameter_set_Item)->vui_parameters->colour_description_present_flag)
  713             {
  714                 Fill(Stream_Video, 0, Video_colour_description_present, "Yes");
  715                 Fill(Stream_Video, 0, Video_colour_primaries, Mpegv_colour_primaries((*seq_parameter_set_Item)->vui_parameters->colour_primaries));
  716                 Fill(Stream_Video, 0, Video_transfer_characteristics, Mpegv_transfer_characteristics((*seq_parameter_set_Item)->vui_parameters->transfer_characteristics));
  717                 Fill(Stream_Video, 0, Video_matrix_coefficients, Mpegv_matrix_coefficients((*seq_parameter_set_Item)->vui_parameters->matrix_coefficients));
  718                 if ((*seq_parameter_set_Item)->vui_parameters->matrix_coefficients!=2)
  719                     Fill(Stream_Video, 0, Video_ColorSpace, Mpegv_matrix_coefficients_ColorSpace((*seq_parameter_set_Item)->vui_parameters->matrix_coefficients), Unlimited, true, true);
  720             }
  721         }
  722 
  723         //hrd_parameter_sets
  724         int64u bit_rate_value=(int64u)-1;
  725         bool   bit_rate_value_IsValid=true;
  726         bool   cbr_flag=false;
  727         bool   cbr_flag_IsSet=false;
  728         bool   cbr_flag_IsValid=true;
  729         seq_parameter_set_struct::vui_parameters_struct::xxl* NAL=(*seq_parameter_set_Item)->vui_parameters->NAL;
  730         if (NAL)
  731             for (size_t Pos=0; Pos<NAL->SchedSel.size(); Pos++)
  732             {
  733                 if (NAL->SchedSel[Pos].cpb_size_value!=(int32u)-1)
  734                     Fill(Stream_Video, 0, Video_BufferSize, NAL->SchedSel[Pos].cpb_size_value);
  735                 if (bit_rate_value!=(int64u)-1 && bit_rate_value!=NAL->SchedSel[Pos].bit_rate_value)
  736                     bit_rate_value_IsValid=false;
  737                 if (bit_rate_value==(int64u)-1)
  738                     bit_rate_value=NAL->SchedSel[Pos].bit_rate_value;
  739                 if (cbr_flag_IsSet==true && cbr_flag!=NAL->SchedSel[Pos].cbr_flag)
  740                     cbr_flag_IsValid=false;
  741                 if (cbr_flag_IsSet==0)
  742                 {
  743                     cbr_flag=NAL->SchedSel[Pos].cbr_flag;
  744                     cbr_flag_IsSet=true;
  745                 }
  746             }
  747         seq_parameter_set_struct::vui_parameters_struct::xxl* VCL=(*seq_parameter_set_Item)->vui_parameters->VCL;
  748         if (VCL)
  749             for (size_t Pos=0; Pos<VCL->SchedSel.size(); Pos++)
  750             {
  751                 Fill(Stream_Video, 0, Video_BufferSize, VCL->SchedSel[Pos].cpb_size_value);
  752                 if (bit_rate_value!=(int64u)-1 && bit_rate_value!=VCL->SchedSel[Pos].bit_rate_value)
  753                     bit_rate_value_IsValid=false;
  754                 if (bit_rate_value==(int64u)-1)
  755                     bit_rate_value=VCL->SchedSel[Pos].bit_rate_value;
  756                 if (cbr_flag_IsSet==true && cbr_flag!=VCL->SchedSel[Pos].cbr_flag)
  757                     cbr_flag_IsValid=false;
  758                 if (cbr_flag_IsSet==0)
  759                 {
  760                     cbr_flag=VCL->SchedSel[Pos].cbr_flag;
  761                     cbr_flag_IsSet=true;
  762                 }
  763             }
  764         if (cbr_flag_IsSet && cbr_flag_IsValid)
  765         {
  766             Fill(Stream_Video, 0, Video_BitRate_Mode, cbr_flag?"CBR":"VBR");
  767             if (bit_rate_value!=(int64u)-1 && bit_rate_value_IsValid)
  768                 Fill(Stream_Video, 0, cbr_flag?Video_BitRate_Nominal:Video_BitRate_Maximum, bit_rate_value);
  769         }
  770     }
  771 
  772     if (Count_Get(Stream_Video)==0)
  773         Stream_Prepare(Stream_Video);
  774     Fill(Stream_Video, 0, Video_Format, "AVC");
  775     Fill(Stream_Video, 0, Video_Codec, "AVC");
  776 
  777     Ztring Profile=Ztring().From_UTF8(Avc_profile_idc((*seq_parameter_set_Item)->profile_idc));
  778     switch ((*seq_parameter_set_Item)->profile_idc)
  779     {
  780         case  44 : // CAVLC 4:4:4 Intra
  781         case 100 : // High
  782         case 110 : // High 10
  783         case 122 : // High 4:2:2"
  784         case 244 : // High 4:4:4 Predictive
  785                     if ((*seq_parameter_set_Item)->constraint_set3_flag)
  786                         Profile+=__T(" Intra");
  787     }
  788     Profile+=__T("@L")+Ztring().From_Number(((float)(*seq_parameter_set_Item)->level_idc)/10, ((*seq_parameter_set_Item)->level_idc%10)?1:0);
  789     Fill(Stream_Video, 0, Video_Format_Profile, Profile);
  790     Fill(Stream_Video, 0, Video_Codec_Profile, Profile);
  791     Fill(Stream_Video, StreamPos_Last, Video_Width, Width);
  792     Fill(Stream_Video, StreamPos_Last, Video_Height, Height);
  793     if ((*seq_parameter_set_Item)->frame_crop_left_offset || (*seq_parameter_set_Item)->frame_crop_right_offset)
  794         Fill(Stream_Video, StreamPos_Last, Video_Stored_Width, ((*seq_parameter_set_Item)->pic_width_in_mbs_minus1       +1)*16);
  795     if ((*seq_parameter_set_Item)->frame_crop_top_offset || (*seq_parameter_set_Item)->frame_crop_bottom_offset)
  796         Fill(Stream_Video, StreamPos_Last, Video_Stored_Height, ((*seq_parameter_set_Item)->pic_height_in_map_units_minus1+1)*16*(2-(*seq_parameter_set_Item)->frame_mbs_only_flag));
  797     Fill(Stream_Video, 0, Video_PixelAspectRatio, PixelAspectRatio, 3, true);
  798     if(Height)
  799         Fill(Stream_Video, 0, Video_DisplayAspectRatio, Width*PixelAspectRatio/Height, 3, true); //More precise
  800     if (FrameRate_Divider==2)
  801     {
  802         Fill(Stream_Video, StreamPos_Last, Video_Format_Settings_FrameMode, "Frame doubling");
  803         Fill(Stream_Video, StreamPos_Last, Video_Format_Settings, "Frame doubling");
  804     }
  805     if (FrameRate_Divider==3)
  806     {
  807         Fill(Stream_Video, StreamPos_Last, Video_Format_Settings_FrameMode, "Frame tripling");
  808         Fill(Stream_Video, StreamPos_Last, Video_Format_Settings, "Frame tripling");
  809     }
  810 
  811     //Interlacement
  812     if ((*seq_parameter_set_Item)->mb_adaptive_frame_field_flag && Structure_Frame>0) //Interlaced macro-block
  813     {
  814         Fill(Stream_Video, 0, Video_ScanType, "MBAFF");
  815         Fill(Stream_Video, 0, Video_Interlacement, "MBAFF");
  816     }
  817     else if ((*seq_parameter_set_Item)->frame_mbs_only_flag || (Structure_Frame>0 && Structure_Field==0)) //No interlaced frame
  818     {
  819         switch ((*seq_parameter_set_Item)->pic_struct_FirstDetected)
  820         {
  821             case  3 :
  822                         Fill(Stream_Video, 0, Video_ScanType, "Interlaced");
  823                         Fill(Stream_Video, 0, Video_Interlacement, "TFF");
  824                         Fill(Stream_Video, 0, Video_Format_Settings_PictureStructure, "Frame");
  825                         Fill(Stream_Video, 0, Video_ScanType_StoreMethod, "InterleavedFields");
  826                         break;
  827             case  4 :
  828                         Fill(Stream_Video, 0, Video_ScanType, "Interlaced");
  829                         Fill(Stream_Video, 0, Video_Interlacement, "BFF");
  830                         Fill(Stream_Video, 0, Video_Format_Settings_PictureStructure, "Frame");
  831                         Fill(Stream_Video, 0, Video_ScanType_StoreMethod, "InterleavedFields");
  832                         break;
  833             default :
  834                         Fill(Stream_Video, 0, Video_ScanType, "Progressive");
  835                         Fill(Stream_Video, 0, Video_Interlacement, "PPF");
  836         }
  837     }
  838     else if (Structure_Field>0)
  839     {
  840         Fill(Stream_Video, 0, Video_ScanType, "Interlaced");
  841         Fill(Stream_Video, 0, Video_Interlacement, "Interlaced");
  842     }
  843     std::string ScanOrders, PictureTypes(PictureTypes_PreviousFrames);
  844     ScanOrders.reserve(TemporalReferences.size());
  845     for (size_t Pos=0; Pos<TemporalReferences.size(); Pos++)
  846         if (TemporalReferences[Pos])
  847         {
  848             ScanOrders+=TemporalReferences[Pos]->IsTop?'T':'B';
  849             if ((Pos%2)==0)
  850                 PictureTypes+=Avc_slice_type[TemporalReferences[Pos]->slice_type];
  851         }
  852         else if (!PictureTypes.empty()) //Only if stream already started
  853         {
  854             ScanOrders+=' ';
  855             if ((Pos%2)==0)
  856                 PictureTypes+=' ';
  857         }
  858     Fill(Stream_Video, 0, Video_ScanOrder, ScanOrder_Detect(ScanOrders));
  859     { //Legacy
  860         string Result=ScanOrder_Detect(ScanOrders);
  861         if (!Result.empty())
  862         {
  863             Fill(Stream_Video, 0, Video_Interlacement, Result, true, true);
  864             Fill(Stream_Video, 0, Video_ScanType_StoreMethod, "SeparatedFields", Unlimited, true, true);
  865         }
  866         else
  867         {
  868             switch ((*seq_parameter_set_Item)->pic_struct_FirstDetected)
  869             {
  870                 case  1 :
  871                             Fill(Stream_Video, 0, Video_ScanOrder, (string) "TFF");
  872                             Fill(Stream_Video, 0, Video_ScanType_StoreMethod, "SeparatedFields", Unlimited, true, true);
  873                             break;
  874                 case  2 :
  875                             Fill(Stream_Video, 0, Video_ScanOrder, (string) "BFF");
  876                             Fill(Stream_Video, 0, Video_ScanType_StoreMethod, "SeparatedFields", Unlimited, true, true);
  877                             break;
  878                 case  3 :
  879                             Fill(Stream_Video, 0, Video_ScanOrder, (string) "TFF");
  880                             Fill(Stream_Video, 0, Video_ScanType_StoreMethod, "InterleavedFields", Unlimited, true, true);
  881                             break;
  882                 case  4 :
  883                             Fill(Stream_Video, 0, Video_ScanOrder, (string) "BFF");
  884                             Fill(Stream_Video, 0, Video_ScanType_StoreMethod, "InterleavedFields", Unlimited, true, true);
  885                             break;
  886                 default : ;
  887             }
  888         }
  889     }
  890     Fill(Stream_Video, 0, Video_Format_Settings_GOP, GOP_Detect(PictureTypes));
  891 
  892     Fill(Stream_General, 0, General_Encoded_Library, Encoded_Library);
  893     Fill(Stream_General, 0, General_Encoded_Library_Name, Encoded_Library_Name);
  894     Fill(Stream_General, 0, General_Encoded_Library_Version, Encoded_Library_Version);
  895     Fill(Stream_General, 0, General_Encoded_Library_Settings, Encoded_Library_Settings);
  896     Fill(Stream_Video, 0, Video_Encoded_Library, Encoded_Library);
  897     Fill(Stream_Video, 0, Video_Encoded_Library_Name, Encoded_Library_Name);
  898     Fill(Stream_Video, 0, Video_Encoded_Library_Version, Encoded_Library_Version);
  899     Fill(Stream_Video, 0, Video_Encoded_Library_Settings, Encoded_Library_Settings);
  900     Fill(Stream_Video, 0, Video_BitRate_Nominal, BitRate_Nominal);
  901     Fill(Stream_Video, 0, Video_MuxingMode, MuxingMode);
  902     for (std::vector<pic_parameter_set_struct*>::iterator pic_parameter_set_Item=pic_parameter_sets.begin(); pic_parameter_set_Item!=pic_parameter_sets.end(); ++pic_parameter_set_Item)
  903         if (*pic_parameter_set_Item && (*pic_parameter_set_Item)->seq_parameter_set_id==seq_parameter_set_Item-(seq_parameter_sets.empty()?subset_seq_parameter_sets.begin():seq_parameter_sets.begin()))
  904         {
  905             if ((*pic_parameter_set_Item)->entropy_coding_mode_flag)
  906             {
  907                 Fill(Stream_Video, 0, Video_Format_Settings, "CABAC");
  908                 Fill(Stream_Video, 0, Video_Format_Settings_CABAC, "Yes");
  909                 Fill(Stream_Video, 0, Video_Codec_Settings, "CABAC");
  910                 Fill(Stream_Video, 0, Video_Codec_Settings_CABAC, "Yes");
  911             }
  912             else
  913             {
  914                 Fill(Stream_Video, 0, Video_Format_Settings_CABAC, "No");
  915                 Fill(Stream_Video, 0, Video_Codec_Settings_CABAC, "No");
  916             }
  917             break; //TODO: currently, testing only the first pic_parameter_set
  918         }
  919     if ((*seq_parameter_set_Item)->max_num_ref_frames>0)
  920     {
  921         Fill(Stream_Video, 0, Video_Format_Settings, Ztring::ToZtring((*seq_parameter_set_Item)->max_num_ref_frames)+__T(" Ref Frames"));
  922         Fill(Stream_Video, 0, Video_Codec_Settings, Ztring::ToZtring((*seq_parameter_set_Item)->max_num_ref_frames)+__T(" Ref Frames"));
  923         Fill(Stream_Video, 0, Video_Format_Settings_RefFrames, (*seq_parameter_set_Item)->max_num_ref_frames);
  924         Fill(Stream_Video, 0, Video_Codec_Settings_RefFrames, (*seq_parameter_set_Item)->max_num_ref_frames);
  925     }
  926     if (Retrieve(Stream_Video, 0, Video_ColorSpace).empty())
  927         Fill(Stream_Video, 0, Video_ColorSpace, Avc_ChromaSubsampling_format_idc_ColorSpace((*seq_parameter_set_Item)->chroma_format_idc));
  928     Fill(Stream_Video, 0, Video_ChromaSubsampling, Avc_ChromaSubsampling_format_idc((*seq_parameter_set_Item)->chroma_format_idc));
  929     if ((*seq_parameter_set_Item)->bit_depth_luma_minus8==(*seq_parameter_set_Item)->bit_depth_chroma_minus8)
  930         Fill(Stream_Video, 0, Video_BitDepth, (*seq_parameter_set_Item)->bit_depth_luma_minus8+8);
  931 
  932     hdr::iterator EtsiTs103433 = HDR.find(HdrFormat_EtsiTs103433);
  933     if (EtsiTs103433 != HDR.end())
  934     {
  935         for (std::map<video, Ztring>::iterator Item = EtsiTs103433->second.begin(); Item != EtsiTs103433->second.end(); ++Item)
  936         {
  937             Fill(Stream_Video, 0, Item->first, Item->second);
  938         }
  939     }
  940     hdr::iterator SmpteSt209440 = HDR.find(HdrFormat_SmpteSt209440);
  941     if (SmpteSt209440 != HDR.end())
  942     {
  943         for (std::map<video, Ztring>::iterator Item = SmpteSt209440->second.begin(); Item != SmpteSt209440->second.end(); ++Item)
  944         {
  945             switch (Item->first)
  946             {
  947             case Video_MasteringDisplay_ColorPrimaries:
  948             case Video_MasteringDisplay_Luminance:
  949                 if (Retrieve_Const(Stream_Video, 0, Item->first) == Item->second)
  950                     break;
  951                 // Fallthrough
  952             default:
  953                 Fill(Stream_Video, 0, Item->first, Item->second);
  954             }
  955         }
  956     }
  957     hdr::iterator SmpteSt2086 = HDR.find(HdrFormat_SmpteSt2086);
  958     if (SmpteSt2086 != HDR.end())
  959     {
  960         for (std::map<video, Ztring>::iterator Item = SmpteSt2086->second.begin(); Item != SmpteSt2086->second.end(); ++Item)
  961         {
  962             bool Ignore;
  963             switch (Item->first)
  964             {
  965             case Video_HDR_Format:
  966                 Ignore = !Retrieve_Const(Stream_Video, 0, Item->first).empty();
  967                 break;
  968             case Video_MasteringDisplay_ColorPrimaries:
  969             case Video_MasteringDisplay_Luminance:
  970                 Ignore = Retrieve_Const(Stream_Video, 0, Item->first) == Item->second;
  971                 break;
  972             }
  973             if (!Ignore)
  974                 Fill(Stream_Video, 0, Item->first, Item->second);
  975         }
  976     }
  977 
  978     if (maximum_content_light_level)
  979         Fill(Stream_Video, 0, "MaxCLL", Ztring::ToZtring(maximum_content_light_level) + __T(" cd/m2"));
  980     if (maximum_frame_average_light_level)
  981         Fill(Stream_Video, 0, "MaxFALL", Ztring::ToZtring(maximum_frame_average_light_level) + __T(" cd/m2"));
  982 }
  983 
  984 //---------------------------------------------------------------------------
  985 void File_Avc::Streams_Fill_subset(const std::vector<seq_parameter_set_struct*>::iterator seq_parameter_set_Item)
  986 {
  987     Ztring Profile=Ztring().From_UTF8(Avc_profile_idc((*seq_parameter_set_Item)->profile_idc))+__T("@L")+Ztring().From_Number(((float)(*seq_parameter_set_Item)->level_idc)/10, 1);
  988     Ztring Profile_Base=Retrieve(Stream_Video, 0, Video_Format_Profile);
  989     Fill(Stream_Video, 0, Video_Format_Profile, Profile, true);
  990     if (!Profile_Base.empty())
  991         Fill(Stream_Video, 0, Video_Format_Profile, Profile_Base);
  992 }
  993 
  994 //---------------------------------------------------------------------------
  995 void File_Avc::Streams_Finish()
  996 {
  997     if (PTS_End!=(int64u)-1 && (IsSub || File_Offset+Buffer_Offset+Element_Size==File_Size))
  998     {
  999         if (PTS_End>PTS_Begin)
 1000             Fill(Stream_Video, 0, Video_Duration, float64_int64s(((float64)(PTS_End-PTS_Begin))/1000000));
 1001     }
 1002 
 1003     //GA94 captions
 1004     #if defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 1005         if (GA94_03_Parser && GA94_03_Parser->Status[IsAccepted])
 1006         {
 1007             Clear(Stream_Text);
 1008 
 1009             Finish(GA94_03_Parser);
 1010             Merge(*GA94_03_Parser);
 1011 
 1012             Ztring LawRating=GA94_03_Parser->Retrieve(Stream_General, 0, General_LawRating);
 1013             if (!LawRating.empty())
 1014                 Fill(Stream_General, 0, General_LawRating, LawRating, true);
 1015             Ztring Title=GA94_03_Parser->Retrieve(Stream_General, 0, General_Title);
 1016             if (!Title.empty() && Retrieve(Stream_General, 0, General_Title).empty())
 1017                 Fill(Stream_General, 0, General_Title, Title);
 1018 
 1019             for (size_t Pos=0; Pos<Count_Get(Stream_Text); Pos++)
 1020             {
 1021                 Ztring MuxingMode=Retrieve(Stream_Text, Pos, "MuxingMode");
 1022                 Fill(Stream_Text, Pos, "MuxingMode", __T("SCTE 128 / ")+MuxingMode, true);
 1023             }
 1024         }
 1025     #endif //defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 1026 
 1027     #if MEDIAINFO_IBIUSAGE
 1028         if (seq_parameter_sets.size()==1 && (*seq_parameter_sets.begin())->vui_parameters && (*seq_parameter_sets.begin())->vui_parameters->timing_info_present_flag && (*seq_parameter_sets.begin())->vui_parameters->fixed_frame_rate_flag)
 1029             Ibi_Stream_Finish((*seq_parameter_sets.begin())->vui_parameters->time_scale, (*seq_parameter_sets.begin())->vui_parameters->num_units_in_tick);
 1030     #endif //MEDIAINFO_IBIUSAGE
 1031 }
 1032 
 1033 //***************************************************************************
 1034 // Buffer - File header
 1035 //***************************************************************************
 1036 
 1037 //---------------------------------------------------------------------------
 1038 bool File_Avc::FileHeader_Begin()
 1039 {
 1040     if (!File__Analyze::FileHeader_Begin_0x000001())
 1041         return false;
 1042 
 1043     if (!MustSynchronize)
 1044     {
 1045         Synched_Init();
 1046         Buffer_TotalBytes_FirstSynched=0;
 1047         File_Offset_FirstSynched=File_Offset;
 1048     }
 1049 
 1050     //All should be OK
 1051     return true;
 1052 }
 1053 
 1054 //***************************************************************************
 1055 // Buffer - Synchro
 1056 //***************************************************************************
 1057 
 1058 //---------------------------------------------------------------------------
 1059 bool File_Avc::Synchronize()
 1060 {
 1061     //Synchronizing
 1062     size_t Buffer_Offset_Min=Buffer_Offset;
 1063     while(Buffer_Offset+4<=Buffer_Size && (Buffer[Buffer_Offset  ]!=0x00
 1064                                         || Buffer[Buffer_Offset+1]!=0x00
 1065                                         || Buffer[Buffer_Offset+2]!=0x01))
 1066     {
 1067         Buffer_Offset+=2;
 1068         while(Buffer_Offset<Buffer_Size && Buffer[Buffer_Offset]!=0x00)
 1069             Buffer_Offset+=2;
 1070         if (Buffer_Offset>=Buffer_Size || Buffer[Buffer_Offset-1]==0x00)
 1071             Buffer_Offset--;
 1072     }
 1073     if (Buffer_Offset>Buffer_Offset_Min && Buffer[Buffer_Offset-1]==0x00)
 1074         Buffer_Offset--;
 1075 
 1076     //Parsing last bytes if needed
 1077     if (Buffer_Offset+4==Buffer_Size && (Buffer[Buffer_Offset  ]!=0x00
 1078                                       || Buffer[Buffer_Offset+1]!=0x00
 1079                                       || Buffer[Buffer_Offset+2]!=0x00
 1080                                       || Buffer[Buffer_Offset+3]!=0x01))
 1081         Buffer_Offset++;
 1082     if (Buffer_Offset+3==Buffer_Size && (Buffer[Buffer_Offset  ]!=0x00
 1083                                       || Buffer[Buffer_Offset+1]!=0x00
 1084                                       || Buffer[Buffer_Offset+2]!=0x01))
 1085         Buffer_Offset++;
 1086     if (Buffer_Offset+2==Buffer_Size && (Buffer[Buffer_Offset  ]!=0x00
 1087                                       || Buffer[Buffer_Offset+1]!=0x00))
 1088         Buffer_Offset++;
 1089     if (Buffer_Offset+1==Buffer_Size &&  Buffer[Buffer_Offset  ]!=0x00)
 1090         Buffer_Offset++;
 1091 
 1092     if (Buffer_Offset+4>Buffer_Size)
 1093         return false;
 1094 
 1095     //Synched is OK
 1096     Synched=true;
 1097     return true;
 1098 }
 1099 
 1100 //---------------------------------------------------------------------------
 1101 bool File_Avc::Synched_Test()
 1102 {
 1103     //Must have enough buffer for having header
 1104     if (Buffer_Offset+6>Buffer_Size)
 1105         return false;
 1106 
 1107     //Quick test of synchro
 1108     if (Buffer[Buffer_Offset  ]!=0x00
 1109      || Buffer[Buffer_Offset+1]!=0x00
 1110      || (Buffer[Buffer_Offset+2]!=0x01 && (Buffer[Buffer_Offset+2]!=0x00 || Buffer[Buffer_Offset+3]!=0x01)))
 1111     {
 1112         Synched=false;
 1113         return true;
 1114     }
 1115 
 1116     //Quick search
 1117     if (!Header_Parser_QuickSearch())
 1118         return false;
 1119 
 1120     #if MEDIAINFO_IBIUSAGE
 1121         bool zero_byte=Buffer[Buffer_Offset+2]==0x00;
 1122         bool RandomAccess=(Buffer[Buffer_Offset+(zero_byte?4:3)]&0x1F)==0x07 || ((Buffer[Buffer_Offset+(zero_byte?4:3)]&0x1F)==0x09 && ((Buffer[Buffer_Offset+(zero_byte?5:4)]&0xE0)==0x00 || (Buffer[Buffer_Offset+(zero_byte?5:4)]&0xE0)==0xA0)); //seq_parameter_set or access_unit_delimiter with value=0 or 5 (3 bits)
 1123         if (RandomAccess)
 1124             Ibi_Add();
 1125     #endif //MEDIAINFO_IBIUSAGE
 1126 
 1127     //We continue
 1128     return true;
 1129 }
 1130 
 1131 //***************************************************************************
 1132 // Buffer - Demux
 1133 //***************************************************************************
 1134 
 1135 //---------------------------------------------------------------------------
 1136 #if MEDIAINFO_DEMUX
 1137 void File_Avc::Data_Parse_Iso14496()
 1138 {
 1139     if (Demux_Avc_Transcode_Iso14496_15_to_Iso14496_10)
 1140     {
 1141         if (Element_Code==0x07)
 1142         {
 1143             std::vector<seq_parameter_set_struct*>::iterator Data_Item=seq_parameter_sets.begin();
 1144             if (Data_Item!=seq_parameter_sets.end() && (*Data_Item))
 1145             {
 1146                 (*Data_Item)->Init_Iso14496_10(0x67, Buffer+Buffer_Offset, Element_Size);
 1147             }
 1148         }
 1149         if (Element_Code==0x08)
 1150         {
 1151             std::vector<pic_parameter_set_struct*>::iterator Data_Item=pic_parameter_sets.begin();
 1152             if (Data_Item!=pic_parameter_sets.end() && (*Data_Item))
 1153             {
 1154                 (*Data_Item)->Init_Iso14496_10(0x68, Buffer+Buffer_Offset, Element_Size);
 1155             }
 1156         }
 1157         if (Element_Code==0x0F)
 1158         {
 1159             std::vector<seq_parameter_set_struct*>::iterator Data_Item=subset_seq_parameter_sets.begin();
 1160             if (Data_Item!=subset_seq_parameter_sets.end() && (*Data_Item))
 1161             {
 1162                 SizeOfNALU_Minus1=0;
 1163                 (*Data_Item)->Init_Iso14496_10(0x6F, Buffer+Buffer_Offset, Element_Size);
 1164             }
 1165         }
 1166     }
 1167 }
 1168 
 1169 //---------------------------------------------------------------------------
 1170 bool File_Avc::Demux_UnpacketizeContainer_Test()
 1171 {
 1172     const int8u*    Buffer_Temp=NULL;
 1173     size_t          Buffer_Temp_Size=0;
 1174     bool            RandomAccess=true; //Default, in case of problem
 1175 
 1176     if ((MustParse_SPS_PPS || SizedBlocks) && Demux_Avc_Transcode_Iso14496_15_to_Iso14496_10)
 1177     {
 1178         if (MustParse_SPS_PPS)
 1179             return true; //Wait for SPS and PPS
 1180 
 1181         //Random access check
 1182         RandomAccess=false;
 1183 
 1184         //Computing final size
 1185         size_t TranscodedBuffer_Size=0;
 1186         size_t Buffer_Offset_Save=Buffer_Offset;
 1187         while (Buffer_Offset+SizeOfNALU_Minus1+1+1<=Buffer_Size)
 1188         {
 1189             size_t Size;
 1190             if (Buffer_Offset+SizeOfNALU_Minus1>Buffer_Size)
 1191             {
 1192                 Size=0;
 1193                 Buffer_Offset=Buffer_Size;
 1194             }
 1195             else
 1196             switch (SizeOfNALU_Minus1)
 1197             {
 1198                 case 0: Size=Buffer[Buffer_Offset];
 1199                         TranscodedBuffer_Size+=2;
 1200                         break;
 1201                 case 1: Size=BigEndian2int16u(Buffer+Buffer_Offset);
 1202                         TranscodedBuffer_Size++;
 1203                         break;
 1204                 case 2: Size=BigEndian2int24u(Buffer+Buffer_Offset);
 1205                         break;
 1206                 case 3: Size=BigEndian2int32u(Buffer+Buffer_Offset);
 1207                         TranscodedBuffer_Size--;
 1208                         break;
 1209                 default:    return true; //Problem
 1210             }
 1211             Size+=SizeOfNALU_Minus1+1;
 1212 
 1213             //Coherency checking
 1214             if (Size==0 || Buffer_Offset+Size>Buffer_Size || (Buffer_Offset+Size!=Buffer_Size && Buffer_Offset+Size+SizeOfNALU_Minus1+1>Buffer_Size))
 1215                 Size=Buffer_Size-Buffer_Offset;
 1216 
 1217             //Random access check
 1218             if (!RandomAccess && Buffer_Offset+SizeOfNALU_Minus1+1<Buffer_Size && (Buffer[Buffer_Offset+SizeOfNALU_Minus1+1]&0x1F) && (Buffer[Buffer_Offset+SizeOfNALU_Minus1+1]&0x1F)<=5) //Is a slice
 1219             {
 1220                 int32u slice_type;
 1221                 Element_Offset=SizeOfNALU_Minus1+1+1;
 1222                 Element_Size=Size;
 1223                 BS_Begin();
 1224                 Skip_UE("first_mb_in_slice");
 1225                 Get_UE (slice_type, "slice_type");
 1226                 BS_End();
 1227                 Element_Offset=0;
 1228 
 1229                 switch (slice_type)
 1230                 {
 1231                     case 2 :
 1232                     case 7 :
 1233                                 RandomAccess=true;
 1234                 }
 1235             }
 1236 
 1237             TranscodedBuffer_Size+=Size;
 1238             Buffer_Offset+=Size;
 1239         }
 1240         Buffer_Offset=Buffer_Offset_Save;
 1241 
 1242         //Adding SPS/PPS sizes
 1243         if (RandomAccess)
 1244         {
 1245             for (seq_parameter_set_structs::iterator Data_Item=seq_parameter_sets.begin(); Data_Item!=seq_parameter_sets.end(); ++Data_Item)
 1246                 TranscodedBuffer_Size+=(*Data_Item)->Iso14496_10_Buffer_Size;
 1247             for (seq_parameter_set_structs::iterator Data_Item=subset_seq_parameter_sets.begin(); Data_Item!=subset_seq_parameter_sets.end(); ++Data_Item)
 1248                 TranscodedBuffer_Size+=(*Data_Item)->Iso14496_10_Buffer_Size;
 1249             for (pic_parameter_set_structs::iterator Data_Item=pic_parameter_sets.begin(); Data_Item!=pic_parameter_sets.end(); ++Data_Item)
 1250                 TranscodedBuffer_Size+=(*Data_Item)->Iso14496_10_Buffer_Size;
 1251         }
 1252 
 1253         //Copying
 1254         int8u* TranscodedBuffer=new int8u[TranscodedBuffer_Size+100];
 1255         size_t TranscodedBuffer_Pos=0;
 1256         if (RandomAccess)
 1257         {
 1258             for (seq_parameter_set_structs::iterator Data_Item=seq_parameter_sets.begin(); Data_Item!=seq_parameter_sets.end(); ++Data_Item)
 1259             {
 1260                 std::memcpy(TranscodedBuffer+TranscodedBuffer_Pos, (*Data_Item)->Iso14496_10_Buffer, (*Data_Item)->Iso14496_10_Buffer_Size);
 1261                 TranscodedBuffer_Pos+=(*Data_Item)->Iso14496_10_Buffer_Size;
 1262             }
 1263             for (seq_parameter_set_structs::iterator Data_Item=subset_seq_parameter_sets.begin(); Data_Item!=subset_seq_parameter_sets.end(); ++Data_Item)
 1264             {
 1265                 std::memcpy(TranscodedBuffer+TranscodedBuffer_Pos, (*Data_Item)->Iso14496_10_Buffer, (*Data_Item)->Iso14496_10_Buffer_Size);
 1266                 TranscodedBuffer_Pos+=(*Data_Item)->Iso14496_10_Buffer_Size;
 1267             }
 1268             for (pic_parameter_set_structs::iterator Data_Item=pic_parameter_sets.begin(); Data_Item!=pic_parameter_sets.end(); ++Data_Item)
 1269             {
 1270                 std::memcpy(TranscodedBuffer+TranscodedBuffer_Pos, (*Data_Item)->Iso14496_10_Buffer, (*Data_Item)->Iso14496_10_Buffer_Size);
 1271                 TranscodedBuffer_Pos+=(*Data_Item)->Iso14496_10_Buffer_Size;
 1272             }
 1273         }
 1274         while (Buffer_Offset<Buffer_Size)
 1275         {
 1276             //Sync layer
 1277             TranscodedBuffer[TranscodedBuffer_Pos]=0x00;
 1278             TranscodedBuffer_Pos++;
 1279             TranscodedBuffer[TranscodedBuffer_Pos]=0x00;
 1280             TranscodedBuffer_Pos++;
 1281             TranscodedBuffer[TranscodedBuffer_Pos]=0x01;
 1282             TranscodedBuffer_Pos++;
 1283 
 1284             //Block
 1285             size_t Size;
 1286             switch (SizeOfNALU_Minus1)
 1287             {
 1288                 case 0: Size=Buffer[Buffer_Offset];
 1289                         Buffer_Offset++;
 1290                         break;
 1291                 case 1: Size=BigEndian2int16u(Buffer+Buffer_Offset);
 1292                         Buffer_Offset+=2;
 1293                         break;
 1294                 case 2: Size=BigEndian2int24u(Buffer+Buffer_Offset);
 1295                         Buffer_Offset+=3;
 1296                         break;
 1297                 case 3: Size=BigEndian2int32u(Buffer+Buffer_Offset);
 1298                         Buffer_Offset+=4;
 1299                         break;
 1300                 default: //Problem
 1301                         delete [] TranscodedBuffer;
 1302                         return false;
 1303             }
 1304 
 1305             //Coherency checking
 1306             if (Size==0 || Buffer_Offset+Size>Buffer_Size || (Buffer_Offset+Size!=Buffer_Size && Buffer_Offset+Size+SizeOfNALU_Minus1+1>Buffer_Size))
 1307                 Size=Buffer_Size-Buffer_Offset;
 1308 
 1309             std::memcpy(TranscodedBuffer+TranscodedBuffer_Pos, Buffer+Buffer_Offset, Size);
 1310             TranscodedBuffer_Pos+=Size;
 1311             Buffer_Offset+=Size;
 1312         }
 1313         Buffer_Offset=0;
 1314 
 1315         Buffer_Temp=Buffer;
 1316         Buffer=TranscodedBuffer;
 1317         Buffer_Temp_Size=Buffer_Size;
 1318         Buffer_Size=TranscodedBuffer_Size;
 1319         Demux_Offset=Buffer_Size;
 1320     }
 1321     else
 1322     {
 1323         bool zero_byte=Buffer[Buffer_Offset+2]==0x00;
 1324         if (!(((Buffer[Buffer_Offset+(zero_byte?4:3)]&0x1B)==0x01 && (Buffer[Buffer_Offset+(zero_byte?5:4)]&0x80)!=0x80)
 1325            || (Buffer[Buffer_Offset+(zero_byte?4:3)]&0x1F)==0x0C))
 1326         {
 1327             if (Demux_Offset==0)
 1328             {
 1329                 Demux_Offset=Buffer_Offset;
 1330                 Demux_IntermediateItemFound=false;
 1331             }
 1332             while (Demux_Offset+6<=Buffer_Size)
 1333             {
 1334                 //Synchronizing
 1335                 while(Demux_Offset+6<=Buffer_Size && (Buffer[Demux_Offset  ]!=0x00
 1336                                                    || Buffer[Demux_Offset+1]!=0x00
 1337                                                    || Buffer[Demux_Offset+2]!=0x01))
 1338                 {
 1339                     Demux_Offset+=2;
 1340                     while(Demux_Offset<Buffer_Size && Buffer[Buffer_Offset]!=0x00)
 1341                         Demux_Offset+=2;
 1342                     if (Demux_Offset>=Buffer_Size || Buffer[Demux_Offset-1]==0x00)
 1343                         Demux_Offset--;
 1344                 }
 1345 
 1346                 if (Demux_Offset+6>Buffer_Size)
 1347                 {
 1348                     if (Config->IsFinishing)
 1349                         Demux_Offset=Buffer_Size;
 1350                     break;
 1351                 }
 1352 
 1353                 zero_byte=Buffer[Demux_Offset+2]==0x00;
 1354                 if (Demux_IntermediateItemFound)
 1355                 {
 1356                     if (!(((Buffer[Demux_Offset+(zero_byte?4:3)]&0x1B)==0x01 && (Buffer[Demux_Offset+(zero_byte?5:4)]&0x80)!=0x80)
 1357                         || (Buffer[Demux_Offset+(zero_byte?4:3)]&0x1F)==0x0C))
 1358                         break;
 1359                 }
 1360                 else
 1361                 {
 1362                     if ((Buffer[Demux_Offset+(zero_byte?4:3)]&0x1B)==0x01 && (Buffer[Demux_Offset+(zero_byte?5:4)]&0x80)==0x80)
 1363                         Demux_IntermediateItemFound=true;
 1364                 }
 1365 
 1366                 Demux_Offset++;
 1367             }
 1368 
 1369             if (Demux_Offset+6>Buffer_Size && !FrameIsAlwaysComplete && !Config->IsFinishing)
 1370                 return false; //No complete frame
 1371 
 1372             if (Demux_Offset && Buffer[Demux_Offset-1]==0x00)
 1373                 Demux_Offset--;
 1374 
 1375             zero_byte=Buffer[Buffer_Offset+2]==0x00;
 1376             size_t Buffer_Offset_Random=Buffer_Offset;
 1377             if ((Buffer[Buffer_Offset_Random+(zero_byte?4:3)]&0x1F)==0x09)
 1378             {
 1379                 Buffer_Offset_Random++;
 1380                 if (zero_byte)
 1381                     Buffer_Offset_Random++;
 1382                 while(Buffer_Offset_Random+6<=Buffer_Size && (Buffer[Buffer_Offset_Random  ]!=0x00
 1383                                                            || Buffer[Buffer_Offset_Random+1]!=0x00
 1384                                                            || Buffer[Buffer_Offset_Random+2]!=0x01))
 1385                     Buffer_Offset_Random++;
 1386                 zero_byte=Buffer[Buffer_Offset_Random+2]==0x00;
 1387             }
 1388             RandomAccess=Buffer_Offset_Random+6<=Buffer_Size && (Buffer[Buffer_Offset_Random+(zero_byte?4:3)]&0x1F)==0x07; //seq_parameter_set
 1389         }
 1390     }
 1391 
 1392     if (!Status[IsAccepted])
 1393     {
 1394         if (Config->Demux_EventWasSent)
 1395             return false;
 1396         File_Avc* MI=new File_Avc;
 1397         Element_Code=(int64u)-1;
 1398         Open_Buffer_Init(MI);
 1399         #ifdef MEDIAINFO_EVENTS
 1400             MediaInfo_Config_PerPackage* Config_PerPackage_Temp=MI->Config->Config_PerPackage;
 1401             MI->Config->Config_PerPackage=NULL;
 1402         #endif //MEDIAINFO_EVENTS
 1403         Open_Buffer_Continue(MI, Buffer, Buffer_Size);
 1404         #ifdef MEDIAINFO_EVENTS
 1405             MI->Config->Config_PerPackage=Config_PerPackage_Temp;
 1406         #endif //MEDIAINFO_EVENTS
 1407         bool IsOk=MI->Status[IsAccepted];
 1408         delete MI;
 1409         if (!IsOk)
 1410             return false;
 1411     }
 1412 
 1413     if (IFrame_Count || RandomAccess)
 1414     {
 1415         bool Frame_Count_NotParsedIncluded_PlusOne=false;
 1416         int64u PTS_Temp=FrameInfo.PTS;
 1417         if (!IsSub)
 1418             FrameInfo.PTS=(int64u)-1;
 1419         if (Frame_Count_NotParsedIncluded!=(int64u)-1 && Interlaced_Top!=Interlaced_Bottom)
 1420         {
 1421             Frame_Count_NotParsedIncluded--;
 1422             Frame_Count_NotParsedIncluded_PlusOne=true;
 1423         }
 1424         Demux_UnpacketizeContainer_Demux(RandomAccess);
 1425         if (!IsSub)
 1426             FrameInfo.PTS=PTS_Temp;
 1427         if (Frame_Count_NotParsedIncluded_PlusOne)
 1428             Frame_Count_NotParsedIncluded++;
 1429     }
 1430     else
 1431         Demux_UnpacketizeContainer_Demux_Clear();
 1432 
 1433     if (Buffer_Temp)
 1434     {
 1435         Demux_TotalBytes-=Buffer_Size;
 1436         Demux_TotalBytes+=Buffer_Temp_Size;
 1437         delete[] Buffer;
 1438         Buffer=Buffer_Temp;
 1439         Buffer_Size=Buffer_Temp_Size;
 1440     }
 1441 
 1442     return true;
 1443 }
 1444 #endif //MEDIAINFO_DEMUX
 1445 
 1446 //---------------------------------------------------------------------------
 1447 void File_Avc::Synched_Init()
 1448 {
 1449     if (!Frame_Count_Valid)
 1450         Frame_Count_Valid=Config->ParseSpeed>=0.3?512:2;
 1451 
 1452     //FrameInfo
 1453     PTS_End=0;
 1454     if (!IsSub)
 1455         FrameInfo.DTS=0; //No DTS in container
 1456     DTS_Begin=FrameInfo.DTS;
 1457     DTS_End=FrameInfo.DTS;
 1458 
 1459     //Temporal references
 1460     TemporalReferences_DelayedElement=NULL;
 1461     TemporalReferences_Min=0;
 1462     TemporalReferences_Max=0;
 1463     TemporalReferences_Reserved=0;
 1464     TemporalReferences_Offset=0;
 1465     TemporalReferences_Offset_pic_order_cnt_lsb_Last=0;
 1466     TemporalReferences_pic_order_cnt_Min=0;
 1467 
 1468     //Text
 1469     #if defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 1470         GA94_03_IsPresent=false;
 1471     #endif //defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 1472 
 1473     //File specific
 1474     SizeOfNALU_Minus1=(int8u)-1;
 1475 
 1476     //Status
 1477     IFrame_Count=0;
 1478     prevPicOrderCntMsb=0;
 1479     prevPicOrderCntLsb=(int32u)-1;
 1480     prevTopFieldOrderCnt=(int32u)-1;
 1481     prevFrameNum=(int32u)-1;
 1482     prevFrameNumOffset=(int32u)-1;
 1483 
 1484     //Count of a Packets
 1485     Block_Count=0;
 1486     Interlaced_Top=0;
 1487     Interlaced_Bottom=0;
 1488     Structure_Field=0;
 1489     Structure_Frame=0;
 1490 
 1491     //Temp
 1492     FrameRate_Divider=1;
 1493     FirstPFrameInGop_IsParsed=false;
 1494     Config_IsRepeated=false;
 1495     tc=0;
 1496     maximum_content_light_level=0;
 1497     maximum_frame_average_light_level=0;
 1498 
 1499     //Default values
 1500     Streams.resize(0x100);
 1501     Streams[0x06].Searching_Payload=true; //sei
 1502     Streams[0x07].Searching_Payload=true; //seq_parameter_set
 1503     Streams[0x09].Searching_Payload=true; //access_unit_delimiter
 1504     Streams[0x0F].Searching_Payload=true; //subset_seq_parameter_set
 1505     for (int8u Pos=0xFF; Pos>=0xB9; Pos--)
 1506         Streams[Pos].Searching_Payload=true; //Testing MPEG-PS
 1507 
 1508     //Options
 1509     Option_Manage();
 1510 
 1511     //Specific cases
 1512     #if MEDIAINFO_EVENTS
 1513         if (Config->ParseUndecodableFrames_Get())
 1514         {
 1515             Accept(); //In some case, we must accept the stream very quickly and before the sequence header is detected
 1516             Streams[0x01].Searching_Payload=true; //slice_header
 1517             Streams[0x05].Searching_Payload=true; //slice_header
 1518         }
 1519     #endif //MEDIAINFO_EVENTS
 1520     #if MEDIAINFO_DEMUX
 1521         Demux_Avc_Transcode_Iso14496_15_to_Iso14496_10=Config->Demux_Avc_Transcode_Iso14496_15_to_Iso14496_10_Get();
 1522     #endif //MEDIAINFO_DEMUX
 1523 }
 1524 
 1525 //***************************************************************************
 1526 // Buffer - Global
 1527 //***************************************************************************
 1528 
 1529 //---------------------------------------------------------------------------
 1530 #if MEDIAINFO_ADVANCED2
 1531 void File_Avc::Read_Buffer_SegmentChange()
 1532 {
 1533 }
 1534 #endif //MEDIAINFO_ADVANCED2
 1535 
 1536 //---------------------------------------------------------------------------
 1537 void File_Avc::Read_Buffer_Unsynched()
 1538 {
 1539     //Temporal references
 1540     Clean_Temp_References();
 1541     delete TemporalReferences_DelayedElement; TemporalReferences_DelayedElement=NULL;
 1542     TemporalReferences_Min=0;
 1543     TemporalReferences_Max=0;
 1544     TemporalReferences_Reserved=0;
 1545     TemporalReferences_Offset=0;
 1546     TemporalReferences_Offset_pic_order_cnt_lsb_Last=0;
 1547     TemporalReferences_pic_order_cnt_Min=0;
 1548 
 1549     //Text
 1550     #if defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 1551         if (GA94_03_Parser)
 1552             GA94_03_Parser->Open_Buffer_Unsynch();
 1553     #endif //defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 1554 
 1555     //parameter_sets
 1556     if (SizedBlocks || !Config_IsRepeated) //If sized blocks, it is not a broadcasted stream so SPS/PPS are only in container header, we must not disable them.
 1557     {
 1558         //Rebuilding immediatly TemporalReferences
 1559         seq_parameter_set_structs* _seq_parameter_sets=!seq_parameter_sets.empty()?&seq_parameter_sets:&subset_seq_parameter_sets; //Some MVC streams have no seq_parameter_sets. TODO: better management of temporal references
 1560         for (std::vector<seq_parameter_set_struct*>::iterator seq_parameter_set_Item=(*_seq_parameter_sets).begin(); seq_parameter_set_Item!=(*_seq_parameter_sets).end(); ++seq_parameter_set_Item)
 1561             if ((*seq_parameter_set_Item))
 1562             {
 1563                 size_t MaxNumber;
 1564                 switch ((*seq_parameter_set_Item)->pic_order_cnt_type)
 1565                 {
 1566                     case 0 : MaxNumber=(*seq_parameter_set_Item)->MaxPicOrderCntLsb; break;
 1567                     case 2 : MaxNumber=(*seq_parameter_set_Item)->MaxFrameNum*2; break;
 1568                     default: Trusted_IsNot("Not supported"); return;
 1569                 }
 1570 
 1571                 TemporalReferences.resize(4*MaxNumber);
 1572                 TemporalReferences_Reserved=MaxNumber;
 1573             }
 1574     }
 1575     else
 1576     {
 1577         Clean_Seq_Parameter();
 1578     }
 1579 
 1580     //Status
 1581     Interlaced_Top=0;
 1582     Interlaced_Bottom=0;
 1583     prevPicOrderCntMsb=0;
 1584     prevPicOrderCntLsb=(int32u)-1;
 1585     prevTopFieldOrderCnt=(int32u)-1;
 1586     prevFrameNum=(int32u)-1;
 1587     prevFrameNumOffset=(int32u)-1;
 1588 
 1589     //Temp
 1590     FrameRate_Divider=1;
 1591     FirstPFrameInGop_IsParsed=false;
 1592     tc=0;
 1593 
 1594     //Impossible to know TimeStamps now
 1595     PTS_End=0;
 1596     DTS_End=0;
 1597 }
 1598 
 1599 //***************************************************************************
 1600 // Buffer - Per element
 1601 //***************************************************************************
 1602 
 1603 //---------------------------------------------------------------------------
 1604 void File_Avc::Header_Parse()
 1605 {
 1606     //Specific case
 1607     if (MustParse_SPS_PPS)
 1608     {
 1609         Header_Fill_Size(Element_Size);
 1610         Header_Fill_Code((int64u)-1, "Specific");
 1611         return;
 1612     }
 1613 
 1614     //Parsing
 1615     int8u nal_unit_type;
 1616     if (!SizedBlocks)
 1617     {
 1618         if (Buffer[Buffer_Offset+2]==0x00)
 1619             Skip_B1(                                            "zero_byte");
 1620         Skip_B3(                                                "start_code_prefix_one_3bytes");
 1621         BS_Begin();
 1622         Mark_0 ();
 1623         Get_S1 ( 2, nal_ref_idc,                                "nal_ref_idc");
 1624         Get_S1 ( 5, nal_unit_type,                              "nal_unit_type");
 1625         BS_End();
 1626         if (!Header_Parser_Fill_Size())
 1627         {
 1628             Element_WaitForMoreData();
 1629             return;
 1630         }
 1631     }
 1632     else
 1633     {
 1634         int64u Size;
 1635         switch (SizeOfNALU_Minus1)
 1636         {
 1637             case 0: {
 1638                         int8u Size_;
 1639                         Get_B1 (Size_,                          "size");
 1640                         Size=Size_;
 1641                     }
 1642                     break;
 1643             case 1: {
 1644                         int16u Size_;
 1645                         Get_B2 (Size_,                          "size");
 1646                         Size=Size_;
 1647                     }
 1648                     break;
 1649             case 2: {
 1650                         int32u Size_;
 1651                         Get_B3 (Size_,                          "size");
 1652                         Size=Size_;
 1653                     }
 1654                     break;
 1655             case 3: {
 1656                         int32u Size_;
 1657                         Get_B4 (Size_,                          "size");
 1658                         Size=Size_;
 1659                     }
 1660                     break;
 1661         }
 1662         Size+=Element_Offset;
 1663         if (Size==Element_Offset || Buffer_Offset+Size>Buffer_Size) //If Size is 0 or Size biger than sample size, it is not normal, we skip the complete frame
 1664             Size=Buffer_Size-Buffer_Offset;
 1665         Header_Fill_Size(Size);
 1666         BS_Begin();
 1667         Mark_0 ();
 1668         Get_S1 ( 2, nal_ref_idc,                                "nal_ref_idc");
 1669         Get_S1 ( 5, nal_unit_type,                              "nal_unit_type");
 1670         BS_End();
 1671     }
 1672 
 1673     //Filling
 1674     #if MEDIAINFO_TRACE
 1675         if (Trace_Activated)
 1676             Header_Fill_Code(nal_unit_type, Ztring().From_CC1(nal_unit_type));
 1677         else
 1678     #endif //MEDIAINFO_TRACE
 1679             Header_Fill_Code(nal_unit_type);
 1680 }
 1681 
 1682 //---------------------------------------------------------------------------
 1683 bool File_Avc::Header_Parser_Fill_Size()
 1684 {
 1685     //Look for next Sync word
 1686     if (Buffer_Offset_Temp==0) //Buffer_Offset_Temp is not 0 if Header_Parse_Fill_Size() has already parsed first frames
 1687         Buffer_Offset_Temp=Buffer_Offset+4;
 1688     while (Buffer_Offset_Temp+5<=Buffer_Size
 1689         && CC3(Buffer+Buffer_Offset_Temp)!=0x000001)
 1690     {
 1691         Buffer_Offset_Temp+=2;
 1692         while(Buffer_Offset_Temp<Buffer_Size && Buffer[Buffer_Offset_Temp]!=0x00)
 1693             Buffer_Offset_Temp+=2;
 1694         if (Buffer_Offset_Temp>=Buffer_Size || Buffer[Buffer_Offset_Temp-1]==0x00)
 1695             Buffer_Offset_Temp--;
 1696     }
 1697 
 1698     //Must wait more data?
 1699     if (Buffer_Offset_Temp+5>Buffer_Size)
 1700     {
 1701         if (FrameIsAlwaysComplete || Config->IsFinishing)
 1702             Buffer_Offset_Temp=Buffer_Size; //We are sure that the next bytes are a start
 1703         else
 1704             return false;
 1705     }
 1706 
 1707     if (Buffer[Buffer_Offset_Temp-1]==0x00)
 1708         Buffer_Offset_Temp--;
 1709 
 1710     //OK, we continue
 1711     Header_Fill_Size(Buffer_Offset_Temp-Buffer_Offset);
 1712     Buffer_Offset_Temp=0;
 1713     return true;
 1714 }
 1715 
 1716 //---------------------------------------------------------------------------
 1717 bool File_Avc::Header_Parser_QuickSearch()
 1718 {
 1719     while (       Buffer_Offset+6<=Buffer_Size
 1720       &&   Buffer[Buffer_Offset  ]==0x00
 1721       &&   Buffer[Buffer_Offset+1]==0x00
 1722       &&  (Buffer[Buffer_Offset+2]==0x01
 1723         || (Buffer[Buffer_Offset+2]==0x00 && Buffer[Buffer_Offset+3]==0x01)))
 1724     {
 1725         //Getting start_code
 1726         int8u start_code;
 1727         if (Buffer[Buffer_Offset+2]==0x00)
 1728             start_code=CC1(Buffer+Buffer_Offset+4)&0x1F;
 1729         else
 1730             start_code=CC1(Buffer+Buffer_Offset+3)&0x1F;
 1731 
 1732         //Searching start
 1733         if (Streams[start_code].Searching_Payload
 1734          || Streams[start_code].ShouldDuplicate)
 1735             return true;
 1736 
 1737         //Synchronizing
 1738         Buffer_Offset+=4;
 1739         Synched=false;
 1740         if (!Synchronize())
 1741         {
 1742             UnSynched_IsNotJunk=true;
 1743             return false;
 1744         }
 1745 
 1746         if (Buffer_Offset+6>Buffer_Size)
 1747         {
 1748             UnSynched_IsNotJunk=true;
 1749             return false;
 1750         }
 1751     }
 1752 
 1753     Trusted_IsNot("AVC, Synchronisation lost");
 1754     return Synchronize();
 1755 }
 1756 
 1757 //---------------------------------------------------------------------------
 1758 void File_Avc::Data_Parse()
 1759 {
 1760     //Specific case
 1761     if (Element_Code==(int64u)-1)
 1762     {
 1763         SPS_PPS();
 1764         return;
 1765     }
 1766 
 1767     //Trailing zeroes
 1768     int64u Element_Size_SaveBeforeZeroes=Element_Size;
 1769     if (Element_Size)
 1770     {
 1771         while (Element_Size && Buffer[Buffer_Offset+(size_t)Element_Size-1]==0)
 1772             Element_Size--;
 1773     }
 1774 
 1775     //Dump of the SPS/PPS - Init
 1776     #if MEDIAINFO_ADVANCED2
 1777         size_t spspps_Size=0;
 1778         if (true) //TODO: add an option for activating this extra piece of information in the output
 1779         {
 1780             switch (Element_Code)
 1781             {
 1782                 case 0x07 : //seq_parameter_set();
 1783                             spspps_Size = seq_parameter_sets.size();
 1784                             break;
 1785                 case 0x08 : //pic_parameter_set();
 1786                             spspps_Size = pic_parameter_sets.size();
 1787                             break;
 1788                 default: ;
 1789             }
 1790         }
 1791     #endif //MEDIAINFO_ADVANCED2
 1792 
 1793     //svc_extension
 1794     bool svc_extension_flag=false;
 1795     if (Element_Code==0x0E || Element_Code==0x14)
 1796     {
 1797         BS_Begin();
 1798         Get_SB (svc_extension_flag,                             "svc_extension_flag");
 1799         if (svc_extension_flag)
 1800             nal_unit_header_svc_extension();
 1801         else
 1802             nal_unit_header_mvc_extension();
 1803         BS_End();
 1804     }
 1805 
 1806     //Searching emulation_prevention_three_byte
 1807     int8u* Buffer_3Bytes=NULL;
 1808     const int8u* Save_Buffer=Buffer;
 1809     int64u Save_File_Offset=File_Offset;
 1810     size_t Save_Buffer_Offset=Buffer_Offset;
 1811     int64u Save_Element_Size=Element_Size;
 1812     size_t Element_Offset_3Bytes=(size_t)Element_Offset;
 1813     std::vector<size_t> ThreeByte_List;
 1814     while (Element_Offset_3Bytes+3<=Element_Size)
 1815     {
 1816         if (CC3(Buffer+Buffer_Offset+(size_t)Element_Offset_3Bytes)==0x000003)
 1817             ThreeByte_List.push_back(Element_Offset_3Bytes+2);
 1818         Element_Offset_3Bytes+=2;
 1819         while(Element_Offset_3Bytes<Element_Size && Buffer[Buffer_Offset+(size_t)Element_Offset_3Bytes]!=0x00)
 1820             Element_Offset_3Bytes+=2;
 1821         if (Element_Offset_3Bytes>=Element_Size || Buffer[Buffer_Offset+(size_t)Element_Offset_3Bytes-1]==0x00)
 1822             Element_Offset_3Bytes--;
 1823     }
 1824 
 1825     if (!ThreeByte_List.empty())
 1826     {
 1827         //We must change the buffer for keeping out
 1828         Element_Size=Save_Element_Size-ThreeByte_List.size();
 1829         File_Offset+=Buffer_Offset;
 1830         Buffer_Offset=0;
 1831         Buffer_3Bytes=new int8u[(size_t)Element_Size];
 1832         for (size_t Pos=0; Pos<=ThreeByte_List.size(); Pos++)
 1833         {
 1834             size_t Pos0=(Pos==ThreeByte_List.size())?(size_t)Save_Element_Size:(ThreeByte_List[Pos]);
 1835             size_t Pos1=(Pos==0)?0:(ThreeByte_List[Pos-1]+1);
 1836             size_t Buffer_3bytes_Begin=Pos1-Pos;
 1837             size_t Save_Buffer_Begin  =Pos1;
 1838             size_t Size=               Pos0-Pos1;
 1839             std::memcpy(Buffer_3Bytes+Buffer_3bytes_Begin, Save_Buffer+Save_Buffer_Offset+Save_Buffer_Begin, Size);
 1840         }
 1841         Buffer=Buffer_3Bytes;
 1842     }
 1843 
 1844     //Parsing
 1845     switch (Element_Code)
 1846     {
 1847         case 0x00 : Element_Name("unspecified"); Skip_XX(Element_Size-Element_Offset, "Data"); break;
 1848         case 0x01 : slice_layer_without_partitioning_non_IDR(); break;
 1849         case 0x02 : Element_Name("slice_data_partition_a_layer"); Skip_XX(Element_Size-Element_Offset, "Data"); break;
 1850         case 0x03 : Element_Name("slice_data_partition_b_layer"); Skip_XX(Element_Size-Element_Offset, "Data"); break;
 1851         case 0x04 : Element_Name("slice_data_partition_c_layer"); Skip_XX(Element_Size-Element_Offset, "Data"); break;
 1852         case 0x05 : slice_layer_without_partitioning_IDR(); break;
 1853         case 0x06 : sei(); break;
 1854         case 0x07 : seq_parameter_set(); break;
 1855         case 0x08 : pic_parameter_set(); break;
 1856         case 0x09 : access_unit_delimiter(); break;
 1857         case 0x0A : Element_Name("end_of_seq"); Skip_XX(Element_Size-Element_Offset, "Data"); break;
 1858         case 0x0B : Element_Name("end_of_stream"); Skip_XX(Element_Size-Element_Offset, "Data"); break;
 1859         case 0x0C : filler_data(); break;
 1860         case 0x0D : Element_Name("seq_parameter_set_extension"); Skip_XX(Element_Size-Element_Offset, "Data"); break;
 1861         case 0x0E : prefix_nal_unit(svc_extension_flag); break;
 1862         case 0x0F : subset_seq_parameter_set(); break;
 1863         case 0x13 : Element_Name("slice_layer_without_partitioning"); Skip_XX(Element_Size-Element_Offset, "Data"); break;
 1864         case 0x14 : slice_layer_extension(svc_extension_flag); break;
 1865         default :
 1866             if (Element_Code<0x18)
 1867                 Element_Name("reserved");
 1868             else
 1869                 Element_Name("unspecified");
 1870             Skip_XX(Element_Size-Element_Offset, "Data");
 1871     }
 1872 
 1873     if (!ThreeByte_List.empty())
 1874     {
 1875         //We must change the buffer for keeping out
 1876         Element_Size=Save_Element_Size;
 1877         File_Offset=Save_File_Offset;
 1878         Buffer_Offset=Save_Buffer_Offset;
 1879         delete[] Buffer; Buffer=Save_Buffer;
 1880         Buffer_3Bytes=NULL; //Same as Buffer...
 1881         Element_Offset+=ThreeByte_List.size();
 1882     }
 1883 
 1884     //Duplicate
 1885     #if MEDIAINFO_DUPLICATE
 1886         if (!Streams.empty() && Streams[(size_t)Element_Code].ShouldDuplicate)
 1887             File__Duplicate_Write(Element_Code);
 1888     #endif //MEDIAINFO_DUPLICATE
 1889 
 1890     #if MEDIAINFO_DEMUX
 1891         Data_Parse_Iso14496();
 1892     #endif //MEDIAINFO_DEMUX
 1893 
 1894     //Dump of the SPS/PPS - Fill
 1895     #if MEDIAINFO_ADVANCED2
 1896         if (false) //TODO: add an option for activating this extra piece of information in the output
 1897         {
 1898             switch (Element_Code)
 1899             {
 1900                 case 0x07 : //seq_parameter_set();
 1901                             if (spspps_Size != seq_parameter_sets.size() && !Status[IsFilled])
 1902                             {
 1903                                 std::string Data_Raw((const char*)(Buffer+(size_t)(Buffer_Offset-1)), (size_t)(Element_Size+1)); //Including the last byte in the header
 1904                                 Dump_SPS.push_back(Base64::encode(Data_Raw));
 1905                             }
 1906                             break;
 1907                 case 0x08 : //pic_parameter_set();
 1908                             if (spspps_Size != pic_parameter_sets.size() && !Status[IsFilled])
 1909                             {
 1910                                 std::string Data_Raw((const char*)(Buffer+(size_t)(Buffer_Offset-1)), (size_t)(Element_Size+1)); //Including the last byte in the header
 1911                                 Dump_PPS.push_back(Base64::encode(Data_Raw));
 1912                             }
 1913                             break;
 1914                 default: ;
 1915             }
 1916         }
 1917     #endif //MEDIAINFO_ADVANCED2
 1918 
 1919     #if MEDIAINFO_DEMUX
 1920         Data_Parse_Iso14496();
 1921     #endif //MEDIAINFO_DEMUX
 1922 
 1923     //Trailing zeroes
 1924     Element_Size=Element_Size_SaveBeforeZeroes;
 1925 }
 1926 
 1927 //***************************************************************************
 1928 // Elements
 1929 //***************************************************************************
 1930 
 1931 //---------------------------------------------------------------------------
 1932 // Packet "01"
 1933 void File_Avc::slice_layer_without_partitioning_non_IDR()
 1934 {
 1935     Element_Name("slice_layer_without_partitioning (non-IDR)");
 1936 
 1937     //Parsing
 1938     BS_Begin();
 1939     slice_header();
 1940     slice_data(true);
 1941     BS_End();
 1942 }
 1943 
 1944 //---------------------------------------------------------------------------
 1945 // Packet "05"
 1946 void File_Avc::slice_layer_without_partitioning_IDR()
 1947 {
 1948     Element_Name("slice_layer_without_partitioning (IDR)");
 1949 
 1950     //Parsing
 1951     BS_Begin();
 1952     slice_header();
 1953     slice_data(true);
 1954     BS_End();
 1955 
 1956     FILLING_BEGIN_PRECISE();
 1957         //NextCode
 1958         for (int8u Pos=0x01; Pos<=0x05; Pos++)
 1959             NextCode_Add(Pos);
 1960     FILLING_END();
 1961 }
 1962 
 1963 //---------------------------------------------------------------------------
 1964 //
 1965 void File_Avc::slice_header()
 1966 {
 1967     //Encryption management
 1968     if (CA_system_ID_MustSkipSlices)
 1969     {
 1970         //Is not decodable
 1971         Skip_BS(Data_BS_Remain(),                               "Data");
 1972         Finish("AVC");
 1973         return;
 1974     }
 1975 
 1976     Element_Begin1("slice_header");
 1977 
 1978     //Parsing
 1979     int32u  slice_type, pic_order_cnt_lsb=(int32u)-1;
 1980     int32u  first_mb_in_slice, pic_parameter_set_id, frame_num, num_ref_idx_l0_active_minus1, num_ref_idx_l1_active_minus1, disable_deblocking_filter_idc;
 1981 
 1982     int32s  delta_pic_order_cnt_bottom=0;
 1983     bool    field_pic_flag=false, bottom_field_flag=false;
 1984     Get_UE (first_mb_in_slice,                                  "first_mb_in_slice");
 1985     Get_UE (slice_type,                                         "slice_type"); Param_Info1C((slice_type<10), Avc_slice_type[slice_type]);
 1986     #if MEDIAINFO_EVENTS
 1987         if (!first_mb_in_slice)
 1988         {
 1989             switch(Element_Code)
 1990             {
 1991                 case 5 :    // This is an IDR frame
 1992                             if (Config->Config_PerPackage) // First slice of an IDR frame
 1993                             {
 1994                                 // IDR
 1995                                 Config->Config_PerPackage->FrameForAlignment(this, true);
 1996                                 Config->Config_PerPackage->IsClosedGOP(this);
 1997                             }
 1998                             break;
 1999                 default :   ; // This is not an IDR frame
 2000             }
 2001 
 2002             EVENT_BEGIN (Video, SliceInfo, 0)
 2003                 Event.FieldPosition=Field_Count;
 2004                 Event.SlicePosition=Element_IsOK()?first_mb_in_slice:(int64u)-1;
 2005                 switch (slice_type)
 2006                 {
 2007                     case 0 :
 2008                     case 3 :
 2009                     case 5 :
 2010                     case 8 :
 2011                                 Event.SliceType=1; break;
 2012                     case 1 :
 2013                     case 6 :
 2014                                 Event.SliceType=2; break;
 2015                     case 2 :
 2016                     case 4 :
 2017                     case 7 :
 2018                     case 9 :
 2019                                 Event.SliceType=0; break;
 2020                     default:
 2021                                 Event.SliceType=(int8u)-1;
 2022                 }
 2023                 Event.Flags=0;
 2024             EVENT_END   ()
 2025         }
 2026     #endif //MEDIAINFO_EVENTS
 2027     if (slice_type>=10)
 2028     {
 2029         Skip_BS(Data_BS_Remain(),                               "Data");
 2030         Element_End0();
 2031         return;
 2032     }
 2033     Get_UE (pic_parameter_set_id,                               "pic_parameter_set_id");
 2034     std::vector<pic_parameter_set_struct*>::iterator pic_parameter_set_Item;
 2035     if (pic_parameter_set_id>=pic_parameter_sets.size() || (*(pic_parameter_set_Item=pic_parameter_sets.begin()+pic_parameter_set_id))==NULL)
 2036     {
 2037         //Not yet present
 2038         Skip_BS(Data_BS_Remain(),                               "Data (pic_parameter_set is missing)");
 2039         Element_End0();
 2040         return;
 2041     }
 2042     std::vector<seq_parameter_set_struct*>::iterator seq_parameter_set_Item;
 2043     if ((*pic_parameter_set_Item)->seq_parameter_set_id>=seq_parameter_sets.size() || (*(seq_parameter_set_Item=seq_parameter_sets.begin()+(*pic_parameter_set_Item)->seq_parameter_set_id))==NULL)
 2044     {
 2045         if ((*pic_parameter_set_Item)->seq_parameter_set_id>=subset_seq_parameter_sets.size() || (*(seq_parameter_set_Item=subset_seq_parameter_sets.begin()+(*pic_parameter_set_Item)->seq_parameter_set_id))==NULL)
 2046         {
 2047             //Not yet present
 2048             Skip_BS(Data_BS_Remain(),                           "Data (seq_parameter_set is missing)");
 2049             Element_End0();
 2050             return;
 2051         }
 2052     }
 2053     if ((*seq_parameter_set_Item)->separate_colour_plane_flag==1)
 2054         Skip_S1(2,                                              "color_plane_id");
 2055     num_ref_idx_l0_active_minus1=(*pic_parameter_set_Item)->num_ref_idx_l0_default_active_minus1; //Default
 2056     num_ref_idx_l1_active_minus1=(*pic_parameter_set_Item)->num_ref_idx_l1_default_active_minus1; //Default
 2057     Get_BS ((*seq_parameter_set_Item)->log2_max_frame_num_minus4+4, frame_num, "frame_num");
 2058     if (!(*seq_parameter_set_Item)->frame_mbs_only_flag)
 2059     {
 2060         TEST_SB_GET(field_pic_flag,                             "field_pic_flag");
 2061             Get_SB (bottom_field_flag,                          "bottom_field_flag");
 2062         TEST_SB_END();
 2063     }
 2064     if (Element_Code==5) //IdrPicFlag
 2065         Skip_UE(                                                "idr_pic_id");
 2066     if ((*seq_parameter_set_Item)->pic_order_cnt_type==0)
 2067     {
 2068         Get_BS ((*seq_parameter_set_Item)->log2_max_pic_order_cnt_lsb_minus4+4, pic_order_cnt_lsb, "pic_order_cnt_lsb");
 2069         if ((*pic_parameter_set_Item)->bottom_field_pic_order_in_frame_present_flag && !field_pic_flag)
 2070             Get_SE (delta_pic_order_cnt_bottom,                 "delta_pic_order_cnt_bottom");
 2071     }
 2072     if ((*seq_parameter_set_Item)->pic_order_cnt_type==1 && !(*seq_parameter_set_Item)->delta_pic_order_always_zero_flag )
 2073     {
 2074         Skip_SE(                                                "delta_pic_order_cnt[0]");
 2075         if((*pic_parameter_set_Item)->bottom_field_pic_order_in_frame_present_flag && !field_pic_flag)
 2076             Skip_SE(                                            "delta_pic_order_cnt[1]");
 2077     }
 2078     if((*pic_parameter_set_Item)->redundant_pic_cnt_present_flag)
 2079         Skip_UE(                                                "redundant_pic_cnt");
 2080     if (slice_type==1 || slice_type==6) //B-Frame
 2081         Skip_SB(                                                "direct_spatial_mv_pred_flag");
 2082     switch (slice_type)
 2083     {
 2084         case 0 : //P-Frame
 2085         case 1 : //B-Frame
 2086         case 3 : //SP-Frame
 2087         case 5 : //P-Frame
 2088         case 6 : //B-Frame
 2089         case 8 : //SP-Frame
 2090                     TEST_SB_SKIP(                               "num_ref_idx_active_override_flag");
 2091                         Get_UE (num_ref_idx_l0_active_minus1,   "num_ref_idx_l0_active_minus1");
 2092                         switch (slice_type)
 2093                         {
 2094                             case 1 : //B-Frame
 2095                             case 6 : //B-Frame
 2096                                         Get_UE (num_ref_idx_l1_active_minus1, "num_ref_idx_l1_active_minus1");
 2097                                         break;
 2098                             default:    ;
 2099                         }
 2100                     TEST_SB_END();
 2101                     break;
 2102         default:    ;
 2103     }
 2104     ref_pic_list_modification(slice_type, Element_Code==20); //nal_unit_type==20 --> ref_pic_list_mvc_modification()
 2105     if (((*pic_parameter_set_Item)->weighted_pred_flag && (slice_type==0 || slice_type==3 || slice_type==5 || slice_type==8))
 2106      || ((*pic_parameter_set_Item)->weighted_bipred_idc==1 && (slice_type==1 || slice_type==6)))
 2107         pred_weight_table(num_ref_idx_l0_active_minus1, num_ref_idx_l1_active_minus1, (*seq_parameter_set_Item)->ChromaArrayType());
 2108     std::vector<int8u> memory_management_control_operations;
 2109     if (nal_ref_idc)
 2110         dec_ref_pic_marking(memory_management_control_operations);
 2111 
 2112     if ((*pic_parameter_set_Item)->entropy_coding_mode_flag &&
 2113         (slice_type!=2 && slice_type!=7 && //I-Frames
 2114          slice_type!=4 && slice_type!=9))  //SI-Frames
 2115         Skip_UE(                                               "cabac_init_idc");
 2116     Skip_SE(                                                   "slice_qp_delta");
 2117     switch (slice_type)
 2118     {
 2119         case 3 : //SP-Frame
 2120         case 4 : //SI-Frame
 2121         case 8 : //SP-Frame
 2122         case 9 : //SI-Frame
 2123                 switch (slice_type)
 2124                 {
 2125                     case 3 : //SP-Frame
 2126                     case 8 : //SP-Frame
 2127                             Skip_SB(                           "sp_for_switch_flag");
 2128                             break;
 2129                     default:    ;
 2130                 }
 2131                 Skip_SE (                                      "slice_qs_delta");
 2132                 break;
 2133         default:    ;
 2134     }
 2135     if ((*pic_parameter_set_Item)->deblocking_filter_control_present_flag)
 2136     {
 2137         Get_UE(disable_deblocking_filter_idc,                  "disable_deblocking_filter_idc");
 2138         if (disable_deblocking_filter_idc!=1)
 2139         {
 2140             Skip_SE(                                           "slice_alpha_c0_offset_div2");
 2141             Skip_SE(                                           "slice_beta_offset_div2");
 2142         }
 2143     }
 2144 
 2145     Element_End0();
 2146 
 2147     FILLING_BEGIN();
 2148         //Count of I-Frames
 2149         if (first_mb_in_slice==0 && Element_Code!=20 && (slice_type==2 || slice_type==7)) //Not slice_layer_extension, I-Frame
 2150             IFrame_Count++;
 2151 
 2152         //pic_struct
 2153         if (field_pic_flag && (*seq_parameter_set_Item)->pic_struct_FirstDetected==(int8u)-1)
 2154             (*seq_parameter_set_Item)->pic_struct_FirstDetected=bottom_field_flag?2:1; //2=BFF, 1=TFF
 2155 
 2156         //Saving some info
 2157         int32s TemporalReferences_Offset_pic_order_cnt_lsb_Diff=0;
 2158         if ((*seq_parameter_set_Item)->pic_order_cnt_type!=1 && first_mb_in_slice==0 && (Element_Code!=0x14 || seq_parameter_sets.empty())) //Not slice_layer_extension except if MVC only
 2159         {
 2160             if (field_pic_flag)
 2161             {
 2162                 Structure_Field++;
 2163                 if (bottom_field_flag)
 2164                     Interlaced_Bottom++;
 2165                 else
 2166                     Interlaced_Top++;
 2167             }
 2168             else
 2169                 Structure_Frame++;
 2170 
 2171             //Frame order detection
 2172             int64s pic_order_cnt=0;
 2173             switch ((*seq_parameter_set_Item)->pic_order_cnt_type)
 2174             {
 2175                 case 0 :
 2176                             {
 2177                             if (Element_Code==5) //IDR
 2178                             {
 2179                                 prevPicOrderCntMsb=0;
 2180                                 prevPicOrderCntLsb=0;
 2181                                 TemporalReferences_Offset=TemporalReferences_Max;
 2182                                 if (TemporalReferences_Offset%2)
 2183                                     TemporalReferences_Offset++;
 2184                                 TemporalReferences_pic_order_cnt_Min=0;
 2185                             }
 2186                             else
 2187                             {
 2188                                 const bool Has5 = std::find(memory_management_control_operations.begin(), memory_management_control_operations.end(), 5) != memory_management_control_operations.end();
 2189                                 if (Has5)
 2190                                 {
 2191                                     prevPicOrderCntMsb=0;
 2192                                     if (bottom_field_flag)
 2193                                         prevPicOrderCntLsb=0;
 2194                                     else
 2195                                         prevPicOrderCntLsb=prevTopFieldOrderCnt;
 2196                                 }
 2197                             }
 2198                             int32s PicOrderCntMsb;
 2199                             if (prevPicOrderCntLsb==(int32u)-1)
 2200                             {
 2201                                 PicOrderCntMsb=0;
 2202                                 if ((int32u)(2*((*seq_parameter_set_Item)->max_num_ref_frames+3))<pic_order_cnt_lsb)
 2203                                     TemporalReferences_Min=pic_order_cnt_lsb-2*((*seq_parameter_set_Item)->max_num_ref_frames+3);
 2204                             }
 2205                             else if (pic_order_cnt_lsb<prevPicOrderCntLsb && prevPicOrderCntLsb-pic_order_cnt_lsb>=(*seq_parameter_set_Item)->MaxPicOrderCntLsb/2)
 2206                                 PicOrderCntMsb=prevPicOrderCntMsb+(*seq_parameter_set_Item)->MaxPicOrderCntLsb;
 2207                             else if (pic_order_cnt_lsb>prevPicOrderCntLsb && pic_order_cnt_lsb-prevPicOrderCntLsb>(*seq_parameter_set_Item)->MaxPicOrderCntLsb/2)
 2208                                 PicOrderCntMsb=prevPicOrderCntMsb-(*seq_parameter_set_Item)->MaxPicOrderCntLsb;
 2209                             else
 2210                                 PicOrderCntMsb=prevPicOrderCntMsb;
 2211 
 2212                             int32s TopFieldOrderCnt=PicOrderCntMsb+pic_order_cnt_lsb;
 2213                             int32s BottomFieldOrderCnt;
 2214                             if (field_pic_flag)
 2215                                 BottomFieldOrderCnt=TopFieldOrderCnt+delta_pic_order_cnt_bottom;
 2216                             else
 2217                                 BottomFieldOrderCnt=PicOrderCntMsb+pic_order_cnt_lsb;
 2218 
 2219                             prevPicOrderCntMsb=PicOrderCntMsb;
 2220                             prevPicOrderCntLsb=pic_order_cnt_lsb;
 2221                             prevTopFieldOrderCnt=TopFieldOrderCnt;
 2222 
 2223                             pic_order_cnt=bottom_field_flag?BottomFieldOrderCnt:TopFieldOrderCnt;
 2224                             }
 2225                             break;
 2226                 case 2 :
 2227                             {
 2228                             const bool Has5 = std::find(memory_management_control_operations.begin(), memory_management_control_operations.end(),5) != memory_management_control_operations.end();
 2229                             if (Has5)
 2230                                 prevFrameNumOffset=0;
 2231                             int32u FrameNumOffset;
 2232 
 2233                             if (Element_Code==5) //IdrPicFlag
 2234                             {
 2235                                 TemporalReferences_Offset=TemporalReferences_Max;
 2236                                 if (TemporalReferences_Offset%2)
 2237                                     TemporalReferences_Offset++;
 2238                                 FrameNumOffset=0;
 2239                             }
 2240                             else if (prevFrameNumOffset==(int32u)-1)
 2241                                 FrameNumOffset=0;
 2242                             else if (prevFrameNum>frame_num)
 2243                                 FrameNumOffset=prevFrameNumOffset+(*seq_parameter_set_Item)->MaxFrameNum;
 2244                             else
 2245                                 FrameNumOffset=prevFrameNumOffset;
 2246 
 2247                             int32u tempPicOrderCnt;
 2248                             if (Element_Code==5) //IdrPicFlag
 2249                                 tempPicOrderCnt=0;
 2250                             else
 2251                             {
 2252                                 tempPicOrderCnt=2*(FrameNumOffset+frame_num);
 2253                                 if (!nal_ref_idc && tempPicOrderCnt) //Note: if nal_ref_idc is 0, tempPicOrderCnt is not expected to be 0 but it may be the case with invalid streams
 2254                                     tempPicOrderCnt--;
 2255                             }
 2256 
 2257                             pic_order_cnt=tempPicOrderCnt;
 2258 
 2259                             prevFrameNum=frame_num;
 2260                             prevFrameNumOffset=FrameNumOffset;
 2261 
 2262                             pic_order_cnt_lsb=frame_num;
 2263                             }
 2264                             break;
 2265                 default:    ;
 2266             }
 2267 
 2268             if (pic_order_cnt<TemporalReferences_pic_order_cnt_Min)
 2269             {
 2270                 if (pic_order_cnt<0)
 2271                 {
 2272                     size_t Base=(size_t)(TemporalReferences_Offset+TemporalReferences_pic_order_cnt_Min);
 2273                     size_t ToInsert=(size_t)(TemporalReferences_pic_order_cnt_Min-pic_order_cnt);
 2274                     if (Base+ToInsert>=4*TemporalReferences_Reserved || Base>=4*TemporalReferences_Reserved || ToInsert+TemporalReferences_Max>=4*TemporalReferences_Reserved || TemporalReferences_Max>=4*TemporalReferences_Reserved || TemporalReferences_Max-Base>=4*TemporalReferences_Reserved)
 2275                     {
 2276                         Trusted_IsNot("Problem in temporal references");
 2277                         return;
 2278                     }
 2279                     Element_Info1(__T("Offset of ")+Ztring::ToZtring(ToInsert));
 2280                     TemporalReferences.insert(TemporalReferences.begin()+Base, ToInsert, NULL);
 2281                     TemporalReferences_Offset+=ToInsert;
 2282                     TemporalReferences_Offset_pic_order_cnt_lsb_Last += ToInsert;
 2283                     TemporalReferences_Max+=ToInsert;
 2284                     TemporalReferences_pic_order_cnt_Min=pic_order_cnt;
 2285                 }
 2286                 else if (TemporalReferences_Min>(size_t)(TemporalReferences_Offset+pic_order_cnt))
 2287                     TemporalReferences_Min=(size_t)(TemporalReferences_Offset+pic_order_cnt);
 2288             }
 2289 
 2290             if (pic_order_cnt<0 && TemporalReferences_Offset<(size_t)(-pic_order_cnt)) //Found in playreadyEncryptedBlowUp.ts without encryption test
 2291             {
 2292                 Trusted_IsNot("Problem in temporal references");
 2293                 return;
 2294             }
 2295 
 2296             if ((size_t)(TemporalReferences_Offset+pic_order_cnt)>=3*TemporalReferences_Reserved)
 2297             {
 2298                 size_t Offset=TemporalReferences_Max-TemporalReferences_Offset;
 2299                 if (Offset%2)
 2300                     Offset++;
 2301                 if (Offset>=TemporalReferences_Reserved && pic_order_cnt>=(int64s)TemporalReferences_Reserved)
 2302                 {
 2303                     TemporalReferences_Offset+=TemporalReferences_Reserved;
 2304                     pic_order_cnt-=TemporalReferences_Reserved;
 2305                     TemporalReferences_pic_order_cnt_Min-=TemporalReferences_Reserved/2;
 2306                     switch ((*seq_parameter_set_Item)->pic_order_cnt_type)
 2307                     {
 2308                         case 0 :
 2309                                 prevPicOrderCntMsb-=(int32u)TemporalReferences_Reserved;
 2310                                 break;
 2311                         case 2 :
 2312                                 prevFrameNumOffset-=(int32u)TemporalReferences_Reserved/2;
 2313                                 break;
 2314                         default:;
 2315                     }
 2316                 }
 2317                 while (TemporalReferences_Offset+pic_order_cnt>=3*TemporalReferences_Reserved)
 2318                 {
 2319                     for (size_t Pos=0; Pos<TemporalReferences_Reserved; Pos++)
 2320                     {
 2321                         if (TemporalReferences[Pos])
 2322                         {
 2323                             if ((Pos%2)==0)
 2324                                 PictureTypes_PreviousFrames+=Avc_slice_type[TemporalReferences[Pos]->slice_type];
 2325                             delete TemporalReferences[Pos];
 2326                             TemporalReferences[Pos] = NULL;
 2327                         }
 2328                         else if (!PictureTypes_PreviousFrames.empty()) //Only if stream already started
 2329                         {
 2330                             if ((Pos%2)==0)
 2331                                 PictureTypes_PreviousFrames+=' ';
 2332                         }
 2333                     }
 2334                     if (PictureTypes_PreviousFrames.size()>=8*TemporalReferences.size())
 2335                         PictureTypes_PreviousFrames.erase(PictureTypes_PreviousFrames.begin(), PictureTypes_PreviousFrames.begin()+PictureTypes_PreviousFrames.size()-TemporalReferences.size());
 2336                     TemporalReferences.erase(TemporalReferences.begin(), TemporalReferences.begin()+TemporalReferences_Reserved);
 2337                     TemporalReferences.resize(4*TemporalReferences_Reserved);
 2338                     if (TemporalReferences_Reserved<TemporalReferences_Offset)
 2339                         TemporalReferences_Offset-=TemporalReferences_Reserved;
 2340                     else
 2341                         TemporalReferences_Offset=0;
 2342                     if (TemporalReferences_Reserved<TemporalReferences_Min)
 2343                         TemporalReferences_Min-=TemporalReferences_Reserved;
 2344                     else
 2345                         TemporalReferences_Min=0;
 2346                     if (TemporalReferences_Reserved<TemporalReferences_Max)
 2347                         TemporalReferences_Max-=TemporalReferences_Reserved;
 2348                     else
 2349                         TemporalReferences_Max=0;
 2350                     if (TemporalReferences_Reserved<TemporalReferences_Offset_pic_order_cnt_lsb_Last)
 2351                         TemporalReferences_Offset_pic_order_cnt_lsb_Last-=TemporalReferences_Reserved;
 2352                     else
 2353                         TemporalReferences_Offset_pic_order_cnt_lsb_Last=0;
 2354                 }
 2355             }
 2356 
 2357             TemporalReferences_Offset_pic_order_cnt_lsb_Diff=(int32s)((int32s)(TemporalReferences_Offset+pic_order_cnt)-TemporalReferences_Offset_pic_order_cnt_lsb_Last);
 2358             TemporalReferences_Offset_pic_order_cnt_lsb_Last=(size_t)(TemporalReferences_Offset+pic_order_cnt);
 2359             if (TemporalReferences_Max<=TemporalReferences_Offset_pic_order_cnt_lsb_Last)
 2360                 TemporalReferences_Max=TemporalReferences_Offset_pic_order_cnt_lsb_Last+((*seq_parameter_set_Item)->frame_mbs_only_flag?2:1);
 2361             if (TemporalReferences_Min>TemporalReferences_Offset_pic_order_cnt_lsb_Last)
 2362                 TemporalReferences_Min=TemporalReferences_Offset_pic_order_cnt_lsb_Last;
 2363             if (TemporalReferences_DelayedElement)
 2364             {
 2365                 delete TemporalReferences[TemporalReferences_Offset_pic_order_cnt_lsb_Last]; TemporalReferences[TemporalReferences_Offset_pic_order_cnt_lsb_Last]=TemporalReferences_DelayedElement;
 2366             }
 2367             if (TemporalReferences[TemporalReferences_Offset_pic_order_cnt_lsb_Last]==NULL)
 2368                 TemporalReferences[TemporalReferences_Offset_pic_order_cnt_lsb_Last]=new temporal_reference();
 2369             TemporalReferences[TemporalReferences_Offset_pic_order_cnt_lsb_Last]->frame_num=frame_num;
 2370             TemporalReferences[TemporalReferences_Offset_pic_order_cnt_lsb_Last]->slice_type=(int8u)slice_type;
 2371             TemporalReferences[TemporalReferences_Offset_pic_order_cnt_lsb_Last]->IsTop=!bottom_field_flag;
 2372             TemporalReferences[TemporalReferences_Offset_pic_order_cnt_lsb_Last]->IsField=field_pic_flag;
 2373             if (TemporalReferences_DelayedElement)
 2374             {
 2375                 TemporalReferences_DelayedElement=NULL;
 2376                 sei_message_user_data_registered_itu_t_t35_GA94_03_Delayed((*pic_parameter_set_Item)->seq_parameter_set_id);
 2377             }
 2378         }
 2379 
 2380         if ((*seq_parameter_set_Item)->vui_parameters && (*seq_parameter_set_Item)->vui_parameters->timing_info_present_flag && (*seq_parameter_set_Item)->vui_parameters->num_units_in_tick)
 2381             tc=float64_int64s(((float64)1000000000)/((float64)(*seq_parameter_set_Item)->vui_parameters->time_scale/(*seq_parameter_set_Item)->vui_parameters->num_units_in_tick/((*seq_parameter_set_Item)->pic_order_cnt_type==2?1:2)/FrameRate_Divider)/((!(*seq_parameter_set_Item)->frame_mbs_only_flag && field_pic_flag)?2:1));
 2382         if (first_mb_in_slice==0)
 2383         {
 2384             if (Frame_Count==0)
 2385             {
 2386                 if (FrameInfo.PTS==(int64u)-1)
 2387                     FrameInfo.PTS=FrameInfo.DTS+tc*(TemporalReferences_Offset_pic_order_cnt_lsb_Diff?2:1)*((!(*seq_parameter_set_Item)->frame_mbs_only_flag && field_pic_flag)?2:1); //No PTS in container
 2388                 PTS_Begin=FrameInfo.PTS;
 2389             }
 2390             #if MEDIAINFO_ADVANCED2
 2391                 if (PTS_Begin_Segment==(int64u)-1 && File_Offset>=Config->File_Current_Offset)
 2392                 {
 2393                     PTS_Begin_Segment=FrameInfo.PTS;
 2394                 }
 2395             #endif //MEDIAINFO_ADVANCED2
 2396             if (slice_type==2 || slice_type==7) //IFrame
 2397                 FirstPFrameInGop_IsParsed=false;
 2398         }
 2399         else
 2400         {
 2401             if (FrameInfo.PTS!=(int64u)-1)
 2402                 FrameInfo.PTS-=tc;
 2403             if (FrameInfo.DTS!=(int64u)-1)
 2404                 FrameInfo.DTS-=tc;
 2405         }
 2406 
 2407         //Frame pos
 2408         if (Frame_Count!=(int64u)-1 && Frame_Count && ((!(*seq_parameter_set_Item)->frame_mbs_only_flag && Interlaced_Top==Interlaced_Bottom && field_pic_flag) || first_mb_in_slice!=0 || (Element_Code==0x14 && !seq_parameter_sets.empty())))
 2409         {
 2410             Frame_Count--;
 2411             if (IFrame_Count && Frame_Count_NotParsedIncluded!=(int64u)-1)
 2412                 Frame_Count_NotParsedIncluded--;
 2413             Frame_Count_InThisBlock--;
 2414         }
 2415         else if (first_mb_in_slice==0)
 2416         {
 2417             if ((*seq_parameter_set_Item)->pic_order_cnt_type!=1 && (Element_Code!=0x14 || seq_parameter_sets.empty())) //Not slice_layer_extension except if MVC only
 2418             {
 2419                 if ((!IsSub || Frame_Count_InThisBlock) && TemporalReferences_Offset_pic_order_cnt_lsb_Diff && TemporalReferences_Offset_pic_order_cnt_lsb_Diff!=2)
 2420                     FrameInfo.PTS+=(TemporalReferences_Offset_pic_order_cnt_lsb_Diff-(field_pic_flag?1:2))/((!(*seq_parameter_set_Item)->frame_mbs_only_flag && field_pic_flag)?1:2)*(int64s)tc;
 2421             }
 2422 
 2423             if (!FirstPFrameInGop_IsParsed && (slice_type==0 || slice_type==5)) //P-Frame
 2424             {
 2425                 FirstPFrameInGop_IsParsed=true;
 2426 
 2427                 //Testing if we have enough to test GOP
 2428                 if (Frame_Count<=Frame_Count_Valid)
 2429                 {
 2430                     std::string PictureTypes(PictureTypes_PreviousFrames);
 2431                     PictureTypes.reserve(TemporalReferences.size());
 2432                     for (size_t Pos=0; Pos<TemporalReferences.size(); Pos++)
 2433                         if (TemporalReferences[Pos])
 2434                         {
 2435                             if ((Pos%2)==0)
 2436                                 PictureTypes+=Avc_slice_type[TemporalReferences[Pos]->slice_type];
 2437                         }
 2438                         else if (!PictureTypes.empty()) //Only if stream already started
 2439                         {
 2440                             if ((Pos%2)==0)
 2441                                 PictureTypes+=' ';
 2442                         }
 2443                         #if defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 2444                             if (!GOP_Detect(PictureTypes).empty() && !GA94_03_IsPresent)
 2445                                 Frame_Count_Valid=Frame_Count; //We have enough frames
 2446                         #endif //defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 2447                 }
 2448             }
 2449         }
 2450 
 2451         #if MEDIAINFO_TRACE
 2452             if (Trace_Activated)
 2453             {
 2454                 Element_Info1(TemporalReferences_Offset_pic_order_cnt_lsb_Last);
 2455                 Element_Info1((((*seq_parameter_set_Item)->frame_mbs_only_flag || !field_pic_flag)?__T("Frame "):(bottom_field_flag?__T("Field (Bottom) "):__T("Field (Top) ")))+Ztring::ToZtring(Frame_Count));
 2456                 if (slice_type<9)
 2457                     Element_Info1(__T("slice_type ")+Ztring().From_UTF8(Avc_slice_type[slice_type]));
 2458                 Element_Info1(__T("frame_num ")+Ztring::ToZtring(frame_num));
 2459                 if ((*seq_parameter_set_Item)->vui_parameters && (*seq_parameter_set_Item)->vui_parameters->fixed_frame_rate_flag)
 2460                 {
 2461                     if (FrameInfo.PCR!=(int64u)-1)
 2462                         Element_Info1(__T("PCR ")+Ztring().Duration_From_Milliseconds(float64_int64s(((float64)FrameInfo.PCR)/1000000)));
 2463                     if (FrameInfo.DTS!=(int64u)-1)
 2464                         Element_Info1(__T("DTS ")+Ztring().Duration_From_Milliseconds(float64_int64s(((float64)FrameInfo.DTS)/1000000)));
 2465                     if (FrameInfo.PTS!=(int64u)-1)
 2466                         Element_Info1(__T("PTS ")+Ztring().Duration_From_Milliseconds(float64_int64s(((float64)FrameInfo.PTS)/1000000)));
 2467                 }
 2468                 if ((*seq_parameter_set_Item)->pic_order_cnt_type==0)
 2469                     Element_Info1(__T("pic_order_cnt_lsb ")+Ztring::ToZtring(pic_order_cnt_lsb));
 2470                 if (first_mb_in_slice)
 2471                     Element_Info1(__T("first_mb_in_slice ")+Ztring::ToZtring(first_mb_in_slice));
 2472             }
 2473         #endif //MEDIAINFO_TRACE
 2474 
 2475         //Counting
 2476         if (Frame_Count!=(int64u)-1)
 2477         {
 2478             if (File_Offset+Buffer_Offset+Element_Size==File_Size)
 2479                 Frame_Count_Valid=Frame_Count; //Finish frames in case of there are less than Frame_Count_Valid frames
 2480             Frame_Count++;
 2481             if (IFrame_Count && Frame_Count_NotParsedIncluded!=(int64u)-1)
 2482                 Frame_Count_NotParsedIncluded++;
 2483             Frame_Count_InThisBlock++;
 2484         }
 2485         if ((*seq_parameter_set_Item)->pic_order_cnt_type==0 && field_pic_flag)
 2486         {
 2487             Field_Count++;
 2488             Field_Count_InThisBlock++;
 2489         }
 2490         if (FrameInfo.PTS!=(int64u)-1)
 2491             FrameInfo.PTS+=tc;
 2492         if (FrameInfo.DTS!=(int64u)-1)
 2493             FrameInfo.DTS+=tc;
 2494         if (FrameInfo.PTS!=(int64u)-1 && (FrameInfo.PTS>PTS_End || (PTS_End>1000000000 && FrameInfo.PTS<=PTS_End-1000000000))) //More than current PTS_End or less than current PTS_End minus 1 second (there is a problem?)
 2495             PTS_End=FrameInfo.PTS;
 2496 
 2497         #if MEDIAINFO_DUPLICATE
 2498             if (Streams[(size_t)Element_Code].ShouldDuplicate)
 2499                 File__Duplicate_Write(Element_Code, (*seq_parameter_set_Item)->pic_order_cnt_type==0?pic_order_cnt_lsb:frame_num);
 2500         #endif //MEDIAINFO_DUPLICATE
 2501 
 2502         //Filling only if not already done
 2503         if (Frame_Count==1 && !Status[IsAccepted])
 2504             Accept("AVC");
 2505         if (!Status[IsFilled])
 2506         {
 2507             #if defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 2508                 if (!GA94_03_IsPresent && IFrame_Count>=8)
 2509                     Frame_Count_Valid=Frame_Count; //We have enough frames
 2510             #endif //defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 2511             if (Frame_Count>=Frame_Count_Valid)
 2512             {
 2513                 Fill("AVC");
 2514                 if (!IsSub && !Streams[(size_t)Element_Code].ShouldDuplicate && Config->ParseSpeed<1.0)
 2515                     Finish("AVC");
 2516             }
 2517         }
 2518     FILLING_END();
 2519 }
 2520 
 2521 //---------------------------------------------------------------------------
 2522 //
 2523 void File_Avc::slice_data(bool AllCategories)
 2524 {
 2525     Element_Begin1("slice_data");
 2526 
 2527     Skip_BS(Data_BS_Remain(),                                   "(ToDo)");
 2528 
 2529     Element_End0();
 2530 }
 2531 
 2532 //---------------------------------------------------------------------------
 2533 //
 2534 void File_Avc::ref_pic_list_modification(int32u slice_type, bool mvc)
 2535 {
 2536     if ((slice_type%5)!=2 && (slice_type%5)!=4)
 2537     {
 2538         TEST_SB_SKIP(                                           "ref_pic_list_modification_flag_l0");
 2539             int32u modification_of_pic_nums_idc;
 2540             do
 2541             {
 2542                 Get_UE (modification_of_pic_nums_idc,           "modification_of_pic_nums_idc");
 2543                 if (modification_of_pic_nums_idc<2)
 2544                     Skip_UE(                                    "abs_diff_pic_num_minus1");
 2545                 else if (modification_of_pic_nums_idc==2)
 2546                     Skip_UE(                                    "long_term_pic_num");
 2547                 else if (mvc && (modification_of_pic_nums_idc==4 || modification_of_pic_nums_idc==5)) //ref_pic_list_mvc_modification only
 2548                     Skip_UE(                                    "abs_diff_view_idx_minus1");
 2549                 else if (modification_of_pic_nums_idc!=3)
 2550                 {
 2551                     Trusted_IsNot("ref_pic_list_modification_flag_l0");
 2552                     Skip_BS(Data_BS_Remain(),                   "(Remaining bits)");
 2553                 }
 2554             }
 2555             while (modification_of_pic_nums_idc!=3 && Data_BS_Remain());
 2556         TEST_SB_END();
 2557     }
 2558     if ((slice_type%5)==1)
 2559     {
 2560         TEST_SB_SKIP(                                           "ref_pic_list_modification_flag_l1");
 2561             int32u modification_of_pic_nums_idc;
 2562             do
 2563             {
 2564                 Get_UE (modification_of_pic_nums_idc,           "modification_of_pic_nums_idc");
 2565                 if (modification_of_pic_nums_idc<2)
 2566                     Skip_UE(                                    "abs_diff_pic_num_minus1");
 2567                 else if (modification_of_pic_nums_idc==2)
 2568                     Skip_UE(                                    "long_term_pic_num");
 2569                 else if (mvc && (modification_of_pic_nums_idc==4 || modification_of_pic_nums_idc==5)) //ref_pic_list_mvc_modification only
 2570                     Skip_UE(                                    "abs_diff_view_idx_minus1");
 2571                 else if (modification_of_pic_nums_idc!=3)
 2572                 {
 2573                     Trusted_IsNot("ref_pic_list_modification_flag_l1");
 2574                     Skip_BS(Data_BS_Remain(),                   "(Remaining bits)");
 2575                 }
 2576             }
 2577             while (modification_of_pic_nums_idc!=3 && Data_BS_Remain());
 2578         TEST_SB_END();
 2579     }
 2580 }
 2581 
 2582 //---------------------------------------------------------------------------
 2583 //
 2584 void File_Avc::pred_weight_table(int32u num_ref_idx_l0_active_minus1, int32u num_ref_idx_l1_active_minus1, int8u ChromaArrayType)
 2585 {
 2586     Skip_UE(                                                    "luma_log2_weight_denom");
 2587     if (ChromaArrayType)
 2588         Skip_UE(                                                "chroma_log2_weight_denom");
 2589     for(int32u i=0; i<=num_ref_idx_l0_active_minus1; i++)
 2590     {
 2591         TEST_SB_SKIP(                                           "luma_weight_l0_flag");
 2592             Skip_SE(                                            "luma_weight_l0");
 2593             Skip_SE(                                            "luma_offset_l0");
 2594         TEST_SB_END();
 2595     }
 2596     if (ChromaArrayType)
 2597     {
 2598         TEST_SB_SKIP(                                           "chroma_weight_l0_flag");
 2599             Skip_SE(                                            "chroma_weight_l0");
 2600             Skip_SE(                                            "chroma_offset_l0");
 2601         TEST_SB_END();
 2602     }
 2603 }
 2604 
 2605 //---------------------------------------------------------------------------
 2606 //
 2607 void File_Avc::dec_ref_pic_marking(std::vector<int8u> &memory_management_control_operations)
 2608 {
 2609     if (Element_Code==5) //IdrPicFlag
 2610     {
 2611         Skip_SB(                                                "no_output_of_prior_pics_flag");
 2612         Skip_SB(                                                "long_term_reference_flag");
 2613     }
 2614     else
 2615     {
 2616         TEST_SB_SKIP(                                           "adaptive_ref_pic_marking_mode_flag");
 2617             int32u memory_management_control_operation;
 2618             do
 2619             {
 2620                 Get_UE (memory_management_control_operation,    "memory_management_control_operation");
 2621                 switch (memory_management_control_operation)
 2622                 {
 2623                     case 1 :
 2624                                 Skip_UE(                        "difference_of_pic_nums_minus1");
 2625                                 break;
 2626                     case 2 :
 2627                                 Skip_UE(                        "long_term_pic_num");
 2628                                 break;
 2629                     case 3 :
 2630                                 Skip_UE(                        "difference_of_pic_nums_minus1");
 2631                                 //break; 3 --> difference_of_pic_nums_minus1 then long_term_frame_idx
 2632                     case 6 :
 2633                                 Skip_UE(                        "long_term_frame_idx");
 2634                                 break;
 2635                     case 4 :
 2636                                 Skip_UE(                        "max_long_term_frame_idx_plus1");
 2637                                 break;
 2638                 }
 2639                 memory_management_control_operations.push_back((int8u)memory_management_control_operation);
 2640             }
 2641             while (Data_BS_Remain() && memory_management_control_operation);
 2642         TEST_SB_END()
 2643     }
 2644 }
 2645 
 2646 //---------------------------------------------------------------------------
 2647 // Packet "06"
 2648 void File_Avc::sei()
 2649 {
 2650     Element_Name("sei");
 2651 
 2652     //Parsing
 2653     int32u seq_parameter_set_id=(int32u)-1;
 2654     while(Element_Offset+1<Element_Size)
 2655     {
 2656         Element_Begin1("sei message");
 2657             sei_message(seq_parameter_set_id);
 2658         Element_End0();
 2659     }
 2660     BS_Begin();
 2661     Mark_1(                                                     );
 2662     BS_End();
 2663 }
 2664 
 2665 //---------------------------------------------------------------------------
 2666 void File_Avc::sei_message(int32u &seq_parameter_set_id)
 2667 {
 2668     //Parsing
 2669     int32u payloadType=0, payloadSize=0;
 2670     int8u payload_type_byte, payload_size_byte;
 2671     Element_Begin1("sei message header");
 2672         do
 2673         {
 2674             Get_B1 (payload_type_byte,                          "payload_type_byte");
 2675             payloadType+=payload_type_byte;
 2676         }
 2677         while(payload_type_byte==0xFF);
 2678         do
 2679         {
 2680             Get_B1 (payload_size_byte,                          "payload_size_byte");
 2681             payloadSize+=payload_size_byte;
 2682         }
 2683         while(payload_size_byte==0xFF);
 2684     Element_End0();
 2685 
 2686     int64u Element_Offset_Save=Element_Offset+payloadSize;
 2687     if (Element_Offset_Save>Element_Size)
 2688     {
 2689         Trusted_IsNot("Wrong size");
 2690         Skip_XX(Element_Size-Element_Offset,                    "unknown");
 2691         return;
 2692     }
 2693     int64u Element_Size_Save=Element_Size;
 2694     Element_Size=Element_Offset_Save;
 2695     switch (payloadType)
 2696     {
 2697         case  0 :   sei_message_buffering_period(seq_parameter_set_id); break;
 2698         case  1 :   sei_message_pic_timing(payloadSize, seq_parameter_set_id); break;
 2699         case  4 :   sei_message_user_data_registered_itu_t_t35(); break;
 2700         case  5 :   sei_message_user_data_unregistered(payloadSize); break;
 2701         case  6 :   sei_message_recovery_point(); break;
 2702         case 32 :   sei_message_mainconcept(payloadSize); break;
 2703         case 147:   sei_alternative_transfer_characteristics(); break;
 2704         case 137:   sei_message_mastering_display_colour_volume(); break;
 2705         case 144:   sei_message_light_level(); break;
 2706         default :
 2707                     Element_Info1("unknown");
 2708                     Skip_XX(payloadSize,                        "data");
 2709     }
 2710     Element_Offset=Element_Offset_Save; //Positionning in the right place.
 2711     Element_Size=Element_Size_Save; //Positionning in the right place.
 2712 }
 2713 
 2714 //---------------------------------------------------------------------------
 2715 // SEI - 0
 2716 void File_Avc::sei_message_buffering_period(int32u &seq_parameter_set_id)
 2717 {
 2718     Element_Info1("buffering_period");
 2719 
 2720     //Parsing
 2721     if (Element_Offset==Element_Size)
 2722         return; //Nothing to do
 2723     BS_Begin();
 2724     Get_UE (seq_parameter_set_id,                               "seq_parameter_set_id");
 2725     std::vector<seq_parameter_set_struct*>::iterator seq_parameter_set_Item;
 2726     if (seq_parameter_set_id>=seq_parameter_sets.size() || (*(seq_parameter_set_Item=seq_parameter_sets.begin()+seq_parameter_set_id))==NULL)
 2727     {
 2728         //Not yet present
 2729         Skip_BS(Data_BS_Remain(),                               "Data (seq_parameter_set is missing)");
 2730         BS_End();
 2731         return;
 2732     }
 2733     if ((*seq_parameter_set_Item)->NalHrdBpPresentFlag())
 2734         sei_message_buffering_period_xxl((*seq_parameter_set_Item)->vui_parameters->NAL);
 2735     if ((*seq_parameter_set_Item)->VclHrdBpPresentFlag())
 2736         sei_message_buffering_period_xxl((*seq_parameter_set_Item)->vui_parameters->VCL);
 2737     BS_End();
 2738 }
 2739 
 2740 void File_Avc::sei_message_buffering_period_xxl(seq_parameter_set_struct::vui_parameters_struct::xxl* xxl)
 2741 {
 2742     if (xxl==NULL)
 2743         return;
 2744     for (int32u SchedSelIdx=0; SchedSelIdx<xxl->SchedSel.size(); SchedSelIdx++)
 2745     {
 2746         //Get_S4 (xxl->initial_cpb_removal_delay_length_minus1+1, xxl->SchedSel[SchedSelIdx].initial_cpb_removal_delay, "initial_cpb_removal_delay"); Param_Info2(xxl->SchedSel[SchedSelIdx].initial_cpb_removal_delay/90, " ms");
 2747         //Get_S4 (xxl->initial_cpb_removal_delay_length_minus1+1, xxl->SchedSel[SchedSelIdx].initial_cpb_removal_delay_offset, "initial_cpb_removal_delay_offset"); Param_Info2(xxl->SchedSel[SchedSelIdx].initial_cpb_removal_delay_offset/90, " ms");
 2748         Info_S4 (xxl->initial_cpb_removal_delay_length_minus1+1, initial_cpb_removal_delay, "initial_cpb_removal_delay"); Param_Info2(initial_cpb_removal_delay/90, " ms");
 2749         Info_S4 (xxl->initial_cpb_removal_delay_length_minus1+1, initial_cpb_removal_delay_offset, "initial_cpb_removal_delay_offset"); Param_Info2(initial_cpb_removal_delay_offset/90, " ms");
 2750     }
 2751 }
 2752 
 2753 //---------------------------------------------------------------------------
 2754 // SEI - 1
 2755 void File_Avc::sei_message_pic_timing(int32u /*payloadSize*/, int32u seq_parameter_set_id)
 2756 {
 2757     Element_Info1("pic_timing");
 2758 
 2759     //Testing if we can parsing it now. TODO: handle case seq_parameter_set_id is unknown (buffering of message, decoding in slice parsing)
 2760     if (seq_parameter_set_id==(int32u)-1 && seq_parameter_sets.size()==1)
 2761         seq_parameter_set_id=0;
 2762     std::vector<seq_parameter_set_struct*>::iterator seq_parameter_set_Item;
 2763     if (seq_parameter_set_id>=seq_parameter_sets.size() || (*(seq_parameter_set_Item=seq_parameter_sets.begin()+seq_parameter_set_id))==NULL)
 2764     {
 2765         //Not yet present
 2766         Skip_BS(Data_BS_Remain(),                               "Data (seq_parameter_set is missing)");
 2767         return;
 2768     }
 2769 
 2770     //Parsing
 2771     int8u   pic_struct=(int8u)-1;
 2772     BS_Begin();
 2773     if ((*seq_parameter_set_Item)->CpbDpbDelaysPresentFlag())
 2774     {
 2775         int8u cpb_removal_delay_length_minus1=(*seq_parameter_set_Item)->vui_parameters->NAL?(*seq_parameter_set_Item)->vui_parameters->NAL->cpb_removal_delay_length_minus1:(*seq_parameter_set_Item)->vui_parameters->VCL->cpb_removal_delay_length_minus1; //Spec is not precise, I am not sure
 2776         int8u dpb_output_delay_length_minus1=(*seq_parameter_set_Item)->vui_parameters->NAL?(*seq_parameter_set_Item)->vui_parameters->NAL->dpb_output_delay_length_minus1:(*seq_parameter_set_Item)->vui_parameters->VCL->dpb_output_delay_length_minus1; //Spec is not precise, I am not sure
 2777         Skip_S4(cpb_removal_delay_length_minus1+1,              "cpb_removal_delay");
 2778         Skip_S4(dpb_output_delay_length_minus1+1,               "dpb_output_delay");
 2779     }
 2780     if ((*seq_parameter_set_Item)->vui_parameters && (*seq_parameter_set_Item)->vui_parameters->pic_struct_present_flag)
 2781     {
 2782         Get_S1 (4, pic_struct,                                  "pic_struct");
 2783         switch (pic_struct)
 2784         {
 2785             case  0 :
 2786             case  1 :
 2787             case  2 :
 2788             case  3 :
 2789             case  4 :
 2790             case  5 :
 2791             case  6 : FrameRate_Divider=1; break;
 2792             case  7 : FrameRate_Divider=2; break;
 2793             case  8 : FrameRate_Divider=3; break;
 2794             default : Param_Info1("Reserved"); return; //NumClockTS is unknown
 2795         }
 2796         Param_Info1(Avc_pic_struct[pic_struct]);
 2797         int8u NumClockTS=Avc_NumClockTS[pic_struct];
 2798         int8u seconds_value=0, minutes_value=0, hours_value=0; //Here because theses values can be reused in later ClockTSs.
 2799         for (int8u i=0; i<NumClockTS; i++)
 2800         {
 2801             Element_Begin1("ClockTS");
 2802             TEST_SB_SKIP(                                       "clock_timestamp_flag");
 2803                 Ztring TimeStamp;
 2804                 int32u time_offset=0;
 2805                 int8u n_frames;
 2806                 bool full_timestamp_flag, nuit_field_based_flag;
 2807                 Info_S1(2, ct_type,                             "ct_type"); Param_Info1(Avc_ct_type[ct_type]);
 2808                 Get_SB (   nuit_field_based_flag,               "nuit_field_based_flag");
 2809                 Skip_S1(5,                                      "counting_type");
 2810                 Get_SB (   full_timestamp_flag,                 "full_timestamp_flag");
 2811                 Skip_SB(                                        "discontinuity_flag");
 2812                 Skip_SB(                                        "cnt_dropped_flag");
 2813                 Get_S1 (8, n_frames,                            "n_frames");
 2814                 if (full_timestamp_flag)
 2815                 {
 2816                     Get_S1 (6, seconds_value,                    "seconds_value");
 2817                     Get_S1 (6, minutes_value,                    "minutes_value");
 2818                     Get_S1 (5, hours_value,                      "hours_value");
 2819                 }
 2820                 else
 2821                 {
 2822                     TEST_SB_SKIP(                               "seconds_flag");
 2823                         Get_S1 (6, seconds_value,               "seconds_value");
 2824                         TEST_SB_SKIP(                           "minutes_flag");
 2825                             Get_S1 (6, minutes_value,           "minutes_value");
 2826                             TEST_SB_SKIP(                       "hours_flag");
 2827                                 Get_S1 (5, hours_value,         "hours_value");
 2828                             TEST_SB_END();
 2829                         TEST_SB_END();
 2830                     TEST_SB_END();
 2831                 }
 2832                 TimeStamp=Ztring::ToZtring(hours_value)+__T(':')+Ztring::ToZtring(minutes_value)+__T(':')+Ztring::ToZtring(seconds_value);
 2833                 if ((*seq_parameter_set_Item)->CpbDpbDelaysPresentFlag())
 2834                 {
 2835                     int8u time_offset_length=(*seq_parameter_set_Item)->vui_parameters->NAL?(*seq_parameter_set_Item)->vui_parameters->NAL->time_offset_length:(*seq_parameter_set_Item)->vui_parameters->VCL->time_offset_length; //Spec is not precise, I am not sure
 2836                     if (time_offset_length)
 2837                         Get_S4 (time_offset_length, time_offset,    "time_offset");
 2838                 }
 2839                 if ((*seq_parameter_set_Item)->vui_parameters && (*seq_parameter_set_Item)->vui_parameters->timing_info_present_flag && (*seq_parameter_set_Item)->vui_parameters->time_scale)
 2840                 {
 2841                     float32 Milliseconds=((float32)(n_frames*((*seq_parameter_set_Item)->vui_parameters->num_units_in_tick*(1+(nuit_field_based_flag?1:0)))+time_offset))/(*seq_parameter_set_Item)->vui_parameters->time_scale;
 2842                     TimeStamp+=__T('.');
 2843                     TimeStamp+=Ztring::ToZtring(Milliseconds);
 2844                 }
 2845                 Element_Info1(TimeStamp);
 2846             TEST_SB_END();
 2847             Element_End0();
 2848         }
 2849     }
 2850     BS_End();
 2851 
 2852     FILLING_BEGIN_PRECISE();
 2853         if ((*seq_parameter_set_Item)->pic_struct_FirstDetected==(int8u)-1 && (*seq_parameter_set_Item)->vui_parameters && (*seq_parameter_set_Item)->vui_parameters->pic_struct_present_flag)
 2854             (*seq_parameter_set_Item)->pic_struct_FirstDetected=pic_struct;
 2855     FILLING_END();
 2856 }
 2857 
 2858 //---------------------------------------------------------------------------
 2859 // SEI - 5
 2860 void File_Avc::sei_message_user_data_registered_itu_t_t35()
 2861 {
 2862     Element_Info1("user_data_registered_itu_t_t35");
 2863 
 2864     //Parsing
 2865     int8u itu_t_t35_country_code;
 2866     Get_B1 (itu_t_t35_country_code,                             "itu_t_t35_country_code");
 2867     if (itu_t_t35_country_code==0xFF)
 2868         Skip_B1(                                                "itu_t_t35_country_code_extension_byte");
 2869     if (itu_t_t35_country_code!=0xB5 || Element_Offset+2>=Element_Size)
 2870     {
 2871         if (Element_Size-Element_Offset)
 2872             Skip_XX(Element_Size-Element_Offset,                "Unknown");
 2873         return;
 2874     }
 2875 
 2876     //United-States
 2877     int16u id;
 2878     Get_B2 (id,                                                 "id?");
 2879     if (id!=0x0031 || Element_Offset+4>=Element_Size)
 2880     {
 2881         if (Element_Size-Element_Offset)
 2882             Skip_XX(Element_Size-Element_Offset,                "Unknown");
 2883         return;
 2884     }
 2885 
 2886     int32u Identifier;
 2887     Peek_B4(Identifier);
 2888     switch (Identifier)
 2889     {
 2890         case 0x44544731 :   sei_message_user_data_registered_itu_t_t35_DTG1(); return;
 2891         case 0x47413934 :   sei_message_user_data_registered_itu_t_t35_GA94(); return;
 2892         default         :   if (Element_Size-Element_Offset)
 2893                                 Skip_XX(Element_Size-Element_Offset, "Unknown");
 2894     }
 2895 }
 2896 
 2897 //---------------------------------------------------------------------------
 2898 // SEI - 5 - DTG1
 2899 void File_Avc::sei_message_user_data_registered_itu_t_t35_DTG1()
 2900 {
 2901     Element_Info1("Active Format Description");
 2902 
 2903     //Parsing
 2904     bool active_format_flag;
 2905     Skip_C4(                                                    "afd_identifier");
 2906     BS_Begin();
 2907     Mark_0();
 2908     Get_SB (active_format_flag,                                 "active_format_flag");
 2909     Mark_0_NoTrustError();
 2910     Mark_0_NoTrustError();
 2911     Mark_0_NoTrustError();
 2912     Mark_0_NoTrustError();
 2913     Mark_0_NoTrustError();
 2914     Mark_1_NoTrustError();
 2915     if (active_format_flag)
 2916     {
 2917         Mark_1_NoTrustError();
 2918         Mark_1_NoTrustError();
 2919         Mark_1_NoTrustError();
 2920         Mark_1_NoTrustError();
 2921         Info_S1(4, active_format,                               "active_format"); Param_Info1(Avc_user_data_DTG1_active_format[active_format]);
 2922     }
 2923     BS_End();
 2924 }
 2925 
 2926 //---------------------------------------------------------------------------
 2927 // SEI - 5 - GA94
 2928 void File_Avc::sei_message_user_data_registered_itu_t_t35_GA94()
 2929 {
 2930     //Parsing
 2931     int8u user_data_type_code;
 2932     Skip_B4(                                                    "GA94_identifier");
 2933     Get_B1 (user_data_type_code,                                "user_data_type_code");
 2934     switch (user_data_type_code)
 2935     {
 2936         case 0x03 : sei_message_user_data_registered_itu_t_t35_GA94_03(); break;
 2937         case 0x06 : sei_message_user_data_registered_itu_t_t35_GA94_06(); break;
 2938         default   : Skip_XX(Element_Size-Element_Offset,        "GA94_reserved_user_data");
 2939     }
 2940 }
 2941 
 2942 //---------------------------------------------------------------------------
 2943 // SEI - 5 - GA94 - 0x03
 2944 void File_Avc::sei_message_user_data_registered_itu_t_t35_GA94_03()
 2945 {
 2946     #if defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 2947         GA94_03_IsPresent=true;
 2948         MustExtendParsingDuration=true;
 2949         Buffer_TotalBytes_Fill_Max=(int64u)-1; //Disabling this feature for this format, this is done in the parser
 2950 
 2951         Element_Info1("DTVCC Transport");
 2952 
 2953         //Coherency
 2954         delete TemporalReferences_DelayedElement; TemporalReferences_DelayedElement=new temporal_reference();
 2955 
 2956         TemporalReferences_DelayedElement->GA94_03=new buffer_data(Buffer+Buffer_Offset+(size_t)Element_Offset,(size_t)(Element_Size-Element_Offset));
 2957 
 2958         //Parsing
 2959         Skip_XX(Element_Size-Element_Offset,                    "CC data");
 2960     #else //defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 2961         Skip_XX(Element_Size-Element_Offset,                    "DTVCC Transport data");
 2962     #endif //defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 2963 }
 2964 
 2965 void File_Avc::sei_message_user_data_registered_itu_t_t35_GA94_03_Delayed(int32u seq_parameter_set_id)
 2966 {
 2967     // Skipping missing frames
 2968     if (TemporalReferences_Max-TemporalReferences_Min>(size_t)(4*(seq_parameter_sets[seq_parameter_set_id]->max_num_ref_frames+3))) // max_num_ref_frames ref frame maximum
 2969     {
 2970         TemporalReferences_Min=TemporalReferences_Max-4*(seq_parameter_sets[seq_parameter_set_id]->max_num_ref_frames+3);
 2971         while (TemporalReferences[TemporalReferences_Min]==NULL)
 2972             TemporalReferences_Min++;
 2973     }
 2974 
 2975     // Parsing captions
 2976     while (TemporalReferences[TemporalReferences_Min] && TemporalReferences_Min+2*seq_parameter_sets[seq_parameter_set_id]->max_num_ref_frames<TemporalReferences_Max)
 2977     {
 2978         #if defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 2979             Element_Begin1("Reordered DTVCC Transport");
 2980 
 2981             //Parsing
 2982             #if MEDIAINFO_DEMUX
 2983                 int64u Element_Code_Old=Element_Code;
 2984                 Element_Code=0x4741393400000003LL;
 2985             #endif //MEDIAINFO_DEMUX
 2986             if (GA94_03_Parser==NULL)
 2987             {
 2988                 GA94_03_Parser=new File_DtvccTransport;
 2989                 Open_Buffer_Init(GA94_03_Parser);
 2990                 ((File_DtvccTransport*)GA94_03_Parser)->Format=File_DtvccTransport::Format_A53_4_GA94_03;
 2991             }
 2992             if (((File_DtvccTransport*)GA94_03_Parser)->AspectRatio==0)
 2993             {
 2994                 float64 PixelAspectRatio=1;
 2995                 std::vector<seq_parameter_set_struct*>::iterator seq_parameter_set_Item=seq_parameter_sets.begin();
 2996                 for (; seq_parameter_set_Item!=seq_parameter_sets.end(); ++seq_parameter_set_Item)
 2997                     if ((*seq_parameter_set_Item))
 2998                         break;
 2999                 if (seq_parameter_set_Item!=seq_parameter_sets.end())
 3000                 {
 3001                     if ((*seq_parameter_set_Item)->vui_parameters->aspect_ratio_info_present_flag)
 3002                     {
 3003                         if ((*seq_parameter_set_Item)->vui_parameters->aspect_ratio_idc<Avc_PixelAspectRatio_Size)
 3004                             PixelAspectRatio=Avc_PixelAspectRatio[(*seq_parameter_set_Item)->vui_parameters->aspect_ratio_idc];
 3005                         else if ((*seq_parameter_set_Item)->vui_parameters->aspect_ratio_idc==0xFF && (*seq_parameter_set_Item)->vui_parameters->sar_height)
 3006                             PixelAspectRatio=((float64)(*seq_parameter_set_Item)->vui_parameters->sar_width)/(*seq_parameter_set_Item)->vui_parameters->sar_height;
 3007                     }
 3008                     const int32u Width =((*seq_parameter_set_Item)->pic_width_in_mbs_minus1       +1)*16;
 3009                     const int32u Height=((*seq_parameter_set_Item)->pic_height_in_map_units_minus1+1)*16*(2-(*seq_parameter_set_Item)->frame_mbs_only_flag);
 3010                     if(Height)
 3011                         ((File_DtvccTransport*)GA94_03_Parser)->AspectRatio=Width*PixelAspectRatio/Height;
 3012                 }
 3013             }
 3014             if (GA94_03_Parser->PTS_DTS_Needed)
 3015             {
 3016                 GA94_03_Parser->FrameInfo.PCR=FrameInfo.PCR;
 3017                 GA94_03_Parser->FrameInfo.PTS=FrameInfo.PTS;
 3018                 GA94_03_Parser->FrameInfo.DTS=FrameInfo.DTS;
 3019             }
 3020             #if MEDIAINFO_DEMUX
 3021                 if (TemporalReferences[TemporalReferences_Min]->GA94_03)
 3022                 {
 3023                     int8u Demux_Level_Save=Demux_Level;
 3024                     Demux_Level=8; //Ancillary
 3025                     Demux(TemporalReferences[TemporalReferences_Min]->GA94_03->Data, TemporalReferences[TemporalReferences_Min]->GA94_03->Size, ContentType_MainStream);
 3026                     Demux_Level=Demux_Level_Save;
 3027                 }
 3028                 Element_Code=Element_Code_Old;
 3029             #endif //MEDIAINFO_DEMUX
 3030             if (TemporalReferences[TemporalReferences_Min]->GA94_03)
 3031             {
 3032                 #if defined(MEDIAINFO_EIA608_YES) || defined(MEDIAINFO_EIA708_YES)
 3033                     GA94_03_Parser->ServiceDescriptors=ServiceDescriptors;
 3034                 #endif
 3035                 Open_Buffer_Continue(GA94_03_Parser, TemporalReferences[TemporalReferences_Min]->GA94_03->Data, TemporalReferences[TemporalReferences_Min]->GA94_03->Size);
 3036             }
 3037 
 3038             Element_End0();
 3039         #endif //defined(MEDIAINFO_DTVCCTRANSPORT_YES)
 3040 
 3041         TemporalReferences_Min+=((seq_parameter_sets[seq_parameter_set_id]->frame_mbs_only_flag | !TemporalReferences[TemporalReferences_Min]->IsField)?2:1);
 3042     }
 3043 }
 3044 
 3045 //---------------------------------------------------------------------------
 3046 // SEI - 5 - GA94 - 0x03
 3047 void File_Avc::sei_message_user_data_registered_itu_t_t35_GA94_06()
 3048 {
 3049     Element_Info1("Bar data");
 3050 
 3051     //Parsing
 3052     bool   top_bar_flag, bottom_bar_flag, left_bar_flag, right_bar_flag;
 3053     BS_Begin();
 3054     Get_SB (top_bar_flag,                                       "top_bar_flag");
 3055     Get_SB (bottom_bar_flag,                                    "bottom_bar_flag");
 3056     Get_SB (left_bar_flag,                                      "left_bar_flag");
 3057     Get_SB (right_bar_flag,                                     "right_bar_flag");
 3058     Mark_1_NoTrustError();
 3059     Mark_1_NoTrustError();
 3060     Mark_1_NoTrustError();
 3061     Mark_1_NoTrustError();
 3062     BS_End();
 3063     if (top_bar_flag)
 3064     {
 3065         Mark_1();
 3066         Mark_1();
 3067         Skip_S2(14,                                             "line_number_end_of_top_bar");
 3068     }
 3069     if (bottom_bar_flag)
 3070     {
 3071         Mark_1();
 3072         Mark_1();
 3073         Skip_S2(14,                                             "line_number_start_of_bottom_bar");
 3074     }
 3075     if (left_bar_flag)
 3076     {
 3077         Mark_1();
 3078         Mark_1();
 3079         Skip_S2(14,                                             "pixel_number_end_of_left_bar");
 3080     }
 3081     if (right_bar_flag)
 3082     {
 3083         Mark_1();
 3084         Mark_1();
 3085         Skip_S2(14,                                             "pixel_number_start_of_right_bar");
 3086     }
 3087     Mark_1();
 3088     Mark_1();
 3089     Mark_1();
 3090     Mark_1();
 3091     Mark_1();
 3092     Mark_1();
 3093     Mark_1();
 3094     Mark_1();
 3095     BS_End();
 3096 
 3097     if (Element_Size-Element_Offset)
 3098         Skip_XX(Element_Size-Element_Offset,                    "additional_bar_data");
 3099 }
 3100 
 3101 //---------------------------------------------------------------------------
 3102 // SEI - 5
 3103 void File_Avc::sei_message_user_data_unregistered(int32u payloadSize)
 3104 {
 3105     Element_Info1("user_data_unregistered");
 3106 
 3107     //Parsing
 3108     int128u uuid_iso_iec_11578;
 3109     Get_UUID(uuid_iso_iec_11578,                                "uuid_iso_iec_11578");
 3110 
 3111     switch (uuid_iso_iec_11578.hi)
 3112     {
 3113         case 0xDC45E9BDE6D948B7LL : Element_Info1("x264");
 3114                                      sei_message_user_data_unregistered_x264(payloadSize-16); break;
 3115         case 0xFB574A60AC924E68LL : Element_Info1("eavc");
 3116                                      sei_message_user_data_unregistered_x264(payloadSize-16); break;
 3117         case 0x17EE8C60F84D11D9LL : Element_Info1("Blu-ray");
 3118                                     sei_message_user_data_unregistered_bluray(payloadSize-16); break;
 3119         default :
 3120                     Element_Info1("unknown");
 3121                     Skip_XX(payloadSize-16,                     "data");
 3122     }
 3123 }
 3124 
 3125 //---------------------------------------------------------------------------
 3126 // SEI - 5 - x264
 3127 void File_Avc::sei_message_user_data_unregistered_x264(int32u payloadSize)
 3128 {
 3129     //Parsing
 3130     string Data;
 3131     Peek_String(payloadSize, Data);
 3132     if (Data.size()!=payloadSize && Data.size()+1!=payloadSize)
 3133     {
 3134         Skip_XX(payloadSize,                                    "Unknown");
 3135         return; //This is not a text string
 3136     }
 3137     size_t Data_Pos_Before=0;
 3138     size_t Loop=0;
 3139     do
 3140     {
 3141         size_t Data_Pos=Data.find(" - ", Data_Pos_Before);
 3142         if (Data_Pos==std::string::npos)
 3143             Data_Pos=Data.size();
 3144         if (Data.find("options: ", Data_Pos_Before)==Data_Pos_Before)
 3145         {
 3146             Element_Begin1("options");
 3147             size_t Options_Pos_Before=Data_Pos_Before;
 3148             Encoded_Library_Settings.clear();
 3149             do
 3150             {
 3151                 size_t Options_Pos=Data.find(__T(' '), Options_Pos_Before);
 3152                 if (Options_Pos==std::string::npos)
 3153                     Options_Pos=Data.size();
 3154                 string option;
 3155                 Get_String (Options_Pos-Options_Pos_Before, option, "option");
 3156                 Options_Pos_Before=Options_Pos;
 3157                 do
 3158                 {
 3159                     string Separator;
 3160                     Peek_String(1, Separator);
 3161                     if (Separator==" ")
 3162                     {
 3163                         Skip_UTF8(1,                                "separator");
 3164                         Options_Pos_Before+=1;
 3165                     }
 3166                     else
 3167                         break;
 3168                 }
 3169                 while (Options_Pos_Before!=Data.size());
 3170 
 3171                 //Filling
 3172                 if (option!="options:")
 3173                 {
 3174                     if (!Encoded_Library_Settings.empty())
 3175                         Encoded_Library_Settings+=__T(" / ");
 3176                     Encoded_Library_Settings+=Ztring().From_UTF8(option.c_str());
 3177                     if (option.find("bitrate=")==0)
 3178                         BitRate_Nominal.From_UTF8(option.substr(8)+"000"); //After "bitrate="
 3179                 }
 3180             }
 3181             while (Options_Pos_Before!=Data.size());
 3182             Element_End0();
 3183         }
 3184         else
 3185         {
 3186             string Value;
 3187             Get_String(Data_Pos-Data_Pos_Before, Value,          "data");
 3188 
 3189             //Saving
 3190             if (Loop==0)
 3191             {
 3192                 //Cleaning a little the value
 3193                 while (!Value.empty() && Value[0]<0x30)
 3194                     Value.erase(Value.begin());
 3195                 while (!Value.empty() && Value[Value.size()-1]<0x30)
 3196                     Value.erase(Value.end()-1);
 3197                 Encoded_Library.From_UTF8(Value.c_str());
 3198             }
 3199             if (Loop==1 && Encoded_Library.find(__T("x264"))==0)
 3200             {
 3201                 Encoded_Library+=__T(" - ");
 3202                 Encoded_Library+=Ztring().From_UTF8(Value.c_str());
 3203             }
 3204         }
 3205         Data_Pos_Before=Data_Pos;
 3206         if (Data_Pos_Before+3<=Data.size())
 3207         {
 3208             Skip_UTF8(3,                                        "separator");
 3209             Data_Pos_Before+=3;
 3210         }
 3211 
 3212         Loop++;
 3213     }
 3214     while (Data_Pos_Before!=Data.size());
 3215 
 3216     //Encoded_Library
 3217     if (Encoded_Library.find(__T("eavc "))==0)
 3218     {
 3219         Encoded_Library_Name=__T("eavc");
 3220         Encoded_Library_Version=Encoded_Library.SubString(__T("eavc "), __T(""));
 3221     }
 3222     else if (Encoded_Library.find(__T("x264 - "))==0)
 3223     {
 3224         Encoded_Library_Name=__T("x264");
 3225         Encoded_Library_Version=Encoded_Library.SubString(__T("x264 - "), __T(""));
 3226     }
 3227     else if (Encoded_Library.find(__T("SUPER(C) by eRightSoft "))==0)
 3228     {
 3229         Encoded_Library_Name=__T("SUPER(C) by eRightSoft");
 3230         Encoded_Library_Date=Ztring(__T("UTC "))+Encoded_Library.SubString(__T("2000-"), __T(" "));
 3231     }
 3232     else
 3233         Encoded_Library_Name=Encoded_Library;
 3234 }
 3235 
 3236 //---------------------------------------------------------------------------
 3237 // SEI - 5 - x264
 3238 void File_Avc::sei_message_user_data_unregistered_bluray(int32u payloadSize)
 3239 {
 3240     if (payloadSize<4)
 3241     {
 3242         Skip_XX(payloadSize,                                    "Unknown");
 3243         return;
 3244     }
 3245     int32u Identifier;
 3246     Get_B4 (Identifier,                                         "Identifier");
 3247     switch (Identifier)
 3248     {
 3249         case 0x47413934 :   sei_message_user_data_registered_itu_t_t35_GA94_03(); return;
 3250         case 0x4D44504D :   sei_message_user_data_unregistered_bluray_MDPM(Element_Size-Element_Offset); return;
 3251         default         :   Skip_XX(Element_Size-Element_Offset, "Unknown");
 3252     }
 3253 }
 3254 
 3255 //---------------------------------------------------------------------------
 3256 // SEI - 5 - bluray - MDPM
 3257 void File_Avc::sei_message_user_data_unregistered_bluray_MDPM(int32u payloadSize)
 3258 {
 3259     if (payloadSize<1)
 3260     {
 3261         Skip_XX(payloadSize, "Unknown");
 3262         return;
 3263     }
 3264 
 3265     Element_Info1("Modified Digital Video Pack Metadata");
 3266 
 3267     Skip_B1(                                                    "Count");
 3268     payloadSize--;
 3269     string DateTime0, DateTime1, DateTime2, Model0, Model1, Model2;
 3270     int16u MakeName=(int16u)-1;
 3271     Ztring IrisFNumber;
 3272     while (payloadSize >= 5)
 3273     {
 3274         Element_Begin0();
 3275         int8u  ID;
 3276         Get_B1(ID,                                              "ID"); Element_Name(MDPM(ID));
 3277         switch (ID)
 3278         {
 3279             case 0x18:
 3280                         {
 3281                         int16u Year;
 3282                         int8u  MM, Zone_Hours;
 3283                         bool   Zone_Sign, Zone_Minutes;
 3284                         BS_Begin();
 3285                         Mark_0();
 3286                         Skip_SB(                                "DST flag");
 3287                         Get_SB (Zone_Sign,                      "Time zone sign");
 3288                         Get_S1 (4, Zone_Hours,                  "Time zone hours");
 3289                         Get_SB (Zone_Minutes,                   "Time zone half-hour flag");
 3290                         BS_End();
 3291                         Get_B2 (Year,                           "Year");
 3292                         Get_B1 (MM,                             "Month");
 3293                         DateTime0+='0'+(Year>>12);
 3294                         DateTime0+='0'+((Year&0xF00)>>8);
 3295                         DateTime0+='0'+((Year&0xF0)>>4);
 3296                         DateTime0+='0'+(Year&0xF);
 3297                         DateTime0+='-';
 3298                         DateTime0+='0'+((MM&0xF0)>>4);
 3299                         DateTime0+='0'+(MM&0xF);
 3300                         DateTime0+='-';
 3301                         Element_Info1(DateTime0);
 3302                         DateTime2+=Zone_Sign?'-':'+';
 3303                         DateTime2+='0'+Zone_Hours/10;
 3304                         DateTime2+='0'+Zone_Hours%10;
 3305                         DateTime2+=':';
 3306                         DateTime2+=Zone_Minutes?'3':'0';
 3307                         DateTime2+='0';
 3308                         Element_Info1(DateTime2);
 3309                         }
 3310                         break;
 3311             case 0x19:
 3312                         {
 3313                         int8u  DD, hh, mm, ss;
 3314                         Get_B1 (DD,                             "Day");
 3315                         Get_B1 (hh,                             "Hour");
 3316                         Get_B1 (mm,                             "Minute");
 3317                         Get_B1 (ss,                             "Second");
 3318                         DateTime1+='0'+(DD>>4);
 3319                         DateTime1+='0'+(DD&0xF);
 3320                         DateTime1+=' ';
 3321                         DateTime1+='0'+(hh>>4);
 3322                         DateTime1+='0'+(hh&0xF);
 3323                         DateTime1+=':';
 3324                         DateTime1+='0'+(mm>>4);
 3325                         DateTime1+='0'+(mm&0xF);
 3326                         DateTime1+=':';
 3327                         DateTime1+='0'+(ss>>4);
 3328                         DateTime1+='0'+(ss&0xF);
 3329                         Element_Info1(DateTime1);
 3330                         }
 3331                         break;
 3332             case 0x70:
 3333                         consumer_camera_1();
 3334                         break;
 3335             case 0x71:
 3336                         consumer_camera_2();
 3337                         break;
 3338             case 0xA1:
 3339                         {
 3340                         int16u D, N;
 3341                         Get_B2 (D,                              "D");
 3342                         Get_B2 (N,                              "N");
 3343                         IrisFNumber.From_Number(((float64)D)/N, 6);
 3344                         Element_Info1(IrisFNumber);
 3345                         }
 3346                         break;
 3347             case 0xE0:
 3348                         {
 3349                         Get_B2 (MakeName,                       "Name");
 3350                         Skip_B2(                                "Category");
 3351                         Element_Info1(MDPM_MakeName(MakeName));
 3352                         }
 3353                         break;
 3354             case 0xE4:
 3355                         Get_String(4, Model0,                   "Data"); Element_Info1(Model0);
 3356                         break;
 3357             case 0xE5:
 3358                         Get_String(4, Model1,                   "Data"); Element_Info1(Model1);
 3359                         break;
 3360             case 0xE6:
 3361                         Get_String(4, Model2,                   "Data");
 3362                         Model2.erase(Model2.find_last_not_of('\0')+1);
 3363                         Element_Info1(Model2);
 3364                         break;
 3365             default: Skip_B4("Data");
 3366         }
 3367         Element_End0();
 3368         payloadSize -= 5;
 3369     }
 3370     if (payloadSize)
 3371         Skip_XX(payloadSize, "Unknown");
 3372 
 3373     FILLING_BEGIN();
 3374         if (!Frame_Count && !seq_parameter_sets.empty() && !pic_parameter_sets.empty())
 3375         {
 3376             if (!DateTime0.empty() && !DateTime1.empty())
 3377                 Fill(Stream_General, 0, General_Recorded_Date, DateTime0+DateTime1+DateTime2);
 3378             if (MDPM_MakeName(MakeName)[0] || !Model0.empty())
 3379             {
 3380                 string Model;
 3381                 if (MDPM_MakeName(MakeName)[0])
 3382                 {
 3383                     Model=MDPM_MakeName(MakeName);
 3384                     Fill(Stream_General, 0, General_Encoded_Application_CompanyName, Model);
 3385                     if (!Model0.empty())
 3386                         Model+=' ';
 3387                 }
 3388                 Fill(Stream_General, 0, General_Encoded_Application, Model+Model0+Model1+Model2);
 3389                 Fill(Stream_General, 0, General_Encoded_Application_Name, Model0+Model1+Model2);
 3390             }
 3391             Fill(Stream_Video, 0, "IrisFNumber", IrisFNumber);
 3392         }
 3393     FILLING_END();
 3394 }
 3395 
 3396 //---------------------------------------------------------------------------
 3397 void File_Avc::consumer_camera_1()
 3398 {
 3399     //Parsing
 3400     BS_Begin();
 3401     int8u ae_mode, wb_mode, white_balance, fcm;
 3402     Mark_1_NoTrustError();
 3403     Mark_1_NoTrustError();
 3404     Skip_S1(6,                                                  "iris");
 3405     Get_S1 (4, ae_mode,                                         "ae mode"); Param_Info1(Dv_consumer_camera_1_ae_mode[ae_mode]);
 3406     Skip_S1(4,                                                  "agc(Automatic Gain Control)");
 3407     Get_S1 (3, wb_mode,                                         "wb mode (white balance mode)"); Param_Info1(Dv_consumer_camera_1_wb_mode[wb_mode]);
 3408     Get_S1 (5, white_balance,                                   "white balance"); Param_Info1(Dv_consumer_camera_1_white_balance(white_balance));
 3409     Get_S1 (1, fcm,                                             "fcm (Focus mode)"); Param_Info1(Dv_consumer_camera_1_fcm[fcm]);
 3410     Skip_S1(7,                                                  "focus (focal point)");
 3411     BS_End();
 3412 
 3413     /* TODO: need some tweaking
 3414     FILLING_BEGIN();
 3415         if (!Frame_Count)
 3416         {
 3417             Ztring Settings;
 3418             if (ae_mode<0x0F) Settings+=__T("ae mode=")+Ztring(Dv_consumer_camera_1_ae_mode[ae_mode])+__T(", ");
 3419             if (wb_mode<0x08) Settings+=__T("wb mode=")+Ztring(Dv_consumer_camera_1_wb_mode[wb_mode])+__T(", ");
 3420             if (wb_mode<0x1F) Settings+=__T("white balance=")+Ztring(Dv_consumer_camera_1_white_balance(white_balance))+__T(", ");
 3421             Settings+=__T("fcm=")+Ztring(Dv_consumer_camera_1_fcm[fcm]);
 3422             Fill(Stream_Video, 0, "Camera_Settings", Settings);
 3423         }
 3424     FILLING_END();
 3425     */
 3426 }
 3427 
 3428 //---------------------------------------------------------------------------
 3429 void File_Avc::consumer_camera_2()
 3430 {
 3431     //Parsing
 3432     BS_Begin();
 3433     Mark_1_NoTrustError();
 3434     Mark_1_NoTrustError();
 3435     Skip_S1(1,                                                  "vpd");
 3436     Skip_S1(5,                                                  "vertical panning speed");
 3437     Skip_S1(1,                                                  "is");
 3438     Skip_S1(1,                                                  "hpd");
 3439     Skip_S1(6,                                                  "horizontal panning speed");
 3440     Skip_S1(8,                                                  "focal length");
 3441     Skip_S1(1,                                                  "zen");
 3442     Info_S1(3, zoom_U,                                          "units of e-zoom");
 3443     Info_S1(4, zoom_D,                                          "1/10 of e-zoom"); /*if (zoom_D!=0xF)*/ Param_Info1(__T("zoom=")+Ztring().From_Number(zoom_U+((float32)zoom_U)/10, 2));
 3444     BS_End();
 3445 }
 3446 
 3447 //---------------------------------------------------------------------------
 3448 void File_Avc::sei_message_mastering_display_colour_volume()
 3449 {
 3450     Element_Info1("mastering_display_colour_volume");
 3451 
 3452     std::map<video, Ztring>& SmpteSt2086 = HDR[HdrFormat_SmpteSt2086];
 3453     Ztring& HDR_Format = SmpteSt2086[Video_HDR_Format];
 3454     if (HDR_Format.empty())
 3455     {
 3456         HDR_Format = __T("SMPTE ST 2086");
 3457         SmpteSt2086[Video_HDR_Format_Compatibility] = "HDR10";
 3458     }
 3459     Get_MasteringDisplayColorVolume(SmpteSt2086[Video_MasteringDisplay_ColorPrimaries], SmpteSt2086[Video_MasteringDisplay_Luminance]);
 3460 }
 3461 //---------------------------------------------------------------------------
 3462 void File_Avc::sei_message_light_level()
 3463 {
 3464     Element_Info1("light_level");
 3465 
 3466     //Parsing
 3467     Get_B2(maximum_content_light_level, "maximum_content_light_level");
 3468     Get_B2(maximum_frame_average_light_level, "maximum_frame_average_light_level");
 3469 }
 3470 
 3471 //---------------------------------------------------------------------------
 3472 // SEI - 6
 3473 void File_Avc::sei_message_recovery_point()
 3474 {
 3475     Element_Info1("recovery_point");
 3476 
 3477     //Parsing
 3478     BS_Begin();
 3479     Skip_UE(                                                    "recovery_frame_cnt");
 3480     Skip_SB(                                                    "exact_match_flag");
 3481     Skip_SB(                                                    "broken_link_flag");
 3482     Skip_S1(2,                                                  "changing_slice_group_idc");
 3483     BS_End();
 3484 }
 3485 
 3486 //---------------------------------------------------------------------------
 3487 // SEI - 32
 3488 void File_Avc::sei_message_mainconcept(int32u payloadSize)
 3489 {
 3490     Element_Info1("MainConcept text");
 3491 
 3492     //Parsing
 3493     string Text;
 3494     Get_String(payloadSize, Text,                               "text");
 3495 
 3496     if (Text.find("produced by MainConcept H.264/AVC Codec v")!=std::string::npos)
 3497     {
 3498         Encoded_Library=Ztring().From_UTF8(Text).SubString(__T("produced by "), __T(" MainConcept AG"));
 3499         Encoded_Library_Name=__T("MainConcept H.264/AVC Codec");
 3500         Encoded_Library_Version= Ztring().From_UTF8(Text).SubString(__T("produced by MainConcept H.264/AVC Codec v"), __T(" (c) "));
 3501         Encoded_Library_Date=MediaInfoLib::Config.Library_Get(InfoLibrary_Format_MainConcept_Avc, Encoded_Library_Version, InfoLibrary_Date);
 3502     }
 3503 }
 3504 
 3505 //---------------------------------------------------------------------------
 3506 void File_Avc::sei_alternative_transfer_characteristics()
 3507 {
 3508     Element_Info1("alternative_transfer_characteristics");
 3509 
 3510     //Parsing
 3511     Get_B1(preferred_transfer_characteristics, "preferred_transfer_characteristics"); Param_Info1(Mpegv_transfer_characteristics(preferred_transfer_characteristics));
 3512 }
 3513 
 3514 //---------------------------------------------------------------------------
 3515 // Packet "07"
 3516 void File_Avc::seq_parameter_set()
 3517 {
 3518     Element_Name("seq_parameter_set");
 3519 
 3520     //parsing
 3521     int32u seq_parameter_set_id;
 3522     seq_parameter_set_struct* Data_Item_New=seq_parameter_set_data(seq_parameter_set_id);
 3523     if (!Data_Item_New)
 3524         return;
 3525     Mark_1(                                                     );
 3526     size_t BS_bits=Data_BS_Remain()%8;
 3527     while (BS_bits)
 3528     {
 3529         Mark_0(                                                 );
 3530         BS_bits--;
 3531     }
 3532     BS_End();
 3533 
 3534     //Hack for 00003.m2ts: There is a trailing 0x89, why?
 3535     if (Element_Offset+1==Element_Size)
 3536     {
 3537         int8u ToTest;
 3538         Peek_B1(ToTest);
 3539         if (ToTest==0x98)
 3540             Skip_B1(                                            "Unknown");
 3541 
 3542     }
 3543 
 3544     //Hack for : There is a trailing data, why?
 3545     if (Element_Offset+4==Element_Size)
 3546     {
 3547         int32u ToTest;
 3548         Peek_B4(ToTest);
 3549         if (ToTest==0xE30633C0)
 3550             Skip_B4(                                            "Unknown");
 3551     }
 3552 
 3553     //NULL bytes
 3554     while (Element_Offset<Element_Size)
 3555     {
 3556         int8u Null;
 3557         Get_B1 (Null,                                           "NULL byte");
 3558         if (Null)
 3559             Trusted_IsNot("Should be NULL byte");
 3560     }
 3561 
 3562     FILLING_BEGIN_PRECISE();
 3563         //NextCode
 3564         NextCode_Clear();
 3565         NextCode_Add(0x08);
 3566 
 3567         //Add
 3568         seq_parameter_set_data_Add(seq_parameter_sets, seq_parameter_set_id, Data_Item_New);
 3569 
 3570         //Autorisation of other streams
 3571         Streams[0x08].Searching_Payload=true; //pic_parameter_set
 3572         if (Streams[0x07].ShouldDuplicate)
 3573             Streams[0x08].ShouldDuplicate=true; //pic_parameter_set
 3574         Streams[0x0A].Searching_Payload=true; //end_of_seq
 3575         if (Streams[0x07].ShouldDuplicate)
 3576             Streams[0x0A].ShouldDuplicate=true; //end_of_seq
 3577         Streams[0x0B].Searching_Payload=true; //end_of_stream
 3578         if (Streams[0x07].ShouldDuplicate)
 3579             Streams[0x0B].ShouldDuplicate=true; //end_of_stream
 3580     FILLING_ELSE();
 3581         delete Data_Item_New;
 3582     FILLING_END();
 3583 }
 3584 
 3585 void File_Avc::seq_parameter_set_data_Add(std::vector<seq_parameter_set_struct*> &Data, const int32u Data_id, seq_parameter_set_struct* Data_Item_New)
 3586 {
 3587     //Creating Data
 3588     if (Data_id>=Data.size())
 3589         Data.resize(Data_id+1);
 3590     else
 3591         FirstPFrameInGop_IsParsed=true;
 3592     std::vector<seq_parameter_set_struct*>::iterator Data_Item=Data.begin()+Data_id;
 3593     delete *Data_Item; *Data_Item=Data_Item_New;
 3594 
 3595     //Computing values (for speed)
 3596     size_t MaxNumber;
 3597     switch (Data_Item_New->pic_order_cnt_type)
 3598     {
 3599         case 0 :
 3600                     MaxNumber=Data_Item_New->MaxPicOrderCntLsb;
 3601                     break;
 3602         case 1 :
 3603         case 2 :
 3604                     MaxNumber=Data_Item_New->MaxFrameNum*2;
 3605                     break;
 3606         default:
 3607                     MaxNumber = 0;
 3608     }
 3609 
 3610     if (MaxNumber>TemporalReferences_Reserved)
 3611     {
 3612         TemporalReferences.resize(4*MaxNumber);
 3613         TemporalReferences_Reserved=MaxNumber;
 3614     }
 3615 }
 3616 
 3617 //---------------------------------------------------------------------------
 3618 // Packet "08"
 3619 void File_Avc::pic_parameter_set()
 3620 {
 3621     Element_Name("pic_parameter_set");
 3622 
 3623     //Parsing
 3624     int32u  pic_parameter_set_id, seq_parameter_set_id, num_slice_groups_minus1, num_ref_idx_l0_default_active_minus1, num_ref_idx_l1_default_active_minus1, slice_group_map_type=0;
 3625     int8u   weighted_bipred_idc=0;
 3626     bool    entropy_coding_mode_flag,bottom_field_pic_order_in_frame_present_flag, redundant_pic_cnt_present_flag, weighted_pred_flag, deblocking_filter_control_present_flag;
 3627     BS_Begin();
 3628     Get_UE (pic_parameter_set_id,                               "pic_parameter_set_id");
 3629     Get_UE (seq_parameter_set_id,                               "seq_parameter_set_id");
 3630     std::vector<seq_parameter_set_struct*>::iterator seq_parameter_set_Item;
 3631     if (seq_parameter_set_id>=seq_parameter_sets.size() || (*(seq_parameter_set_Item=seq_parameter_sets.begin()+seq_parameter_set_id))==NULL)
 3632     {
 3633         if (seq_parameter_set_id>=subset_seq_parameter_sets.size() || (*(seq_parameter_set_Item=subset_seq_parameter_sets.begin()+seq_parameter_set_id))==NULL)
 3634         {
 3635             //Not yet present
 3636             Skip_BS(Data_BS_Remain(),                               "Data (seq_parameter_set is missing)");
 3637             return;
 3638         }
 3639     }
 3640     Get_SB (entropy_coding_mode_flag,                           "entropy_coding_mode_flag");
 3641     Get_SB (bottom_field_pic_order_in_frame_present_flag,       "bottom_field_pic_order_in_frame_present_flag");
 3642     Get_UE (num_slice_groups_minus1,                            "num_slice_groups_minus1");
 3643     if (num_slice_groups_minus1>7)
 3644     {
 3645         Trusted_IsNot("num_slice_groups_minus1 too high");
 3646         num_slice_groups_minus1=0;
 3647     }
 3648     if (num_slice_groups_minus1>0)
 3649     {
 3650         Get_UE (slice_group_map_type,                           "slice_group_map_type");
 3651         if (slice_group_map_type==0)
 3652         {
 3653             for (int32u Pos=0; Pos<=num_slice_groups_minus1; Pos++)
 3654                 Skip_UE(                                        "run_length_minus1");
 3655         }
 3656         else if (slice_group_map_type==2)
 3657         {
 3658             for (int32u Pos=0; Pos<num_slice_groups_minus1; Pos++)
 3659             {
 3660                 Skip_UE(                                        "top_left");
 3661                 Skip_UE(                                        "bottom_right");
 3662             }
 3663         }
 3664         else if (slice_group_map_type==3
 3665               || slice_group_map_type==4
 3666               || slice_group_map_type==5)
 3667         {
 3668             Skip_SB(                                            "slice_group_change_direction_flag");
 3669             Skip_UE(                                            "slice_group_change_rate_minus1");
 3670         }
 3671         else if (slice_group_map_type==6)
 3672         {
 3673             int32u pic_size_in_map_units_minus1;
 3674             Get_UE (pic_size_in_map_units_minus1,               "pic_size_in_map_units_minus1");
 3675             if(pic_size_in_map_units_minus1>((*seq_parameter_set_Item)->pic_width_in_mbs_minus1+1)*((*seq_parameter_set_Item)->pic_height_in_map_units_minus1+1))
 3676             {
 3677                 Trusted_IsNot("pic_size_in_map_units_minus1 too high");
 3678                 return;
 3679             }
 3680             #if defined (__mips__)       || defined (__mipsel__)
 3681                 int32u slice_group_id_Size=(int32u)(std::ceil(std::log((double)(num_slice_groups_minus1+1))/std::log((double)10))); //std::log is natural logarithm
 3682             #else
 3683                 int32u slice_group_id_Size=(int32u)(std::ceil(std::log((float32)(num_slice_groups_minus1+1))/std::log((float32)10))); //std::log is natural logarithm
 3684             #endif
 3685             for (int32u Pos=0; Pos<=pic_size_in_map_units_minus1; Pos++)
 3686                 Skip_BS(slice_group_id_Size,                    "slice_group_id");
 3687         }
 3688     }
 3689     Get_UE (num_ref_idx_l0_default_active_minus1,               "num_ref_idx_l0_default_active_minus1");
 3690     Get_UE (num_ref_idx_l1_default_active_minus1,               "num_ref_idx_l1_default_active_minus1");
 3691     Get_SB (weighted_pred_flag,                                 "weighted_pred_flag");
 3692     Get_S1 (2, weighted_bipred_idc,                             "weighted_bipred_idc");
 3693     Skip_SE(                                                    "pic_init_qp_minus26");
 3694     Skip_SE(                                                    "pic_init_qs_minus26");
 3695     Skip_SE(                                                    "chroma_qp_index_offset");
 3696     Get_SB (deblocking_filter_control_present_flag,             "deblocking_filter_control_present_flag");
 3697     Skip_SB(                                                    "constrained_intra_pred_flag");
 3698     Get_SB (redundant_pic_cnt_present_flag,                     "redundant_pic_cnt_present_flag");
 3699     bool more_rbsp_data=false;
 3700     if (Element_Size)
 3701     {
 3702         int64u Offset=Element_Size-1;
 3703         while (Offset && Buffer[Buffer_Offset+(size_t)Offset]==0x00) //Searching if there are NULL bytes at the end of the data
 3704             Offset--;
 3705         size_t Bit_Pos=7;
 3706         while (Bit_Pos && !(Buffer[Buffer_Offset+(size_t)Offset]&(1<<(7-Bit_Pos))))
 3707             Bit_Pos--;
 3708         if (Data_BS_Remain()>1+(7-Bit_Pos)+(Element_Size-Offset-1)*8)
 3709             more_rbsp_data=true;
 3710     }
 3711     if (more_rbsp_data)
 3712     {
 3713         bool transform_8x8_mode_flag;
 3714         Get_SB (transform_8x8_mode_flag,                        "transform_8x8_mode_flag");
 3715         TEST_SB_SKIP(                                           "pic_scaling_matrix_present_flag");
 3716         for (int8u Pos=0; Pos<6+(transform_8x8_mode_flag?((*seq_parameter_set_Item)->chroma_format_idc!=3?2:6):0); Pos++ )
 3717             {
 3718                 TEST_SB_SKIP(                                   "pic_scaling_list_present_flag");
 3719                     scaling_list(Pos<6?16:64);
 3720                 TEST_SB_END();
 3721             }
 3722         TEST_SB_END();
 3723         Skip_SE(                                                "second_chroma_qp_index_offset");
 3724     }
 3725     Mark_1(                                                     );
 3726     BS_End();
 3727 
 3728     while (Element_Offset<Element_Size) //Not always removed from the stream, ie in MPEG-4
 3729     {
 3730         int8u Padding;
 3731         Peek_B1(Padding);
 3732         if (!Padding)
 3733             Skip_B1(                                            "Padding");
 3734         else
 3735             break;
 3736     }
 3737 
 3738     FILLING_BEGIN_PRECISE();
 3739         //Integrity
 3740         if (pic_parameter_set_id>=256)
 3741         {
 3742             Trusted_IsNot("pic_parameter_set_id not valid");
 3743             return; //Problem, not valid
 3744         }
 3745         if (seq_parameter_set_id>=32)
 3746         {
 3747             Trusted_IsNot("seq_parameter_set_id not valid");
 3748             return; //Problem, not valid
 3749         }
 3750 
 3751         //NextCode
 3752         NextCode_Clear();
 3753         NextCode_Add(0x05);
 3754         NextCode_Add(0x06);
 3755         if (!subset_seq_parameter_sets.empty())
 3756             NextCode_Add(0x14); //slice_layer_extension
 3757 
 3758         //Filling
 3759         if (pic_parameter_set_id>=pic_parameter_sets.size())
 3760             pic_parameter_sets.resize(pic_parameter_set_id+1);
 3761         std::vector<pic_parameter_set_struct*>::iterator pic_parameter_sets_Item=pic_parameter_sets.begin()+pic_parameter_set_id;
 3762         delete *pic_parameter_sets_Item; *pic_parameter_sets_Item = new pic_parameter_set_struct(
 3763                                                                                                     (int8u)seq_parameter_set_id,
 3764                                                                                                     (int8u)num_ref_idx_l0_default_active_minus1,
 3765                                                                                                     (int8u)num_ref_idx_l1_default_active_minus1,
 3766                                                                                                     weighted_bipred_idc,
 3767                                                                                                     num_slice_groups_minus1,
 3768                                                                                                     slice_group_map_type,
 3769                                                                                                     entropy_coding_mode_flag,
 3770                                                                                                     bottom_field_pic_order_in_frame_present_flag,
 3771                                                                                                     weighted_pred_flag,
 3772                                                                                                     redundant_pic_cnt_present_flag,
 3773                                                                                                     deblocking_filter_control_present_flag
 3774                                                                                                 );
 3775 
 3776         //Autorisation of other streams
 3777         if (!seq_parameter_sets.empty())
 3778         {
 3779             for (int8u Pos=0x01; Pos<=0x06; Pos++)
 3780             {
 3781                 Streams[Pos].Searching_Payload=true; //Coded slice...
 3782                 if (Streams[0x08].ShouldDuplicate)
 3783                     Streams[Pos].ShouldDuplicate=true;
 3784             }
 3785         }
 3786         if (!subset_seq_parameter_sets.empty())
 3787         {
 3788             Streams[0x14].Searching_Payload=true; //slice_layer_extension
 3789             if (Streams[0x08].ShouldDuplicate)
 3790                 Streams[0x14].ShouldDuplicate=true; //slice_layer_extension
 3791         }
 3792 
 3793         //Setting as OK
 3794         if (!Status[IsAccepted])
 3795             Accept("AVC");
 3796     FILLING_END();
 3797 }
 3798 
 3799 //---------------------------------------------------------------------------
 3800 // Packet "09"
 3801 void File_Avc::access_unit_delimiter()
 3802 {
 3803     Element_Name("access_unit_delimiter");
 3804 
 3805     int8u primary_pic_type;
 3806     BS_Begin();
 3807     Get_S1 ( 3, primary_pic_type,                               "primary_pic_type"); Param_Info1(Avc_primary_pic_type[primary_pic_type]);
 3808     Mark_1_NoTrustError(                                        ); //Found 1 file without this bit
 3809     BS_End();
 3810 }
 3811 
 3812 //---------------------------------------------------------------------------
 3813 // Packet "09"
 3814 void File_Avc::filler_data()
 3815 {
 3816     Element_Name("filler_data");
 3817 
 3818     while (Element_Offset<Element_Size)
 3819     {
 3820         int8u FF;
 3821         Peek_B1(FF);
 3822         if (FF!=0xFF)
 3823             break;
 3824         Element_Offset++;
 3825     }
 3826     BS_Begin();
 3827     Mark_1(                                                     );
 3828     BS_End();
 3829 }
 3830 
 3831 //---------------------------------------------------------------------------
 3832 // Packet "0E"
 3833 void File_Avc::prefix_nal_unit(bool svc_extension_flag)
 3834 {
 3835     Element_Name("prefix_nal_unit");
 3836 
 3837     //Parsing
 3838     if (svc_extension_flag)
 3839     {
 3840         Skip_XX(Element_Size-Element_Offset,                    "prefix_nal_unit_svc");
 3841     }
 3842 }
 3843 
 3844 //---------------------------------------------------------------------------
 3845 // Packet "0F"
 3846 void File_Avc::subset_seq_parameter_set()
 3847 {
 3848     Element_Name("subset_seq_parameter_set");
 3849 
 3850     //Parsing
 3851     int32u subset_seq_parameter_set_id;
 3852     seq_parameter_set_struct* Data_Item_New=seq_parameter_set_data(subset_seq_parameter_set_id);
 3853     if (!Data_Item_New)
 3854         return;
 3855     if (Data_Item_New->profile_idc==83 || Data_Item_New->profile_idc==86)
 3856     {
 3857         //bool svc_vui_parameters_present_flag;
 3858         seq_parameter_set_svc_extension();
 3859         /* The rest is not yet implemented
 3860         Get_SB (svc_vui_parameters_present_flag,                "svc_vui_parameters_present_flag");
 3861         if (svc_vui_parameters_present_flag)
 3862             svc_vui_parameters_extension();
 3863         */
 3864     }
 3865     else if (Data_Item_New->profile_idc==118 || Data_Item_New->profile_idc==128)
 3866     {
 3867         //bool mvc_vui_parameters_present_flag, additional_extension2_flag;
 3868         Mark_1();
 3869         seq_parameter_set_mvc_extension(Data_Item_New);
 3870         /* The rest is not yet implemented
 3871         Get_SB (mvc_vui_parameters_present_flag,                "mvc_vui_parameters_present_flag");
 3872         if (mvc_vui_parameters_present_flag)
 3873             mvc_vui_parameters_extension();
 3874         Get_SB (additional_extension2_flag,                     "additional_extension2_flag");
 3875         if (additional_extension2_flag)
 3876         {
 3877             //Not handled, should skip all bits except 1
 3878             BS_End();
 3879             return;
 3880         }
 3881         */
 3882     }
 3883     /* The rest is not yet implemented
 3884     Mark_1(                                                     );
 3885     */
 3886     BS_End();
 3887 
 3888     FILLING_BEGIN();
 3889         //NextCode
 3890         NextCode_Clear();
 3891         NextCode_Add(0x08);
 3892 
 3893         //Add
 3894         seq_parameter_set_data_Add(subset_seq_parameter_sets, subset_seq_parameter_set_id, Data_Item_New);
 3895 
 3896         //Autorisation of other streams
 3897         Streams[0x08].Searching_Payload=true; //pic_parameter_set
 3898         if (Streams[0x0F].ShouldDuplicate)
 3899             Streams[0x08].ShouldDuplicate=true; //pic_parameter_set
 3900         Streams[0x0A].Searching_Payload=true; //end_of_seq
 3901         if (Streams[0x0F].ShouldDuplicate)
 3902             Streams[0x0A].ShouldDuplicate=true; //end_of_seq
 3903         Streams[0x0B].Searching_Payload=true; //end_of_stream
 3904         if (Streams[0x0F].ShouldDuplicate)
 3905             Streams[0x0B].ShouldDuplicate=true; //end_of_stream
 3906     FILLING_END();
 3907 }
 3908 
 3909 //---------------------------------------------------------------------------
 3910 // Packet "14"
 3911 void File_Avc::slice_layer_extension(bool svc_extension_flag)
 3912 {
 3913     Element_Name("slice_layer_extension");
 3914 
 3915     //Parsing
 3916     if (svc_extension_flag)
 3917     {
 3918         Skip_XX(Element_Size-Element_Offset,                    "slice_header_in_scalable_extension + slice_data_in_scalable_extension");
 3919     }
 3920     else
 3921     {
 3922         BS_Begin();
 3923         slice_header();
 3924         slice_data(true);
 3925         BS_End();
 3926     }
 3927 }
 3928 
 3929 //***************************************************************************
 3930 // SubElements
 3931 //***************************************************************************
 3932 
 3933 //---------------------------------------------------------------------------
 3934 File_Avc::seq_parameter_set_struct* File_Avc::seq_parameter_set_data(int32u &Data_id)
 3935 {
 3936     //Parsing
 3937     seq_parameter_set_struct::vui_parameters_struct* vui_parameters_Item=NULL;
 3938     int32u  chroma_format_idc=1, bit_depth_luma_minus8=0, bit_depth_chroma_minus8=0, log2_max_frame_num_minus4, pic_order_cnt_type, log2_max_pic_order_cnt_lsb_minus4=(int32u)-1, max_num_ref_frames, pic_width_in_mbs_minus1, pic_height_in_map_units_minus1, frame_crop_left_offset=0, frame_crop_right_offset=0, frame_crop_top_offset=0, frame_crop_bottom_offset=0;
 3939     int8u   profile_idc, level_idc;
 3940     bool    constraint_set3_flag, separate_colour_plane_flag=false, delta_pic_order_always_zero_flag=false, frame_mbs_only_flag, mb_adaptive_frame_field_flag=false;
 3941     Get_B1 (profile_idc,                                        "profile_idc");
 3942     BS_Begin();
 3943     Element_Begin1("constraints");
 3944         Skip_SB(                                                "constraint_set0_flag");
 3945         Skip_SB(                                                "constraint_set1_flag");
 3946         Skip_SB(                                                "constraint_set2_flag");
 3947         Get_SB (constraint_set3_flag,                           "constraint_set3_flag");
 3948         Skip_SB(                                                "constraint_set4_flag");
 3949         Skip_SB(                                                "constraint_set5_flag");
 3950         Skip_BS(2,                                              "reserved_zero_2bits");
 3951     Element_End0();
 3952     Get_S1 ( 8, level_idc,                                      "level_idc");
 3953     Get_UE (    Data_id,                                        "seq_parameter_set_id");
 3954     switch (profile_idc)
 3955     {
 3956         case 100 :
 3957         case 110 :
 3958         case 122 :
 3959         case 244 :
 3960         case  44 :
 3961         case  83 :
 3962         case  86 :
 3963         case 118 :
 3964         case 128 :  //High profiles
 3965         case 138 :
 3966                     Element_Begin1("high profile specific");
 3967                     Get_UE (chroma_format_idc,                  "chroma_format_idc"); Param_Info1C((chroma_format_idc<3), Avc_ChromaSubsampling_format_idc(chroma_format_idc));
 3968                     if (chroma_format_idc==3)
 3969                         Get_SB (separate_colour_plane_flag,     "separate_colour_plane_flag");
 3970                     Get_UE (bit_depth_luma_minus8,              "bit_depth_luma_minus8");
 3971                     Get_UE (bit_depth_chroma_minus8,            "bit_depth_chroma_minus8");
 3972                     Skip_SB(                                    "qpprime_y_zero_transform_bypass_flag");
 3973                     TEST_SB_SKIP(                               "seq_scaling_matrix_present_flag");
 3974                         for (int32u Pos=0; Pos<(int32u)((chroma_format_idc!=3) ? 8 : 12); Pos++)
 3975                         {
 3976                             TEST_SB_SKIP(                       "seq_scaling_list_present_flag");
 3977                                 scaling_list(Pos<6?16:64);
 3978                             TEST_SB_END();
 3979                         }
 3980                     TEST_SB_END();
 3981                     Element_End0();
 3982                     break;
 3983         default  :  ;
 3984     }
 3985     Get_UE (log2_max_frame_num_minus4,                          "log2_max_frame_num_minus4");
 3986     Get_UE (pic_order_cnt_type,                                 "pic_order_cnt_type");
 3987     if (pic_order_cnt_type==0)
 3988         Get_UE (log2_max_pic_order_cnt_lsb_minus4,              "log2_max_pic_order_cnt_lsb_minus4");
 3989     else if (pic_order_cnt_type==1)
 3990     {
 3991         int32u num_ref_frames_in_pic_order_cnt_cycle;
 3992         Get_SB (delta_pic_order_always_zero_flag,               "delta_pic_order_always_zero_flag");
 3993         Skip_SE(                                                "offset_for_non_ref_pic");
 3994         Skip_SE(                                                "offset_for_top_to_bottom_field");
 3995         Get_UE (num_ref_frames_in_pic_order_cnt_cycle,          "num_ref_frames_in_pic_order_cnt_cycle");
 3996         if (num_ref_frames_in_pic_order_cnt_cycle>=256)
 3997         {
 3998             Trusted_IsNot("num_ref_frames_in_pic_order_cnt_cycle too high");
 3999             return NULL;
 4000         }
 4001         for(int32u Pos=0; Pos<num_ref_frames_in_pic_order_cnt_cycle; Pos++)
 4002             Skip_SE(                                            "offset_for_ref_frame");
 4003     }
 4004     else if (pic_order_cnt_type > 2)
 4005     {
 4006         Trusted_IsNot("pic_order_cnt_type not supported");
 4007         return NULL;
 4008     }
 4009     Get_UE (max_num_ref_frames,                                 "max_num_ref_frames");
 4010     Skip_SB(                                                    "gaps_in_frame_num_value_allowed_flag");
 4011     Get_UE (pic_width_in_mbs_minus1,                            "pic_width_in_mbs_minus1");
 4012     Get_UE (pic_height_in_map_units_minus1,                     "pic_height_in_map_units_minus1");
 4013     Get_SB (frame_mbs_only_flag,                                "frame_mbs_only_flag");
 4014     if (!frame_mbs_only_flag)
 4015         Get_SB (mb_adaptive_frame_field_flag,                   "mb_adaptive_frame_field_flag");
 4016     Skip_SB(                                                    "direct_8x8_inference_flag");
 4017     TEST_SB_SKIP(                                               "frame_cropping_flag");
 4018         Get_UE (frame_crop_left_offset,                         "frame_crop_left_offset");
 4019         Get_UE (frame_crop_right_offset,                        "frame_crop_right_offset");
 4020         Get_UE (frame_crop_top_offset,                          "frame_crop_top_offset");
 4021         Get_UE (frame_crop_bottom_offset,                       "frame_crop_bottom_offset");
 4022     TEST_SB_END();
 4023     TEST_SB_SKIP(                                               "vui_parameters_present_flag");
 4024         vui_parameters(vui_parameters_Item);
 4025     TEST_SB_END();
 4026 
 4027     FILLING_BEGIN();
 4028         //Integrity
 4029         if (Data_id>=32)
 4030         {
 4031             Trusted_IsNot("seq_parameter_set_id not valid");
 4032             delete (seq_parameter_set_struct::vui_parameters_struct*)vui_parameters_Item;
 4033             return NULL; //Problem, not valid
 4034         }
 4035         if (pic_order_cnt_type==0 && log2_max_pic_order_cnt_lsb_minus4>12)
 4036         {
 4037             Trusted_IsNot("log2_max_pic_order_cnt_lsb_minus4 not valid");
 4038             delete (seq_parameter_set_struct::vui_parameters_struct*)vui_parameters_Item;
 4039             return NULL; //Problem, not valid
 4040         }
 4041         if (log2_max_frame_num_minus4>12)
 4042         {
 4043             Trusted_IsNot("log2_max_frame_num_minus4 not valid");
 4044             delete (seq_parameter_set_struct::vui_parameters_struct*)vui_parameters_Item;
 4045             return NULL; //Problem, not valid
 4046         }
 4047 
 4048         //Creating Data
 4049         return new seq_parameter_set_struct(
 4050                                                                     vui_parameters_Item,
 4051                                                                     pic_width_in_mbs_minus1,
 4052                                                                     pic_height_in_map_units_minus1,
 4053                                                                     frame_crop_left_offset,
 4054                                                                     frame_crop_right_offset,
 4055                                                                     frame_crop_top_offset,
 4056                                                                     frame_crop_bottom_offset,
 4057                                                                     (int8u)chroma_format_idc,
 4058                                                                     profile_idc,
 4059                                                                     level_idc,
 4060                                                                     (int8u)bit_depth_luma_minus8,
 4061                                                                     (int8u)bit_depth_chroma_minus8,
 4062                                                                     (int8u)log2_max_frame_num_minus4,
 4063                                                                     (int8u)pic_order_cnt_type,
 4064                                                                     (int8u)log2_max_pic_order_cnt_lsb_minus4,
 4065                                                                     (int8u)max_num_ref_frames,
 4066                                                                     constraint_set3_flag,
 4067                                                                     separate_colour_plane_flag,
 4068                                                                     delta_pic_order_always_zero_flag,
 4069                                                                     frame_mbs_only_flag,
 4070                                                                     mb_adaptive_frame_field_flag
 4071                                                                   );
 4072     FILLING_ELSE();
 4073         delete vui_parameters_Item; //vui_parameters_Item=NULL;
 4074         return NULL;
 4075     FILLING_END();
 4076 }
 4077 
 4078 //---------------------------------------------------------------------------
 4079 void File_Avc::scaling_list(int32u ScalingList_Size)
 4080 {
 4081     //From http://mpeg4ip.cvs.sourceforge.net/mpeg4ip/mpeg4ip/util/h264/main.cpp?revision=1.17&view=markup
 4082     int32u lastScale=8, nextScale=8;
 4083     for (int32u Pos=0; Pos<ScalingList_Size; Pos++)
 4084     {
 4085         if (nextScale!=0)
 4086         {
 4087             int32s delta_scale;
 4088             Get_SE (delta_scale,                                "scale_delta");
 4089             nextScale=(lastScale+delta_scale+256)%256;
 4090         }
 4091         if (nextScale)
 4092             lastScale=nextScale;
 4093     }
 4094 }
 4095 
 4096 //---------------------------------------------------------------------------
 4097 void File_Avc::vui_parameters(seq_parameter_set_struct::vui_parameters_struct* &vui_parameters_Item_)
 4098 {
 4099     //Parsing
 4100     seq_parameter_set_struct::vui_parameters_struct::xxl *NAL=NULL, *VCL=NULL;
 4101     int32u  num_units_in_tick=(int32u)-1, time_scale=(int32u)-1;
 4102     int16u  sar_width=(int16u)-1, sar_height=(int16u)-1;
 4103     int8u   aspect_ratio_idc=0, video_format=5, video_full_range_flag = 0, colour_primaries=2, transfer_characteristics=2, matrix_coefficients=2;
 4104     bool    aspect_ratio_info_present_flag, video_signal_type_present_flag, colour_description_present_flag=false, timing_info_present_flag, fixed_frame_rate_flag=false, nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag, pic_struct_present_flag;
 4105     TEST_SB_GET (aspect_ratio_info_present_flag,                "aspect_ratio_info_present_flag");
 4106         Get_S1 (8, aspect_ratio_idc,                            "aspect_ratio_idc"); Param_Info1C((aspect_ratio_idc<Avc_PixelAspectRatio_Size), Avc_PixelAspectRatio[aspect_ratio_idc]);
 4107         if (aspect_ratio_idc==0xFF)
 4108         {
 4109             Get_S2 (16, sar_width,                              "sar_width");
 4110             Get_S2 (16, sar_height,                             "sar_height");
 4111         }
 4112     TEST_SB_END();
 4113     TEST_SB_SKIP(                                               "overscan_info_present_flag");
 4114         Skip_SB(                                                "overscan_appropriate_flag");
 4115     TEST_SB_END();
 4116     TEST_SB_GET (video_signal_type_present_flag,                "video_signal_type_present_flag");
 4117         Get_S1 (3, video_format,                                "video_format"); Param_Info1(Avc_video_format[video_format]);
 4118         Get_S1 (1, video_full_range_flag,                       "video_full_range_flag"); Param_Info1(Avc_video_full_range[video_full_range_flag]);
 4119         TEST_SB_GET (colour_description_present_flag,           "colour_description_present_flag");
 4120             Get_S1 (8, colour_primaries,                        "colour_primaries"); Param_Info1(Mpegv_colour_primaries(colour_primaries));
 4121             Get_S1 (8, transfer_characteristics,                "transfer_characteristics"); Param_Info1(Mpegv_transfer_characteristics(transfer_characteristics));
 4122             Get_S1 (8, matrix_coefficients,                     "matrix_coefficients"); Param_Info1(Mpegv_matrix_coefficients(matrix_coefficients));
 4123         TEST_SB_END();
 4124     TEST_SB_END();
 4125     TEST_SB_SKIP(                                               "chroma_loc_info_present_flag");
 4126         Skip_UE(                                                "chroma_sample_loc_type_top_field");
 4127         Skip_UE(                                                "chroma_sample_loc_type_bottom_field");
 4128     TEST_SB_END();
 4129     TEST_SB_GET (timing_info_present_flag,                      "timing_info_present_flag");
 4130         Get_S4 (32, num_units_in_tick,                          "num_units_in_tick");
 4131         Get_S4 (32, time_scale,                                 "time_scale");
 4132         Get_SB (    fixed_frame_rate_flag,                      "fixed_frame_rate_flag");
 4133     TEST_SB_END();
 4134     TEST_SB_GET (nal_hrd_parameters_present_flag,               "nal_hrd_parameters_present_flag");
 4135         hrd_parameters(NAL);
 4136     TEST_SB_END();
 4137     TEST_SB_GET (vcl_hrd_parameters_present_flag,               "vcl_hrd_parameters_present_flag");
 4138         hrd_parameters(VCL);
 4139     TEST_SB_END();
 4140     if (nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
 4141         Skip_SB(                                                "low_delay_hrd_flag");
 4142     Get_SB (   pic_struct_present_flag,                         "pic_struct_present_flag");
 4143     TEST_SB_SKIP(                                               "bitstream_restriction_flag");
 4144         int32u  max_num_reorder_frames;
 4145         Skip_SB(                                                "motion_vectors_over_pic_boundaries_flag");
 4146         Skip_UE(                                                "max_bytes_per_pic_denom");
 4147         Skip_UE(                                                "max_bits_per_mb_denom");
 4148         Skip_UE(                                                "log2_max_mv_length_horizontal");
 4149         Skip_UE(                                                "log2_max_mv_length_vertical");
 4150         Get_UE (max_num_reorder_frames,                         "max_num_reorder_frames");
 4151         Skip_UE(                                                "max_dec_frame_buffering");
 4152     TEST_SB_END();
 4153 
 4154     FILLING_BEGIN();
 4155         vui_parameters_Item_=new seq_parameter_set_struct::vui_parameters_struct(
 4156                                                                                     NAL,
 4157                                                                                     VCL,
 4158                                                                                     num_units_in_tick,
 4159                                                                                     time_scale,
 4160                                                                                     sar_width,
 4161                                                                                     sar_height,
 4162                                                                                     aspect_ratio_idc,
 4163                                                                                     video_format,
 4164                                                                                     video_full_range_flag,
 4165                                                                                     colour_primaries,
 4166                                                                                     transfer_characteristics,
 4167                                                                                     matrix_coefficients,
 4168                                                                                     aspect_ratio_info_present_flag,
 4169                                                                                     video_signal_type_present_flag,
 4170                                                                                     colour_description_present_flag,
 4171                                                                                     timing_info_present_flag,
 4172                                                                                     fixed_frame_rate_flag,
 4173                                                                                     pic_struct_present_flag
 4174                                                                                 );
 4175     FILLING_ELSE();
 4176         delete NAL; NAL=NULL;
 4177         delete VCL; VCL=NULL;
 4178     FILLING_END();
 4179 }
 4180 
 4181 //---------------------------------------------------------------------------
 4182 void File_Avc::hrd_parameters(seq_parameter_set_struct::vui_parameters_struct::xxl* &hrd_parameters_Item_)
 4183 {
 4184     //Parsing
 4185     int32u cpb_cnt_minus1;
 4186     int8u  bit_rate_scale, cpb_size_scale, initial_cpb_removal_delay_length_minus1, cpb_removal_delay_length_minus1, dpb_output_delay_length_minus1, time_offset_length;
 4187     Get_UE (   cpb_cnt_minus1,                                  "cpb_cnt_minus1");
 4188     Get_S1 (4, bit_rate_scale,                                  "bit_rate_scale");
 4189     Get_S1 (4, cpb_size_scale,                                  "cpb_size_scale");
 4190     if (cpb_cnt_minus1>31)
 4191     {
 4192         Trusted_IsNot("cpb_cnt_minus1 too high");
 4193         cpb_cnt_minus1=0;
 4194     }
 4195     vector<seq_parameter_set_struct::vui_parameters_struct::xxl::xxl_data>  SchedSel;
 4196     SchedSel.reserve(cpb_cnt_minus1+1);
 4197     for (int8u SchedSelIdx = 0; SchedSelIdx <= cpb_cnt_minus1; ++SchedSelIdx)
 4198     {
 4199         Element_Begin1("ShedSel");
 4200         int64u bit_rate_value, cpb_size_value;
 4201         int32u bit_rate_value_minus1, cpb_size_value_minus1;
 4202         bool cbr_flag;
 4203         Get_UE (bit_rate_value_minus1,                          "bit_rate_value_minus1");
 4204         bit_rate_value=(int64u)((bit_rate_value_minus1+1)*pow(2.0, 6+bit_rate_scale)); Param_Info2(bit_rate_value, " bps");
 4205         Get_UE (cpb_size_value_minus1,                          "cpb_size_value_minus1");
 4206         cpb_size_value=(int64u)((cpb_size_value_minus1+1)*pow(2.0, 4+cpb_size_scale)); Param_Info2(cpb_size_value, " bits");
 4207         Get_SB (cbr_flag,                                       "cbr_flag");
 4208         Element_End0();
 4209 
 4210         FILLING_BEGIN();
 4211             SchedSel.push_back(seq_parameter_set_struct::vui_parameters_struct::xxl::xxl_data(
 4212                                                                                                 bit_rate_value,
 4213                                                                                                 cpb_size_value,
 4214                                                                                                 cbr_flag
 4215                                                                                              ));
 4216         FILLING_END();
 4217     }
 4218     Get_S1 (5, initial_cpb_removal_delay_length_minus1,         "initial_cpb_removal_delay_length_minus1");
 4219     Get_S1 (5, cpb_removal_delay_length_minus1,                 "cpb_removal_delay_length_minus1");
 4220     Get_S1 (5, dpb_output_delay_length_minus1,                  "dpb_output_delay_length_minus1");
 4221     Get_S1 (5, time_offset_length,                              "time_offset_length");
 4222 
 4223     //Validity test
 4224     if (!Element_IsOK() || (SchedSel.size() == 1 && SchedSel[0].bit_rate_value == 64))
 4225     {
 4226         return; //We do not trust this kind of data
 4227     }
 4228 
 4229     //Filling
 4230     hrd_parameters_Item_=new seq_parameter_set_struct::vui_parameters_struct::xxl(
 4231                                                                                     SchedSel,
 4232                                                                                     initial_cpb_removal_delay_length_minus1,
 4233                                                                                     cpb_removal_delay_length_minus1,
 4234                                                                                     dpb_output_delay_length_minus1,
 4235                                                                                     time_offset_length
 4236                                                                                  );
 4237 }
 4238 
 4239 //---------------------------------------------------------------------------
 4240 void File_Avc::nal_unit_header_svc_extension()
 4241 {
 4242     //Parsing
 4243     Element_Begin1("nal_unit_header_svc_extension");
 4244     Skip_SB(                                                    "idr_flag");
 4245     Skip_S1( 6,                                                 "priority_id");
 4246     Skip_SB(                                                    "no_inter_layer_pred_flag");
 4247     Skip_S1( 3,                                                 "dependency_id");
 4248     Skip_S1( 4,                                                 "quality_id");
 4249     Skip_S1( 3,                                                 "temporal_id");
 4250     Skip_SB(                                                    "use_ref_base_pic_flag");
 4251     Skip_SB(                                                    "discardable_flag");
 4252     Skip_SB(                                                    "output_flag");
 4253     Skip_S1( 2,                                                 "reserved_three_2bits");
 4254     Element_End0();
 4255 }
 4256 
 4257 //---------------------------------------------------------------------------
 4258 void File_Avc::nal_unit_header_mvc_extension()
 4259 {
 4260     //Parsing
 4261     Element_Begin1("nal_unit_header_mvc_extension");
 4262     Skip_SB(                                                    "non_idr_flag");
 4263     Skip_S1( 6,                                                 "priority_id");
 4264     Skip_S1(10,                                                 "view_id");
 4265     Skip_S1( 3,                                                 "temporal_id");
 4266     Skip_SB(                                                    "anchor_pic_flag");
 4267     Skip_SB(                                                    "inter_view_flag");
 4268     Skip_SB(                                                    "reserved_one_bit");
 4269     Element_End0();
 4270 }
 4271 
 4272 //---------------------------------------------------------------------------
 4273 void File_Avc::seq_parameter_set_svc_extension()
 4274 {
 4275     //Parsing
 4276     Element_Begin1("seq_parameter_set_svc_extension");
 4277     //Skip_SB(                                                    "");
 4278     Element_End0();
 4279 }
 4280 
 4281 //---------------------------------------------------------------------------
 4282 void File_Avc::svc_vui_parameters_extension()
 4283 {
 4284     //Parsing
 4285     Element_Begin1("svc_vui_parameters_extension");
 4286     //Skip_SB(                                                    "");
 4287     Element_End0();
 4288 }
 4289 
 4290 //---------------------------------------------------------------------------
 4291 void File_Avc::seq_parameter_set_mvc_extension(seq_parameter_set_struct* Data_Item)
 4292 {
 4293     //Parsing
 4294     Element_Begin1("seq_parameter_set_mvc_extension");
 4295     int32u num_views_minus1;
 4296     Get_UE (num_views_minus1,                                   "num_views_minus1");
 4297     //(Not implemented)
 4298     Element_End0();
 4299 
 4300     FILLING_BEGIN();
 4301         Data_Item->num_views_minus1 = (int16u)num_views_minus1;
 4302     FILLING_END();
 4303 }
 4304 
 4305 //---------------------------------------------------------------------------
 4306 void File_Avc::mvc_vui_parameters_extension()
 4307 {
 4308     //Parsing
 4309     Element_Begin1("mvc_vui_parameters_extension");
 4310     Skip_SB(                                                    "");
 4311     Element_End0();
 4312 }
 4313 
 4314 //***************************************************************************
 4315 // Specific
 4316 //***************************************************************************
 4317 
 4318 //---------------------------------------------------------------------------
 4319 void File_Avc::SPS_PPS()
 4320 {
 4321     //Parsing
 4322     int8u Profile, Level, seq_parameter_set_count, pic_parameter_set_count;
 4323     if (SizedBlocks)
 4324         Skip_B1(                                                "Version");
 4325     Get_B1 (Profile,                                            "Profile");
 4326     Skip_B1(                                                    "Compatible profile");
 4327     Get_B1 (Level,                                              "Level");
 4328     BS_Begin();
 4329     Skip_S1(6,                                                  "Reserved");
 4330     Get_S1 (2, SizeOfNALU_Minus1,                               "Size of NALU length minus 1");
 4331     Skip_S1(3,                                                  "Reserved");
 4332     Get_S1 (5, seq_parameter_set_count,                         "seq_parameter_set count");
 4333     BS_End();
 4334     for (int8u Pos=0; Pos<seq_parameter_set_count; Pos++)
 4335     {
 4336         Element_Begin1("seq_parameter_set");
 4337         int16u Size;
 4338         Get_B2 (Size,                                           "Size");
 4339         BS_Begin();
 4340         Mark_0 ();
 4341         Skip_S1( 2,                                             "nal_ref_idc");
 4342         Skip_S1( 5,                                             "nal_unit_type");
 4343         BS_End();
 4344         if (Element_Offset+Size-1>Element_Size)
 4345         {
 4346             Trusted_IsNot("Size is wrong");
 4347             break; //There is an error
 4348         }
 4349         int64u Element_Offset_Save=Element_Offset;
 4350         int64u Element_Size_Save=Element_Size;
 4351         Buffer_Offset+=(size_t)Element_Offset_Save;
 4352         Element_Offset=0;
 4353         Element_Size=Size-(Size?1:0);
 4354         Element_Code=0x07; //seq_parameter_set
 4355         Data_Parse();
 4356         Buffer_Offset-=(size_t)Element_Offset_Save;
 4357         Element_Offset=Element_Offset_Save+Size-1;
 4358         Element_Size=Element_Size_Save;
 4359         Element_End0();
 4360     }
 4361     Get_B1 (pic_parameter_set_count,                            "pic_parameter_set count");
 4362     for (int8u Pos=0; Pos<pic_parameter_set_count; Pos++)
 4363     {
 4364         Element_Begin1("pic_parameter_set");
 4365         int16u Size;
 4366         Get_B2 (Size,                                           "Size");
 4367         BS_Begin();
 4368         Mark_0 ();
 4369         Skip_S1( 2,                                             "nal_ref_idc");
 4370         Skip_S1( 5,                                             "nal_unit_type");
 4371         BS_End();
 4372         int64u Element_Offset_Save=Element_Offset;
 4373         int64u Element_Size_Save=Element_Size;
 4374         Buffer_Offset+=(size_t)Element_Offset_Save;
 4375         Element_Offset=0;
 4376         Element_Size=Size-1;
 4377         if (Element_Size>Element_Size_Save-Element_Offset_Save)
 4378             break; //There is an error
 4379         Element_Code=0x08; //pic_parameter_set
 4380         Data_Parse();
 4381         Buffer_Offset-=(size_t)Element_Offset_Save;
 4382         Element_Offset=Element_Offset_Save+Size-1;
 4383         Element_Size=Element_Size_Save;
 4384         Element_End0();
 4385     }
 4386     if (Element_Offset<Element_Size)
 4387         Skip_XX(Element_Size-Element_Offset,                    "Padding?");
 4388 
 4389     //Filling
 4390     FILLING_BEGIN_PRECISE();
 4391         //Detection of some bugs in the file
 4392         if (!seq_parameter_sets.empty() && seq_parameter_sets[0] && (Profile!=seq_parameter_sets[0]->profile_idc || Level!=seq_parameter_sets[0]->level_idc))
 4393             MuxingMode=Ztring("Container profile=")+Ztring().From_UTF8(Avc_profile_idc(Profile))+__T("@")+Ztring().From_Number(((float)Level)/10, 1);
 4394 
 4395         MustParse_SPS_PPS=false;
 4396         if (!Status[IsAccepted])
 4397             Accept("AVC");
 4398     FILLING_END();
 4399 }
 4400 
 4401 //***************************************************************************
 4402 // Helpers
 4403 //***************************************************************************
 4404 
 4405 //---------------------------------------------------------------------------
 4406 std::string File_Avc::GOP_Detect (std::string PictureTypes)
 4407 {
 4408     //Finding a string without blanks
 4409     size_t PictureTypes_Limit=PictureTypes.find(' ');
 4410     if (PictureTypes_Limit!=string::npos)
 4411     {
 4412         if (PictureTypes_Limit>PictureTypes.size()/2)
 4413             PictureTypes.resize(PictureTypes_Limit);
 4414         else
 4415         {
 4416             //Trim
 4417             size_t TrimPos;
 4418             TrimPos=PictureTypes.find_first_not_of(' ');
 4419             if (TrimPos!=string::npos)
 4420                 PictureTypes.erase(0, TrimPos);
 4421             TrimPos=PictureTypes.find_last_not_of(' ');
 4422             if (TrimPos!=string::npos)
 4423                 PictureTypes.erase(TrimPos+1);
 4424 
 4425             //Finding the longest string
 4426             ZtringList List; List.Separator_Set(0, __T(" "));
 4427             List.Write(Ztring().From_UTF8(PictureTypes));
 4428             size_t MaxLength=0;
 4429             size_t MaxLength_Pos=0;
 4430             for (size_t Pos=0; Pos<List.size(); Pos++)
 4431                 if (List[Pos].size()>MaxLength)
 4432                 {
 4433                     MaxLength=List[Pos].size();
 4434                     MaxLength_Pos=Pos;
 4435                 }
 4436             PictureTypes=List[MaxLength_Pos].To_UTF8();
 4437 
 4438         }
 4439     }
 4440 
 4441     //Creating all GOP values
 4442     std::vector<Ztring>