1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 #include "mp4dec_lib.h"
19 #include "vlc_decode.h"
20 #include "bitstream.h"
23 /***********************************************************CommentBegin******
24 * 04/13/2000 : initial modification to the new PV-Decoder
26 * 04/16/2001 : Removed PV_END_OF_BUFFER case, error resilience
27 ***********************************************************CommentEnd********/
28 PV_STATUS PV_ReadVideoPacketHeader(VideoDecData *video, int *next_MB)
31 Vol *currVol = video->vol[video->currLayer];
32 Vop *currVop = video->currVop;
33 BitstreamDecVideo *stream = video->bitstream;
35 int resync_marker_length;
36 int nbits = video->nBitsForMBID;
40 int nTotalMB = video->nTotalMB;
42 fcode_forward = currVop->fcodeForward;
43 resync_marker_length = 17;
45 if (currVop->predictionType != I_VOP) resync_marker_length = 16 + fcode_forward;
47 status = PV_BitstreamShowBitsByteAlign(stream, resync_marker_length, &tmpvar32);
48 /* if (status != PV_SUCCESS && status != PV_END_OF_BUFFER) return status; */
49 if (tmpvar32 == RESYNC_MARKER)
51 // DecNextStartCode(stream);
52 PV_BitstreamByteAlign(stream);
53 BitstreamReadBits32(stream, resync_marker_length);
55 int mbnum = (int) BitstreamReadBits16(stream, nbits);
60 // if (*next_MB <= video->mbnum) /* needs more investigation */
61 // *next_MB = video->mbnum+1;
63 if (*next_MB >= nTotalMB) /* fix 04/05/01 */
65 *next_MB = video->mbnum + 1;
66 if (*next_MB >= nTotalMB) /* this check is needed */
67 *next_MB = nTotalMB - 1;
69 quantizer = (int16) BitstreamReadBits16(stream, currVol->quantPrecision);
70 if (quantizer == 0) return PV_FAIL; /* 04/03/01 */
72 currVop->quantizer = quantizer;
74 /* if we have HEC, read some redundant VOP header information */
75 /* this part needs improvement 04/05/01 */
76 if (BitstreamRead1Bits(stream))
80 /* modulo_time_base (? bits) */
84 tmpvar16 = BitstreamRead1Bits(stream);
86 while (tmpvar16 == 1);
89 BitstreamRead1Bits(stream);
91 /* vop_time_increment (1-15 bits) */
92 BitstreamReadBits16(stream, currVol->nbitsTimeIncRes);
95 BitstreamRead1Bits(stream);
97 /* vop_prediction_type (2 bits) */
98 BitstreamReadBits16(stream, 2);
100 /* Added intra_dc_vlc_thr reading */
101 BitstreamReadBits16(stream, 3);
104 if (currVop->predictionType != I_VOP)
106 fcode_forward = (int) BitstreamReadBits16(stream, 3);
108 if (currVop->predictionType == B_VOP)
110 BitstreamReadBits16(stream, 3);
118 PV_BitstreamByteAlign(stream); /* */
119 status = BitstreamCheckEndBuffer(stream); /* return end_of_VOP 03/30/01 */
120 if (status != PV_SUCCESS)
124 status = BitstreamShowBits32HC(stream, &tmpvar32); /* 07/07/01 */
125 /* -16 = 0xFFFFFFF0*/
126 if ((tmpvar32 & 0xFFFFFFF0) == VISUAL_OBJECT_SEQUENCE_START_CODE) /* start code mask 00 00 01 */
129 /* we don't have to check for legl stuffing here. 05/08/2000 */
130 return PV_END_OF_VOP;
143 /***********************************************************CommentBegin******
144 * 3/10/00 : initial modification to the
145 * new PV-Decoder Lib format.
146 * 04/17/01 : remove PV_END_OF_BUFFER, error checking
147 ***********************************************************CommentEnd********/
148 PV_STATUS PV_GobHeader(VideoDecData *video)
151 Vop *currVop = video->currVop;
152 BitstreamDecVideo *stream = video->bitstream;
153 int quantPrecision = 5;
156 BitstreamShowBits32(stream, GOB_RESYNC_MARKER_LENGTH, &tmpvar);
158 if (tmpvar != GOB_RESYNC_MARKER)
160 PV_BitstreamShowBitsByteAlign(stream, GOB_RESYNC_MARKER_LENGTH, &tmpvar);
162 if (tmpvar != GOB_RESYNC_MARKER)
167 PV_BitstreamByteAlign(stream); /* if bytealigned GOBHEADER search is performed */
168 /* then no more noforcestuffing */
171 /* we've got a GOB header info here */
172 BitstreamShowBits32(stream, GOB_RESYNC_MARKER_LENGTH + 5, &tmpvar);
177 return PV_END_OF_VOP;
182 PV_BitstreamFlushBits(stream, GOB_RESYNC_MARKER_LENGTH + 5);
183 BitstreamByteAlignNoForceStuffing(stream);
184 return PV_END_OF_VOP;
187 PV_BitstreamFlushBits(stream, GOB_RESYNC_MARKER_LENGTH + 5);
188 currVop->gobNumber = (int) tmpvar;
189 if (currVop->gobNumber >= video->nGOBinVop) return PV_FAIL;
190 currVop->gobFrameID = (int) BitstreamReadBits16(stream, 2);
191 quantizer = (int16) BitstreamReadBits16(stream, quantPrecision);
192 if (quantizer == 0) return PV_FAIL; /* 04/03/01 */
194 currVop->quantizer = quantizer;
197 #ifdef PV_ANNEX_IJKT_SUPPORT
198 PV_STATUS PV_H263SliceHeader(VideoDecData *video, int *next_MB)
202 Vop *currVop = video->currVop;
203 BitstreamDecVideo *stream = video->bitstream;
204 int nTotalMB = video->nTotalMB;
207 PV_BitstreamShowBitsByteAlignNoForceStuffing(stream, 17, &tmpvar);
208 if (tmpvar == RESYNC_MARKER)
210 BitstreamByteAlignNoForceStuffing(stream);
211 PV_BitstreamFlushBits(stream, 17);
212 if (!BitstreamRead1Bits(stream))
216 *next_MB = BitstreamReadBits16(stream, video->nBitsForMBID);
217 if (*next_MB >= nTotalMB) /* fix 04/05/01 */
219 *next_MB = video->mbnum + 1;
220 if (*next_MB >= nTotalMB) /* this check is needed */
221 *next_MB = nTotalMB - 1;
223 /* we will not parse sebp2 for large pictures 3GPP */
224 quantizer = (int16) BitstreamReadBits16(stream, 5);
225 if (quantizer == 0) return PV_FAIL;
227 currVop->quantizer = quantizer;
228 if (!BitstreamRead1Bits(stream))
232 currVop->gobFrameID = (int) BitstreamReadBits16(stream, 2);
236 status = BitstreamCheckEndBuffer(stream); /* return end_of_VOP 03/30/01 */
237 if (status != PV_SUCCESS)
241 PV_BitstreamShowBitsByteAlign(stream, SHORT_VIDEO_START_MARKER_LENGTH, &tmpvar);
243 if (tmpvar == SHORT_VIDEO_START_MARKER)
245 /* we don't have to check for legal stuffing here. 05/08/2000 */
246 return PV_END_OF_VOP;