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