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 "quant.h"
27
28 #ifdef GPAC_ENABLE_BIFS_PMF
29
30 u32 gf_bifs_dec_qp14_get_bits(GF_BifsDecoder *codec);
31
32 typedef struct
33 {
34 s32 comp_min[3];
35 s32 previous_val[3];
36 s32 current_val[3];
37 s32 m_delta[3];
38
39 u32 intra_mode, intra_inter, compNbBits, num_bounds, num_comp, num_fields, QNbBits;
40 u8 QType;
41 Bool use_default;
42 SFVec3f BMin, BMax;
43
44 s32 direction, orientation, inverse;
45
46 u32 cur_field;
47
48 GF_AAModel *models[3];
49 GF_AAModel *dir_model;
50 GF_AADecoder *dec;
51 } PredMF;
52
53
PMF_ResetModels(PredMF * pmf)54 void PMF_ResetModels(PredMF *pmf)
55 {
56 u32 i;
57 for (i=0; i<pmf->num_bounds; i++) {
58 gp_bifs_aa_model_init(pmf->models[i], pmf->compNbBits);
59 }
60 gp_bifs_aa_model_init(pmf->dir_model, 1);
61 }
62
63
PMF_UnquantizeFloat(s32 vq,Fixed BMin,Fixed BMax,u32 NbBits,Bool unit_vector)64 Fixed PMF_UnquantizeFloat(s32 vq, Fixed BMin, Fixed BMax, u32 NbBits, Bool unit_vector)
65 {
66 Fixed scale = 0;
67 Fixed width = BMax - BMin;
68 if (unit_vector) NbBits -= 1;
69 if (width > FIX_EPSILON) {
70 if (NbBits) {
71 scale = gf_divfix(width , INT2FIX( (1<<NbBits) - 1) );
72 } else {
73 scale = width/2;
74 }
75 }
76 return BMin + scale * vq;
77 }
78
PMF_UnquantizeNormal(PredMF * pmf,GF_FieldInfo * field)79 GF_Err PMF_UnquantizeNormal(PredMF *pmf, GF_FieldInfo *field)
80 {
81 void *slot;
82 Fixed comp[3];
83 Fixed tang[2];
84 u32 i;
85 Fixed delta=FIX_ONE;
86 for (i=0; i<2; i++) {
87 Fixed v = PMF_UnquantizeFloat(pmf->current_val[i] - (1<< (pmf->QNbBits -1) ), 0 , FIX_ONE, pmf->QNbBits, 1);
88 tang[i]= gf_tan(gf_mulfix(GF_PI * 4, v));
89 delta += gf_mulfix(tang[i], tang[i]);
90 }
91 delta = gf_divfix(pmf->direction, gf_sqrt(delta) );
92
93 comp[(pmf->orientation) % 3] = delta;
94 for (i=0; i<2; i++)
95 comp[(pmf->orientation + i+1)%3] = gf_mulfix(tang[i], delta);
96
97 gf_sg_vrml_mf_get_item(field->far_ptr, field->fieldType, &slot, pmf->cur_field);
98 ((SFVec3f *)slot)->x = comp[0];
99 ((SFVec3f *)slot)->y = comp[1];
100 ((SFVec3f *)slot)->z = comp[2];
101 return GF_OK;
102 }
103
PMF_UnquantizeRotation(PredMF * pmf,GF_FieldInfo * field)104 GF_Err PMF_UnquantizeRotation(PredMF *pmf, GF_FieldInfo *field)
105 {
106 u32 i;
107 void *slot;
108 Fixed comp[4];
109 Fixed tang[3];
110 Fixed sine, delta = FIX_ONE;
111
112 for (i=0; i<3; i++) {
113 Fixed v = PMF_UnquantizeFloat(pmf->current_val[i] - (1<<(pmf->QNbBits - 1)), 0, FIX_ONE, pmf->QNbBits, 1);
114 tang[i] = gf_tan(gf_mulfix(GF_PI / 4, v));
115 delta += gf_mulfix(tang[i], tang[i]);
116 }
117 delta = gf_divfix(pmf->direction , gf_sqrt(delta) );
118
119 comp[(pmf->orientation)%4] = delta;
120 for (i=0; i<3; i++)
121 comp[(pmf->orientation + i+1)%4] = gf_mulfix(tang[i], delta);
122
123 gf_sg_vrml_mf_get_item(field->far_ptr, field->fieldType, &slot, pmf->cur_field);
124 delta = 2 * gf_acos(comp[0]);
125 sine = gf_sin(delta / 2);
126 if (sine != 0) {
127 for(i=1; i<4; i++)
128 comp[i] = gf_divfix(comp[i], sine);
129
130 ((SFRotation *)slot)->x = comp[1];
131 ((SFRotation *)slot)->y = comp[2];
132 ((SFRotation *)slot)->z = comp[3];
133 } else {
134 ((SFRotation *)slot)->x = FIX_ONE;
135 ((SFRotation *)slot)->y = 0;
136 ((SFRotation *)slot)->z = 0;
137 }
138 ((SFRotation *)slot)->q = delta;
139 return GF_OK;
140 }
141
PMF_Unquantize(PredMF * pmf,GF_FieldInfo * field)142 GF_Err PMF_Unquantize(PredMF *pmf, GF_FieldInfo *field)
143 {
144 void *slot;
145 if (pmf->QType == QC_NORMALS) {
146 return PMF_UnquantizeNormal(pmf, field);
147 }
148 if (pmf->QType == QC_ROTATION) {
149 return PMF_UnquantizeRotation(pmf, field);
150 }
151 /*regular*/
152 gf_sg_vrml_mf_get_item(field->far_ptr, field->fieldType, &slot, pmf->cur_field);
153 switch (field->fieldType) {
154 case GF_SG_VRML_MFVEC3F:
155 ((SFVec3f *) slot)->x = PMF_UnquantizeFloat(pmf->current_val[0], pmf->BMin.x, pmf->BMax.x, pmf->QNbBits, 0);
156 ((SFVec3f *) slot)->y = PMF_UnquantizeFloat(pmf->current_val[1], pmf->BMin.y, pmf->BMax.y, pmf->QNbBits, 0);
157 ((SFVec3f *) slot)->z = PMF_UnquantizeFloat(pmf->current_val[2], pmf->BMin.z, pmf->BMax.z, pmf->QNbBits, 0);
158 break;
159 case GF_SG_VRML_MFVEC2F:
160 ((SFVec2f *) slot)->x = PMF_UnquantizeFloat(pmf->current_val[0], pmf->BMin.x, pmf->BMax.x, pmf->QNbBits, 0);
161 ((SFVec2f *) slot)->y = PMF_UnquantizeFloat(pmf->current_val[1], pmf->BMin.y, pmf->BMax.y, pmf->QNbBits, 0);
162 break;
163 case GF_SG_VRML_MFFLOAT:
164 *((SFFloat *) slot) = PMF_UnquantizeFloat(pmf->current_val[0], pmf->BMin.x, pmf->BMax.x, pmf->QNbBits, 0);
165 break;
166 case GF_SG_VRML_MFCOLOR:
167 ((SFColor *) slot)->red = PMF_UnquantizeFloat(pmf->current_val[0], pmf->BMin.x, pmf->BMax.x, pmf->QNbBits, 0);
168 ((SFColor *) slot)->green = PMF_UnquantizeFloat(pmf->current_val[1], pmf->BMin.y, pmf->BMax.y, pmf->QNbBits, 0);
169 ((SFColor *) slot)->blue = PMF_UnquantizeFloat(pmf->current_val[2], pmf->BMin.z, pmf->BMax.z, pmf->QNbBits, 0);
170 break;
171 case GF_SG_VRML_MFINT32:
172 switch (pmf->QType) {
173 case QC_LINEAR_SCALAR:
174 case QC_COORD_INDEX:
175 *((SFInt32 *) slot) = pmf->current_val[0] + (s32) pmf->BMin.x;
176 break;
177 }
178 break;
179 }
180 return GF_OK;
181 }
182
183
PMF_ParsePValue(PredMF * pmf,GF_BitStream * bs,GF_FieldInfo * field)184 GF_Err PMF_ParsePValue(PredMF *pmf, GF_BitStream *bs, GF_FieldInfo *field)
185 {
186 u32 i, numModel;
187 s32 prev_dir = 0;
188 switch (pmf->QType) {
189 case QC_NORMALS:
190 prev_dir = pmf->direction;
191 pmf->direction = gp_bifs_aa_decode(pmf->dec, pmf->dir_model);
192 break;
193 }
194 /*decode (one model per component)*/
195 numModel = 0;
196 for (i=0; i<pmf->num_comp; i++) {
197 pmf->previous_val[i]= pmf->current_val[i];
198 pmf->current_val[i] = gp_bifs_aa_decode(pmf->dec, pmf->models[numModel]) + pmf->comp_min[numModel];
199 numModel += (pmf->num_bounds==1) ? 0 : 1;
200 }
201
202 /*compensate values*/
203 switch (pmf->QType) {
204 case QC_NORMALS:
205 case QC_ROTATION:
206 /*NOT TESTED*/
207 {
208 s32 temp_val[3];
209 s32 diff_dir = prev_dir * (pmf->direction ? -1 : 1);
210 s32 inv=1;
211 s32 diff_ori = 0;
212 s32 shift = 1 << (pmf->QNbBits - 1);
213
214 for (i=0; i<3; i++) {
215 pmf->previous_val[i] -= shift;
216 pmf->current_val[i] -= shift;
217 }
218 for (i=0; i< pmf->num_comp; i++) {
219 temp_val[i] = pmf->previous_val[i] + pmf->current_val[i];
220 if ( abs(temp_val[i]) > shift - 1) {
221 diff_ori = i+1;
222 inv = ( temp_val[i] > 0) ? 1 : -1;
223 break;
224 }
225 }
226 if (diff_ori != 0) {
227 s32 k=0;
228 for (i=0; i< pmf->num_comp - diff_ori; i++) {
229 //k = (i + diff_ori) % pmf->num_comp;
230 temp_val[i] = inv * ( pmf->previous_val[i] + pmf->current_val[i]);
231 }
232 k = diff_ori - 1;
233 temp_val[pmf->num_comp - diff_ori] = inv * 2 * (shift - 1) - (pmf->previous_val[k] + pmf->current_val[k]) ;
234 for (i = pmf->num_comp - diff_ori + 1; i<pmf->num_comp; i++) {
235 k = (i+diff_ori-1) % pmf->num_comp;
236 temp_val[i] = inv * (pmf->previous_val[k] + pmf->current_val[k]);
237 }
238 }
239 pmf->orientation = (pmf->orientation + diff_ori) % (pmf->num_comp + 1);
240 pmf->direction = diff_dir * inv;
241 for (i=0; i< pmf->num_comp; i++)
242 pmf->current_val[i]= temp_val[i] + shift;
243 }
244 break;
245 default:
246 for (i=0; i< pmf->num_comp; i++)
247 pmf->current_val[i] += pmf->previous_val[i];
248 }
249 /*unquantize*/
250 return PMF_Unquantize(pmf, field);
251 }
252
PMF_ParseIValue(PredMF * pmf,GF_BitStream * bs,GF_FieldInfo * field)253 GF_Err PMF_ParseIValue(PredMF *pmf, GF_BitStream *bs, GF_FieldInfo *field)
254 {
255 u32 i;
256 switch (pmf->QType) {
257 case QC_NORMALS:
258 i = gf_bs_read_int(bs, 1);
259 pmf->direction = i ? -1 : 1;
260 case QC_ROTATION:
261 pmf->orientation = gf_bs_read_int(bs, 2);
262 break;
263 }
264 /*read all vals*/
265 for (i=0; i<pmf->num_comp; i++) {
266 pmf->current_val[i] = gf_bs_read_int(bs, pmf->QNbBits);
267 }
268 /*reset after each I*/
269 if (pmf->cur_field + 1<pmf->num_fields) gp_bifs_aa_dec_reset(pmf->dec);
270
271 return PMF_Unquantize(pmf, field);
272 }
273
274 /*bit access shall be done through the AA decoder since bits may be cached there*/
PMF_UpdateArrayQP(PredMF * pmf,GF_BitStream * bs)275 GF_Err PMF_UpdateArrayQP(PredMF *pmf, GF_BitStream *bs)
276 {
277 u32 flag, i;
278 switch (pmf->intra_mode) {
279 case 1:
280 flag = gf_bs_read_int(bs, 5);
281 pmf->intra_inter = gf_bs_read_int(bs, flag);
282 case 2:
283 case 0:
284 flag = gf_bs_read_int(bs, 1);
285 if (flag) {
286 pmf->compNbBits = gf_bs_read_int(bs, 5);
287 }
288 flag = gf_bs_read_int(bs, 1);
289 if (flag) {
290 for (i=0; i<pmf->num_bounds; i++) {
291 flag = gf_bs_read_int(bs, pmf->QNbBits + 1);
292 pmf->comp_min[i] = flag - (1<<pmf->QNbBits);
293 }
294 }
295 break;
296 }
297 /*reset all models when new settings are received*/
298 PMF_ResetModels(pmf);
299 return GF_OK;
300 }
301
302
303
gf_bifs_dec_pred_mf_field(GF_BifsDecoder * codec,GF_BitStream * bs,GF_Node * node,GF_FieldInfo * field)304 GF_Err gf_bifs_dec_pred_mf_field(GF_BifsDecoder *codec, GF_BitStream *bs, GF_Node *node, GF_FieldInfo *field)
305 {
306 GF_Err e;
307 Bool HasQ;
308 u8 AType;
309 Fixed b_min, b_max;
310 u32 i, flag;
311 PredMF pmf;
312
313 memset(&pmf, 0, sizeof(PredMF));
314
315 HasQ = gf_bifs_get_aq_info(node, field->fieldIndex, &pmf.QType, &AType, &b_min, &b_max, &pmf.QNbBits);
316 if (!HasQ || !pmf.QType) return GF_EOS;
317
318 /*get NbBits for QP14 (QC_COORD_INDEX)*/
319 if (pmf.QType == QC_COORD_INDEX)
320 pmf.QNbBits = gf_bifs_dec_qp14_get_bits(codec);
321
322 pmf.BMin.x = pmf.BMin.y = pmf.BMin.z = b_min;
323 pmf.BMax.x = pmf.BMax.y = pmf.BMax.z = b_max;
324
325 /*check is the QP is on and retrieves the bounds*/
326 if (!Q_IsTypeOn(codec->ActiveQP, pmf.QType, &pmf.QNbBits, &pmf.BMin, &pmf.BMax)) return GF_EOS;
327
328 switch (field->fieldType) {
329 case GF_SG_VRML_MFCOLOR:
330 case GF_SG_VRML_MFVEC3F:
331 if (pmf.QType==QC_NORMALS) {
332 pmf.num_comp = 2;
333 break;
334 }
335 case GF_SG_VRML_MFROTATION:
336 pmf.num_comp = 3;
337 break;
338 case GF_SG_VRML_MFVEC2F:
339 pmf.num_comp = 2;
340 break;
341 case GF_SG_VRML_MFFLOAT:
342 case GF_SG_VRML_MFINT32:
343 pmf.num_comp = 1;
344 break;
345 default:
346 return GF_NON_COMPLIANT_BITSTREAM;
347 }
348
349
350 /*parse array header*/
351 flag = gf_bs_read_int(bs, 5);
352 pmf.num_fields = gf_bs_read_int(bs, flag);
353 pmf.intra_mode = gf_bs_read_int(bs, 2);
354 switch (pmf.intra_mode) {
355 case 1:
356 flag = gf_bs_read_int(bs, 5);
357 pmf.intra_inter = gf_bs_read_int(bs, flag);
358 /*no break*/
359 case 2:
360 case 0:
361 pmf.compNbBits = gf_bs_read_int(bs, 5);
362 if (pmf.QType==1) pmf.num_bounds = 3;
363 else if (pmf.QType==2) pmf.num_bounds = 2;
364 else pmf.num_bounds = 1;
365 for (i=0; i<pmf.num_bounds; i++) {
366 flag = gf_bs_read_int(bs, pmf.QNbBits + 1);
367 pmf.comp_min[i] = flag - (1<<pmf.QNbBits);
368 }
369 break;
370 case 3:
371 break;
372 }
373
374
375 pmf.dec = gp_bifs_aa_dec_new(bs);
376 pmf.models[0] = gp_bifs_aa_model_new();
377 pmf.models[1] = gp_bifs_aa_model_new();
378 pmf.models[2] = gp_bifs_aa_model_new();
379 pmf.dir_model = gp_bifs_aa_model_new();
380
381 PMF_ResetModels(&pmf);
382
383 gf_sg_vrml_mf_alloc(field->far_ptr, field->fieldType, pmf.num_fields);
384 pmf.cur_field = 0;
385 /*parse initial I*/
386 e = PMF_ParseIValue(&pmf, bs, field);
387 if (e) return e;
388
389 for (pmf.cur_field=1; pmf.cur_field<pmf.num_fields; pmf.cur_field++) {
390 switch (pmf.intra_mode) {
391 case 0:
392 e = PMF_ParsePValue(&pmf, bs, field);
393 break;
394
395 /*NOT TESTED*/
396 case 1:
397 if (!(pmf.cur_field % pmf.intra_inter)) {
398 /*resync bitstream*/
399 gp_bifs_aa_dec_resync(pmf.dec);
400 flag = gf_bs_read_int(bs, 1);
401 /*update settings ?*/
402 if (flag) {
403 e = PMF_UpdateArrayQP(&pmf, bs);
404 if (e) goto err_exit;
405 }
406 e = PMF_ParseIValue(&pmf, bs, field);
407 } else {
408 e = PMF_ParsePValue(&pmf, bs, field);
409 }
410 break;
411
412 /*NOT TESTED*/
413 case 2:
414 /*is intra ? - WARNING: this is from the arithmetic context !!*/
415 flag = gp_bifs_aa_dec_get_bit(pmf.dec);
416 if (flag) {
417 /*resync bitstream*/
418 gp_bifs_aa_dec_resync_bit(pmf.dec);
419 flag = gf_bs_read_int(bs, 1);
420 /*update settings ?*/
421 if (flag) {
422 e = PMF_UpdateArrayQP(&pmf, bs);
423 if (e) goto err_exit;
424 }
425 e = PMF_ParseIValue(&pmf, bs, field);
426 } else {
427 e = PMF_ParsePValue(&pmf, bs, field);
428 gp_bifs_aa_dec_flush(pmf.dec);
429 }
430 break;
431 }
432 if (e) goto err_exit;
433 }
434
435
436 if (pmf.intra_mode==2) {
437 gp_bifs_aa_dec_resync_bit(pmf.dec);
438 } else {
439 gp_bifs_aa_dec_resync(pmf.dec);
440 }
441
442 err_exit:
443 gp_bifs_aa_model_del(pmf.models[0]);
444 gp_bifs_aa_model_del(pmf.models[1]);
445 gp_bifs_aa_model_del(pmf.models[2]);
446 gp_bifs_aa_model_del(pmf.dir_model);
447 gp_bifs_aa_dec_del(pmf.dec);
448 return e;
449 }
450
451 #endif /*GPAC_ENABLE_BIFS_PMF*/
452