d03b9ccf5b3002be4acbaa7ca06c480c8afd135f
[libav.git] / libavcodec / h263dec.c
1 /*
2 * H263 decoder
3 * Copyright (c) 2001 Fabrice Bellard.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "avcodec.h"
20 #include "dsputil.h"
21 #include "mpegvideo.h"
22
23 //#define DEBUG
24 //#define PRINT_FRAME_TIME
25 #ifdef PRINT_FRAME_TIME
26 static inline long long rdtsc()
27 {
28 long long l;
29 asm volatile( "rdtsc\n\t"
30 : "=A" (l)
31 );
32 // printf("%d\n", int(l/1000));
33 return l;
34 }
35 #endif
36
37 static int h263_decode_init(AVCodecContext *avctx)
38 {
39 MpegEncContext *s = avctx->priv_data;
40
41 s->avctx = avctx;
42 s->out_format = FMT_H263;
43
44 s->width = avctx->width;
45 s->height = avctx->height;
46 s->workaround_bugs= avctx->workaround_bugs;
47
48 /* select sub codec */
49 switch(avctx->codec->id) {
50 case CODEC_ID_H263:
51 s->gob_number = 0;
52 s->first_slice_line = 0;
53 break;
54 case CODEC_ID_MPEG4:
55 s->time_increment_bits = 4; /* default value for broken headers */
56 s->h263_pred = 1;
57 s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing
58 break;
59 case CODEC_ID_MSMPEG4V1:
60 s->h263_msmpeg4 = 1;
61 s->h263_pred = 1;
62 s->msmpeg4_version=1;
63 break;
64 case CODEC_ID_MSMPEG4V2:
65 s->h263_msmpeg4 = 1;
66 s->h263_pred = 1;
67 s->msmpeg4_version=2;
68 break;
69 case CODEC_ID_MSMPEG4V3:
70 s->h263_msmpeg4 = 1;
71 s->h263_pred = 1;
72 s->msmpeg4_version=3;
73 break;
74 case CODEC_ID_WMV1:
75 s->h263_msmpeg4 = 1;
76 s->h263_pred = 1;
77 s->msmpeg4_version=4;
78 break;
79 case CODEC_ID_WMV2:
80 s->h263_msmpeg4 = 1;
81 s->h263_pred = 1;
82 s->msmpeg4_version=5;
83 break;
84 case CODEC_ID_H263I:
85 s->h263_intel = 1;
86 break;
87 default:
88 return -1;
89 }
90 s->codec_id= avctx->codec->id;
91 avctx->mbskip_table= s->mbskip_table;
92
93 /* for h263, we allocate the images after having read the header */
94 if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
95 if (MPV_common_init(s) < 0)
96 return -1;
97
98 if (s->h263_msmpeg4)
99 ff_msmpeg4_decode_init(s);
100 else
101 h263_decode_init_vlc(s);
102
103 return 0;
104 }
105
106 static int h263_decode_end(AVCodecContext *avctx)
107 {
108 MpegEncContext *s = avctx->priv_data;
109
110 MPV_common_end(s);
111 return 0;
112 }
113
114 static int h263_decode_frame(AVCodecContext *avctx,
115 void *data, int *data_size,
116 UINT8 *buf, int buf_size)
117 {
118 MpegEncContext *s = avctx->priv_data;
119 int ret;
120 AVPicture *pict = data;
121 #ifdef PRINT_FRAME_TIME
122 uint64_t time= rdtsc();
123 #endif
124 #ifdef DEBUG
125 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
126 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
127 #endif
128
129 s->hurry_up= avctx->hurry_up;
130 s->error_resilience= avctx->error_resilience;
131 s->workaround_bugs= avctx->workaround_bugs;
132 s->flags= avctx->flags;
133
134 /* no supplementary picture */
135 if (buf_size == 0) {
136 *data_size = 0;
137 return 0;
138 }
139
140 if(s->bitstream_buffer_size && buf_size<20){ //divx 5.01+ frame reorder
141 init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
142 }else
143 init_get_bits(&s->gb, buf, buf_size);
144 s->bitstream_buffer_size=0;
145
146 /* let's go :-) */
147 if (s->h263_msmpeg4) {
148 ret = msmpeg4_decode_picture_header(s);
149 } else if (s->h263_pred) {
150 ret = mpeg4_decode_picture_header(s);
151 s->has_b_frames= !s->low_delay;
152 } else if (s->h263_intel) {
153 ret = intel_h263_decode_picture_header(s);
154 } else {
155 ret = h263_decode_picture_header(s);
156 }
157 avctx->has_b_frames= s->has_b_frames;
158
159
160 /* After H263 & mpeg4 header decode we have the height, width,*/
161 /* and other parameters. So then we could init the picture */
162 /* FIXME: By the way H263 decoder is evolving it should have */
163 /* an H263EncContext */
164 if (s->width != avctx->width || s->height != avctx->height) {
165 /* H.263 could change picture size any time */
166 MPV_common_end(s);
167 s->context_initialized=0;
168 }
169 if (!s->context_initialized) {
170 avctx->width = s->width;
171 avctx->height = s->height;
172 avctx->aspect_ratio_info= s->aspect_ratio_info;
173 if (MPV_common_init(s) < 0)
174 return -1;
175 }
176
177 if(ret==FRAME_SKIPED) return buf_size;
178 /* skip if the header was thrashed */
179 if (ret < 0){
180 fprintf(stderr, "header damaged\n");
181 return -1;
182 }
183 /* skip b frames if we dont have reference frames */
184 if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return buf_size;
185 /* skip b frames if we are in a hurry */
186 if(s->hurry_up && s->pict_type==B_TYPE) return buf_size;
187
188 if(s->next_p_frame_damaged){
189 if(s->pict_type==B_TYPE)
190 return buf_size;
191 else
192 s->next_p_frame_damaged=0;
193 }
194
195 MPV_frame_start(s, avctx);
196
197 #ifdef DEBUG
198 printf("qscale=%d\n", s->qscale);
199 #endif
200
201 /* init resync/ error resilience specific variables */
202 s->next_resync_qscale= s->qscale;
203 s->next_resync_gb= s->gb;
204 if(s->resync_marker) s->mb_num_left= 0;
205 else s->mb_num_left= s->mb_num;
206
207 /* decode each macroblock */
208 s->block_wrap[0]=
209 s->block_wrap[1]=
210 s->block_wrap[2]=
211 s->block_wrap[3]= s->mb_width*2 + 2;
212 s->block_wrap[4]=
213 s->block_wrap[5]= s->mb_width + 2;
214 for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
215 /* Check for GOB headers on H.263 */
216 /* FIXME: In the future H.263+ will have intra prediction */
217 /* and we are gonna need another way to detect MPEG4 */
218 if (s->mb_y && !s->h263_pred) {
219 s->first_slice_line = h263_decode_gob_header(s);
220 }
221
222 if(s->msmpeg4_version==1){
223 s->last_dc[0]=
224 s->last_dc[1]=
225 s->last_dc[2]= 128;
226 }
227
228 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
229 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
230
231 s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
232 s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
233 s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
234 s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
235 s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
236 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);
237 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
238 s->block_index[0]+=2;
239 s->block_index[1]+=2;
240 s->block_index[2]+=2;
241 s->block_index[3]+=2;
242 s->block_index[4]++;
243 s->block_index[5]++;
244 #ifdef DEBUG
245 printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
246 #endif
247
248 if(s->resync_marker){
249 if(s->mb_num_left<=0){
250 /* except the first block */
251 if(s->mb_x!=0 || s->mb_y!=0){
252 /* did we miss the next resync marker without noticing an error yet */
253 if(((get_bits_count(&s->gb)+8)&(~7)) != s->next_resync_pos && s->decoding_error==0){
254 fprintf(stderr, "slice end missmatch x:%d y:%d %d %d\n",
255 s->mb_x, s->mb_y, get_bits_count(&s->gb), s->next_resync_pos);
256 ff_conceal_past_errors(s, 1);
257 }
258 }
259 s->qscale= s->next_resync_qscale;
260 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
261 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
262
263 s->gb= s->next_resync_gb;
264 s->resync_mb_x= s->mb_x; //we know that the marker is here cuz mb_num_left was the distance to it
265 s->resync_mb_y= s->mb_y;
266 s->first_slice_line=1;
267
268 if(s->codec_id==CODEC_ID_MPEG4){
269 ff_mpeg4_clean_buffers(s);
270 ff_mpeg4_resync(s);
271 }
272 }
273
274 if( s->resync_mb_x==s->mb_x
275 && s->resync_mb_y==s->mb_y && s->decoding_error!=0){
276 fprintf(stderr, "resynced at %d %d\n", s->mb_x, s->mb_y);
277 s->decoding_error= 0;
278 }
279 }
280
281 //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
282 /* DCT & quantize */
283 if(s->decoding_error!=DECODING_DESYNC){
284 int last_error= s->decoding_error;
285 clear_blocks(s->block[0]);
286
287 s->mv_dir = MV_DIR_FORWARD;
288 s->mv_type = MV_TYPE_16X16;
289 if (s->h263_msmpeg4) {
290 if (msmpeg4_decode_mb(s, s->block) < 0) {
291 fprintf(stderr,"Error at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
292 s->decoding_error=DECODING_DESYNC;
293 }
294 } else {
295 if (h263_decode_mb(s, s->block) < 0) {
296 fprintf(stderr,"Error at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
297 s->decoding_error=DECODING_DESYNC;
298 }
299 }
300
301 if(s->decoding_error!=last_error){
302 ff_conceal_past_errors(s, 0);
303 }
304 }
305
306 /* conceal errors */
307 if( s->decoding_error==DECODING_DESYNC
308 || (s->decoding_error==DECODING_ACDC_LOST && s->mb_intra)){
309 s->mv_dir = MV_DIR_FORWARD;
310 s->mv_type = MV_TYPE_16X16;
311 s->mb_skiped=0;
312 s->mb_intra=0;
313 s->mv[0][0][0]=0; //FIXME this is not optimal
314 s->mv[0][0][1]=0;
315 clear_blocks(s->block[0]);
316 }else if(s->decoding_error && !s->mb_intra){
317 clear_blocks(s->block[0]);
318 }
319 //FIXME remove AC for intra
320
321 MPV_decode_mb(s, s->block);
322
323 s->mb_num_left--;
324 }
325 if ( avctx->draw_horiz_band
326 && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
327 UINT8 *src_ptr[3];
328 int y, h, offset;
329 y = s->mb_y * 16;
330 h = s->height - y;
331 if (h > 16)
332 h = 16;
333 offset = y * s->linesize;
334 if(s->pict_type==B_TYPE || (!s->has_b_frames)){
335 src_ptr[0] = s->current_picture[0] + offset;
336 src_ptr[1] = s->current_picture[1] + (offset >> 2);
337 src_ptr[2] = s->current_picture[2] + (offset >> 2);
338 } else {
339 src_ptr[0] = s->last_picture[0] + offset;
340 src_ptr[1] = s->last_picture[1] + (offset >> 2);
341 src_ptr[2] = s->last_picture[2] + (offset >> 2);
342 }
343 avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
344 y, s->width, h);
345 }
346 }
347
348 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
349 if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
350
351 /* divx 5.01+ bistream reorder stuff */
352 if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0){
353 int current_pos= get_bits_count(&s->gb)>>3;
354
355 if( buf_size - current_pos > 5
356 && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
357 int i;
358 int startcode_found=0;
359 for(i=current_pos; i<buf_size; i++){
360 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
361 startcode_found=1;
362 break;
363 }
364 }
365 if(startcode_found){
366 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
367 s->bitstream_buffer_size= buf_size - current_pos;
368 }
369 }
370 }
371
372 if(s->bitstream_buffer_size==0 && s->error_resilience>0){
373 int left= s->gb.size*8 - get_bits_count(&s->gb);
374 int max_extra=8;
375
376 if(s->codec_id==CODEC_ID_MPEG4) max_extra+=32;
377
378 if(left>max_extra){
379 fprintf(stderr, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
380 if(s->decoding_error==0)
381 ff_conceal_past_errors(s, 1);
382 }
383 if(left<0){
384 fprintf(stderr, "overreading %d bits\n", -left);
385 if(s->decoding_error==0)
386 ff_conceal_past_errors(s, 1);
387 }
388 }
389
390 MPV_frame_end(s);
391 #if 0 //dirty show MVs, we should export the MV tables and write a filter to show them
392 {
393 int mb_y;
394 s->has_b_frames=1;
395 for(mb_y=0; mb_y<s->mb_height; mb_y++){
396 int mb_x;
397 int y= mb_y*16 + 8;
398 for(mb_x=0; mb_x<s->mb_width; mb_x++){
399 int x= mb_x*16 + 8;
400 uint8_t *ptr= s->last_picture[0];
401 int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
402 int mx= (s->motion_val[xy][0]>>1) + x;
403 int my= (s->motion_val[xy][1]>>1) + y;
404 int i;
405 int max;
406
407 if(mx<0) mx=0;
408 if(my<0) my=0;
409 if(mx>=s->width) mx= s->width -1;
410 if(my>=s->height) my= s->height-1;
411 max= ABS(mx-x);
412 if(ABS(my-y) > max) max= ABS(my-y);
413 /* the ugliest linedrawing routine ... */
414 for(i=0; i<max; i++){
415 int x1= x + (mx-x)*i/max;
416 int y1= y + (my-y)*i/max;
417 ptr[y1*s->linesize + x1]+=100;
418 }
419 ptr[y*s->linesize + x]+=100;
420 s->mbskip_table[mb_x + mb_y*s->mb_width]=0;
421 }
422 }
423
424 }
425 #endif
426 if(s->pict_type==B_TYPE || (!s->has_b_frames)){
427 pict->data[0] = s->current_picture[0];
428 pict->data[1] = s->current_picture[1];
429 pict->data[2] = s->current_picture[2];
430 } else {
431 pict->data[0] = s->last_picture[0];
432 pict->data[1] = s->last_picture[1];
433 pict->data[2] = s->last_picture[2];
434 }
435 pict->linesize[0] = s->linesize;
436 pict->linesize[1] = s->uvlinesize;
437 pict->linesize[2] = s->uvlinesize;
438
439 avctx->quality = s->qscale;
440
441 /* Return the Picture timestamp as the frame number */
442 /* we substract 1 because it is added on utils.c */
443 avctx->frame_number = s->picture_number - 1;
444
445 /* dont output the last pic after seeking
446 note we allready added +1 for the current pix in MPV_frame_end(s) */
447 if(s->num_available_buffers>=2 || (!s->has_b_frames))
448 *data_size = sizeof(AVPicture);
449 #ifdef PRINT_FRAME_TIME
450 printf("%Ld\n", rdtsc()-time);
451 #endif
452 return buf_size;
453 }
454
455 AVCodec mpeg4_decoder = {
456 "mpeg4",
457 CODEC_TYPE_VIDEO,
458 CODEC_ID_MPEG4,
459 sizeof(MpegEncContext),
460 h263_decode_init,
461 NULL,
462 h263_decode_end,
463 h263_decode_frame,
464 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
465 };
466
467 AVCodec h263_decoder = {
468 "h263",
469 CODEC_TYPE_VIDEO,
470 CODEC_ID_H263,
471 sizeof(MpegEncContext),
472 h263_decode_init,
473 NULL,
474 h263_decode_end,
475 h263_decode_frame,
476 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
477 };
478
479 AVCodec msmpeg4v1_decoder = {
480 "msmpeg4v1",
481 CODEC_TYPE_VIDEO,
482 CODEC_ID_MSMPEG4V1,
483 sizeof(MpegEncContext),
484 h263_decode_init,
485 NULL,
486 h263_decode_end,
487 h263_decode_frame,
488 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
489 };
490
491 AVCodec msmpeg4v2_decoder = {
492 "msmpeg4v2",
493 CODEC_TYPE_VIDEO,
494 CODEC_ID_MSMPEG4V2,
495 sizeof(MpegEncContext),
496 h263_decode_init,
497 NULL,
498 h263_decode_end,
499 h263_decode_frame,
500 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
501 };
502
503 AVCodec msmpeg4v3_decoder = {
504 "msmpeg4",
505 CODEC_TYPE_VIDEO,
506 CODEC_ID_MSMPEG4V3,
507 sizeof(MpegEncContext),
508 h263_decode_init,
509 NULL,
510 h263_decode_end,
511 h263_decode_frame,
512 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
513 };
514
515 AVCodec wmv1_decoder = {
516 "wmv1",
517 CODEC_TYPE_VIDEO,
518 CODEC_ID_WMV1,
519 sizeof(MpegEncContext),
520 h263_decode_init,
521 NULL,
522 h263_decode_end,
523 h263_decode_frame,
524 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
525 };
526
527 AVCodec wmv2_decoder = {
528 "wmv2",
529 CODEC_TYPE_VIDEO,
530 CODEC_ID_WMV2,
531 sizeof(MpegEncContext),
532 h263_decode_init,
533 NULL,
534 h263_decode_end,
535 h263_decode_frame,
536 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
537 };
538
539 AVCodec h263i_decoder = {
540 "h263i",
541 CODEC_TYPE_VIDEO,
542 CODEC_ID_H263I,
543 sizeof(MpegEncContext),
544 h263_decode_init,
545 NULL,
546 h263_decode_end,
547 h263_decode_frame,
548 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
549 };
550