1 #include <stdlib.h>
2 #include <string.h>
3 #include "h263decoder.h"
4 #include "h263.h"
5 #include "bitIn.h"
6
7
8 H263Global *NewH263Decoder(void) {
9 H263Global *h263Data;
10
11 h263Data = NewH263DecoderNoBuffer();
12 InitStreamBuffer(h263Data->bs, 1);
13
14 return(h263Data);
15 }
16
17 H263Global *NewH263DecoderNoBuffer(void) {
18 H263Global *h263Data = (H263Global *) malloc(sizeof(H263Global));
19
20 h263Data->bs = (Bitstream *) malloc(sizeof(Bitstream));
21 InitStreamBuffer(h263Data->bs, 0);
22
23 h263Data->first = 1;
24 h263Data->expand = 0;
25
26 h263Data->ptype = 0;
27
28 h263Data->decGOBsCounter = 0;
29
30 h263Data->gobs_in_pict = 100000; /* Just an utopic high number */
31 memset(&(h263Data->decGOBs),0,sizeof(h263Data->decGOBs));
32
33 return(h263Data);
34 }
35
36 void FreeH263Decoder(H263Global *h263Data)
37 {
38 FreeStreamBuffer(h263Data->bs);
39 free(h263Data->bs);
40 free(h263Data);
41 }
42
43
44
45 /* Decode one GOB (or a picture) including the header */
46 /* Input Arguments: h263Data->...
47 Bitstream *bs
48 int first set to 1 if the function is called the first time.
49 In this case memory is allocated.
50 Otherwise set to 0.
51 int quiet set to 1 to suppress warnings.
52
53 int dec_until_sync 1: decode until a sync is found.
54 0: decode exactly one GOB.
55
56 Output Arguments: h263Data->...
57 unsigned char *bframe[3] decoded B-frame if 'h263Data->pb_frame == 1'.
58 unsigned char *newframe[3] decoded frame.
59 int temp_ref temporal reference (of I- or P-frame).
60 int coded_picture_width width of 'bframe[0]' and 'newframe[0]'.
61 int coded_picture_height height of 'bframe[0]' and 'newframe[0]'.
62 int dec_gobs number of decoded gobs in this picture.
63 unsigned char gob_dec[] flag if the GOB is already decoded.
64 */
65 int H263DecodeGOB(H263Global *h263Data, int dec_until_sync)
66 {
67
68 if (!_getheader(h263Data)) {
69 H263ConcealGOB(h263Data);
70 h263Data->decGOBsCounter++;
71 h263Data->decGOBs[h263Data->gob] = 1;
72 h263Data->bs->BufState = GOB_EMPTY;
73 return 0;
74 }
75
76 H263getGOB(h263Data, dec_until_sync);
77 h263Data->bs->BufState = GOB_COMPLETE;
78 return 1;
79 }
80
81
82 void newpictureinit(H263Global *h263Data)
83 {
84 int i;
85 unsigned char *tmp;
86
87
88 h263Data->decGOBsCounter = 0;
89 memset(h263Data->decGOBs, 0, MAX_GOBS * sizeof(unsigned char));
90
91 for (i=0; i<3; i++) {
92 tmp = h263Data->oldrefframe[i];
93 h263Data->oldrefframe[i] = h263Data->refframe[i];
94 h263Data->refframe[i] = tmp;
95 h263Data->newframe[i] = h263Data->refframe[i];
96 }
97
98 if (h263Data->mv_outside_frame && h263Data->pict_type == PCT_INTER) {
99 make_edge_image(h263Data->oldrefframe[0], h263Data->edgeframe[0],
100 h263Data->coded_picture_width,
101 h263Data->coded_picture_height,32);
102 make_edge_image(h263Data->oldrefframe[1], h263Data->edgeframe[1],
103 h263Data->chrom_width, h263Data->chrom_height,16);
104 make_edge_image(h263Data->oldrefframe[2], h263Data->edgeframe[2],
105 h263Data->chrom_width, h263Data->chrom_height,16);
106 }
107
108 }
109
110 /* This function needs only 'h263Data->source_format' as input */
111 void InitH263Decoder(H263Global *h263Data)
112 {
113 int i, k;
114 int ysize, csize, size;
115
116
117 h263Data->first = 0;
118
119 switch (h263Data->source_format) {
120 case SQCIF:
121 h263Data->horizontal_size = 128;
122 h263Data->vertical_size = 96;
123 h263Data->mbs_in_gob = 128 / 16;
124 h263Data->gobs_in_pict = 96 / 16;
125 break;
126 case QCIF:
127 h263Data->horizontal_size = 176;
128 h263Data->vertical_size = 144;
129 h263Data->mbs_in_gob = 176 / 16;
130 h263Data->gobs_in_pict = 144 / 16;
131 break;
132 case CIF:
133 h263Data->horizontal_size = 352;
134 h263Data->vertical_size = 288;
135 h263Data->mbs_in_gob = 352 / 16;
136 h263Data->gobs_in_pict = 288 / 16;
137 break;
138 case CIF4:
139 h263Data->horizontal_size = 704;
140 h263Data->vertical_size = 576;
141 h263Data->mbs_in_gob = 2 * 704 / 16;
142 h263Data->gobs_in_pict = 576 / 16 / 2;
143 break;
144 case CIF16:
145 h263Data->horizontal_size = 1408;
146 h263Data->vertical_size = 1152;
147 h263Data->mbs_in_gob = 4 * 1408 / 16;
148 h263Data->gobs_in_pict = 1152 / 16 / 4;
149 break;
150 case CUSTOM_SOURCE_FORMAT:
151 h263Data->mbs_in_gob = h263Data->horizontal_size / 16;
152 h263Data->gobs_in_pict = h263Data->vertical_size / 16;
153 if (h263Data->vertical_size > 800) {
154 h263Data->mbs_in_gob *= 4;
155 h263Data->gobs_in_pict /= 4;
156 } else if (h263Data->vertical_size > 400) {
157 h263Data->mbs_in_gob *= 2;
158 h263Data->gobs_in_pict /= 2;
159 }
160 break;
161 default:
162 printf("ERROR: Illegal input format\n");
163 exit(-1);
164 break;
165 }
166
167
168 h263Data->mb_width = h263Data->horizontal_size/16;
169 h263Data->mb_height = h263Data->vertical_size/16;
170 h263Data->coded_picture_width = h263Data->horizontal_size;
171 h263Data->coded_picture_height = h263Data->vertical_size;
172 h263Data->chrom_width = h263Data->coded_picture_width>>1;
173 h263Data->chrom_height = h263Data->coded_picture_height>>1;
174
175 ysize = h263Data->coded_picture_width*h263Data->coded_picture_height;
176 csize = h263Data->chrom_width*h263Data->chrom_height;
177 size = ysize + 2 * csize;
178
179 /* refframe (= newframe) */
180 if (!(h263Data->refframe[0] = (unsigned char *)malloc(size)))
181 EXIT_WITH_ERR("malloc failed\n");
182 h263Data->refframe[1] = h263Data->refframe[0] + ysize;
183 h263Data->refframe[2] = h263Data->refframe[1] + csize;
184 /* Init with 128 */
185 memset(h263Data->refframe[0], 128, size);
186
187 /* oldrefframe */
188 if (!(h263Data->oldrefframe[0] = (unsigned char *)malloc(size)))
189 EXIT_WITH_ERR("malloc failed\n");
190 h263Data->oldrefframe[1] = h263Data->oldrefframe[0] + ysize;
191 h263Data->oldrefframe[2] = h263Data->oldrefframe[1] + csize;
192
193 /* bframe */
194 if (!(h263Data->bframe[0] = (unsigned char *)malloc(size)))
195 EXIT_WITH_ERR("malloc failed\n");
196 h263Data->bframe[1] = h263Data->bframe[0] + ysize;
197 h263Data->bframe[2] = h263Data->bframe[1] + csize;
198
199 /* edgeframes */
200 ysize = (h263Data->coded_picture_width+64) *
201 (h263Data->coded_picture_height+64);
202 csize = (h263Data->chrom_width+32)*(h263Data->chrom_height+32);
203 size = ysize + 2 * csize;
204 if (!(h263Data->edgeframeorig[0] = (unsigned char *)malloc(size)))
205 EXIT_WITH_ERR("malloc failed\n");
206 h263Data->edgeframe[0] = h263Data->edgeframeorig[0] +
207 (h263Data->coded_picture_width+64) * 32 + 32;
208 h263Data->edgeframeorig[1] = h263Data->edgeframeorig[0] + ysize;
209 h263Data->edgeframe[1] = h263Data->edgeframeorig[1] +
210 (h263Data->chrom_width+32) * 16 + 16;
211 h263Data->edgeframeorig[2] = h263Data->edgeframeorig[1] + csize;
212 h263Data->edgeframe[2] = h263Data->edgeframeorig[2] +
213 (h263Data->chrom_width+32) * 16 + 16;
214
215 if (h263Data->expand) {
216 ysize = h263Data->coded_picture_width*h263Data->coded_picture_height*4;
217 csize = h263Data->chrom_width*h263Data->chrom_height*4;
218 size = ysize + 2 * csize;
219 if (!(h263Data->exnewframe[0] = (unsigned char *)malloc(size)))
220 EXIT_WITH_ERR("malloc failed\n");
221 h263Data->exnewframe[1] = h263Data->exnewframe[1] + ysize;
222 h263Data->exnewframe[2] = h263Data->exnewframe[2] + csize;
223 } else
224 h263Data->exnewframe[0] = NULL;
225
226 /* mark MV's above the picture */
227 for (i = 1; i < h263Data->mb_width+1; i++) {
228 for (k = 0; k < 5; k++) {
229 h263Data->MV[0][k][0][i] = NO_VEC;
230 h263Data->MV[1][k][0][i] = NO_VEC;
231 }
232 h263Data->modemap[0][i] = MODE_INTRA;
233 }
234 /* zero MV's on the sides of the picture */
235 for (i = 0; i < h263Data->mb_height+1; i++) {
236 for (k = 0; k < 5; k++) {
237 h263Data->MV[0][k][i][0] = 0;
238 h263Data->MV[1][k][i][0] = 0;
239 h263Data->MV[0][k][i][h263Data->mb_width+1] = 0;
240 h263Data->MV[1][k][i][h263Data->mb_width+1] = 0;
241 }
242 h263Data->modemap[i][0] = MODE_INTRA;
243 h263Data->modemap[i][h263Data->mb_width+1] = MODE_INTRA;
244 }
245 }
246
247
248 void DisinitH263Decoder(H263Global *h263Data)
249 {
250 if (!h263Data->first) {
251 h263Data->first = 1;
252
253 free(h263Data->refframe[0]);
254 free(h263Data->oldrefframe[0]);
255 free(h263Data->bframe[0]);
256
257 free(h263Data->edgeframeorig[0]);
258
259 if (h263Data->expand) {
260 free(h263Data->exnewframe[0]);
261 h263Data->exnewframe[0] = NULL;
262 }
263 }
264 }
265
266
267 void H263FinishPicture(H263Global *h263Data)
268 {
269 int gn;
270 int n = 0;
271
272
273 if (!h263Data->first) {
274 for (gn = 0; gn < h263Data->gobs_in_pict; gn++)
275 if (!h263Data->decGOBs[gn]) {
276 h263Data->gob = gn;
277 H263ConcealGOB(h263Data);
278 n++;
279 }
280
281 #if 0
282 if (n == 1 && h263Data->decGOBs[0]) {
283 /* We have only decoded the first GOB;
284 Thus we have to set GFID to an unreachable number.
285 In that way decoding can only continue with a new picture header. */
286 h263Data->gfid = 4;
287 }
288 #endif
289
290 memset(h263Data->decGOBs, 0, MAX_GOBS * sizeof(unsigned char));
291 h263Data->decGOBsCounter = 0;
292 }
293 }
294
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.