Mercurial > libavcodec.hg
annotate h263dec.c @ 296:c1a8a1b4a24b libavcodec
sizeof(s->block) isnt 64*6*2 anymore bugfix
mpeg12 decoding optimization
author | michaelni |
---|---|
date | Thu, 28 Mar 2002 13:41:04 +0000 |
parents | 1fc96b02142e |
children | 764aeec1320e |
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; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
274
diff
changeset
|
126 avctx->aspect_ratio_info= s->aspect_ratio_info; |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
127 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
|
128 return -1; |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
129 } 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
|
130 /* 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
|
131 MPV_common_end(s); |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
132 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
|
133 return -1; |
0 | 134 } |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
135 |
0 | 136 if (ret < 0) |
137 return -1; | |
138 | |
139 MPV_frame_start(s); | |
140 | |
141 #ifdef DEBUG | |
142 printf("qscale=%d\n", s->qscale); | |
143 #endif | |
144 | |
145 /* decode each macroblock */ | |
266 | 146 s->block_wrap[0]= |
147 s->block_wrap[1]= | |
148 s->block_wrap[2]= | |
149 s->block_wrap[3]= s->mb_width*2 + 2; | |
150 s->block_wrap[4]= | |
151 s->block_wrap[5]= s->mb_width + 2; | |
0 | 152 for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) { |
162 | 153 /* Check for GOB headers on H.263 */ |
154 /* FIXME: In the future H.263+ will have intra prediction */ | |
155 /* and we are gonna need another way to detect MPEG4 */ | |
156 if (s->mb_y && !s->h263_pred) { | |
157 s->first_gob_line = h263_decode_gob_header(s); | |
158 } | |
296 | 159 |
266 | 160 s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1; |
161 s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1); | |
162 s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1; | |
163 s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2); | |
164 s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
165 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 | 166 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) { |
266 | 167 s->block_index[0]+=2; |
168 s->block_index[1]+=2; | |
169 s->block_index[2]+=2; | |
170 s->block_index[3]+=2; | |
171 s->block_index[4]++; | |
172 s->block_index[5]++; | |
0 | 173 #ifdef DEBUG |
174 printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); | |
175 #endif | |
144 | 176 //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x); |
0 | 177 /* DCT & quantize */ |
178 if (s->h263_msmpeg4) { | |
179 msmpeg4_dc_scale(s); | |
180 } else if (s->h263_pred) { | |
181 h263_dc_scale(s); | |
182 } else { | |
183 /* default quantization values */ | |
184 s->y_dc_scale = 8; | |
185 s->c_dc_scale = 8; | |
186 } | |
296 | 187 clear_blocks(s->block[0]); |
188 | |
0 | 189 s->mv_dir = MV_DIR_FORWARD; |
190 s->mv_type = MV_TYPE_16X16; | |
191 if (s->h263_msmpeg4) { | |
242
d1a9663b973b
* continue after error in msmpeg4_decode_mb - helps for some movie samples
kabi
parents:
231
diff
changeset
|
192 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
|
193 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x); |
0 | 194 return -1; |
242
d1a9663b973b
* continue after error in msmpeg4_decode_mb - helps for some movie samples
kabi
parents:
231
diff
changeset
|
195 } |
0 | 196 } else { |
161 | 197 if (h263_decode_mb(s, s->block) < 0) { |
198 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x); | |
0 | 199 return -1; |
161 | 200 } |
0 | 201 } |
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
0
diff
changeset
|
202 MPV_decode_mb(s, s->block); |
0 | 203 } |
67 | 204 if (avctx->draw_horiz_band) { |
205 UINT8 *src_ptr[3]; | |
206 int y, h, offset; | |
207 y = s->mb_y * 16; | |
208 h = s->height - y; | |
209 if (h > 16) | |
210 h = 16; | |
211 offset = y * s->linesize; | |
212 src_ptr[0] = s->current_picture[0] + offset; | |
213 src_ptr[1] = s->current_picture[1] + (offset >> 2); | |
214 src_ptr[2] = s->current_picture[2] + (offset >> 2); | |
215 avctx->draw_horiz_band(avctx, src_ptr, s->linesize, | |
216 y, s->width, h); | |
217 } | |
0 | 218 } |
208 | 219 |
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
|
220 if (s->h263_msmpeg4 && s->pict_type==I_TYPE) |
208 | 221 if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1; |
0 | 222 |
223 MPV_frame_end(s); | |
224 | |
273
34f40a0fc840
msmpeg4 bugfix (wrong frame displayed if some frames are skipped)
michaelni
parents:
266
diff
changeset
|
225 if(s->pict_type==B_TYPE || (!s->has_b_frames)){ |
262 | 226 pict->data[0] = s->current_picture[0]; |
227 pict->data[1] = s->current_picture[1]; | |
228 pict->data[2] = s->current_picture[2]; | |
229 } else { | |
230 pict->data[0] = s->last_picture[0]; | |
231 pict->data[1] = s->last_picture[1]; | |
232 pict->data[2] = s->last_picture[2]; | |
233 } | |
0 | 234 pict->linesize[0] = s->linesize; |
235 pict->linesize[1] = s->linesize / 2; | |
236 pict->linesize[2] = s->linesize / 2; | |
237 | |
238 avctx->quality = s->qscale; | |
231 | 239 |
240 /* Return the Picture timestamp as the frame number */ | |
241 /* we substract 1 because it is added on utils.c */ | |
242 avctx->frame_number = s->picture_number - 1; | |
243 | |
0 | 244 *data_size = sizeof(AVPicture); |
245 return buf_size; | |
246 } | |
247 | |
67 | 248 AVCodec mpeg4_decoder = { |
249 "mpeg4", | |
0 | 250 CODEC_TYPE_VIDEO, |
67 | 251 CODEC_ID_MPEG4, |
0 | 252 sizeof(MpegEncContext), |
253 h263_decode_init, | |
254 NULL, | |
255 h263_decode_end, | |
256 h263_decode_frame, | |
67 | 257 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 258 }; |
259 | |
260 AVCodec h263_decoder = { | |
261 "h263", | |
262 CODEC_TYPE_VIDEO, | |
263 CODEC_ID_H263, | |
264 sizeof(MpegEncContext), | |
265 h263_decode_init, | |
266 NULL, | |
267 h263_decode_end, | |
268 h263_decode_frame, | |
67 | 269 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 270 }; |
271 | |
272 AVCodec msmpeg4_decoder = { | |
273 "msmpeg4", | |
274 CODEC_TYPE_VIDEO, | |
275 CODEC_ID_MSMPEG4, | |
276 sizeof(MpegEncContext), | |
277 h263_decode_init, | |
278 NULL, | |
279 h263_decode_end, | |
280 h263_decode_frame, | |
67 | 281 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 282 }; |
283 | |
284 AVCodec h263i_decoder = { | |
285 "h263i", | |
286 CODEC_TYPE_VIDEO, | |
287 CODEC_ID_H263I, | |
288 sizeof(MpegEncContext), | |
289 h263_decode_init, | |
290 NULL, | |
291 h263_decode_end, | |
292 h263_decode_frame, | |
67 | 293 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 294 }; |
295 |