1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "M4OSA_Types.h"
18#include "M4OSA_Debug.h"
19
20#include "M4VD_Tools.h"
21
22/**
23 ************************************************************************
24 * @file   M4VD_Tools.c
25 * @brief
26 * @note   This file implements helper functions for Bitstream parser
27 ************************************************************************
28 */
29
30M4OSA_UInt32 M4VD_Tools_GetBitsFromMemory(M4VS_Bitstream_ctxt* parsingCtxt,
31     M4OSA_UInt32 nb_bits)
32{
33    M4OSA_UInt32    code;
34    M4OSA_UInt32    i;
35    code = 0;
36    for (i = 0; i < nb_bits; i++)
37    {
38        if (parsingCtxt->stream_index == 8)
39        {
40            //M4OSA_memcpy( (M4OSA_MemAddr8)&(parsingCtxt->stream_byte), parsingCtxt->in,
41            //     sizeof(unsigned char));
42            parsingCtxt->stream_byte = (unsigned char)(parsingCtxt->in)[0];
43            parsingCtxt->in++;
44            //fread(&stream_byte, sizeof(unsigned char),1,in);
45            parsingCtxt->stream_index = 0;
46        }
47        code = (code << 1);
48        code |= ((parsingCtxt->stream_byte & 0x80) >> 7);
49
50        parsingCtxt->stream_byte = (parsingCtxt->stream_byte << 1);
51        parsingCtxt->stream_index++;
52    }
53
54    return code;
55}
56
57M4OSA_ERR M4VD_Tools_WriteBitsToMemory(M4OSA_UInt32 bitsToWrite,
58                                     M4OSA_MemAddr32 dest_bits,
59                                     M4OSA_UInt8 offset, M4OSA_UInt8 nb_bits)
60{
61    M4OSA_UInt8 i,j;
62    M4OSA_UInt32 temp_dest = 0, mask = 0, temp = 1;
63    M4OSA_UInt32 input = bitsToWrite;
64    input = (input << (32 - nb_bits - offset));
65
66    /* Put destination buffer to 0 */
67    for(j=0;j<3;j++)
68    {
69        for(i=0;i<8;i++)
70        {
71            if((j*8)+i >= offset && (j*8)+i < nb_bits + offset)
72            {
73                mask |= (temp << ((7*(j+1))-i+j));
74            }
75        }
76    }
77    mask = ~mask;
78    *dest_bits &= mask;
79
80    /* Parse input bits, and fill output buffer */
81    for(j=0;j<3;j++)
82    {
83        for(i=0;i<8;i++)
84        {
85            if((j*8)+i >= offset && (j*8)+i < nb_bits + offset)
86            {
87                temp = ((input & (0x80000000 >> offset)) >> (31-offset));
88                //*dest_bits |= (temp << (31 - i));
89                *dest_bits |= (temp << ((7*(j+1))-i+j));
90                input = (input << 1);
91            }
92        }
93    }
94
95    return M4NO_ERROR;
96}
97
98
99
100