LCOV - code coverage report
Current view: top level - bifs - com_dec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 494 637 77.6 %
Date: 2021-04-29 23:48:07 Functions: 22 22 100.0 %

          Line data    Source code
       1             : /*
       2             :  *                      GPAC - Multimedia Framework C SDK
       3             :  *
       4             :  *                      Authors: Jean Le Feuvre
       5             :  *                      Copyright (c) Telecom ParisTech 2000-2012
       6             :  *                                      All rights reserved
       7             :  *
       8             :  *  This file is part of GPAC / BIFS codec sub-project
       9             :  *
      10             :  *  GPAC is free software; you can redistribute it and/or modify
      11             :  *  it under the terms of the GNU Lesser General Public License as published by
      12             :  *  the Free Software Foundation; either version 2, or (at your option)
      13             :  *  any later version.
      14             :  *
      15             :  *  GPAC is distributed in the hope that it will be useful,
      16             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18             :  *  GNU Lesser General Public License for more details.
      19             :  *
      20             :  *  You should have received a copy of the GNU Lesser General Public
      21             :  *  License along with this library; see the file COPYING.  If not, write to
      22             :  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
      23             :  *
      24             :  */
      25             : 
      26             : #include <gpac/maths.h>
      27             : #include <gpac/internal/bifs_dev.h>
      28             : #include "quant.h"
      29             : 
      30             : #ifndef GPAC_DISABLE_BIFS
      31             : 
      32             : 
      33             : GF_Err BD_DecMFFieldList(GF_BifsDecoder * codec, GF_BitStream *bs, GF_Node *node, GF_FieldInfo *field);
      34             : GF_Err BD_DecMFFieldVec(GF_BifsDecoder * codec, GF_BitStream *bs, GF_Node *node, GF_FieldInfo *field);
      35             : 
      36             : 
      37             : 
      38         240 : void gf_bifs_dec_name(GF_BitStream *bs, char *name)
      39             : {
      40             :         u32 i = 0;
      41             :         while (1) {
      42        2520 :                 name[i] = gf_bs_read_int(bs, 8);
      43        1380 :                 if (!name[i]) break;
      44        1140 :                 i++;
      45             :         }
      46         240 : }
      47             : 
      48             : 
      49           6 : static GF_Err BD_XReplace(GF_BifsDecoder * codec, GF_BitStream *bs)
      50             : {
      51             :         GF_FieldInfo targetField, fromField;
      52             :         GF_Node *target, *n, *fromNode;
      53             :         s32 pos = -2;
      54             :         void *slot_ptr;
      55             :         u32 id, nbBits, ind, aind;
      56             :         GF_Err e;
      57             : 
      58             : 
      59           6 :         id = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
      60           6 :         target = gf_sg_find_node(codec->current_graph, id);
      61           6 :         if (!target) return GF_SG_UNKNOWN_NODE;
      62             : 
      63           6 :         nbBits = gf_get_bit_size(gf_node_get_num_fields_in_mode(target, GF_SG_FIELD_CODING_IN)-1);
      64           6 :         ind = gf_bs_read_int(bs, nbBits);
      65           6 :         e = gf_bifs_get_field_index(target, ind, GF_SG_FIELD_CODING_IN, &aind);
      66           6 :         if (e) return e;
      67           6 :         e = gf_node_get_field(target, aind, &targetField);
      68           6 :         if (e) return e;
      69             : 
      70           6 :         if (!gf_sg_vrml_is_sf_field(targetField.fieldType)) {
      71             :                 /*this is indexed replacement*/
      72           3 :                 if (gf_bs_read_int(bs, 1)) {
      73             :                         /*index is dynamic*/
      74           3 :                         if (gf_bs_read_int(bs, 1)) {
      75           3 :                                 id = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
      76           3 :                                 n = gf_sg_find_node(codec->current_graph, id);
      77           3 :                                 if (!n) return GF_SG_UNKNOWN_NODE;
      78             : 
      79           3 :                                 nbBits = gf_get_bit_size(gf_node_get_num_fields_in_mode(n, GF_SG_FIELD_CODING_DEF)-1);
      80           3 :                                 ind = gf_bs_read_int(bs, nbBits);
      81           3 :                                 e = gf_bifs_get_field_index(n, ind, GF_SG_FIELD_CODING_DEF, &aind);
      82           3 :                                 if (e) return e;
      83           3 :                                 e = gf_node_get_field(n, aind, &fromField);
      84           3 :                                 if (e) return e;
      85             : 
      86             :                                 pos = 0;
      87           3 :                                 switch (fromField.fieldType) {
      88           0 :                                 case GF_SG_VRML_SFBOOL:
      89           0 :                                         if (*(SFBool*)fromField.far_ptr) pos = 1;
      90             :                                         break;
      91           3 :                                 case GF_SG_VRML_SFINT32:
      92           3 :                                         if (*(SFInt32*)fromField.far_ptr >=0) pos = *(SFInt32*)fromField.far_ptr;
      93             :                                         break;
      94           0 :                                 case GF_SG_VRML_SFFLOAT:
      95           0 :                                         if ( (*(SFFloat *)fromField.far_ptr) >=0) pos = (s32) floor( FIX2FLT(*(SFFloat*)fromField.far_ptr) );
      96             :                                         break;
      97           0 :                                 case GF_SG_VRML_SFTIME:
      98           0 :                                         if ( (*(SFTime *)fromField.far_ptr) >=0) pos = (s32) floor( (*(SFTime *)fromField.far_ptr) );
      99             :                                         break;
     100             :                                 }
     101             :                         } else {
     102           0 :                                 u32 type = gf_bs_read_int(bs, 2);
     103           0 :                                 switch (type) {
     104           0 :                                 case 0:
     105           0 :                                         pos = gf_bs_read_int(bs, 16);
     106           0 :                                         break;
     107           0 :                                 case 2:
     108             :                                         pos = 0;
     109           0 :                                         break;
     110           0 :                                 case 3:
     111             :                                         pos = -1;
     112           0 :                                         break;
     113             :                                 }
     114             :                         }
     115             :                 }
     116             : 
     117           3 :                 if (targetField.fieldType==GF_SG_VRML_MFNODE) {
     118           3 :                         if (gf_bs_read_int(bs, 1)) {
     119           3 :                                 target = gf_node_list_get_child(*(GF_ChildNodeItem **)targetField.far_ptr, pos);
     120           3 :                                 if (!target) return GF_SG_UNKNOWN_NODE;
     121             : 
     122           3 :                                 nbBits = gf_get_bit_size(gf_node_get_num_fields_in_mode(target, GF_SG_FIELD_CODING_IN)-1);
     123           3 :                                 ind = gf_bs_read_int(bs, nbBits);
     124           3 :                                 e = gf_bifs_get_field_index(target, ind, GF_SG_FIELD_CODING_IN, &aind);
     125           3 :                                 if (e) return e;
     126           3 :                                 e = gf_node_get_field(target, aind, &targetField);
     127           3 :                                 if (e) return e;
     128             :                                 pos = -2;
     129             :                         }
     130             :                 }
     131             :         }
     132             : 
     133             :         fromNode = NULL;
     134           6 :         if (gf_bs_read_int(bs, 1)) {
     135           3 :                 id = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     136           3 :                 fromNode = gf_sg_find_node(codec->current_graph, id);
     137           3 :                 if (!fromNode) return GF_SG_UNKNOWN_NODE;
     138             : 
     139           3 :                 nbBits = gf_get_bit_size(gf_node_get_num_fields_in_mode(fromNode, GF_SG_FIELD_CODING_DEF)-1);
     140           3 :                 ind = gf_bs_read_int(bs, nbBits);
     141           3 :                 e = gf_bifs_get_field_index(fromNode, ind, GF_SG_FIELD_CODING_DEF, &aind);
     142           3 :                 if (e) return e;
     143           3 :                 e = gf_node_get_field(fromNode, aind, &fromField);
     144           3 :                 if (e) return e;
     145             :         }
     146             : 
     147             :         /*indexed replacement*/
     148           6 :         if (pos>=-1) {
     149             :                 /*if MFNode remove the child and set new node*/
     150           0 :                 if (targetField.fieldType == GF_SG_VRML_MFNODE) {
     151             :                         GF_Node *newnode;
     152           0 :                         if (fromNode) {
     153           0 :                                 newnode = *(GF_Node**)fromField.far_ptr;
     154             :                         } else {
     155           0 :                                 newnode = gf_bifs_dec_node(codec, bs, targetField.NDTtype);
     156             :                         }
     157           0 :                         gf_node_replace_child(target, (GF_ChildNodeItem**) targetField.far_ptr, pos, newnode);
     158           0 :                         if (newnode) gf_node_register(newnode, NULL);
     159             :                 }
     160             :                 /*erase the field item*/
     161             :                 else {
     162             :                         u32 sftype;
     163           0 :                         if ((pos < 0) || ((u32) pos >= ((GenMFField *) targetField.far_ptr)->count) ) {
     164           0 :                                 pos = ((GenMFField *)targetField.far_ptr)->count - 1;
     165             :                                 /*may happen with text and default value*/
     166           0 :                                 if (pos < 0) {
     167             :                                         pos = 0;
     168           0 :                                         gf_sg_vrml_mf_alloc(targetField.far_ptr, targetField.fieldType, 1);
     169             :                                 }
     170             :                         }
     171           0 :                         e = gf_sg_vrml_mf_get_item(targetField.far_ptr, targetField.fieldType, & slot_ptr, pos);
     172           0 :                         if (e) return e;
     173           0 :                         sftype = gf_sg_vrml_get_sf_type(targetField.fieldType);
     174             : 
     175           0 :                         if (fromNode) {
     176           0 :                                 if (sftype==fromField.fieldType)
     177           0 :                                         gf_sg_vrml_field_copy(slot_ptr, fromField.far_ptr, sftype);
     178             :                         } else {
     179             :                                 GF_FieldInfo sffield;
     180             :                                 memcpy(&sffield, &targetField, sizeof(GF_FieldInfo));
     181           0 :                                 sffield.fieldType = sftype;
     182           0 :                                 sffield.far_ptr = slot_ptr;
     183           0 :                                 gf_bifs_dec_sf_field(codec, bs, target, &sffield, GF_FALSE);
     184             :                         }
     185             :                 }
     186           0 :                 gf_bifs_check_field_change(target, &targetField);
     187           0 :                 return GF_OK;
     188             :         }
     189           6 :         switch (targetField.fieldType) {
     190           0 :         case GF_SG_VRML_SFNODE:
     191             :         {
     192             :                 GF_Node *newnode;
     193           0 :                 if (fromNode) {
     194           0 :                         newnode = *(GF_Node**)fromField.far_ptr;
     195             :                 } else {
     196           0 :                         newnode = gf_bifs_dec_node(codec, bs, targetField.NDTtype);
     197             :                 }
     198             : 
     199           0 :                 n = *((GF_Node **) targetField.far_ptr);
     200             : 
     201           0 :                 *((GF_Node **) targetField.far_ptr) = newnode;
     202           0 :                 if (newnode) gf_node_register(newnode, target);
     203             : 
     204           0 :                 if (n) gf_node_unregister(n, target);
     205             : 
     206             :                 break;
     207             :         }
     208           0 :         case GF_SG_VRML_MFNODE:
     209             :         {
     210           0 :                 GF_ChildNodeItem *previous = * (GF_ChildNodeItem **) targetField.far_ptr;
     211           0 :                 * ((GF_ChildNodeItem **) targetField.far_ptr) = NULL;
     212             : 
     213           0 :                 if (fromNode) {
     214             :                         GF_ChildNodeItem *list, *prev, *cur;
     215           0 :                         list = * ((GF_ChildNodeItem **) targetField.far_ptr);
     216             :                         prev=NULL;
     217           0 :                         while (list) {
     218           0 :                                 cur = (GF_ChildNodeItem*)gf_malloc(sizeof(GF_ChildNodeItem));
     219           0 :                                 cur->next = NULL;
     220           0 :                                 cur->node = list->node;
     221           0 :                                 if (prev) {
     222           0 :                                         prev->next = cur;
     223             :                                 } else {
     224           0 :                                         * ((GF_ChildNodeItem **) targetField.far_ptr) = cur;
     225             :                                 }
     226           0 :                                 gf_node_register(list->node, target);
     227             :                                 prev = cur;
     228           0 :                                 list = list->next;
     229             :                         }
     230             :                 } else {
     231           0 :                         e = gf_bifs_dec_field(codec, bs, target, &targetField, GF_FALSE);
     232           0 :                         if (e) return e;
     233             :                 }
     234           0 :                 if (previous)
     235           0 :                         gf_node_unregister_children(target, previous);
     236             : 
     237             :                 break;
     238             :         }
     239           6 :         default:
     240           6 :                 if (!gf_sg_vrml_is_sf_field(targetField.fieldType)) {
     241           0 :                         e = gf_sg_vrml_mf_reset(targetField.far_ptr, targetField.fieldType);
     242             :                 }
     243           6 :                 if (e) return e;
     244             : 
     245           6 :                 if (fromNode) {
     246           3 :                         if (fromField.fieldType == targetField.fieldType)
     247           3 :                                 gf_sg_vrml_field_clone(targetField.far_ptr, fromField.far_ptr, targetField.fieldType, codec->current_graph);
     248             :                 } else {
     249           3 :                         e = gf_bifs_dec_field(codec, bs, target, &targetField, GF_FALSE);
     250             :                 }
     251             :                 break;
     252             :         }
     253           6 :         gf_bifs_check_field_change(target, &targetField);
     254           6 :         return e;
     255             : }
     256             : 
     257           3 : static GF_Err BD_DecProtoDelete(GF_BifsDecoder * codec, GF_BitStream *bs)
     258             : {
     259             :         u32 ID, flag, count;
     260             :         GF_Proto *proto;
     261             : 
     262           3 :         flag = gf_bs_read_int(bs, 1);
     263           3 :         if (flag) {
     264           3 :                 flag = gf_bs_read_int(bs, 1);
     265           6 :                 while (flag) {
     266           3 :                         ID = gf_bs_read_int(bs, codec->info->config.ProtoIDBits);
     267           3 :                         proto = gf_sg_find_proto(codec->current_graph, ID, NULL);
     268           3 :                         if (proto) gf_sg_proto_del(proto);
     269           3 :                         flag = gf_bs_read_int(bs, 1);
     270             :                 }
     271             :         } else {
     272           0 :                 flag = gf_bs_read_int(bs, 5);
     273           0 :                 count = gf_bs_read_int(bs, flag);
     274           0 :                 while (count) {
     275           0 :                         ID = gf_bs_read_int(bs, codec->info->config.ProtoIDBits);
     276           0 :                         proto = gf_sg_find_proto(codec->current_graph, ID, NULL);
     277           0 :                         if (proto) gf_sg_proto_del(proto);
     278           0 :                         count--;
     279             :                 }
     280             :         }
     281           3 :         return GF_OK;
     282             : }
     283             : 
     284             : 
     285           3 : static GF_Err BD_DecMultipleIndexReplace(GF_BifsDecoder * codec, GF_BitStream *bs)
     286             : {
     287             :         u32 ID, ind, field_ind, NumBits, lenpos, lennum, count, pos;
     288             :         GF_Node *node;
     289             :         GF_Err e;
     290             :         GF_FieldInfo field, sffield;
     291             : 
     292           3 :         ID = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     293           3 :         node = gf_sg_find_node(codec->current_graph, ID);
     294           3 :         if (!node) return GF_NON_COMPLIANT_BITSTREAM;
     295           3 :         NumBits = gf_get_bit_size(gf_node_get_num_fields_in_mode(node, GF_SG_FIELD_CODING_IN)-1);
     296           3 :         ind = gf_bs_read_int(bs, NumBits);
     297           3 :         e = gf_bifs_get_field_index(node, ind, GF_SG_FIELD_CODING_IN, &field_ind);
     298           3 :         if (e) return e;
     299           3 :         e = gf_node_get_field(node, field_ind, &field);
     300           3 :         if (gf_sg_vrml_is_sf_field(field.fieldType)) return GF_NON_COMPLIANT_BITSTREAM;
     301             : 
     302           3 :         lenpos = gf_bs_read_int(bs, 5);
     303           3 :         lennum = gf_bs_read_int(bs, 5);
     304           3 :         count = gf_bs_read_int(bs, lennum);
     305             : 
     306             : 
     307             :         /*cf index value replace */
     308           3 :         if (field.fieldType == GF_SG_VRML_MFNODE) {
     309           0 :                 while (count) {
     310             :                         GF_Node *new_node;
     311           0 :                         pos = gf_bs_read_int(bs, lenpos);
     312             :                         /*first decode*/
     313           0 :                         new_node = gf_bifs_dec_node(codec, bs, field.NDTtype);
     314           0 :                         if (!new_node) return codec->LastError;
     315           0 :                         e = gf_node_register(new_node, node);
     316           0 :                         if (e) return e;
     317             :                         /*then replace*/
     318           0 :                         e = gf_node_replace_child(node, (GF_ChildNodeItem**) field.far_ptr, pos, new_node);
     319           0 :                         count--;
     320             :                 }
     321           0 :                 if (!e) gf_bifs_check_field_change(node, &field);
     322             :                 return e;
     323             :         }
     324             :         /*Not a node list*/
     325             :         memcpy(&sffield, &field, sizeof(GF_FieldInfo));
     326           3 :         sffield.fieldType = gf_sg_vrml_get_sf_type(field.fieldType);
     327             : 
     328          15 :         while (count) {
     329           9 :                 pos = gf_bs_read_int(bs, lenpos);
     330             : 
     331           9 :                 if (pos && pos >= ((GenMFField *)field.far_ptr)->count) {
     332           3 :                         pos = ((GenMFField *)field.far_ptr)->count - 1;
     333             :                 }
     334             : 
     335           9 :                 e = gf_sg_vrml_mf_get_item(field.far_ptr, field.fieldType, & sffield.far_ptr, pos);
     336           9 :                 if (e) return e;
     337           9 :                 e = gf_bifs_dec_sf_field(codec, bs, node, &sffield, GF_FALSE);
     338           9 :                 if (e) break;
     339           9 :                 count--;
     340             :         }
     341           3 :         if (!e) gf_bifs_check_field_change(node, &field);
     342             :         return e;
     343             : }
     344             : 
     345           4 : static GF_Err BD_DecMultipleReplace(GF_BifsDecoder * codec, GF_BitStream *bs)
     346             : {
     347             :         GF_Err e;
     348             :         u32 NodeID, flag;
     349             :         GF_Node *node;
     350             : 
     351           4 :         NodeID = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     352           4 :         node = gf_sg_find_node(codec->current_graph, NodeID);
     353           4 :         if (!node) return GF_NON_COMPLIANT_BITSTREAM;
     354             : 
     355           4 :         flag = gf_bs_read_int(bs, 1);
     356           4 :         if (flag) {
     357           4 :                 e = gf_bifs_dec_node_mask(codec, bs, node, GF_FALSE);
     358             :         } else {
     359           0 :                 e = gf_bifs_dec_node_list(codec, bs, node, GF_FALSE);
     360             :         }
     361             :         return e;
     362             : }
     363             : 
     364             : 
     365           3 : static GF_Err BD_DecGlobalQuantizer(GF_BifsDecoder * codec, GF_BitStream *bs)
     366             : {
     367             :         GF_Node *node;
     368           3 :         node = gf_bifs_dec_node(codec, bs, NDT_SFWorldNode);
     369             : 
     370             :         /*reset global QP*/
     371           3 :         if (codec->scenegraph->global_qp) {
     372           0 :                 gf_node_unregister(codec->scenegraph->global_qp, NULL);
     373           0 :                 codec->scenegraph->global_qp = NULL;
     374             :         }
     375           3 :         codec->ActiveQP = NULL;
     376             : 
     377           3 :         if (!node || (gf_node_get_tag(node) != TAG_MPEG4_QuantizationParameter)) {
     378           0 :                 if (node) gf_node_unregister(node, NULL);
     379           0 :                 return codec->LastError;
     380             :         }
     381             : 
     382             :         /*register global QP*/
     383           3 :         codec->scenegraph->global_qp = node;
     384           3 :         gf_node_register(node, NULL);
     385           3 :         codec->ActiveQP = (M_QuantizationParameter *) node;
     386           3 :         codec->ActiveQP->isLocal = 0;
     387           3 :         return GF_OK;
     388             : }
     389             : 
     390           3 : static GF_Err BD_DecNodeDeleteEx(GF_BifsDecoder * codec, GF_BitStream *bs)
     391             : {
     392             :         u32 NodeID;
     393             :         GF_Node *node;
     394           3 :         NodeID = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     395           3 :         node = gf_sg_find_node(codec->current_graph, NodeID);
     396           3 :         if (!node) return GF_NON_COMPLIANT_BITSTREAM;
     397           3 :         return gf_node_replace(node, NULL, GF_TRUE);
     398             : }
     399             : 
     400             : #ifdef GPAC_UNUSED_FUNC
     401             : static GF_Err BD_DecOperandReplace(GF_BifsDecoder * codec, GF_BitStream *bs)
     402             : {
     403             :         s32 pos;
     404             :         GF_FieldInfo field;
     405             :         GF_FieldInfo src_field;
     406             :         GF_Err e;
     407             :         u32 NodeID, NumBits, ind, field_ind, type, src_type, dst_type;
     408             :         GF_Node *node, *src;
     409             :         void *src_ptr, *dst_ptr;
     410             : 
     411             :         NodeID = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     412             :         node = gf_sg_find_node(codec->current_graph, NodeID);
     413             :         if (!node) return GF_NON_COMPLIANT_BITSTREAM;
     414             :         NumBits = gf_get_bit_size(gf_node_get_num_fields_in_mode(node, GF_SG_FIELD_CODING_IN)-1);
     415             :         ind = gf_bs_read_int(bs, NumBits);
     416             :         e = gf_bifs_get_field_index(node, ind, GF_SG_FIELD_CODING_IN, &field_ind);
     417             :         if (e) return e;
     418             :         e = gf_node_get_field(node, field_ind, &field);
     419             :         if (e) return e;
     420             : 
     421             :         dst_type = field.fieldType;
     422             :         dst_ptr = field.far_ptr;
     423             :         pos = -2;
     424             :         if (! gf_sg_vrml_is_sf_field(field.fieldType)) {
     425             :                 type = gf_bs_read_int(bs, 2);
     426             :                 switch (type) {
     427             :                 /*no index*/
     428             :                 case 0:
     429             :                         break;
     430             :                 /*specified*/
     431             :                 case 1:
     432             :                         pos = gf_bs_read_int(bs, 8);
     433             :                         break;
     434             :                 /*first*/
     435             :                 case 2:
     436             :                         pos = 0;
     437             :                         break;
     438             :                 /*last*/
     439             :                 case 3:
     440             :                         /*-1 means append*/
     441             :                         pos = ((GenMFField *)field.far_ptr)->count;
     442             :                         if (!pos)
     443             :                                 return GF_NON_COMPLIANT_BITSTREAM;
     444             :                         pos -= 1;
     445             :                         break;
     446             :                 default:
     447             :                         return GF_NON_COMPLIANT_BITSTREAM;
     448             :                 }
     449             :                 if (pos>-2) {
     450             :                         dst_type = gf_sg_vrml_get_sf_type(field.fieldType);
     451             :                         e = gf_sg_vrml_mf_get_item(field.far_ptr, field.fieldType, &dst_ptr, pos);
     452             :                         if (e) return e;
     453             :                 }
     454             :         }
     455             : 
     456             :         NodeID = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     457             :         src = gf_sg_find_node(codec->current_graph, NodeID);
     458             :         if (!src) return GF_NON_COMPLIANT_BITSTREAM;
     459             :         NumBits = gf_get_bit_size(gf_node_get_num_fields_in_mode(src, GF_SG_FIELD_CODING_IN)-1);
     460             :         ind = gf_bs_read_int(bs, NumBits);
     461             :         e = gf_bifs_get_field_index(src, ind, GF_SG_FIELD_CODING_IN, &field_ind);
     462             :         if (e) return e;
     463             :         e = gf_node_get_field(src, field_ind, &src_field);
     464             :         if (e) return e;
     465             : 
     466             :         src_type = src_field.fieldType;
     467             :         src_ptr = src_field.far_ptr;
     468             :         pos = -2;
     469             :         if (! gf_sg_vrml_is_sf_field(src_field.fieldType)) {
     470             :                 type = gf_bs_read_int(bs, 2);
     471             :                 switch (type) {
     472             :                 /*no index*/
     473             :                 case 0:
     474             :                         break;
     475             :                 /*specified*/
     476             :                 case 1:
     477             :                         pos = gf_bs_read_int(bs, 8);
     478             :                         break;
     479             :                 /*first*/
     480             :                 case 2:
     481             :                         pos = 0;
     482             :                         break;
     483             :                 /*last*/
     484             :                 case 3:
     485             :                         /*-1 means append*/
     486             :                         pos = ((GenMFField *)src_field.far_ptr)->count;
     487             :                         if (!pos)
     488             :                                 return GF_NON_COMPLIANT_BITSTREAM;
     489             :                         pos -= 1;
     490             :                         break;
     491             :                 default:
     492             :                         return GF_NON_COMPLIANT_BITSTREAM;
     493             :                 }
     494             :                 if (pos>-2) {
     495             :                         src_type = gf_sg_vrml_get_sf_type(src_field.fieldType);
     496             :                         e = gf_sg_vrml_mf_get_item(src_field.far_ptr, src_field.fieldType, &src_ptr, pos);
     497             :                         if (e) return e;
     498             :                 }
     499             :         }
     500             :         if (src_type!=dst_type) return GF_NON_COMPLIANT_BITSTREAM;
     501             :         gf_sg_vrml_field_copy(dst_ptr, src_ptr, src_type);
     502             :         return GF_OK;
     503             : }
     504             : #endif /*GPAC_UNUSED_FUNC*/
     505             : 
     506          28 : static GF_Err BD_DecExtendedUpdate(GF_BifsDecoder * codec, GF_BitStream *bs)
     507             : {
     508             :         u32 type;
     509             : 
     510          28 :         type = gf_bs_read_int(bs, 8);
     511          28 :         switch (type) {
     512           3 :         case 0:
     513           3 :                 return gf_bifs_dec_proto_list(codec, bs, NULL);
     514           3 :         case 1:
     515           3 :                 return BD_DecProtoDelete(codec, bs);
     516           3 :         case 2:
     517           3 :                 return gf_sg_delete_all_protos(codec->current_graph);
     518           3 :         case 3:
     519           3 :                 return BD_DecMultipleIndexReplace(codec, bs);
     520           4 :         case 4:
     521           4 :                 return BD_DecMultipleReplace(codec, bs);
     522           3 :         case 5:
     523           3 :                 return BD_DecGlobalQuantizer(codec, bs);
     524           3 :         case 6:
     525           3 :                 return BD_DecNodeDeleteEx(codec, bs);
     526           6 :         case 7:
     527           6 :                 return BD_XReplace(codec, bs);
     528             :         default:
     529             :                 return GF_BIFS_UNKNOWN_VERSION;
     530             :         }
     531             : }
     532             : 
     533             : /*inserts a node in a container (node.children)*/
     534          15 : static GF_Err BD_DecNodeInsert(GF_BifsDecoder * codec, GF_BitStream *bs)
     535             : {
     536             :         u32 NodeID, NDT;
     537             :         s32 type, pos;
     538             :         GF_Node *node, *def;
     539             :         GF_Err e;
     540             : 
     541          15 :         NodeID = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     542          15 :         def = gf_sg_find_node(codec->current_graph, NodeID);
     543          15 :         if (!def) return GF_NON_COMPLIANT_BITSTREAM;
     544             : 
     545          15 :         NDT = gf_bifs_get_child_table(def);
     546          15 :         if (!NDT) return GF_NON_COMPLIANT_BITSTREAM;
     547             : 
     548          15 :         type = gf_bs_read_int(bs, 2);
     549          15 :         switch (type) {
     550           0 :         case 0:
     551           0 :                 pos = gf_bs_read_int(bs, 8);
     552           0 :                 break;
     553             :         case 2:
     554             :                 pos = 0;
     555             :                 break;
     556           9 :         case 3:
     557             :                 /*-1 means append*/
     558             :                 pos = -1;
     559           9 :                 break;
     560             :         default:
     561             :                 return GF_NON_COMPLIANT_BITSTREAM;
     562             :         }
     563             : 
     564          15 :         node = gf_bifs_dec_node(codec, bs, NDT);
     565          15 :         if (!node) return codec->LastError;
     566             : 
     567          15 :         e = gf_node_register(node, def);
     568          15 :         if (e) return e;
     569          15 :         e = gf_node_insert_child(def, node, pos);
     570          15 :         if (!e) {
     571             :                 GF_FieldInfo field;
     572             :                 /*get it by name in case no add/removeChildren*/
     573          15 :                 e = gf_node_get_field_by_name(def, "children", &field);
     574          15 :                 if (e) return e;
     575          15 :                 gf_bifs_check_field_change(def, &field);
     576             :         }
     577             :         return e;
     578             : }
     579             : 
     580             : /*NB This can insert a node as well (but usually not in the .children field)*/
     581           4 : static GF_Err BD_DecIndexInsert(GF_BifsDecoder * codec, GF_BitStream *bs)
     582             : {
     583             :         GF_Err e;
     584             :         u32 NodeID;
     585             :         u32 NumBits, ind, field_ind;
     586             :         u8 type;
     587             :         s32 pos;
     588             :         GF_Node *def;
     589             :         GF_FieldInfo field, sffield;
     590             : 
     591           4 :         NodeID = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     592           4 :         def = gf_sg_find_node(codec->current_graph, NodeID);
     593           4 :         if (!def) return GF_NON_COMPLIANT_BITSTREAM;
     594             :         /*index insertion uses IN mode for field index*/
     595           4 :         NumBits = gf_get_bit_size(gf_node_get_num_fields_in_mode(def, GF_SG_FIELD_CODING_IN)-1);
     596           4 :         ind = gf_bs_read_int(bs, NumBits);
     597             : 
     598           4 :         e = gf_bifs_get_field_index(def, ind, GF_SG_FIELD_CODING_IN, &field_ind);
     599           4 :         if (e) return e;
     600             : 
     601           4 :         type = gf_bs_read_int(bs, 2);
     602           4 :         switch (type) {
     603           1 :         case 0:
     604           1 :                 pos = gf_bs_read_int(bs, 16);
     605           1 :                 break;
     606             :         case 2:
     607             :                 pos = 0;
     608             :                 break;
     609           0 :         case 3:
     610             :                 pos = -1;
     611           0 :                 break;
     612             :         default:
     613             :                 return GF_NON_COMPLIANT_BITSTREAM;
     614             :         }
     615             : 
     616           4 :         e = gf_node_get_field(def, field_ind, &field);
     617           4 :         if (e) return e;
     618           4 :         if (gf_sg_vrml_is_sf_field(field.fieldType)) return GF_NON_COMPLIANT_BITSTREAM;
     619             : 
     620             :         memcpy(&sffield, &field, sizeof(GF_FieldInfo));
     621           4 :         sffield.fieldType = gf_sg_vrml_get_sf_type(field.fieldType);
     622             : 
     623             :         /*rescale the MFField and parse the SFField*/
     624           4 :         if (field.fieldType==GF_SG_VRML_MFNODE) {
     625           1 :                 GF_Node *node = gf_bifs_dec_node(codec, bs, field.NDTtype);
     626           1 :                 if (!node) return codec->LastError;
     627             : 
     628           0 :                 e = gf_node_register(node, def);
     629           0 :                 if (e) return e;
     630             :                 /*this is generic MFNode container*/
     631           0 :                 if (pos== -1) {
     632           0 :                         e = gf_node_list_add_child( (GF_ChildNodeItem **) field.far_ptr, node);
     633             :                 } else {
     634           0 :                         e = gf_node_list_insert_child((GF_ChildNodeItem **) field.far_ptr, node, pos);
     635             :                 }
     636             : 
     637           0 :                 if (!e) gf_bifs_check_field_change(def, &field);
     638             :         } else {
     639           3 :                 if (pos == -1) {
     640           0 :                         e = gf_sg_vrml_mf_append(field.far_ptr, field.fieldType, & sffield.far_ptr);
     641             :                 } else {
     642             :                         /*insert is 0-based*/
     643           3 :                         e = gf_sg_vrml_mf_insert(field.far_ptr, field.fieldType, & sffield.far_ptr, pos);
     644             :                 }
     645           3 :                 if (e) return e;
     646           3 :                 e = gf_bifs_dec_sf_field(codec, bs, def, &sffield, GF_FALSE);
     647           3 :                 if (!e) gf_bifs_check_field_change(def, &field);
     648             :         }
     649             :         return e;
     650             : }
     651             : 
     652             : 
     653          50 : static GF_Err BD_DecInsert(GF_BifsDecoder * codec, GF_BitStream *bs)
     654             : {
     655             :         u8 type;
     656             : 
     657          50 :         type = gf_bs_read_int(bs, 2);
     658          50 :         switch (type) {
     659          15 :         case 0:
     660          15 :                 return BD_DecNodeInsert(codec, bs);
     661             :         /*Extended BIFS updates*/
     662          28 :         case 1:
     663          28 :                 return BD_DecExtendedUpdate(codec, bs);
     664           4 :         case 2:
     665           4 :                 return BD_DecIndexInsert(codec, bs);
     666           3 :         case 3:
     667           3 :                 return gf_bifs_dec_route(codec, bs, GF_TRUE);
     668             :         default:
     669             :                 return GF_NON_COMPLIANT_BITSTREAM;
     670             :         }
     671             : }
     672             : 
     673             : 
     674           3 : static GF_Err BD_DecIndexDelete(GF_BifsDecoder * codec, GF_BitStream *bs)
     675             : {
     676             :         u32 NodeID, NumBits, SF_type, ind, field_ind;
     677             :         s32 pos;
     678             :         u8 type;
     679             :         GF_Node *node;
     680             :         GF_Err e;
     681             :         GF_FieldInfo field;
     682             : 
     683           3 :         NodeID = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     684           3 :         node = gf_sg_find_node(codec->current_graph, NodeID);
     685           3 :         if (!node) return GF_NON_COMPLIANT_BITSTREAM;
     686             : 
     687           3 :         NumBits = gf_get_bit_size(gf_node_get_num_fields_in_mode(node, GF_SG_FIELD_CODING_IN) - 1);
     688           3 :         ind = gf_bs_read_int(bs, NumBits);
     689             : 
     690           3 :         type = gf_bs_read_int(bs, 2);
     691           3 :         switch (type) {
     692           0 :         case 0:
     693           0 :                 pos = (u32) gf_bs_read_int(bs, 16);
     694             :                 break;
     695             :         case 2:
     696             :                 pos = 0;
     697             :                 break;
     698           0 :         case 3:
     699             :                 pos = -1;
     700             :                 break;
     701             :         default:
     702             :                 return GF_NON_COMPLIANT_BITSTREAM;
     703             :         }
     704           3 :         e = gf_bifs_get_field_index(node, ind, GF_SG_FIELD_CODING_IN, &field_ind);
     705           3 :         if (e) return e;
     706           3 :         e = gf_node_get_field(node, field_ind, &field);
     707           3 :         if (e) return e;
     708           3 :         if (gf_sg_vrml_is_sf_field(field.fieldType)) return GF_NON_COMPLIANT_BITSTREAM;
     709             : 
     710           3 :         SF_type = gf_sg_vrml_get_sf_type(field.fieldType);
     711             : 
     712             :         /*special handling in case of a node*/
     713           3 :         if (SF_type == GF_SG_VRML_SFNODE) {
     714           0 :                 GF_ChildNodeItem** nlist_ptr = (GF_ChildNodeItem**) field.far_ptr;
     715           0 :                 if (*nlist_ptr) {
     716           0 :                         e = gf_node_replace_child(node, nlist_ptr, pos, NULL);
     717             :                 }
     718             :         } else {
     719           3 :                 e = gf_sg_vrml_mf_remove(field.far_ptr, field.fieldType, pos);
     720             :         }
     721             :         /*deletion -> node has changed*/
     722           3 :         if (!e) gf_bifs_check_field_change(node, &field);
     723             : 
     724             :         return e;
     725             : }
     726             : 
     727           9 : static GF_Err BD_DecDelete(GF_BifsDecoder * codec, GF_BitStream *bs)
     728             : {
     729             :         u8 type;
     730             :         u32 ID;
     731             :         GF_Node *n;
     732             : 
     733           9 :         type = gf_bs_read_int(bs, 2);
     734           9 :         switch (type) {
     735           3 :         case 0:
     736           3 :                 ID = 1+gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     737           3 :                 n = gf_sg_find_node(codec->current_graph, ID);
     738             : #ifdef MPEG4_STRICT
     739             :                 if (!n) return GF_NON_COMPLIANT_BITSTREAM;
     740             : #else
     741           3 :                 if (!n) return GF_OK;
     742             : #endif
     743             :                 /*this is a delete of a DEF node, remove ALL INSTANCES*/
     744           3 :                 return gf_node_replace(n, NULL, GF_FALSE);
     745           3 :         case 2:
     746           3 :                 return BD_DecIndexDelete(codec, bs);
     747           3 :         case 3:
     748           3 :                 ID = 1+gf_bs_read_int(bs, codec->info->config.RouteIDBits);
     749             :                 /*don't complain if route can't be deleted (not present)*/
     750           3 :                 gf_sg_route_del_by_id(codec->current_graph, ID);
     751           3 :                 return GF_OK;
     752             :         default:
     753             :                 return GF_NON_COMPLIANT_BITSTREAM;
     754             :         }
     755             :         return GF_OK;
     756             : }
     757             : 
     758             : 
     759          15 : static GF_Err BD_DecNodeReplace(GF_BifsDecoder * codec, GF_BitStream *bs)
     760             : {
     761             :         u32 NodeID;
     762             :         GF_Node *node, *new_node;
     763             :         GF_Err e;
     764             : 
     765          15 :         NodeID = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     766             :         /*this is delete / new on a DEF node: replace ALL instances*/
     767          15 :         node = gf_sg_find_node(codec->current_graph, NodeID);
     768          15 :         if (!node) return GF_NON_COMPLIANT_BITSTREAM;
     769             : 
     770             :         /*and just parse a new GF_Node - it is encoded in SFWorldNode table */
     771          15 :         new_node = gf_bifs_dec_node(codec, bs, NDT_SFWorldNode);
     772          15 :         if (!new_node && codec->LastError) return codec->LastError;
     773             : 
     774          15 :         e = gf_node_replace(node, new_node, GF_FALSE);
     775          15 :         return e;
     776             : }
     777             : 
     778          14 : static GF_Err BD_DecFieldReplace(GF_BifsDecoder * codec, GF_BitStream *bs)
     779             : {
     780             :         GF_Err e;
     781             :         u32 NodeID, ind, field_ind, NumBits;
     782             :         GF_Node *node, *prev_node;
     783             :         GF_ChildNodeItem *prev_child;
     784             :         GF_FieldInfo field;
     785             : 
     786          14 :         NodeID = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     787          14 :         node = gf_sg_find_node(codec->current_graph, NodeID);
     788          14 :         if (!node) return GF_NON_COMPLIANT_BITSTREAM;
     789          14 :         NumBits = gf_get_bit_size(gf_node_get_num_fields_in_mode(node, GF_SG_FIELD_CODING_IN)-1);
     790          14 :         ind = gf_bs_read_int(bs, NumBits);
     791          14 :         e = gf_bifs_get_field_index(node, ind, GF_SG_FIELD_CODING_IN, &field_ind);
     792          14 :         if (e) return e;
     793             : 
     794          14 :         e = gf_node_get_field(node, field_ind, &field);
     795          14 :         if (e) return e;
     796             : 
     797             :         prev_node = NULL;
     798             :         prev_child = NULL;
     799             :         /*store prev SF node*/
     800          14 :         if (field.fieldType == GF_SG_VRML_SFNODE) {
     801           2 :                 prev_node = *((GF_Node **) field.far_ptr);
     802             :         }
     803             :         /*store prev MFNode content*/
     804          12 :         else if (field.fieldType == GF_SG_VRML_MFNODE) {
     805           1 :                 prev_child = * ((GF_ChildNodeItem **) field.far_ptr);
     806           1 :                 * ((GF_ChildNodeItem **) field.far_ptr) = NULL;
     807             :         }
     808             :         /*regular field*/
     809          11 :         else if (!gf_sg_vrml_is_sf_field(field.fieldType)) {
     810           0 :                 gf_sg_vrml_mf_reset(field.far_ptr, field.fieldType);
     811             :         }
     812             : 
     813             :         /*parse the field*/
     814          14 :         codec->is_com_dec = GF_TRUE;
     815          14 :         e = gf_bifs_dec_field(codec, bs, node, &field, GF_FALSE);
     816          14 :         codec->is_com_dec = GF_FALSE;
     817             :         /*remove prev nodes*/
     818          14 :         if (field.fieldType == GF_SG_VRML_SFNODE) {
     819           2 :                 if (prev_node) e = gf_node_unregister(prev_node, node);
     820          12 :         } else if (field.fieldType == GF_SG_VRML_MFNODE) {
     821           1 :                 gf_node_unregister_children(node, prev_child);
     822             :         }
     823          14 :         if (!e) gf_bifs_check_field_change(node, &field);
     824             :         return e;
     825             : }
     826             : 
     827         950 : static GF_Err BD_DecIndexValueReplace(GF_BifsDecoder * codec, GF_BitStream *bs)
     828             : {
     829             :         GF_Node *new_node;
     830             :         u32 NodeID, ind, field_ind, NumBits, pos;
     831             :         u8 type;
     832             :         GF_Node *node;
     833             :         GF_Err e;
     834             :         GF_FieldInfo field, sffield;
     835             : 
     836             :         /*get the node*/
     837         950 :         NodeID = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     838             : 
     839         950 :         node = gf_sg_find_node(codec->current_graph, NodeID);
     840         950 :         if (!node) return GF_NON_COMPLIANT_BITSTREAM;
     841         950 :         NumBits = gf_get_bit_size(gf_node_get_num_fields_in_mode(node, GF_SG_FIELD_CODING_IN)-1);
     842         950 :         ind = gf_bs_read_int(bs, NumBits);
     843         950 :         e = gf_bifs_get_field_index(node, ind, GF_SG_FIELD_CODING_IN, &field_ind);
     844         950 :         if (e) return e;
     845             : 
     846         950 :         e = gf_node_get_field(node, field_ind, &field);
     847         950 :         if (e) return e;
     848         950 :         if (gf_sg_vrml_is_sf_field(field.fieldType)) return GF_NON_COMPLIANT_BITSTREAM;
     849             : 
     850         950 :         type = gf_bs_read_int(bs, 2);
     851         950 :         switch (type) {
     852         944 :         case 0:
     853         944 :                 pos = gf_bs_read_int(bs, 16);
     854         944 :                 break;
     855             :         case 2:
     856             :                 pos = 0;
     857             :                 break;
     858           0 :         case 3:
     859           0 :                 pos = ((GenMFField *) field.far_ptr)->count - 1;
     860           0 :                 break;
     861             :         default:
     862             :                 return GF_NON_COMPLIANT_BITSTREAM;
     863             :         }
     864             : 
     865             :         /*if MFNode remove the child and parse new node*/
     866         950 :         if (field.fieldType == GF_SG_VRML_MFNODE) {
     867             :                 /*get the new node*/
     868         947 :                 new_node = gf_bifs_dec_node(codec, bs, field.NDTtype);
     869         947 :                 if (codec->LastError) {
     870             :                         e = codec->LastError;
     871             :                         goto exit;
     872             :                 }
     873         947 :                 if (new_node) {
     874         941 :                         e = gf_node_register(new_node, node);
     875         941 :                         if (e) return e;
     876             :                 }
     877             :                 /*replace prev node*/
     878         947 :                 e = gf_node_replace_child(node, (GF_ChildNodeItem**) field.far_ptr, pos, new_node);
     879         947 :                 if (!e) gf_bifs_check_field_change(node, &field);
     880             :         }
     881             :         /*erase the field item*/
     882             :         else {
     883             :                 memcpy(&sffield, &field, sizeof(GF_FieldInfo));
     884           3 :                 sffield.fieldType = gf_sg_vrml_get_sf_type(field.fieldType);
     885             : 
     886             :                 /*make sure this is consistent*/
     887           3 :                 if (pos && pos >= ((GenMFField *)field.far_ptr)->count) {
     888           0 :                         pos = ((GenMFField *)field.far_ptr)->count - 1;
     889             :                 }
     890             : 
     891           3 :                 e = gf_sg_vrml_mf_get_item(field.far_ptr, field.fieldType, & sffield.far_ptr, pos);
     892           3 :                 if (e) return e;
     893           3 :                 e = gf_bifs_dec_sf_field(codec, bs, node, &sffield, GF_FALSE);
     894           6 :                 if (!e) gf_bifs_check_field_change(node, &field);
     895             :         }
     896             : 
     897           0 : exit:
     898             :         return e;
     899             : }
     900             : 
     901           3 : static GF_Err BD_DecRouteReplace(GF_BifsDecoder * codec, GF_BitStream *bs)
     902             : {
     903             :         GF_Err e;
     904             :         u32 RouteID, numBits, ind, node_id, fromID, toID;
     905             :         char name[1000], *ptr;
     906             :         GF_Route *r;
     907             :         GF_Node *OutNode, *InNode;
     908             : 
     909           3 :         RouteID = 1+gf_bs_read_int(bs, codec->info->config.RouteIDBits);
     910             : 
     911           3 :         r = gf_sg_route_find(codec->current_graph, RouteID);
     912             : #ifdef MPEG4_STRICT
     913             :         if (!r) return GF_NON_COMPLIANT_BITSTREAM;
     914             :         ptr = gf_sg_route_get_name(r);
     915             :         gf_sg_route_del(r);
     916             : #else
     917             :         ptr = NULL;
     918           3 :         if (r) {
     919           3 :                 ptr = gf_sg_route_get_name(r);
     920             : //              gf_sg_route_del(r);
     921             :         }
     922             : #endif
     923             : 
     924           3 :         if (ptr) strcpy(name, ptr);
     925             : 
     926             :         /*origin*/
     927           3 :         node_id = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     928           3 :         OutNode = gf_sg_find_node(codec->current_graph, node_id);
     929           3 :         if (!OutNode) return GF_NON_COMPLIANT_BITSTREAM;
     930           3 :         numBits = gf_get_bit_size(gf_node_get_num_fields_in_mode(OutNode, GF_SG_FIELD_CODING_OUT) - 1);
     931           3 :         ind = gf_bs_read_int(bs, numBits);
     932           3 :         e = gf_bifs_get_field_index(OutNode, ind, GF_SG_FIELD_CODING_OUT, &fromID);
     933           3 :         if (e) return e;
     934             : 
     935             :         /*target*/
     936           3 :         node_id = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
     937           3 :         InNode = gf_sg_find_node(codec->current_graph, node_id);
     938           3 :         if (!InNode) return GF_NON_COMPLIANT_BITSTREAM;
     939           3 :         numBits = gf_get_bit_size(gf_node_get_num_fields_in_mode(InNode, GF_SG_FIELD_CODING_IN) - 1);
     940           3 :         ind = gf_bs_read_int(bs, numBits);
     941           3 :         e = gf_bifs_get_field_index(InNode, ind, GF_SG_FIELD_CODING_IN, &toID);
     942           3 :         if (e) return e;
     943             : 
     944           3 :         if (r) {
     945           3 :                 if (r->FromNode->sgprivate->interact)
     946           3 :                         gf_list_del_item(r->FromNode->sgprivate->interact->routes, r);
     947             : 
     948           3 :                 r->is_setup = 0;
     949           3 :                 r->lastActivateTime = 0;
     950             : 
     951           3 :                 r->FromNode = OutNode;
     952           3 :                 r->FromField.fieldIndex = fromID;
     953           3 :                 r->ToNode = InNode;
     954           3 :                 r->ToField.fieldIndex = toID;
     955             : 
     956           3 :                 if (!r->FromNode->sgprivate->interact) {
     957           0 :                         GF_SAFEALLOC(r->FromNode->sgprivate->interact, struct _node_interactive_ext);
     958           0 :                         if (!r->FromNode->sgprivate->interact) return GF_OUT_OF_MEM;
     959             :                 }
     960           3 :                 if (!r->FromNode->sgprivate->interact->routes) {
     961           0 :                         r->FromNode->sgprivate->interact->routes = gf_list_new();
     962           0 :                         if (!r->FromNode->sgprivate->interact->routes) return GF_OUT_OF_MEM;
     963             :                 }
     964           3 :                 gf_list_add(r->FromNode->sgprivate->interact->routes, r);
     965             :         } else {
     966           0 :                 r = gf_sg_route_new(codec->current_graph, OutNode, fromID, InNode, toID);
     967           0 :                 if (!r) return GF_OUT_OF_MEM;
     968           0 :                 gf_sg_route_set_id(r, RouteID);
     969           0 :                 if (ptr) e = gf_sg_route_set_name(r, name);
     970             :         }
     971             :         return e;
     972             : }
     973             : 
     974             : 
     975         982 : static GF_Err BD_DecReplace(GF_BifsDecoder * codec, GF_BitStream *bs)
     976             : {
     977             :         u8 type;
     978         982 :         type = gf_bs_read_int(bs, 2);
     979         982 :         switch (type) {
     980          15 :         case 0:
     981          15 :                 return BD_DecNodeReplace(codec, bs);
     982          14 :         case 1:
     983          14 :                 return BD_DecFieldReplace(codec, bs);
     984         950 :         case 2:
     985         950 :                 return BD_DecIndexValueReplace(codec, bs);
     986           3 :         case 3:
     987           3 :                 return BD_DecRouteReplace(codec, bs);
     988             :         }
     989             :         return GF_OK;
     990             : }
     991             : 
     992             : 
     993             : /*if parent is non-NULL, we are in a proto code parsing, otherwise this is a top-level proto*/
     994         106 : GF_Err gf_bifs_dec_proto_list(GF_BifsDecoder * codec, GF_BitStream *bs, GF_List *proto_list)
     995             : {
     996             :         u8 flag, field_type, event_type, useQuant, useAnim, f;
     997             :         u32 i, NbRoutes, ID, numProtos, numFields, count, qpsftype, QP_Type, NumBits;
     998             :         GF_Node *node;
     999             :         char name[1000];
    1000             :         GF_ProtoFieldInterface *proto_field;
    1001             :         GF_Proto *proto, *ParentProto;
    1002             :         GF_Err e;
    1003             :         u32 hasMinMax;
    1004             :         void *qp_min_value, *qp_max_value;
    1005             :         GF_SceneGraph *rootSG;
    1006             :         GF_FieldInfo field;
    1007             : 
    1008             :         NumBits = qpsftype = 0;
    1009             :         //store proto at codec level
    1010         106 :         rootSG = codec->current_graph;
    1011         106 :         ParentProto = codec->pCurrentProto;
    1012             :         e = GF_OK;
    1013             : 
    1014             :         numProtos = 0;
    1015             :         proto = NULL;
    1016         106 :         flag = gf_bs_read_int(bs, 1);
    1017         261 :         while (flag) {
    1018             : 
    1019          49 :                 if (!codec->info->config.ProtoIDBits) return GF_NON_COMPLIANT_BITSTREAM;
    1020             : 
    1021             :                 /*1- proto interface declaration*/
    1022          49 :                 ID = gf_bs_read_int(bs, codec->info->config.ProtoIDBits);
    1023             : 
    1024          49 :                 if (codec->UseName) {
    1025             :                         gf_bifs_dec_name(bs, name);
    1026             :                 } else {
    1027          49 :                         sprintf(name, "Proto%d", gf_list_count(codec->current_graph->protos) );
    1028             :                 }
    1029             :                 /*create a proto in the current graph*/
    1030          49 :                 proto = gf_sg_proto_new(codec->current_graph, ID, name, proto_list ? GF_TRUE : GF_FALSE);
    1031          49 :                 if (!proto) {
    1032             :                         e = GF_NON_COMPLIANT_BITSTREAM;
    1033             :                         goto exit;
    1034             :                 }
    1035          49 :                 if (proto_list) gf_list_add(proto_list, proto);
    1036             : 
    1037             :                 /*during parsing, this proto is the current active one - all nodes/proto defined/declared
    1038             :                 below it will belong to its namespace*/
    1039          49 :                 codec->current_graph = gf_sg_proto_get_graph(proto);
    1040          49 :                 codec->pCurrentProto = proto;
    1041             : 
    1042             :                 numFields = 0;
    1043          49 :                 flag = gf_bs_read_int(bs, 1);
    1044         420 :                 while (flag) {
    1045         322 :                         event_type = gf_bs_read_int(bs, 2);
    1046         322 :                         field_type = gf_bs_read_int(bs, 6);
    1047             : 
    1048         322 :                         if (codec->UseName) {
    1049             :                                 gf_bifs_dec_name(bs, name);
    1050             :                         } else {
    1051             :                                 sprintf(name, "_field%d", numFields);
    1052             :                         }
    1053             : 
    1054             :                         /*create field interface*/
    1055         322 :                         proto_field = gf_sg_proto_field_new(proto, field_type, event_type, name);
    1056             : 
    1057             :                         /*get field info */
    1058         322 :                         gf_sg_proto_field_get_field(proto_field, &field);
    1059             : 
    1060         322 :                         switch (event_type) {
    1061         292 :                         case GF_SG_EVENT_EXPOSED_FIELD:
    1062             :                         case GF_SG_EVENT_FIELD:
    1063             :                                 /*parse default value except nodes ...*/
    1064         292 :                                 if (gf_sg_vrml_is_sf_field(field_type)) {
    1065         289 :                                         e = gf_bifs_dec_sf_field(codec, bs, NULL, &field, GF_FALSE);
    1066             :                                 } else {
    1067           3 :                                         if (codec->info->config.UsePredictiveMFField) {
    1068           0 :                                                 f = gf_bs_read_int(bs, 1);
    1069             :                                                 /*predictive encoding of proto field is not possible since QP info is not present yet*/
    1070             :                                                 assert(!f);
    1071             :                                         }
    1072             :                                         /*reserved*/
    1073           3 :                                         f = gf_bs_read_int(bs, 1);
    1074           3 :                                         if (!f) {
    1075           3 :                                                 if (gf_bs_read_int(bs, 1)) {
    1076           3 :                                                         e = BD_DecMFFieldList(codec, bs, NULL, &field);
    1077             :                                                 } else {
    1078           0 :                                                         e = BD_DecMFFieldVec(codec, bs, NULL, &field);
    1079             :                                                 }
    1080             :                                         }
    1081             :                                 }
    1082         292 :                                 if (e) goto exit;
    1083             : 
    1084             :                                 break;
    1085             :                         }
    1086             : 
    1087         322 :                         flag = gf_bs_read_int(bs, 1);
    1088         322 :                         numFields++;
    1089             :                 }
    1090             : 
    1091             :                 /*2- parse proto code*/
    1092          49 :                 flag = gf_bs_read_int(bs, 1);
    1093             : 
    1094             :                 /*externProto*/
    1095          49 :                 if (flag) {
    1096             :                         memset(&field, 0, sizeof(GF_FieldInfo));
    1097          16 :                         field.far_ptr = gf_sg_proto_get_extern_url(proto);
    1098          16 :                         field.fieldType = GF_SG_VRML_MFURL;
    1099          16 :                         field.name = "ExternProto";
    1100             : 
    1101          16 :                         if (codec->info->config.UsePredictiveMFField) {
    1102           0 :                                 flag = gf_bs_read_int(bs, 1);
    1103             :                                 assert(!flag);
    1104             :                         }
    1105             :                         /*reserved*/
    1106          16 :                         gf_bs_read_int(bs, 1);
    1107             : 
    1108             :                         /*list or vector*/
    1109          16 :                         flag = gf_bs_read_int(bs, 1);
    1110          16 :                         if (flag) {
    1111          16 :                                 e = BD_DecMFFieldList(codec, bs, NULL, &field);
    1112             :                         } else {
    1113           0 :                                 e = BD_DecMFFieldVec(codec, bs, NULL, &field);
    1114             :                         }
    1115          16 :                         if (e) goto exit;
    1116             :                 }
    1117             :                 /*get proto code*/
    1118             :                 else {
    1119             :                         /*parse sub-proto list - subprotos are ALWAYS registered with parent proto graph*/
    1120          33 :                         e = gf_bifs_dec_proto_list(codec, bs, NULL);
    1121          33 :                         if (e) goto exit;
    1122             : 
    1123             :                         flag = 1;
    1124             : 
    1125          98 :                         while (flag) {
    1126             :                                 /*parse all nodes in SFWorldNode table*/
    1127          65 :                                 node = gf_bifs_dec_node(codec, bs, NDT_SFWorldNode);
    1128          65 :                                 if (!node) {
    1129           0 :                                         if (codec->LastError) {
    1130             :                                                 e = codec->LastError;
    1131             :                                                 goto exit;
    1132             :                                         } else {
    1133           0 :                                                 flag = gf_bs_read_int(bs, 1);
    1134           0 :                                                 continue;
    1135             :                                         }
    1136             :                                 }
    1137          65 :                                 e = gf_node_register(node, NULL);
    1138          65 :                                 if (e) goto exit;
    1139             : 
    1140             :                                 //Ivica patch - Flush immediately because of proto instantiation
    1141          65 :                                 gf_bifs_flush_command_list(codec);
    1142             : 
    1143          65 :                                 gf_sg_proto_add_node_code(proto, node);
    1144          65 :                                 flag = gf_bs_read_int(bs, 1);
    1145             :                         }
    1146             : 
    1147             :                         /*routes*/
    1148          33 :                         flag = gf_bs_read_int(bs, 1);
    1149          33 :                         if (flag) {
    1150           1 :                                 flag = gf_bs_read_int(bs, 1);
    1151           1 :                                 if (flag) {
    1152             :                                         /*list route*/
    1153           3 :                                         while (flag) {
    1154           2 :                                                 e = gf_bifs_dec_route(codec, bs, GF_FALSE);
    1155           2 :                                                 if (e) goto exit;
    1156           2 :                                                 flag = gf_bs_read_int(bs, 1);
    1157             :                                         }
    1158             :                                 } else {
    1159             :                                         /*vector*/
    1160           0 :                                         i = gf_bs_read_int(bs, 5);
    1161           0 :                                         NbRoutes = gf_bs_read_int(bs, i);
    1162           0 :                                         for (i=0; i<NbRoutes; i++) {
    1163           0 :                                                 e = gf_bifs_dec_route(codec, bs, GF_FALSE);
    1164           0 :                                                 if (e) goto exit;
    1165             :                                         }
    1166             :                                 }
    1167             :                         }
    1168             :                 }
    1169             : 
    1170             :                 /*restore the namespace*/
    1171          49 :                 codec->current_graph = rootSG;
    1172             : 
    1173             :                 /*3- parse anim and Quantization stuff*/
    1174          49 :                 useQuant = gf_bs_read_int(bs, 1);
    1175          49 :                 useAnim = gf_bs_read_int(bs, 1);
    1176             : 
    1177          49 :                 count = gf_sg_proto_get_field_count(proto);
    1178         371 :                 for (i=0; i<count; i++) {
    1179         322 :                         proto_field = gf_sg_proto_field_find(proto, i);
    1180         322 :                         gf_sg_proto_field_get_field(proto_field, &field);
    1181             : 
    1182             :                         /*quant*/
    1183         322 :                         if (useQuant && ( (field.eventType == GF_SG_EVENT_FIELD) || (field.eventType == GF_SG_EVENT_EXPOSED_FIELD) )) {
    1184           4 :                                 QP_Type = gf_bs_read_int(bs, 4);
    1185             : 
    1186           4 :                                 if (QP_Type==QC_LINEAR_SCALAR) {
    1187           1 :                                         NumBits = gf_bs_read_int(bs, 5);
    1188             :                                 }
    1189           4 :                                 hasMinMax = gf_bs_read_int(bs, 1);
    1190             :                                 qp_min_value = qp_max_value = NULL;
    1191           4 :                                 if (hasMinMax) {
    1192             :                                         /*parse min and max*/
    1193           2 :                                         qpsftype = gf_sg_vrml_get_sf_type(field.fieldType);
    1194           2 :                                         switch (qpsftype) {
    1195             :                                         case GF_SG_VRML_SFINT32:
    1196             :                                         case GF_SG_VRML_SFTIME:
    1197             :                                                 break;
    1198             :                                         /*other fields are of elementary type SFFloat or shouldn't have min/max*/
    1199           0 :                                         default:
    1200             :                                                 qpsftype = GF_SG_VRML_SFFLOAT;
    1201           0 :                                                 break;
    1202             :                                         }
    1203           2 :                                         field.fieldType = qpsftype;
    1204             : 
    1205           2 :                                         qp_min_value = gf_sg_vrml_field_pointer_new(qpsftype);
    1206           2 :                                         field.name = "QPMinValue";
    1207           2 :                                         field.far_ptr = qp_min_value;
    1208           2 :                                         gf_bifs_dec_sf_field(codec, bs, NULL, &field, GF_FALSE);
    1209             : 
    1210           2 :                                         qp_max_value = gf_sg_vrml_field_pointer_new(qpsftype);
    1211           2 :                                         field.name = "QPMaxValue";
    1212           2 :                                         field.far_ptr = qp_max_value;
    1213           2 :                                         gf_bifs_dec_sf_field(codec, bs, NULL, &field, GF_FALSE);
    1214             :                                 }
    1215             : 
    1216             :                                 /*and store*/
    1217           4 :                                 if (QP_Type) {
    1218           2 :                                         e = gf_bifs_proto_field_set_aq_info(proto_field, QP_Type, hasMinMax, qpsftype, qp_min_value, qp_max_value, NumBits);
    1219           2 :                                         gf_sg_vrml_field_pointer_del(qp_min_value, qpsftype);
    1220           2 :                                         gf_sg_vrml_field_pointer_del(qp_max_value, qpsftype);
    1221             :                                 }
    1222             :                         }
    1223             : 
    1224             :                         /*anim - not supported yet*/
    1225         322 :                         if (useAnim && ( (field.eventType == GF_SG_EVENT_IN) || (field.eventType == GF_SG_EVENT_EXPOSED_FIELD) )) {
    1226           0 :                                 flag = gf_bs_read_int(bs, 1);
    1227           0 :                                 if (flag) {
    1228           0 :                                         /*Anim_Type = */gf_bs_read_int(bs, 4);
    1229             :                                 }
    1230             :                         }
    1231             :                 }
    1232             : 
    1233             :                 numProtos ++;
    1234             : 
    1235             :                 /*4- get next proto*/
    1236          49 :                 flag = gf_bs_read_int(bs, 1);
    1237             :         }
    1238             : 
    1239         106 : exit:
    1240         106 :         if (e) {
    1241           0 :                 if (proto) {
    1242           0 :                         if (proto_list) gf_list_del_item(proto_list, proto);
    1243           0 :                         gf_sg_proto_del(proto);
    1244             :                 }
    1245           0 :                 codec->current_graph = rootSG;
    1246             :         }
    1247             :         /*restore original parent proto at codec level*/
    1248         106 :         codec->pCurrentProto = ParentProto;
    1249         106 :         return e;
    1250             : }
    1251             : 
    1252             : 
    1253             : 
    1254          96 : GF_Err gf_bifs_dec_route(GF_BifsDecoder * codec, GF_BitStream *bs, Bool is_insert)
    1255             : {
    1256             :         GF_Err e;
    1257             :         u8 flag;
    1258             :         GF_Route *r;
    1259             :         GF_Node *InNode, *OutNode;
    1260             :         u32 RouteID, outField, inField, numBits, ind, node_id;
    1261             :         char name[1000];
    1262             : 
    1263             :         RouteID = 0;
    1264             : 
    1265          96 :         flag = gf_bs_read_int(bs, 1);
    1266             :         /*def'ed route*/
    1267          96 :         if (flag) {
    1268          15 :                 RouteID = 1 + gf_bs_read_int(bs, codec->info->config.RouteIDBits);
    1269          15 :                 if (codec->UseName) gf_bifs_dec_name(bs, name);
    1270             :         }
    1271             :         /*origin*/
    1272          96 :         node_id = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
    1273          96 :         OutNode = gf_sg_find_node(codec->current_graph, node_id);
    1274          96 :         if (!OutNode) return GF_SG_UNKNOWN_NODE;
    1275             : 
    1276          96 :         numBits = gf_node_get_num_fields_in_mode(OutNode, GF_SG_FIELD_CODING_OUT) - 1;
    1277          96 :         numBits = gf_get_bit_size(numBits);
    1278          96 :         ind = gf_bs_read_int(bs, numBits);
    1279          96 :         e = gf_bifs_get_field_index(OutNode, ind, GF_SG_FIELD_CODING_OUT, &outField);
    1280          96 :         if (e) return e;
    1281             : 
    1282             :         /*target*/
    1283          96 :         node_id = 1 + gf_bs_read_int(bs, codec->info->config.NodeIDBits);
    1284          96 :         InNode = gf_sg_find_node(codec->current_graph, node_id);
    1285          96 :         if (!InNode) return GF_SG_UNKNOWN_NODE;
    1286             : 
    1287          96 :         numBits = gf_node_get_num_fields_in_mode(InNode, GF_SG_FIELD_CODING_IN) - 1;
    1288          96 :         numBits = gf_get_bit_size(numBits);
    1289          96 :         ind = gf_bs_read_int(bs, numBits);
    1290          96 :         e = gf_bifs_get_field_index(InNode, ind, GF_SG_FIELD_CODING_IN, &inField);
    1291          96 :         if (e) return e;
    1292             : 
    1293          96 :         r = gf_sg_route_new(codec->current_graph, OutNode, outField, InNode, inField);
    1294          96 :         if (!r) return GF_OUT_OF_MEM;
    1295          96 :         if (RouteID) {
    1296          15 :                 e = gf_sg_route_set_id(r, RouteID);
    1297          15 :                 if (!e && codec->UseName) e = gf_sg_route_set_name(r, name);
    1298             :         }
    1299             :         return e;
    1300             : }
    1301             : 
    1302             : 
    1303          58 : GF_Err BD_DecSceneReplace(GF_BifsDecoder * codec, GF_BitStream *bs, GF_List *proto_list)
    1304             : {
    1305             :         u8 flag;
    1306             :         u32 i, nbR;
    1307             :         GF_Err e;
    1308             :         GF_Node *root;
    1309             : 
    1310             :         /*Reset the existing scene / scene graph, protos and route lists*/
    1311          58 :         if (!proto_list) gf_sg_reset(codec->current_graph);
    1312             : 
    1313             :         /*reserved*/
    1314          58 :         gf_bs_read_int(bs, 6);
    1315             : 
    1316          58 :         codec->UseName = (Bool)gf_bs_read_int(bs, 1);
    1317             :         /*parse PROTOS*/
    1318          58 :         e = gf_bifs_dec_proto_list(codec, bs, proto_list);
    1319          58 :         if (e) goto exit;
    1320             : 
    1321             :         assert(codec->pCurrentProto==NULL);
    1322             :         /*Parse the top node - always of type SFTopNode*/
    1323          58 :         root = gf_bifs_dec_node(codec, bs, NDT_SFTopNode);
    1324          58 :         if (!root && codec->LastError) {
    1325             :                 e = codec->LastError;
    1326             :                 goto exit;
    1327             :         }
    1328             : 
    1329          58 :         if (root) {
    1330          43 :                 e = gf_node_register(root, NULL);
    1331          43 :                 if (e) goto exit;
    1332             :         }
    1333          58 :         gf_sg_set_root_node(codec->current_graph, root);
    1334             : 
    1335             :         /*Parse the routes*/
    1336          58 :         flag = gf_bs_read_int(bs, 1);
    1337             : 
    1338             : 
    1339          58 :         if (flag) {
    1340          16 :                 flag = gf_bs_read_int(bs, 1);
    1341          16 :                 if (flag) {
    1342             :                         /*list*/
    1343         107 :                         while (flag) {
    1344          91 :                                 e = gf_bifs_dec_route(codec, bs, GF_FALSE);
    1345          91 :                                 if (e) goto exit;
    1346          91 :                                 flag = gf_bs_read_int(bs, 1);
    1347             :                         }
    1348             :                 } else {
    1349             :                         /*vector*/
    1350           0 :                         i = gf_bs_read_int(bs, 5);
    1351           0 :                         nbR = gf_bs_read_int(bs, i);
    1352           0 :                         for (i=0; i<nbR; i++) {
    1353           0 :                                 e = gf_bifs_dec_route(codec, bs, GF_FALSE);
    1354           0 :                                 if (e) goto exit;
    1355             :                         }
    1356             :                 }
    1357             :         }
    1358             : 
    1359          42 : exit:
    1360          58 :         return e;
    1361             : }
    1362             : 
    1363         478 : GF_Err gf_bifs_dec_command(GF_BifsDecoder * codec, GF_BitStream *bs)
    1364             : {
    1365             :         GF_Err e;
    1366         478 :         e = codec->LastError = GF_OK;
    1367             : 
    1368         478 :         codec->ActiveQP = (M_QuantizationParameter*)codec->scenegraph->global_qp;
    1369             : 
    1370             :         while (1) {
    1371        1059 :                 u8 type = gf_bs_read_int(bs, 2);
    1372        1059 :                 switch (type) {
    1373          50 :                 case 0:
    1374          50 :                         e = BD_DecInsert(codec, bs);
    1375          50 :                         break;
    1376           9 :                 case 1:
    1377           9 :                         e = BD_DecDelete(codec, bs);
    1378           9 :                         break;
    1379         982 :                 case 2:
    1380         982 :                         e = BD_DecReplace(codec, bs);
    1381         982 :                         break;
    1382          18 :                 case 3:
    1383          18 :                         e = BD_DecSceneReplace(codec, bs, NULL);
    1384          18 :                         break;
    1385             :                 }
    1386        1059 :                 if (e) return e;
    1387        1059 :                 if (! gf_bs_read_int(bs, 1)) break;
    1388             :         }
    1389             : 
    1390         478 :         while (gf_list_count(codec->QPs)) {
    1391           0 :                 gf_bifs_dec_qp_remove(codec, GF_TRUE);
    1392             :         }
    1393         478 :         gf_bifs_flush_command_list(codec);
    1394             : 
    1395         478 :         return GF_OK;
    1396             : }
    1397             : 
    1398             : #endif /*GPAC_DISABLE_BIFS*/

Generated by: LCOV version 1.13