summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/on2/h264dec/source/h264bsd_pic_order_cnt.c
blob: fb23352d2b2abb47deac63850d7ff11aa4848437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*------------------------------------------------------------------------------

    Table of contents

     1. Include headers
     2. External compiler flags
     3. Module defines
     4. Local function prototypes
     5. Functions
          h264bsdDecodePicOrderCnt

------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------
    1. Include headers
------------------------------------------------------------------------------*/

#include "h264bsd_util.h"
#include "h264bsd_pic_order_cnt.h"

/*------------------------------------------------------------------------------
    2. External compiler flags
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
    3. Module defines
------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------
    4. Local function prototypes
------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------

    Function: h264bsdDecodePicOrderCnt

        Functional description:
            Compute picture order count for a picture. Function implements
            computation of all POC types (0, 1 and 2), type is obtained from
            sps. See standard for description of the POC types and how POC is
            computed for each type.

            Function returns the minimum of top field and bottom field pic
            order counts.

        Inputs:
            poc         pointer to previous results
            sps         pointer to sequence parameter set
            slicHeader  pointer to current slice header, frame number and
                        other params needed for POC computation
            pNalUnit    pointer to current NAL unit structrue, function needs
                        to know if this is an IDR picture and also if this is
                        a reference picture

        Outputs:
            poc         results stored here for computation of next POC

        Returns:
            picture order count

------------------------------------------------------------------------------*/

i32 h264bsdDecodePicOrderCnt(pocStorage_t *poc, seqParamSet_t *sps,
    sliceHeader_t *pSliceHeader, nalUnit_t *pNalUnit)
{

/* Variables */

    u32 i;
    i32 picOrderCnt;
    u32 frameNumOffset, absFrameNum, picOrderCntCycleCnt;
    u32 frameNumInPicOrderCntCycle;
    i32 expectedDeltaPicOrderCntCycle;
    u32 containsMmco5;

/* Code */

    ASSERT(poc);
    ASSERT(sps);
    ASSERT(pSliceHeader);
    ASSERT(pNalUnit);
    ASSERT(sps->picOrderCntType <= 2);

#if 0
    /* JanSa: I don't think this is necessary, don't see any reason to
     * increment prevFrameNum one by one instead of one big increment.
     * However, standard specifies that this should be done -> if someone
     * figures out any case when the outcome would be different for step by
     * step increment, this part of the code should be enabled */

    /* if there was a gap in frame numbering and picOrderCntType is 1 or 2 ->
     * "compute" pic order counts for non-existing frames. These are not
     * actually computed, but process needs to be done to update the
     * prevFrameNum and prevFrameNumOffset */
    if ( sps->picOrderCntType > 0 &&
         pSliceHeader->frameNum != poc->prevFrameNum &&
         pSliceHeader->frameNum != ((poc->prevFrameNum + 1) % sps->maxFrameNum))
    {

        /* use variable i for unUsedShortTermFrameNum */
        i = (poc->prevFrameNum + 1) % sps->maxFrameNum;

        do
        {
            if (poc->prevFrameNum > i)
                frameNumOffset = poc->prevFrameNumOffset + sps->maxFrameNum;
            else
                frameNumOffset = poc->prevFrameNumOffset;

            poc->prevFrameNumOffset = frameNumOffset;
            poc->prevFrameNum = i;

            i = (i + 1) % sps->maxFrameNum;

        } while (i != pSliceHeader->frameNum);
    }
#endif

    /* check if current slice includes mmco equal to 5 */
    containsMmco5 = HANTRO_FALSE;
    if (pSliceHeader->decRefPicMarking.adaptiveRefPicMarkingModeFlag)
    {
        i = 0;
        while (pSliceHeader->decRefPicMarking.operation[i].
            memoryManagementControlOperation)
        {
            if (pSliceHeader->decRefPicMarking.operation[i].
                memoryManagementControlOperation == 5)
            {
                containsMmco5 = HANTRO_TRUE;
                break;
            }
            i++;
        }
    }
    switch (sps->picOrderCntType)
    {

        case 0:
            /* set prevPicOrderCnt values for IDR frame */
            if (IS_IDR_NAL_UNIT(pNalUnit))
            {
                poc->prevPicOrderCntMsb = 0;
                poc->prevPicOrderCntLsb = 0;
            }

            /* compute picOrderCntMsb (stored in picOrderCnt variable) */
            if ( (pSliceHeader->picOrderCntLsb < poc->prevPicOrderCntLsb) &&
                ((poc->prevPicOrderCntLsb - pSliceHeader->picOrderCntLsb) >=
                 sps->maxPicOrderCntLsb/2) )
            {
                picOrderCnt = poc->prevPicOrderCntMsb +
                    (i32)sps->maxPicOrderCntLsb;
            }
            else if ((pSliceHeader->picOrderCntLsb > poc->prevPicOrderCntLsb) &&
                ((pSliceHeader->picOrderCntLsb - poc->prevPicOrderCntLsb) >
                 sps->maxPicOrderCntLsb/2) )
            {
                picOrderCnt = poc->prevPicOrderCntMsb -
                    (i32)sps->maxPicOrderCntLsb;
            }
            else
                picOrderCnt = poc->prevPicOrderCntMsb;

            /* standard specifies that prevPicOrderCntMsb is from previous
             * rererence frame -> replace old value only if current frame is
             * rererence frame */
            if (pNalUnit->nalRefIdc)
                poc->prevPicOrderCntMsb = picOrderCnt;

            /* compute top field order cnt (stored in picOrderCnt) */
            picOrderCnt += (i32)pSliceHeader->picOrderCntLsb;

            /* if delta for bottom field is negative -> bottom will be the
             * minimum pic order count */
            if (pSliceHeader->deltaPicOrderCntBottom < 0)
                picOrderCnt += pSliceHeader->deltaPicOrderCntBottom;

            /* standard specifies that prevPicOrderCntLsb is from previous
             * rererence frame -> replace old value only if current frame is
             * rererence frame */
            if (pNalUnit->nalRefIdc)
            {
                /* if current frame contains mmco5 -> modify values to be
                 * stored */
                if (containsMmco5)
                {
                    poc->prevPicOrderCntMsb = 0;
                    /* prevPicOrderCntLsb should be the top field picOrderCnt
                     * if previous frame included mmco5. Top field picOrderCnt
                     * for frames containing mmco5 is obtained by subtracting
                     * the picOrderCnt from original top field order count ->
                     * value is zero if top field was the minimum, i.e. delta
                     * for bottom was positive, otherwise value is
                     * -deltaPicOrderCntBottom */
                    if (pSliceHeader->deltaPicOrderCntBottom < 0)
                        poc->prevPicOrderCntLsb =
                            (u32)(-pSliceHeader->deltaPicOrderCntBottom);
                    else
                        poc->prevPicOrderCntLsb = 0;
                    picOrderCnt = 0;
                }
                else
                {
                    poc->prevPicOrderCntLsb = pSliceHeader->picOrderCntLsb;
                }
            }

            break;

        case 1:

            /* step 1 (in the description in the standard) */
            if (IS_IDR_NAL_UNIT(pNalUnit))
                frameNumOffset = 0;
            else if (poc->prevFrameNum > pSliceHeader->frameNum)
                frameNumOffset = poc->prevFrameNumOffset + sps->maxFrameNum;
            else
                frameNumOffset = poc->prevFrameNumOffset;

            /* step 2 */
            if (sps->numRefFramesInPicOrderCntCycle)
                absFrameNum = frameNumOffset + pSliceHeader->frameNum;
            else
                absFrameNum = 0;

            if (pNalUnit->nalRefIdc == 0 && absFrameNum > 0)
                absFrameNum -= 1;

            /* step 3 */
            if (absFrameNum > 0)
            {
                picOrderCntCycleCnt =
                    (absFrameNum - 1)/sps->numRefFramesInPicOrderCntCycle;
                frameNumInPicOrderCntCycle =
                    (absFrameNum - 1)%sps->numRefFramesInPicOrderCntCycle;
            }

            /* step 4 */
            expectedDeltaPicOrderCntCycle = 0;
            for (i = 0; i < sps->numRefFramesInPicOrderCntCycle; i++)
                expectedDeltaPicOrderCntCycle += sps->offsetForRefFrame[i];

            /* step 5 (picOrderCnt used to store expectedPicOrderCnt) */
            /*lint -esym(644,picOrderCntCycleCnt) always initialized */
            /*lint -esym(644,frameNumInPicOrderCntCycle) always initialized */
            if (absFrameNum > 0)
            {
                picOrderCnt =
                    (i32)picOrderCntCycleCnt * expectedDeltaPicOrderCntCycle;
                for (i = 0; i <= frameNumInPicOrderCntCycle; i++)
                    picOrderCnt += sps->offsetForRefFrame[i];
            }
            else
                picOrderCnt = 0;

            if (pNalUnit->nalRefIdc == 0)
                picOrderCnt += sps->offsetForNonRefPic;

            /* step 6 (picOrderCnt is top field order cnt if delta for bottom
             * is positive, otherwise it is bottom field order cnt) */
            picOrderCnt += pSliceHeader->deltaPicOrderCnt[0];

            if ( (sps->offsetForTopToBottomField +
                    pSliceHeader->deltaPicOrderCnt[1]) < 0 )
            {
                picOrderCnt += sps->offsetForTopToBottomField +
                    pSliceHeader->deltaPicOrderCnt[1];
            }

            /* if current picture contains mmco5 -> set prevFrameNumOffset and
             * prevFrameNum to 0 for computation of picOrderCnt of next
             * frame, otherwise store frameNum and frameNumOffset to poc
             * structure */
            if (!containsMmco5)
            {
                poc->prevFrameNumOffset = frameNumOffset;
                poc->prevFrameNum = pSliceHeader->frameNum;
            }
            else
            {
                poc->prevFrameNumOffset = 0;
                poc->prevFrameNum = 0;
                picOrderCnt = 0;
            }
            break;

        default: /* case 2 */
            /* derive frameNumOffset */
            if (IS_IDR_NAL_UNIT(pNalUnit))
                frameNumOffset = 0;
            else if (poc->prevFrameNum > pSliceHeader->frameNum)
                frameNumOffset = poc->prevFrameNumOffset + sps->maxFrameNum;
            else
                frameNumOffset = poc->prevFrameNumOffset;

            /* derive picOrderCnt (type 2 has same value for top and bottom
             * field order cnts) */
            if (IS_IDR_NAL_UNIT(pNalUnit))
                picOrderCnt = 0;
            else if (pNalUnit->nalRefIdc == 0)
                picOrderCnt =
                    2 * (i32)(frameNumOffset + pSliceHeader->frameNum) - 1;
            else
                picOrderCnt =
                    2 * (i32)(frameNumOffset + pSliceHeader->frameNum);

            /* if current picture contains mmco5 -> set prevFrameNumOffset and
             * prevFrameNum to 0 for computation of picOrderCnt of next
             * frame, otherwise store frameNum and frameNumOffset to poc
             * structure */
            if (!containsMmco5)
            {
                poc->prevFrameNumOffset = frameNumOffset;
                poc->prevFrameNum = pSliceHeader->frameNum;
            }
            else
            {
                poc->prevFrameNumOffset = 0;
                poc->prevFrameNum = 0;
                picOrderCnt = 0;
            }
            break;

    }

    /*lint -esym(644,picOrderCnt) always initialized */
    return(picOrderCnt);

}