Mercurial > libavcodec.hg
annotate h263dec.c @ 274:d0c186bcf075 libavcodec
use the width & height from the mpeg4 header ... in the case that its complete
author | michaelni |
---|---|
date | Tue, 19 Mar 2002 03:51:36 +0000 |
parents | 34f40a0fc840 |
children | 1fc96b02142e |
rev | line source |
---|---|
0 | 1 /* |
2 * H263 decoder | |
3 * Copyright (c) 2001 Gerard Lantau. | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 */ | |
19 #include <stdlib.h> | |
20 #include <stdio.h> | |
21 #include <string.h> | |
22 #include "dsputil.h" | |
23 #include "avcodec.h" | |
24 #include "mpegvideo.h" | |
25 | |
26 //#define DEBUG | |
27 | |
28 static int h263_decode_init(AVCodecContext *avctx) | |
29 { | |
30 MpegEncContext *s = avctx->priv_data; | |
60 | 31 int i; |
32 | |
67 | 33 s->avctx = avctx; |
0 | 34 s->out_format = FMT_H263; |
35 | |
36 s->width = avctx->width; | |
37 s->height = avctx->height; | |
38 | |
39 /* select sub codec */ | |
40 switch(avctx->codec->id) { | |
41 case CODEC_ID_H263: | |
154
f914f710b8d0
- Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents:
144
diff
changeset
|
42 s->gob_number = 0; |
f914f710b8d0
- Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents:
144
diff
changeset
|
43 s->first_gob_line = 0; |
0 | 44 break; |
67 | 45 case CODEC_ID_MPEG4: |
0 | 46 s->time_increment_bits = 4; /* default value for broken headers */ |
47 s->h263_pred = 1; | |
262 | 48 s->has_b_frames = 1; |
0 | 49 break; |
50 case CODEC_ID_MSMPEG4: | |
51 s->h263_msmpeg4 = 1; | |
52 s->h263_pred = 1; | |
53 break; | |
54 case CODEC_ID_H263I: | |
55 s->h263_intel = 1; | |
56 break; | |
57 default: | |
58 return -1; | |
59 } | |
60 | |
61 /* for h263, we allocate the images after having read the header */ | |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
62 if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4) |
144 | 63 if (MPV_common_init(s) < 0) |
64 return -1; | |
0 | 65 |
60 | 66 /* XXX: suppress this matrix init, only needed because using mpeg1 |
67 dequantize in mmx case */ | |
68 for(i=0;i<64;i++) | |
69 s->non_intra_matrix[i] = default_non_intra_matrix[i]; | |
70 | |
0 | 71 if (s->h263_msmpeg4) |
72 msmpeg4_decode_init_vlc(s); | |
73 else | |
74 h263_decode_init_vlc(s); | |
75 | |
76 return 0; | |
77 } | |
78 | |
79 static int h263_decode_end(AVCodecContext *avctx) | |
80 { | |
81 MpegEncContext *s = avctx->priv_data; | |
82 | |
83 MPV_common_end(s); | |
84 return 0; | |
85 } | |
86 | |
87 static int h263_decode_frame(AVCodecContext *avctx, | |
88 void *data, int *data_size, | |
89 UINT8 *buf, int buf_size) | |
90 { | |
91 MpegEncContext *s = avctx->priv_data; | |
92 int ret; | |
93 AVPicture *pict = data; | |
94 | |
95 #ifdef DEBUG | |
96 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size); | |
97 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]); | |
98 #endif | |
144 | 99 |
0 | 100 /* no supplementary picture */ |
101 if (buf_size == 0) { | |
102 *data_size = 0; | |
103 return 0; | |
104 } | |
105 | |
106 init_get_bits(&s->gb, buf, buf_size); | |
107 | |
108 /* let's go :-) */ | |
109 if (s->h263_msmpeg4) { | |
110 ret = msmpeg4_decode_picture_header(s); | |
111 } else if (s->h263_pred) { | |
112 ret = mpeg4_decode_picture_header(s); | |
113 } else if (s->h263_intel) { | |
114 ret = intel_h263_decode_picture_header(s); | |
115 } else { | |
116 ret = h263_decode_picture_header(s); | |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
117 } |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
118 |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
119 /* After H263 & mpeg4 header decode we have the height, width,*/ |
160 | 120 /* and other parameters. So then we could init the picture */ |
121 /* FIXME: By the way H263 decoder is evolving it should have */ | |
122 /* an H263EncContext */ | |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
123 if (!s->context_initialized) { |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
124 avctx->width = s->width; |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
125 avctx->height = s->height; |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
126 if (MPV_common_init(s) < 0) |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
127 return -1; |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
128 } else if (s->width != avctx->width || s->height != avctx->height) { |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
129 /* H.263 could change picture size any time */ |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
130 MPV_common_end(s); |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
131 if (MPV_common_init(s) < 0) |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
132 return -1; |
0 | 133 } |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
134 |
0 | 135 if (ret < 0) |
136 return -1; | |
137 | |
138 MPV_frame_start(s); | |
139 | |
140 #ifdef DEBUG | |
141 printf("qscale=%d\n", s->qscale); | |
142 #endif | |
143 | |
144 /* decode each macroblock */ | |
266 | 145 s->block_wrap[0]= |
146 s->block_wrap[1]= | |
147 s->block_wrap[2]= | |
148 s->block_wrap[3]= s->mb_width*2 + 2; | |
149 s->block_wrap[4]= | |
150 s->block_wrap[5]= s->mb_width + 2; | |
0 | 151 for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) { |
162 | 152 /* Check for GOB headers on H.263 */ |
153 /* FIXME: In the future H.263+ will have intra prediction */ | |
154 /* and we are gonna need another way to detect MPEG4 */ | |
155 if (s->mb_y && !s->h263_pred) { | |
156 s->first_gob_line = h263_decode_gob_header(s); | |
157 } | |
266 | 158 s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1; |
159 s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1); | |
160 s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1; | |
161 s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2); | |
162 s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
163 s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
0 | 164 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) { |
266 | 165 s->block_index[0]+=2; |
166 s->block_index[1]+=2; | |
167 s->block_index[2]+=2; | |
168 s->block_index[3]+=2; | |
169 s->block_index[4]++; | |
170 s->block_index[5]++; | |
0 | 171 #ifdef DEBUG |
172 printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); | |
173 #endif | |
144 | 174 //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x); |
0 | 175 /* DCT & quantize */ |
176 if (s->h263_msmpeg4) { | |
177 msmpeg4_dc_scale(s); | |
178 } else if (s->h263_pred) { | |
179 h263_dc_scale(s); | |
180 } else { | |
181 /* default quantization values */ | |
182 s->y_dc_scale = 8; | |
183 s->c_dc_scale = 8; | |
184 } | |
185 | |
203 | 186 #ifdef HAVE_MMX |
187 if (mm_flags & MM_MMX) { | |
188 asm volatile( | |
189 "pxor %%mm7, %%mm7 \n\t" | |
190 "movl $-128*6, %%eax \n\t" | |
191 "1: \n\t" | |
192 "movq %%mm7, (%0, %%eax) \n\t" | |
193 "movq %%mm7, 8(%0, %%eax) \n\t" | |
194 "movq %%mm7, 16(%0, %%eax) \n\t" | |
195 "movq %%mm7, 24(%0, %%eax) \n\t" | |
196 "addl $32, %%eax \n\t" | |
197 " js 1b \n\t" | |
198 : : "r" (((int)s->block)+128*6) | |
199 : "%eax" | |
200 ); | |
201 }else{ | |
202 memset(s->block, 0, sizeof(s->block)); | |
203 } | |
204 #else | |
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
0
diff
changeset
|
205 memset(s->block, 0, sizeof(s->block)); |
203 | 206 #endif |
0 | 207 s->mv_dir = MV_DIR_FORWARD; |
208 s->mv_type = MV_TYPE_16X16; | |
209 if (s->h263_msmpeg4) { | |
242
d1a9663b973b
* continue after error in msmpeg4_decode_mb - helps for some movie samples
kabi
parents:
231
diff
changeset
|
210 if (msmpeg4_decode_mb(s, s->block) < 0) { |
d1a9663b973b
* continue after error in msmpeg4_decode_mb - helps for some movie samples
kabi
parents:
231
diff
changeset
|
211 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x); |
0 | 212 return -1; |
242
d1a9663b973b
* continue after error in msmpeg4_decode_mb - helps for some movie samples
kabi
parents:
231
diff
changeset
|
213 } |
0 | 214 } else { |
161 | 215 if (h263_decode_mb(s, s->block) < 0) { |
216 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x); | |
0 | 217 return -1; |
161 | 218 } |
0 | 219 } |
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
0
diff
changeset
|
220 MPV_decode_mb(s, s->block); |
0 | 221 } |
67 | 222 if (avctx->draw_horiz_band) { |
223 UINT8 *src_ptr[3]; | |
224 int y, h, offset; | |
225 y = s->mb_y * 16; | |
226 h = s->height - y; | |
227 if (h > 16) | |
228 h = 16; | |
229 offset = y * s->linesize; | |
230 src_ptr[0] = s->current_picture[0] + offset; | |
231 src_ptr[1] = s->current_picture[1] + (offset >> 2); | |
232 src_ptr[2] = s->current_picture[2] + (offset >> 2); | |
233 avctx->draw_horiz_band(avctx, src_ptr, s->linesize, | |
234 y, s->width, h); | |
235 } | |
0 | 236 } |
208 | 237 |
251
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
249
diff
changeset
|
238 if (s->h263_msmpeg4 && s->pict_type==I_TYPE) |
208 | 239 if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1; |
0 | 240 |
241 MPV_frame_end(s); | |
242 | |
273
34f40a0fc840
msmpeg4 bugfix (wrong frame displayed if some frames are skipped)
michaelni
parents:
266
diff
changeset
|
243 if(s->pict_type==B_TYPE || (!s->has_b_frames)){ |
262 | 244 pict->data[0] = s->current_picture[0]; |
245 pict->data[1] = s->current_picture[1]; | |
246 pict->data[2] = s->current_picture[2]; | |
247 } else { | |
248 pict->data[0] = s->last_picture[0]; | |
249 pict->data[1] = s->last_picture[1]; | |
250 pict->data[2] = s->last_picture[2]; | |
251 } | |
0 | 252 pict->linesize[0] = s->linesize; |
253 pict->linesize[1] = s->linesize / 2; | |
254 pict->linesize[2] = s->linesize / 2; | |
255 | |
256 avctx->quality = s->qscale; | |
231 | 257 |
258 /* Return the Picture timestamp as the frame number */ | |
259 /* we substract 1 because it is added on utils.c */ | |
260 avctx->frame_number = s->picture_number - 1; | |
261 | |
0 | 262 *data_size = sizeof(AVPicture); |
263 return buf_size; | |
264 } | |
265 | |
67 | 266 AVCodec mpeg4_decoder = { |
267 "mpeg4", | |
0 | 268 CODEC_TYPE_VIDEO, |
67 | 269 CODEC_ID_MPEG4, |
0 | 270 sizeof(MpegEncContext), |
271 h263_decode_init, | |
272 NULL, | |
273 h263_decode_end, | |
274 h263_decode_frame, | |
67 | 275 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 276 }; |
277 | |
278 AVCodec h263_decoder = { | |
279 "h263", | |
280 CODEC_TYPE_VIDEO, | |
281 CODEC_ID_H263, | |
282 sizeof(MpegEncContext), | |
283 h263_decode_init, | |
284 NULL, | |
285 h263_decode_end, | |
286 h263_decode_frame, | |
67 | 287 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 288 }; |
289 | |
290 AVCodec msmpeg4_decoder = { | |
291 "msmpeg4", | |
292 CODEC_TYPE_VIDEO, | |
293 CODEC_ID_MSMPEG4, | |
294 sizeof(MpegEncContext), | |
295 h263_decode_init, | |
296 NULL, | |
297 h263_decode_end, | |
298 h263_decode_frame, | |
67 | 299 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 300 }; |
301 | |
302 AVCodec h263i_decoder = { | |
303 "h263i", | |
304 CODEC_TYPE_VIDEO, | |
305 CODEC_ID_H263I, | |
306 sizeof(MpegEncContext), | |
307 h263_decode_init, | |
308 NULL, | |
309 h263_decode_end, | |
310 h263_decode_frame, | |
67 | 311 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 312 }; |
313 |