Mercurial > libavcodec.hg
comparison h263dec.c @ 2967:ef2149182f1c libavcodec
COSMETICS: Remove all trailing whitespace.
author | diego |
---|---|
date | Sat, 17 Dec 2005 18:14:38 +0000 |
parents | f3bc39da0cc4 |
children | 383eee00b898 |
comparison
equal
deleted
inserted
replaced
2966:564788471dd4 | 2967:ef2149182f1c |
---|---|
15 * | 15 * |
16 * You should have received a copy of the GNU Lesser General Public | 16 * You should have received a copy of the GNU Lesser General Public |
17 * License along with this library; if not, write to the Free Software | 17 * License along with this library; if not, write to the Free Software |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 */ | 19 */ |
20 | 20 |
21 /** | 21 /** |
22 * @file h263dec.c | 22 * @file h263dec.c |
23 * H.263 decoder. | 23 * H.263 decoder. |
24 */ | 24 */ |
25 | 25 |
26 #include "avcodec.h" | 26 #include "avcodec.h" |
27 #include "dsputil.h" | 27 #include "dsputil.h" |
28 #include "mpegvideo.h" | 28 #include "mpegvideo.h" |
29 | 29 |
30 //#define DEBUG | 30 //#define DEBUG |
107 | 107 |
108 if (s->h263_msmpeg4) | 108 if (s->h263_msmpeg4) |
109 ff_msmpeg4_decode_init(s); | 109 ff_msmpeg4_decode_init(s); |
110 else | 110 else |
111 h263_decode_init_vlc(s); | 111 h263_decode_init_vlc(s); |
112 | 112 |
113 return 0; | 113 return 0; |
114 } | 114 } |
115 | 115 |
116 int ff_h263_decode_end(AVCodecContext *avctx) | 116 int ff_h263_decode_end(AVCodecContext *avctx) |
117 { | 117 { |
124 /** | 124 /** |
125 * returns the number of bytes consumed for building the current frame | 125 * returns the number of bytes consumed for building the current frame |
126 */ | 126 */ |
127 static int get_consumed_bytes(MpegEncContext *s, int buf_size){ | 127 static int get_consumed_bytes(MpegEncContext *s, int buf_size){ |
128 int pos= (get_bits_count(&s->gb)+7)>>3; | 128 int pos= (get_bits_count(&s->gb)+7)>>3; |
129 | 129 |
130 if(s->divx_packed){ | 130 if(s->divx_packed){ |
131 //we would have to scan through the whole buf to handle the weird reordering ... | 131 //we would have to scan through the whole buf to handle the weird reordering ... |
132 return buf_size; | 132 return buf_size; |
133 }else if(s->flags&CODEC_FLAG_TRUNCATED){ | 133 }else if(s->flags&CODEC_FLAG_TRUNCATED){ |
134 pos -= s->parse_context.last_index; | 134 pos -= s->parse_context.last_index; |
135 if(pos<0) pos=0; // padding is not really read so this might be -1 | 135 if(pos<0) pos=0; // padding is not really read so this might be -1 |
136 return pos; | 136 return pos; |
137 }else{ | 137 }else{ |
145 static int decode_slice(MpegEncContext *s){ | 145 static int decode_slice(MpegEncContext *s){ |
146 const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F; | 146 const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F; |
147 const int mb_size= 16>>s->avctx->lowres; | 147 const int mb_size= 16>>s->avctx->lowres; |
148 s->last_resync_gb= s->gb; | 148 s->last_resync_gb= s->gb; |
149 s->first_slice_line= 1; | 149 s->first_slice_line= 1; |
150 | 150 |
151 s->resync_mb_x= s->mb_x; | 151 s->resync_mb_x= s->mb_x; |
152 s->resync_mb_y= s->mb_y; | 152 s->resync_mb_y= s->mb_y; |
153 | 153 |
154 ff_set_qscale(s, s->qscale); | 154 ff_set_qscale(s, s->qscale); |
155 | 155 |
156 if(s->partitioned_frame){ | 156 if(s->partitioned_frame){ |
157 const int qscale= s->qscale; | 157 const int qscale= s->qscale; |
158 | 158 |
159 if(s->codec_id==CODEC_ID_MPEG4){ | 159 if(s->codec_id==CODEC_ID_MPEG4){ |
160 if(ff_mpeg4_decode_partitions(s) < 0) | 160 if(ff_mpeg4_decode_partitions(s) < 0) |
161 return -1; | 161 return -1; |
162 } | 162 } |
163 | 163 |
164 /* restore variables which were modified */ | 164 /* restore variables which were modified */ |
165 s->first_slice_line=1; | 165 s->first_slice_line=1; |
166 s->mb_x= s->resync_mb_x; | 166 s->mb_x= s->resync_mb_x; |
167 s->mb_y= s->resync_mb_y; | 167 s->mb_y= s->resync_mb_y; |
168 ff_set_qscale(s, qscale); | 168 ff_set_qscale(s, qscale); |
175 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END); | 175 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END); |
176 | 176 |
177 return 0; | 177 return 0; |
178 } | 178 } |
179 } | 179 } |
180 | 180 |
181 if(s->msmpeg4_version==1){ | 181 if(s->msmpeg4_version==1){ |
182 s->last_dc[0]= | 182 s->last_dc[0]= |
183 s->last_dc[1]= | 183 s->last_dc[1]= |
184 s->last_dc[2]= 128; | 184 s->last_dc[2]= 128; |
185 } | 185 } |
186 | 186 |
187 ff_init_block_index(s); | 187 ff_init_block_index(s); |
188 for(; s->mb_x < s->mb_width; s->mb_x++) { | 188 for(; s->mb_x < s->mb_width; s->mb_x++) { |
189 int ret; | 189 int ret; |
190 | 190 |
191 ff_update_block_index(s); | 191 ff_update_block_index(s); |
192 | 192 |
193 if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){ | 193 if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){ |
194 s->first_slice_line=0; | 194 s->first_slice_line=0; |
195 } | 195 } |
196 | 196 |
197 /* DCT & quantize */ | 197 /* DCT & quantize */ |
198 | 198 |
199 s->mv_dir = MV_DIR_FORWARD; | 199 s->mv_dir = MV_DIR_FORWARD; |
200 s->mv_type = MV_TYPE_16X16; | 200 s->mv_type = MV_TYPE_16X16; |
201 // s->mb_skipped = 0; | 201 // s->mb_skipped = 0; |
202 //printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24)); | 202 //printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24)); |
203 ret= s->decode_mb(s, s->block); | 203 ret= s->decode_mb(s, s->block); |
214 | 214 |
215 //printf("%d %d %d %06X\n", s->mb_x, s->mb_y, s->gb.size*8 - get_bits_count(&s->gb), show_bits(&s->gb, 24)); | 215 //printf("%d %d %d %06X\n", s->mb_x, s->mb_y, s->gb.size*8 - get_bits_count(&s->gb), show_bits(&s->gb, 24)); |
216 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); | 216 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); |
217 | 217 |
218 s->padding_bug_score--; | 218 s->padding_bug_score--; |
219 | 219 |
220 if(++s->mb_x >= s->mb_width){ | 220 if(++s->mb_x >= s->mb_width){ |
221 s->mb_x=0; | 221 s->mb_x=0; |
222 ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size); | 222 ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size); |
223 s->mb_y++; | 223 s->mb_y++; |
224 } | 224 } |
225 return 0; | 225 return 0; |
226 }else if(ret==SLICE_NOEND){ | 226 }else if(ret==SLICE_NOEND){ |
227 av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy); | 227 av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy); |
228 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x+1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); | 228 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x+1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); |
229 return -1; | 229 return -1; |
230 } | 230 } |
231 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy); | 231 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy); |
232 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask); | 232 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask); |
233 | 233 |
234 return -1; | 234 return -1; |
235 } | 235 } |
236 | 236 |
237 MPV_decode_mb(s, s->block); | 237 MPV_decode_mb(s, s->block); |
238 if(s->loop_filter) | 238 if(s->loop_filter) |
239 ff_h263_loop_filter(s); | 239 ff_h263_loop_filter(s); |
240 } | 240 } |
241 | 241 |
242 ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size); | 242 ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size); |
243 | 243 |
244 s->mb_x= 0; | 244 s->mb_x= 0; |
245 } | 245 } |
246 | 246 |
247 assert(s->mb_x==0 && s->mb_y==s->mb_height); | 247 assert(s->mb_x==0 && s->mb_y==s->mb_height); |
248 | 248 |
249 /* try to detect the padding bug */ | 249 /* try to detect the padding bug */ |
250 if( s->codec_id==CODEC_ID_MPEG4 | 250 if( s->codec_id==CODEC_ID_MPEG4 |
251 && (s->workaround_bugs&FF_BUG_AUTODETECT) | 251 && (s->workaround_bugs&FF_BUG_AUTODETECT) |
252 && s->gb.size_in_bits - get_bits_count(&s->gb) >=0 | 252 && s->gb.size_in_bits - get_bits_count(&s->gb) >=0 |
253 && s->gb.size_in_bits - get_bits_count(&s->gb) < 48 | 253 && s->gb.size_in_bits - get_bits_count(&s->gb) < 48 |
254 // && !s->resync_marker | 254 // && !s->resync_marker |
255 && !s->data_partitioning){ | 255 && !s->data_partitioning){ |
256 | 256 |
257 const int bits_count= get_bits_count(&s->gb); | 257 const int bits_count= get_bits_count(&s->gb); |
258 const int bits_left = s->gb.size_in_bits - bits_count; | 258 const int bits_left = s->gb.size_in_bits - bits_count; |
259 | 259 |
260 if(bits_left==0){ | 260 if(bits_left==0){ |
261 s->padding_bug_score+=16; | 261 s->padding_bug_score+=16; |
262 } else if(bits_left != 1){ | 262 } else if(bits_left != 1){ |
263 int v= show_bits(&s->gb, 8); | 263 int v= show_bits(&s->gb, 8); |
264 v|= 0x7F >> (7-(bits_count&7)); | 264 v|= 0x7F >> (7-(bits_count&7)); |
266 if(v==0x7F && bits_left<=8) | 266 if(v==0x7F && bits_left<=8) |
267 s->padding_bug_score--; | 267 s->padding_bug_score--; |
268 else if(v==0x7F && ((get_bits_count(&s->gb)+8)&8) && bits_left<=16) | 268 else if(v==0x7F && ((get_bits_count(&s->gb)+8)&8) && bits_left<=16) |
269 s->padding_bug_score+= 4; | 269 s->padding_bug_score+= 4; |
270 else | 270 else |
271 s->padding_bug_score++; | 271 s->padding_bug_score++; |
272 } | 272 } |
273 } | 273 } |
274 | 274 |
275 if(s->workaround_bugs&FF_BUG_AUTODETECT){ | 275 if(s->workaround_bugs&FF_BUG_AUTODETECT){ |
276 if(s->padding_bug_score > -2 && !s->data_partitioning /*&& (s->divx_version || !s->resync_marker)*/) | 276 if(s->padding_bug_score > -2 && !s->data_partitioning /*&& (s->divx_version || !s->resync_marker)*/) |
277 s->workaround_bugs |= FF_BUG_NO_PADDING; | 277 s->workaround_bugs |= FF_BUG_NO_PADDING; |
278 else | 278 else |
279 s->workaround_bugs &= ~FF_BUG_NO_PADDING; | 279 s->workaround_bugs &= ~FF_BUG_NO_PADDING; |
281 | 281 |
282 // handle formats which don't have unique end markers | 282 // handle formats which don't have unique end markers |
283 if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly | 283 if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly |
284 int left= s->gb.size_in_bits - get_bits_count(&s->gb); | 284 int left= s->gb.size_in_bits - get_bits_count(&s->gb); |
285 int max_extra=7; | 285 int max_extra=7; |
286 | 286 |
287 /* no markers in M$ crap */ | 287 /* no markers in M$ crap */ |
288 if(s->msmpeg4_version && s->pict_type==I_TYPE) | 288 if(s->msmpeg4_version && s->pict_type==I_TYPE) |
289 max_extra+= 17; | 289 max_extra+= 17; |
290 | 290 |
291 /* buggy padding but the frame should still end approximately at the bitstream end */ | 291 /* buggy padding but the frame should still end approximately at the bitstream end */ |
292 if((s->workaround_bugs&FF_BUG_NO_PADDING) && s->error_resilience>=3) | 292 if((s->workaround_bugs&FF_BUG_NO_PADDING) && s->error_resilience>=3) |
293 max_extra+= 48; | 293 max_extra+= 48; |
294 else if((s->workaround_bugs&FF_BUG_NO_PADDING)) | 294 else if((s->workaround_bugs&FF_BUG_NO_PADDING)) |
295 max_extra+= 256*256*256*64; | 295 max_extra+= 256*256*256*64; |
296 | 296 |
297 if(left>max_extra){ | 297 if(left>max_extra){ |
298 av_log(s->avctx, AV_LOG_ERROR, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24)); | 298 av_log(s->avctx, AV_LOG_ERROR, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24)); |
299 } | 299 } |
300 else if(left<0){ | 300 else if(left<0){ |
301 av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left); | 301 av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left); |
302 }else | 302 }else |
303 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END); | 303 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END); |
304 | 304 |
305 return 0; | 305 return 0; |
306 } | 306 } |
307 | 307 |
308 av_log(s->avctx, AV_LOG_ERROR, "slice end not reached but screenspace end (%d left %06X, score= %d)\n", | 308 av_log(s->avctx, AV_LOG_ERROR, "slice end not reached but screenspace end (%d left %06X, score= %d)\n", |
309 s->gb.size_in_bits - get_bits_count(&s->gb), | 309 s->gb.size_in_bits - get_bits_count(&s->gb), |
310 show_bits(&s->gb, 24), s->padding_bug_score); | 310 show_bits(&s->gb, 24), s->padding_bug_score); |
311 | 311 |
312 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); | 312 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); |
313 | 313 |
314 return -1; | 314 return -1; |
315 } | 315 } |
316 | 316 |
319 * @return the position of the first byte of the next frame, or -1 | 319 * @return the position of the first byte of the next frame, or -1 |
320 */ | 320 */ |
321 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){ | 321 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){ |
322 int vop_found, i; | 322 int vop_found, i; |
323 uint32_t state; | 323 uint32_t state; |
324 | 324 |
325 vop_found= pc->frame_start_found; | 325 vop_found= pc->frame_start_found; |
326 state= pc->state; | 326 state= pc->state; |
327 | 327 |
328 i=0; | 328 i=0; |
329 if(!vop_found){ | 329 if(!vop_found){ |
330 for(i=0; i<buf_size; i++){ | 330 for(i=0; i<buf_size; i++){ |
331 state= (state<<8) | buf[i]; | 331 state= (state<<8) | buf[i]; |
332 if(state == 0x1B6){ | 332 if(state == 0x1B6){ |
343 return 0; | 343 return 0; |
344 for(; i<buf_size; i++){ | 344 for(; i<buf_size; i++){ |
345 state= (state<<8) | buf[i]; | 345 state= (state<<8) | buf[i]; |
346 if((state&0xFFFFFF00) == 0x100){ | 346 if((state&0xFFFFFF00) == 0x100){ |
347 pc->frame_start_found=0; | 347 pc->frame_start_found=0; |
348 pc->state=-1; | 348 pc->state=-1; |
349 return i-3; | 349 return i-3; |
350 } | 350 } |
351 } | 351 } |
352 } | 352 } |
353 pc->frame_start_found= vop_found; | 353 pc->frame_start_found= vop_found; |
356 } | 356 } |
357 | 357 |
358 static int h263_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){ | 358 static int h263_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){ |
359 int vop_found, i; | 359 int vop_found, i; |
360 uint32_t state; | 360 uint32_t state; |
361 | 361 |
362 vop_found= pc->frame_start_found; | 362 vop_found= pc->frame_start_found; |
363 state= pc->state; | 363 state= pc->state; |
364 | 364 |
365 i=0; | 365 i=0; |
366 if(!vop_found){ | 366 if(!vop_found){ |
367 for(i=0; i<buf_size; i++){ | 367 for(i=0; i<buf_size; i++){ |
368 state= (state<<8) | buf[i]; | 368 state= (state<<8) | buf[i]; |
369 if(state>>(32-22) == 0x20){ | 369 if(state>>(32-22) == 0x20){ |
372 break; | 372 break; |
373 } | 373 } |
374 } | 374 } |
375 } | 375 } |
376 | 376 |
377 if(vop_found){ | 377 if(vop_found){ |
378 for(; i<buf_size; i++){ | 378 for(; i<buf_size; i++){ |
379 state= (state<<8) | buf[i]; | 379 state= (state<<8) | buf[i]; |
380 if(state>>(32-22) == 0x20){ | 380 if(state>>(32-22) == 0x20){ |
381 pc->frame_start_found=0; | 381 pc->frame_start_found=0; |
382 pc->state=-1; | 382 pc->state=-1; |
383 return i-3; | 383 return i-3; |
384 } | 384 } |
385 } | 385 } |
386 } | 386 } |
387 pc->frame_start_found= vop_found; | 387 pc->frame_start_found= vop_found; |
388 pc->state= state; | 388 pc->state= state; |
389 | 389 |
390 return END_NOT_FOUND; | 390 return END_NOT_FOUND; |
391 } | 391 } |
392 | 392 |
393 static int h263_parse(AVCodecParserContext *s, | 393 static int h263_parse(AVCodecParserContext *s, |
394 AVCodecContext *avctx, | 394 AVCodecContext *avctx, |
395 uint8_t **poutbuf, int *poutbuf_size, | 395 uint8_t **poutbuf, int *poutbuf_size, |
396 const uint8_t *buf, int buf_size) | 396 const uint8_t *buf, int buf_size) |
397 { | 397 { |
398 ParseContext *pc = s->priv_data; | 398 ParseContext *pc = s->priv_data; |
399 int next; | 399 int next; |
400 | 400 |
401 next= h263_find_frame_end(pc, buf, buf_size); | 401 next= h263_find_frame_end(pc, buf, buf_size); |
402 | 402 |
403 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { | 403 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
404 *poutbuf = NULL; | 404 *poutbuf = NULL; |
405 *poutbuf_size = 0; | 405 *poutbuf_size = 0; |
409 *poutbuf = (uint8_t *)buf; | 409 *poutbuf = (uint8_t *)buf; |
410 *poutbuf_size = buf_size; | 410 *poutbuf_size = buf_size; |
411 return next; | 411 return next; |
412 } | 412 } |
413 | 413 |
414 int ff_h263_decode_frame(AVCodecContext *avctx, | 414 int ff_h263_decode_frame(AVCodecContext *avctx, |
415 void *data, int *data_size, | 415 void *data, int *data_size, |
416 uint8_t *buf, int buf_size) | 416 uint8_t *buf, int buf_size) |
417 { | 417 { |
418 MpegEncContext *s = avctx->priv_data; | 418 MpegEncContext *s = avctx->priv_data; |
419 int ret; | 419 int ret; |
420 AVFrame *pict = data; | 420 AVFrame *pict = data; |
421 | 421 |
422 #ifdef PRINT_FRAME_TIME | 422 #ifdef PRINT_FRAME_TIME |
423 uint64_t time= rdtsc(); | 423 uint64_t time= rdtsc(); |
424 #endif | 424 #endif |
425 #ifdef DEBUG | 425 #ifdef DEBUG |
426 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size); | 426 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size); |
442 return 0; | 442 return 0; |
443 } | 443 } |
444 | 444 |
445 if(s->flags&CODEC_FLAG_TRUNCATED){ | 445 if(s->flags&CODEC_FLAG_TRUNCATED){ |
446 int next; | 446 int next; |
447 | 447 |
448 if(s->codec_id==CODEC_ID_MPEG4){ | 448 if(s->codec_id==CODEC_ID_MPEG4){ |
449 next= ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size); | 449 next= ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size); |
450 }else if(s->codec_id==CODEC_ID_H263){ | 450 }else if(s->codec_id==CODEC_ID_H263){ |
451 next= h263_find_frame_end(&s->parse_context, buf, buf_size); | 451 next= h263_find_frame_end(&s->parse_context, buf, buf_size); |
452 }else{ | 452 }else{ |
453 av_log(s->avctx, AV_LOG_ERROR, "this codec does not support truncated bitstreams\n"); | 453 av_log(s->avctx, AV_LOG_ERROR, "this codec does not support truncated bitstreams\n"); |
454 return -1; | 454 return -1; |
455 } | 455 } |
456 | 456 |
457 if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 ) | 457 if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 ) |
458 return buf_size; | 458 return buf_size; |
459 } | 459 } |
460 | 460 |
461 | 461 |
462 retry: | 462 retry: |
463 | 463 |
464 if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){ //divx 5.01+/xvid frame reorder | 464 if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){ //divx 5.01+/xvid frame reorder |
465 init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8); | 465 init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8); |
466 }else | 466 }else |
467 init_get_bits(&s->gb, buf, buf_size*8); | 467 init_get_bits(&s->gb, buf, buf_size*8); |
468 s->bitstream_buffer_size=0; | 468 s->bitstream_buffer_size=0; |
469 | 469 |
470 if (!s->context_initialized) { | 470 if (!s->context_initialized) { |
471 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix | 471 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix |
472 return -1; | 472 return -1; |
473 } | 473 } |
474 | 474 |
475 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there | 475 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there |
476 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){ | 476 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){ |
477 int i= ff_find_unused_picture(s, 0); | 477 int i= ff_find_unused_picture(s, 0); |
478 s->current_picture_ptr= &s->picture[i]; | 478 s->current_picture_ptr= &s->picture[i]; |
479 } | 479 } |
480 | 480 |
481 /* let's go :-) */ | 481 /* let's go :-) */ |
482 if (s->msmpeg4_version==5) { | 482 if (s->msmpeg4_version==5) { |
483 ret= ff_wmv2_decode_picture_header(s); | 483 ret= ff_wmv2_decode_picture_header(s); |
484 } else if (s->msmpeg4_version) { | 484 } else if (s->msmpeg4_version) { |
485 ret = msmpeg4_decode_picture_header(s); | 485 ret = msmpeg4_decode_picture_header(s); |
486 } else if (s->h263_pred) { | 486 } else if (s->h263_pred) { |
487 if(s->avctx->extradata_size && s->picture_number==0){ | 487 if(s->avctx->extradata_size && s->picture_number==0){ |
488 GetBitContext gb; | 488 GetBitContext gb; |
489 | 489 |
490 init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8); | 490 init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8); |
491 ret = ff_mpeg4_decode_picture_header(s, &gb); | 491 ret = ff_mpeg4_decode_picture_header(s, &gb); |
492 } | 492 } |
493 ret = ff_mpeg4_decode_picture_header(s, &s->gb); | 493 ret = ff_mpeg4_decode_picture_header(s, &s->gb); |
494 | 494 |
499 } else if (s->h263_flv) { | 499 } else if (s->h263_flv) { |
500 ret = flv_h263_decode_picture_header(s); | 500 ret = flv_h263_decode_picture_header(s); |
501 } else { | 501 } else { |
502 ret = h263_decode_picture_header(s); | 502 ret = h263_decode_picture_header(s); |
503 } | 503 } |
504 | 504 |
505 if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_size); | 505 if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_size); |
506 | 506 |
507 /* skip if the header was thrashed */ | 507 /* skip if the header was thrashed */ |
508 if (ret < 0){ | 508 if (ret < 0){ |
509 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n"); | 509 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n"); |
510 return -1; | 510 return -1; |
511 } | 511 } |
512 | 512 |
513 avctx->has_b_frames= !s->low_delay; | 513 avctx->has_b_frames= !s->low_delay; |
514 | 514 |
515 if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){ | 515 if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){ |
516 if(s->avctx->stream_codec_tag == ff_get_fourcc("XVID") || | 516 if(s->avctx->stream_codec_tag == ff_get_fourcc("XVID") || |
517 s->avctx->codec_tag == ff_get_fourcc("XVID") || s->avctx->codec_tag == ff_get_fourcc("XVIX")) | 517 s->avctx->codec_tag == ff_get_fourcc("XVID") || s->avctx->codec_tag == ff_get_fourcc("XVIX")) |
518 s->xvid_build= -1; | 518 s->xvid_build= -1; |
519 #if 0 | 519 #if 0 |
520 if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==1 | 520 if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==1 |
521 && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc | 521 && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc |
522 s->xvid_build= -1; | 522 s->xvid_build= -1; |
523 #endif | 523 #endif |
524 } | 524 } |
525 | 525 |
526 if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){ | 526 if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){ |
527 if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==0) | 527 if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==0) |
528 s->divx_version= 400; //divx 4 | 528 s->divx_version= 400; //divx 4 |
529 } | 529 } |
530 | 530 |
531 if(s->xvid_build && s->divx_version){ | 531 if(s->xvid_build && s->divx_version){ |
532 s->divx_version= | 532 s->divx_version= |
533 s->divx_build= 0; | 533 s->divx_build= 0; |
534 } | 534 } |
535 | 535 |
536 if(s->workaround_bugs&FF_BUG_AUTODETECT){ | 536 if(s->workaround_bugs&FF_BUG_AUTODETECT){ |
537 if(s->avctx->codec_tag == ff_get_fourcc("XVIX")) | 537 if(s->avctx->codec_tag == ff_get_fourcc("XVIX")) |
538 s->workaround_bugs|= FF_BUG_XVID_ILACE; | 538 s->workaround_bugs|= FF_BUG_XVID_ILACE; |
539 | 539 |
540 if(s->avctx->codec_tag == ff_get_fourcc("UMP4")){ | 540 if(s->avctx->codec_tag == ff_get_fourcc("UMP4")){ |
541 s->workaround_bugs|= FF_BUG_UMP4; | 541 s->workaround_bugs|= FF_BUG_UMP4; |
542 } | 542 } |
549 s->workaround_bugs|= FF_BUG_QPEL_CHROMA2; | 549 s->workaround_bugs|= FF_BUG_QPEL_CHROMA2; |
550 } | 550 } |
551 | 551 |
552 if(s->xvid_build && s->xvid_build<=3) | 552 if(s->xvid_build && s->xvid_build<=3) |
553 s->padding_bug_score= 256*256*256*64; | 553 s->padding_bug_score= 256*256*256*64; |
554 | 554 |
555 if(s->xvid_build && s->xvid_build<=1) | 555 if(s->xvid_build && s->xvid_build<=1) |
556 s->workaround_bugs|= FF_BUG_QPEL_CHROMA; | 556 s->workaround_bugs|= FF_BUG_QPEL_CHROMA; |
557 | 557 |
558 if(s->xvid_build && s->xvid_build<=12) | 558 if(s->xvid_build && s->xvid_build<=12) |
559 s->workaround_bugs|= FF_BUG_EDGE; | 559 s->workaround_bugs|= FF_BUG_EDGE; |
566 s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\ | 566 s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\ |
567 s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2; | 567 s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2; |
568 | 568 |
569 if(s->lavc_build && s->lavc_build<4653) | 569 if(s->lavc_build && s->lavc_build<4653) |
570 s->workaround_bugs|= FF_BUG_STD_QPEL; | 570 s->workaround_bugs|= FF_BUG_STD_QPEL; |
571 | 571 |
572 if(s->lavc_build && s->lavc_build<4655) | 572 if(s->lavc_build && s->lavc_build<4655) |
573 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE; | 573 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE; |
574 | 574 |
575 if(s->lavc_build && s->lavc_build<4670){ | 575 if(s->lavc_build && s->lavc_build<4670){ |
576 s->workaround_bugs|= FF_BUG_EDGE; | 576 s->workaround_bugs|= FF_BUG_EDGE; |
577 } | 577 } |
578 | 578 |
579 if(s->lavc_build && s->lavc_build<=4712) | 579 if(s->lavc_build && s->lavc_build<=4712) |
580 s->workaround_bugs|= FF_BUG_DC_CLIP; | 580 s->workaround_bugs|= FF_BUG_DC_CLIP; |
581 | 581 |
582 if(s->divx_version) | 582 if(s->divx_version) |
583 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE; | 583 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE; |
586 s->padding_bug_score= 256*256*256*64; | 586 s->padding_bug_score= 256*256*256*64; |
587 | 587 |
588 if(s->divx_version && s->divx_version<500){ | 588 if(s->divx_version && s->divx_version<500){ |
589 s->workaround_bugs|= FF_BUG_EDGE; | 589 s->workaround_bugs|= FF_BUG_EDGE; |
590 } | 590 } |
591 | 591 |
592 if(s->divx_version) | 592 if(s->divx_version) |
593 s->workaround_bugs|= FF_BUG_HPEL_CHROMA; | 593 s->workaround_bugs|= FF_BUG_HPEL_CHROMA; |
594 #if 0 | 594 #if 0 |
595 if(s->divx_version==500) | 595 if(s->divx_version==500) |
596 s->padding_bug_score= 256*256*256*64; | 596 s->padding_bug_score= 256*256*256*64; |
599 * lets hope this at least works | 599 * lets hope this at least works |
600 */ | 600 */ |
601 if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0 | 601 if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0 |
602 && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0) | 602 && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0) |
603 s->workaround_bugs|= FF_BUG_NO_PADDING; | 603 s->workaround_bugs|= FF_BUG_NO_PADDING; |
604 | 604 |
605 if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok | 605 if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok |
606 s->workaround_bugs|= FF_BUG_NO_PADDING; | 606 s->workaround_bugs|= FF_BUG_NO_PADDING; |
607 #endif | 607 #endif |
608 } | 608 } |
609 | 609 |
610 if(s->workaround_bugs& FF_BUG_STD_QPEL){ | 610 if(s->workaround_bugs& FF_BUG_STD_QPEL){ |
611 SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c) | 611 SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c) |
612 SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c) | 612 SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c) |
613 SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c) | 613 SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c) |
614 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c) | 614 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c) |
622 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c) | 622 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c) |
623 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c) | 623 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c) |
624 } | 624 } |
625 | 625 |
626 if(avctx->debug & FF_DEBUG_BUGS) | 626 if(avctx->debug & FF_DEBUG_BUGS) |
627 av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n", | 627 av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n", |
628 s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build, | 628 s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build, |
629 s->divx_packed ? "p" : ""); | 629 s->divx_packed ? "p" : ""); |
630 | 630 |
631 #if 0 // dump bits per frame / qp / complexity | 631 #if 0 // dump bits per frame / qp / complexity |
632 { | 632 { |
633 static FILE *f=NULL; | 633 static FILE *f=NULL; |
634 if(!f) f=fopen("rate_qp_cplx.txt", "w"); | 634 if(!f) f=fopen("rate_qp_cplx.txt", "w"); |
635 fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale); | 635 fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale); |
645 | 645 |
646 /* After H263 & mpeg4 header decode we have the height, width,*/ | 646 /* After H263 & mpeg4 header decode we have the height, width,*/ |
647 /* and other parameters. So then we could init the picture */ | 647 /* and other parameters. So then we could init the picture */ |
648 /* FIXME: By the way H263 decoder is evolving it should have */ | 648 /* FIXME: By the way H263 decoder is evolving it should have */ |
649 /* an H263EncContext */ | 649 /* an H263EncContext */ |
650 | 650 |
651 if ( s->width != avctx->coded_width | 651 if ( s->width != avctx->coded_width |
652 || s->height != avctx->coded_height) { | 652 || s->height != avctx->coded_height) { |
653 /* H.263 could change picture size any time */ | 653 /* H.263 could change picture size any time */ |
654 ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat | 654 ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat |
655 s->parse_context.buffer=0; | 655 s->parse_context.buffer=0; |
656 MPV_common_end(s); | 656 MPV_common_end(s); |
662 goto retry; | 662 goto retry; |
663 } | 663 } |
664 | 664 |
665 if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)) | 665 if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)) |
666 s->gob_index = ff_h263_get_gob_height(s); | 666 s->gob_index = ff_h263_get_gob_height(s); |
667 | 667 |
668 // for hurry_up==5 | 668 // for hurry_up==5 |
669 s->current_picture.pict_type= s->pict_type; | 669 s->current_picture.pict_type= s->pict_type; |
670 s->current_picture.key_frame= s->pict_type == I_TYPE; | 670 s->current_picture.key_frame= s->pict_type == I_TYPE; |
671 | 671 |
672 /* skip B-frames if we don't have reference frames */ | 672 /* skip B-frames if we don't have reference frames */ |
673 if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return get_consumed_bytes(s, buf_size); | 673 if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return get_consumed_bytes(s, buf_size); |
674 /* skip b frames if we are in a hurry */ | 674 /* skip b frames if we are in a hurry */ |
675 if(avctx->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size); | 675 if(avctx->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size); |
676 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE) | 676 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE) |
677 || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE) | 677 || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE) |
678 || avctx->skip_frame >= AVDISCARD_ALL) | 678 || avctx->skip_frame >= AVDISCARD_ALL) |
679 return get_consumed_bytes(s, buf_size); | 679 return get_consumed_bytes(s, buf_size); |
680 /* skip everything if we are in a hurry>=5 */ | 680 /* skip everything if we are in a hurry>=5 */ |
681 if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size); | 681 if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size); |
682 | 682 |
683 if(s->next_p_frame_damaged){ | 683 if(s->next_p_frame_damaged){ |
684 if(s->pict_type==B_TYPE) | 684 if(s->pict_type==B_TYPE) |
685 return get_consumed_bytes(s, buf_size); | 685 return get_consumed_bytes(s, buf_size); |
686 else | 686 else |
687 s->next_p_frame_damaged=0; | 687 s->next_p_frame_damaged=0; |
693 #ifdef DEBUG | 693 #ifdef DEBUG |
694 printf("qscale=%d\n", s->qscale); | 694 printf("qscale=%d\n", s->qscale); |
695 #endif | 695 #endif |
696 | 696 |
697 ff_er_frame_start(s); | 697 ff_er_frame_start(s); |
698 | 698 |
699 //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type | 699 //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type |
700 //which isnt available before MPV_frame_start() | 700 //which isnt available before MPV_frame_start() |
701 if (s->msmpeg4_version==5){ | 701 if (s->msmpeg4_version==5){ |
702 if(ff_wmv2_decode_secondary_picture_header(s) < 0) | 702 if(ff_wmv2_decode_secondary_picture_header(s) < 0) |
703 return -1; | 703 return -1; |
704 } | 704 } |
705 | 705 |
706 /* decode each macroblock */ | 706 /* decode each macroblock */ |
707 s->mb_x=0; | 707 s->mb_x=0; |
708 s->mb_y=0; | 708 s->mb_y=0; |
709 | 709 |
710 decode_slice(s); | 710 decode_slice(s); |
711 while(s->mb_y<s->mb_height){ | 711 while(s->mb_y<s->mb_height){ |
712 if(s->msmpeg4_version){ | 712 if(s->msmpeg4_version){ |
713 if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits) | 713 if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits) |
714 break; | 714 break; |
715 }else{ | 715 }else{ |
716 if(ff_h263_resync(s)<0) | 716 if(ff_h263_resync(s)<0) |
717 break; | 717 break; |
718 } | 718 } |
719 | 719 |
720 if(s->msmpeg4_version<4 && s->h263_pred) | 720 if(s->msmpeg4_version<4 && s->h263_pred) |
721 ff_mpeg4_clean_buffers(s); | 721 ff_mpeg4_clean_buffers(s); |
722 | 722 |
723 decode_slice(s); | 723 decode_slice(s); |
724 } | 724 } |
725 | 725 |
726 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE) | 726 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE) |
727 if(msmpeg4_decode_ext_header(s, buf_size) < 0){ | 727 if(msmpeg4_decode_ext_header(s, buf_size) < 0){ |
728 s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR; | 728 s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR; |
729 } | 729 } |
730 | 730 |
731 /* divx 5.01+ bistream reorder stuff */ | 731 /* divx 5.01+ bistream reorder stuff */ |
732 if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){ | 732 if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){ |
733 int current_pos= get_bits_count(&s->gb)>>3; | 733 int current_pos= get_bits_count(&s->gb)>>3; |
734 int startcode_found=0; | 734 int startcode_found=0; |
735 | 735 |
736 if(buf_size - current_pos > 5){ | 736 if(buf_size - current_pos > 5){ |
737 int i; | 737 int i; |
738 for(i=current_pos; i<buf_size-3; i++){ | 738 for(i=current_pos; i<buf_size-3; i++){ |
739 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){ | 739 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){ |
740 startcode_found=1; | 740 startcode_found=1; |
747 current_pos=0; | 747 current_pos=0; |
748 } | 748 } |
749 | 749 |
750 if(startcode_found){ | 750 if(startcode_found){ |
751 s->bitstream_buffer= av_fast_realloc( | 751 s->bitstream_buffer= av_fast_realloc( |
752 s->bitstream_buffer, | 752 s->bitstream_buffer, |
753 &s->allocated_bitstream_buffer_size, | 753 &s->allocated_bitstream_buffer_size, |
754 buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE); | 754 buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE); |
755 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos); | 755 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos); |
756 s->bitstream_buffer_size= buf_size - current_pos; | 756 s->bitstream_buffer_size= buf_size - current_pos; |
757 } | 757 } |
758 } | 758 } |