1/* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18#include "mp4dec_lib.h" /* video decoder function prototypes */
19#include "vlc_decode.h"
20#include "bitstream.h"
21#include "scaling.h"
22#include "log/log.h"
23
24/* ====================================================================== /
25Function : ConcealTexture_I()
26Date     : 06/12/2001
27Purpose  : Conceal texture for I-partition
28In/out   :
29Return   :
30Modified :
31/ ====================================================================== */
32void ConcealTexture_I(VideoDecData *video, int32 startFirstPartition, int mb_start, int mb_stop, int slice_counter)
33{
34    int mbnum;
35    BitstreamDecVideo *stream = video->bitstream;
36    int16 QP;
37    int intra_dc_vlc_thr = video->currVop->intraDCVlcThr;
38
39    movePointerTo(stream, startFirstPartition);
40
41    video->usePrevQP = 0;
42    for (mbnum = mb_start; mbnum < mb_stop; mbnum++)
43    {
44        video->mbnum = mbnum;
45        video->mbnum_row = PV_GET_ROW(mbnum, video->nMBPerRow);
46        video->mbnum_col = mbnum - video->mbnum_row * video->nMBPerRow;
47        video->sliceNo[mbnum] = (uint8) slice_counter;
48        QP = video->QPMB[mbnum];
49        PV_VlcDecMCBPC_com_intra(stream);
50        GetMBheaderDataPart_DQUANT_DC(video, &QP);
51
52        if (intra_dc_vlc_thr)
53        {
54            if (video->usePrevQP)
55                QP = video->QPMB[mbnum-1];
56            if (intra_dc_vlc_thr == 7 || QP >= intra_dc_vlc_thr*2 + 11)  /* if switched then conceal from previous frame  */
57            {
58                ConcealPacket(video, mbnum, mb_stop, slice_counter);
59                video->mbnum = mb_stop - 1;
60                video->mbnum_row = PV_GET_ROW(video->mbnum, video->nMBPerRow);
61                video->mbnum_col = video->mbnum - video->mbnum_row * video->nMBPerRow;
62                break;
63            }
64        }
65
66        video->headerInfo.CBP[mbnum] = 0;
67        video->acPredFlag[mbnum] = 0;
68        GetMBData_DataPart(video);
69        video->usePrevQP = 1;
70    }
71    return;
72}
73
74/* ====================================================================== /
75Function : ConcealTexture_P()
76Date     : 05/16/2000
77Purpose  : Conceal texture for P-partition
78In/out   :
79Return   :
80/ ====================================================================== */
81
82void ConcealTexture_P(VideoDecData *video, int mb_start, int mb_stop, int slice_counter)
83{
84    int mbnum;
85
86    for (mbnum = mb_start; mbnum < mb_stop; mbnum++)
87    {
88        video->mbnum = mbnum;
89        video->mbnum_row = PV_GET_ROW(mbnum, video->nMBPerRow);
90        video->mbnum_col = mbnum - video->mbnum_row * video->nMBPerRow;
91        video->sliceNo[mbnum] = (uint8) slice_counter;
92        oscl_memset(video->mblock->block, 0, sizeof(typeMBStore));
93        /*  to get rid of dark region caused by INTRA blocks */
94        /* 05/19/2000 */
95        if (video->headerInfo.Mode[mbnum] & INTER_MASK)
96        {
97            MBMotionComp(video, 0);
98        }
99        else
100        {
101            video->headerInfo.Mode[mbnum] = MODE_SKIPPED;
102            SkippedMBMotionComp(video);
103        }
104    }
105
106    return;
107}
108
109/***************************************************************
110Function:   ConcealPacket
111Purpose :   Conceal motion and texture of a packet by direct
112copying from previous frame.
113Returned:   void
114Modified:
115*************************************************************/
116void ConcealPacket(VideoDecData *video,
117                   int mb_start,
118                   int mb_stop,
119                   int slice_counter)
120{
121    int i;
122    for (i = mb_start; i < mb_stop; i++)
123    {
124        CopyVopMB(video->currVop, video->concealFrame, i, video->width, video->height);
125        video->sliceNo[i] = (uint8) slice_counter;
126        video->headerInfo.Mode[i] = MODE_SKIPPED;
127    }
128
129    return;
130}
131
132/****************************************************************************
133Function:   CopyVopMB
134Purpose :   Fill a macroblock with previous Vop.
135Returned    :   void
136Modified:   6/04/2001 rewrote the function
137            copies from concealFrame
138****************************************************************************/
139void CopyVopMB(Vop *curr, uint8 *prevFrame, int mbnum, int width_Y, int height)
140{
141    if (curr == NULL || prevFrame == NULL) {
142        ALOGE("b/24630158");
143        return;
144    }
145    int width_C = width_Y >> 1;
146    int row = MB_SIZE;
147    uint8              *y1, *y2, *u1, *u2, *v1, *v2;
148    int xpos, ypos, MB_in_width;
149    int32 lumstart, chrstart, size;
150
151    MB_in_width = (width_Y + 15) >> 4;
152    ypos = PV_GET_ROW(mbnum, MB_in_width);
153    xpos = mbnum - ypos * MB_in_width;
154    lumstart = (ypos << 4) * (int32)width_Y  + (xpos << 4);
155    chrstart = (ypos << 3) * (int32)width_C  + (xpos << 3);
156
157    size = (int32)height * width_Y;
158
159    y1 =  curr->yChan + lumstart;
160    u1 =  curr->uChan + chrstart;
161    v1 =  curr->vChan + chrstart;
162    y2 =  prevFrame + lumstart;
163    u2 =  prevFrame + size + chrstart;
164    v2 =  prevFrame + size + (size >> 2) + chrstart;
165    while (row)
166    {
167        oscl_memcpy(y1, y2, MB_SIZE);
168        y1 += width_Y;
169        y2 += width_Y;
170        oscl_memcpy(y1, y2, MB_SIZE);
171        y1 += width_Y;
172        y2 += width_Y;
173        oscl_memcpy(y1, y2, MB_SIZE);
174        y1 += width_Y;
175        y2 += width_Y;
176        oscl_memcpy(y1, y2, MB_SIZE);
177        y1 += width_Y;
178        y2 += width_Y;
179
180        oscl_memcpy(u1, u2, B_SIZE);
181        u1 += width_C;
182        u2 += width_C;
183        oscl_memcpy(u1, u2, B_SIZE);
184        u1 += width_C;
185        u2 += width_C;
186
187        oscl_memcpy(v1, v2, B_SIZE);
188        v1 += width_C;
189        v2 += width_C;
190        oscl_memcpy(v1, v2, B_SIZE);
191        v1 += width_C;
192        v2 += width_C;
193
194        row -= 4;
195    }
196    return;
197}               /* CopyVopMB */
198
199