Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h263dec.c @ 3088635a

History | View | Annotate | Download (25 KB)

1 de6d9b64 Fabrice Bellard
/*
2 983e3246 Michael Niedermayer
 * H.263 decoder
3 406792e7 Diego Biurrun
 * Copyright (c) 2001 Fabrice Bellard
4 8f2ab833 Michael Niedermayer
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 de6d9b64 Fabrice Bellard
 *
6 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9 ff4ec49e Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
12 de6d9b64 Fabrice Bellard
 *
13 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
14 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17 de6d9b64 Fabrice Bellard
 *
18 ff4ec49e Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
19 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
20 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 de6d9b64 Fabrice Bellard
 */
22 115329f1 Diego Biurrun
23 983e3246 Michael Niedermayer
/**
24 ba87f080 Diego Biurrun
 * @file
25 983e3246 Michael Niedermayer
 * H.263 decoder.
26
 */
27 115329f1 Diego Biurrun
28 c6c98d08 Stefano Sabatini
#include "libavutil/cpu.h"
29 40e5d31b Gwenole Beauchesne
#include "internal.h"
30 de6d9b64 Fabrice Bellard
#include "avcodec.h"
31 6000abfa Fabrice Bellard
#include "dsputil.h"
32 de6d9b64 Fabrice Bellard
#include "mpegvideo.h"
33 fc53b6af Michael Niedermayer
#include "h263.h"
34 c33d9814 Diego Biurrun
#include "h263_parser.h"
35 df495dbd Aurelien Jacobs
#include "mpeg4video_parser.h"
36 15025553 Aurelien Jacobs
#include "msmpeg4.h"
37 70e0c871 NVIDIA Corporation
#include "vdpau_internal.h"
38 a0b0d753 Michael Niedermayer
#include "flv.h"
39 ca334dd1 Michael Niedermayer
#include "mpeg4video.h"
40 de6d9b64 Fabrice Bellard
41
//#define DEBUG
42 02734c6a Michael Niedermayer
//#define PRINT_FRAME_TIME
43 de6d9b64 Fabrice Bellard
44 98a6fff9 Zuxy Meng
av_cold int ff_h263_decode_init(AVCodecContext *avctx)
45 de6d9b64 Fabrice Bellard
{
46
    MpegEncContext *s = avctx->priv_data;
47 519c2b6d Fabrice Bellard
48 bf89e6b1 Fabrice Bellard
    s->avctx = avctx;
49 de6d9b64 Fabrice Bellard
    s->out_format = FMT_H263;
50
51 21adafec Michael Niedermayer
    s->width  = avctx->coded_width;
52
    s->height = avctx->coded_height;
53 92ba5ffb Michael Niedermayer
    s->workaround_bugs= avctx->workaround_bugs;
54 de6d9b64 Fabrice Bellard
55 4d2858de Michael Niedermayer
    // set defaults
56 3edcacde Michael Niedermayer
    MPV_decode_defaults(s);
57 4d2858de Michael Niedermayer
    s->quant_precision=5;
58
    s->decode_mb= ff_h263_decode_mb;
59 4e00e76b Michael Niedermayer
    s->low_delay= 1;
60 09a9b45e Michael Niedermayer
    avctx->pix_fmt= avctx->get_format(avctx, avctx->codec->pix_fmts);
61 a573cc27 Michael Niedermayer
    s->unrestricted_mv= 1;
62 4d2858de Michael Niedermayer
63 de6d9b64 Fabrice Bellard
    /* select sub codec */
64
    switch(avctx->codec->id) {
65
    case CODEC_ID_H263:
66 68b94c35 Michael Niedermayer
        s->unrestricted_mv= 0;
67 580a7465 David Conrad
        avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
68 de6d9b64 Fabrice Bellard
        break;
69 bf89e6b1 Fabrice Bellard
    case CODEC_ID_MPEG4:
70 de6d9b64 Fabrice Bellard
        break;
71 84afee34 Michael Niedermayer
    case CODEC_ID_MSMPEG4V1:
72 de6d9b64 Fabrice Bellard
        s->h263_msmpeg4 = 1;
73
        s->h263_pred = 1;
74 84afee34 Michael Niedermayer
        s->msmpeg4_version=1;
75
        break;
76
    case CODEC_ID_MSMPEG4V2:
77
        s->h263_msmpeg4 = 1;
78
        s->h263_pred = 1;
79
        s->msmpeg4_version=2;
80
        break;
81
    case CODEC_ID_MSMPEG4V3:
82
        s->h263_msmpeg4 = 1;
83
        s->h263_pred = 1;
84
        s->msmpeg4_version=3;
85 de6d9b64 Fabrice Bellard
        break;
86 e1a9dbff Michael Niedermayer
    case CODEC_ID_WMV1:
87
        s->h263_msmpeg4 = 1;
88
        s->h263_pred = 1;
89
        s->msmpeg4_version=4;
90
        break;
91 8f8402e4 Michael Niedermayer
    case CODEC_ID_WMV2:
92
        s->h263_msmpeg4 = 1;
93
        s->h263_pred = 1;
94
        s->msmpeg4_version=5;
95
        break;
96 c6bb50e0 Reimar Döffinger
    case CODEC_ID_VC1:
97 0d33db8a anonymous
    case CODEC_ID_WMV3:
98
        s->h263_msmpeg4 = 1;
99
        s->h263_pred = 1;
100
        s->msmpeg4_version=6;
101 580a7465 David Conrad
        avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
102 0d33db8a anonymous
        break;
103 de6d9b64 Fabrice Bellard
    case CODEC_ID_H263I:
104
        break;
105 d4f5d74a Garrick Meeker
    case CODEC_ID_FLV1:
106
        s->h263_flv = 1;
107
        break;
108 de6d9b64 Fabrice Bellard
    default:
109
        return -1;
110
    }
111 d7e9533a Michael Niedermayer
    s->codec_id= avctx->codec->id;
112 a05aa821 Michael Niedermayer
    avctx->hwaccel= ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
113 93a21abd Michael Niedermayer
114 de6d9b64 Fabrice Bellard
    /* for h263, we allocate the images after having read the header */
115 488fa19a Michael Niedermayer
    if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
116 4949028f Juanjo
        if (MPV_common_init(s) < 0)
117
            return -1;
118 de6d9b64 Fabrice Bellard
119
        h263_decode_init_vlc(s);
120 115329f1 Diego Biurrun
121 de6d9b64 Fabrice Bellard
    return 0;
122
}
123
124 98a6fff9 Zuxy Meng
av_cold int ff_h263_decode_end(AVCodecContext *avctx)
125 de6d9b64 Fabrice Bellard
{
126
    MpegEncContext *s = avctx->priv_data;
127
128
    MPV_common_end(s);
129
    return 0;
130
}
131
132 7866eeff Michael Niedermayer
/**
133 bb628dae Diego Biurrun
 * returns the number of bytes consumed for building the current frame
134 7866eeff Michael Niedermayer
 */
135
static int get_consumed_bytes(MpegEncContext *s, int buf_size){
136
    int pos= (get_bits_count(&s->gb)+7)>>3;
137 115329f1 Diego Biurrun
138 c0aec489 Gwenole Beauchesne
    if(s->divx_packed || s->avctx->hwaccel){
139 7866eeff Michael Niedermayer
        //we would have to scan through the whole buf to handle the weird reordering ...
140 115329f1 Diego Biurrun
        return buf_size;
141 d7425f59 Michael Niedermayer
    }else if(s->flags&CODEC_FLAG_TRUNCATED){
142
        pos -= s->parse_context.last_index;
143
        if(pos<0) pos=0; // padding is not really read so this might be -1
144
        return pos;
145 7866eeff Michael Niedermayer
    }else{
146 755bfeab Diego Biurrun
        if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
147 77277835 Michael Niedermayer
        if(pos+10>buf_size) pos=buf_size; // oops ;)
148 7866eeff Michael Niedermayer
149
        return pos;
150
    }
151
}
152
153 4d2858de Michael Niedermayer
static int decode_slice(MpegEncContext *s){
154 46b4feec Michael Niedermayer
    const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
155 af2a9e8f Michael Niedermayer
    const int mb_size= 16>>s->avctx->lowres;
156 4d2858de Michael Niedermayer
    s->last_resync_gb= s->gb;
157
    s->first_slice_line= 1;
158 115329f1 Diego Biurrun
159 4d2858de Michael Niedermayer
    s->resync_mb_x= s->mb_x;
160
    s->resync_mb_y= s->mb_y;
161
162 554daa24 Michael Niedermayer
    ff_set_qscale(s, s->qscale);
163 115329f1 Diego Biurrun
164 1468d503 Gwenole Beauchesne
    if (s->avctx->hwaccel) {
165
        const uint8_t *start= s->gb.buffer + get_bits_count(&s->gb)/8;
166
        const uint8_t *end  = ff_h263_find_resync_marker(start + 1, s->gb.buffer_end);
167
        skip_bits_long(&s->gb, 8*(end - start));
168
        return s->avctx->hwaccel->decode_slice(s->avctx, start, end - start);
169
    }
170 40e5d31b Gwenole Beauchesne
171 4d2858de Michael Niedermayer
    if(s->partitioned_frame){
172
        const int qscale= s->qscale;
173
174 51ce2207 Michael Niedermayer
        if(CONFIG_MPEG4_DECODER && s->codec_id==CODEC_ID_MPEG4){
175 4d2858de Michael Niedermayer
            if(ff_mpeg4_decode_partitions(s) < 0)
176 115329f1 Diego Biurrun
                return -1;
177 4d2858de Michael Niedermayer
        }
178 115329f1 Diego Biurrun
179 d99ce8d7 Fabrice Bellard
        /* restore variables which were modified */
180 4d2858de Michael Niedermayer
        s->first_slice_line=1;
181
        s->mb_x= s->resync_mb_x;
182
        s->mb_y= s->resync_mb_y;
183 554daa24 Michael Niedermayer
        ff_set_qscale(s, qscale);
184 4d2858de Michael Niedermayer
    }
185
186
    for(; s->mb_y < s->mb_height; s->mb_y++) {
187
        /* per-row end of slice checks */
188
        if(s->msmpeg4_version){
189
            if(s->resync_mb_y + s->slice_height == s->mb_y){
190 46b4feec Michael Niedermayer
                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);
191
192 4d2858de Michael Niedermayer
                return 0;
193
            }
194
        }
195 115329f1 Diego Biurrun
196 4d2858de Michael Niedermayer
        if(s->msmpeg4_version==1){
197
            s->last_dc[0]=
198
            s->last_dc[1]=
199
            s->last_dc[2]= 128;
200
        }
201 115329f1 Diego Biurrun
202 4d2858de Michael Niedermayer
        ff_init_block_index(s);
203
        for(; s->mb_x < s->mb_width; s->mb_x++) {
204
            int ret;
205
206
            ff_update_block_index(s);
207
208
            if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
209 115329f1 Diego Biurrun
                s->first_slice_line=0;
210 4d2858de Michael Niedermayer
            }
211
212
            /* DCT & quantize */
213 115329f1 Diego Biurrun
214 4d2858de Michael Niedermayer
            s->mv_dir = MV_DIR_FORWARD;
215
            s->mv_type = MV_TYPE_16X16;
216 160d679c Mike Melanson
//            s->mb_skipped = 0;
217 4d2858de Michael Niedermayer
//printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
218
            ret= s->decode_mb(s, s->block);
219
220 9701840b Aurelien Jacobs
            if (s->pict_type!=FF_B_TYPE)
221 7d1c3fc1 Michael Niedermayer
                ff_h263_update_motion_val(s);
222
223 4d2858de Michael Niedermayer
            if(ret<0){
224 7bc9090a Michael Niedermayer
                const int xy= s->mb_x + s->mb_y*s->mb_stride;
225 4d2858de Michael Niedermayer
                if(ret==SLICE_END){
226 aef95c8c Michael Niedermayer
                    MPV_decode_mb(s, s->block);
227 332f9ac4 Michael Niedermayer
                    if(s->loop_filter)
228
                        ff_h263_loop_filter(s);
229 aef95c8c Michael Niedermayer
230 35aa136a Michael Niedermayer
//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));
231 46b4feec Michael Niedermayer
                    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);
232 a61f8265 Michael Niedermayer
233
                    s->padding_bug_score--;
234 115329f1 Diego Biurrun
235 4d2858de Michael Niedermayer
                    if(++s->mb_x >= s->mb_width){
236
                        s->mb_x=0;
237 af2a9e8f Michael Niedermayer
                        ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
238 4d2858de Michael Niedermayer
                        s->mb_y++;
239
                    }
240 115329f1 Diego Biurrun
                    return 0;
241 4d2858de Michael Niedermayer
                }else if(ret==SLICE_NOEND){
242 9b879566 Michel Bardiaux
                    av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
243 46b4feec Michael Niedermayer
                    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);
244 4d2858de Michael Niedermayer
                    return -1;
245
                }
246 9b879566 Michel Bardiaux
                av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
247 46b4feec Michael Niedermayer
                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);
248 115329f1 Diego Biurrun
249 4d2858de Michael Niedermayer
                return -1;
250
            }
251 aef95c8c Michael Niedermayer
252
            MPV_decode_mb(s, s->block);
253 332f9ac4 Michael Niedermayer
            if(s->loop_filter)
254
                ff_h263_loop_filter(s);
255 4d2858de Michael Niedermayer
        }
256 115329f1 Diego Biurrun
257 af2a9e8f Michael Niedermayer
        ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
258 115329f1 Diego Biurrun
259 4d2858de Michael Niedermayer
        s->mb_x= 0;
260
    }
261 115329f1 Diego Biurrun
262 4d2858de Michael Niedermayer
    assert(s->mb_x==0 && s->mb_y==s->mb_height);
263
264 df59c9c0 Michael Niedermayer
    if(s->codec_id==CODEC_ID_MPEG4
265
       && (s->workaround_bugs&FF_BUG_AUTODETECT)
266
       && get_bits_left(&s->gb) >= 48
267
       && show_bits(&s->gb, 24)==0x4010
268
       && !s->data_partitioning)
269
        s->padding_bug_score+=32;
270
271 a61f8265 Michael Niedermayer
    /* try to detect the padding bug */
272
    if(      s->codec_id==CODEC_ID_MPEG4
273 115329f1 Diego Biurrun
       &&   (s->workaround_bugs&FF_BUG_AUTODETECT)
274 6e44ba15 Ronald S. Bultje
       &&    get_bits_left(&s->gb) >=0
275
       &&    get_bits_left(&s->gb) < 48
276 96d9cb22 Michael Niedermayer
//       &&   !s->resync_marker
277 a61f8265 Michael Niedermayer
       &&   !s->data_partitioning){
278 115329f1 Diego Biurrun
279 a61f8265 Michael Niedermayer
        const int bits_count= get_bits_count(&s->gb);
280 68f593b4 Michael Niedermayer
        const int bits_left = s->gb.size_in_bits - bits_count;
281 115329f1 Diego Biurrun
282 650faac6 Michael Niedermayer
        if(bits_left==0){
283
            s->padding_bug_score+=16;
284 8671359b Michael Niedermayer
        } else if(bits_left != 1){
285 a61f8265 Michael Niedermayer
            int v= show_bits(&s->gb, 8);
286
            v|= 0x7F >> (7-(bits_count&7));
287 8671359b Michael Niedermayer
288 4940ed6a Michael Niedermayer
            if(v==0x7F && bits_left<=8)
289 a61f8265 Michael Niedermayer
                s->padding_bug_score--;
290 4940ed6a Michael Niedermayer
            else if(v==0x7F && ((get_bits_count(&s->gb)+8)&8) && bits_left<=16)
291
                s->padding_bug_score+= 4;
292 a61f8265 Michael Niedermayer
            else
293 115329f1 Diego Biurrun
                s->padding_bug_score++;
294
        }
295 a61f8265 Michael Niedermayer
    }
296 115329f1 Diego Biurrun
297 4940ed6a Michael Niedermayer
    if(s->workaround_bugs&FF_BUG_AUTODETECT){
298 b317567c Michael Niedermayer
        if(s->padding_bug_score > -2 && !s->data_partitioning /*&& (s->divx_version>=0 || !s->resync_marker)*/)
299 4940ed6a Michael Niedermayer
            s->workaround_bugs |=  FF_BUG_NO_PADDING;
300
        else
301
            s->workaround_bugs &= ~FF_BUG_NO_PADDING;
302
    }
303 a61f8265 Michael Niedermayer
304 bb628dae Diego Biurrun
    // handle formats which don't have unique end markers
305 cb7c98d6 Michael Niedermayer
    if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
306 6e44ba15 Ronald S. Bultje
        int left= get_bits_left(&s->gb);
307 4d2858de Michael Niedermayer
        int max_extra=7;
308 115329f1 Diego Biurrun
309 4d2858de Michael Niedermayer
        /* no markers in M$ crap */
310 9701840b Aurelien Jacobs
        if(s->msmpeg4_version && s->pict_type==FF_I_TYPE)
311 4d2858de Michael Niedermayer
            max_extra+= 17;
312 115329f1 Diego Biurrun
313 4d2858de Michael Niedermayer
        /* buggy padding but the frame should still end approximately at the bitstream end */
314 047599a4 Michael Niedermayer
        if((s->workaround_bugs&FF_BUG_NO_PADDING) && s->error_recognition>=3)
315 4d2858de Michael Niedermayer
            max_extra+= 48;
316 cb7c98d6 Michael Niedermayer
        else if((s->workaround_bugs&FF_BUG_NO_PADDING))
317 4d2858de Michael Niedermayer
            max_extra+= 256*256*256*64;
318 115329f1 Diego Biurrun
319 4d2858de Michael Niedermayer
        if(left>max_extra){
320 9b879566 Michel Bardiaux
            av_log(s->avctx, AV_LOG_ERROR, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
321 4d2858de Michael Niedermayer
        }
322
        else if(left<0){
323 9b879566 Michel Bardiaux
            av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
324 4d2858de Michael Niedermayer
        }else
325 46b4feec Michael Niedermayer
            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);
326 115329f1 Diego Biurrun
327 4d2858de Michael Niedermayer
        return 0;
328
    }
329 a61f8265 Michael Niedermayer
330 115329f1 Diego Biurrun
    av_log(s->avctx, AV_LOG_ERROR, "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
331 6e44ba15 Ronald S. Bultje
            get_bits_left(&s->gb),
332 9b6a5b87 Michael Niedermayer
            show_bits(&s->gb, 24), s->padding_bug_score);
333 115329f1 Diego Biurrun
334 46b4feec Michael Niedermayer
    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);
335
336 4d2858de Michael Niedermayer
    return -1;
337
}
338
339 115329f1 Diego Biurrun
int ff_h263_decode_frame(AVCodecContext *avctx,
340 de6d9b64 Fabrice Bellard
                             void *data, int *data_size,
341 7a00bbad Thilo Borgmann
                             AVPacket *avpkt)
342 de6d9b64 Fabrice Bellard
{
343 7a00bbad Thilo Borgmann
    const uint8_t *buf = avpkt->data;
344
    int buf_size = avpkt->size;
345 de6d9b64 Fabrice Bellard
    MpegEncContext *s = avctx->priv_data;
346 1df1df0b Fabrice Bellard
    int ret;
347 115329f1 Diego Biurrun
    AVFrame *pict = data;
348
349 02734c6a Michael Niedermayer
#ifdef PRINT_FRAME_TIME
350
uint64_t time= rdtsc();
351
#endif
352 b50eef3a Michael Niedermayer
    s->flags= avctx->flags;
353 303e50e6 Michael Niedermayer
    s->flags2= avctx->flags2;
354 6733417a Michael Niedermayer
355 e9174ba4 Michael Niedermayer
    /* no supplementary picture */
356 de6d9b64 Fabrice Bellard
    if (buf_size == 0) {
357 e9174ba4 Michael Niedermayer
        /* special case for last picture */
358
        if (s->low_delay==0 && s->next_picture_ptr) {
359
            *pict= *(AVFrame*)s->next_picture_ptr;
360
            s->next_picture_ptr= NULL;
361
362
            *data_size = sizeof(AVFrame);
363
        }
364
365 de6d9b64 Fabrice Bellard
        return 0;
366
    }
367 b7a22d84 Michael Niedermayer
368 d7425f59 Michael Niedermayer
    if(s->flags&CODEC_FLAG_TRUNCATED){
369
        int next;
370 115329f1 Diego Biurrun
371 49fb20cb Aurelien Jacobs
        if(CONFIG_MPEG4_DECODER && s->codec_id==CODEC_ID_MPEG4){
372 e4cb187d Michael Niedermayer
            next= ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
373 49fb20cb Aurelien Jacobs
        }else if(CONFIG_H263_DECODER && s->codec_id==CODEC_ID_H263){
374 c33d9814 Diego Biurrun
            next= ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
375 d7425f59 Michael Niedermayer
        }else{
376 160d679c Mike Melanson
            av_log(s->avctx, AV_LOG_ERROR, "this codec does not support truncated bitstreams\n");
377 d7425f59 Michael Niedermayer
            return -1;
378
        }
379 115329f1 Diego Biurrun
380 c53d2d90 Aurelien Jacobs
        if( ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
381 d7425f59 Michael Niedermayer
            return buf_size;
382
    }
383 de6d9b64 Fabrice Bellard
384 115329f1 Diego Biurrun
385 4a3d7fbc Michael Niedermayer
retry:
386 1b4580d1 Michael Niedermayer
    if(s->divx_packed && s->xvid_build>=0 && s->bitstream_buffer_size){
387
        int i;
388
        for(i=0; i<buf_size-3; i++){
389
            if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1){
390 3088635a Michael Niedermayer
                if(buf[i+3]==0xB0){
391
                    av_log(s->avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n");
392 1b4580d1 Michael Niedermayer
                    s->bitstream_buffer_size=0;
393 3088635a Michael Niedermayer
                }
394 1b4580d1 Michael Niedermayer
                break;
395
            }
396
        }
397
    }
398 115329f1 Diego Biurrun
399 fb204d4b Michael Niedermayer
    if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){ //divx 5.01+/xvid frame reorder
400 68f593b4 Michael Niedermayer
        init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
401 51b8fd19 Michael Niedermayer
    }else
402 68f593b4 Michael Niedermayer
        init_get_bits(&s->gb, buf, buf_size*8);
403 4136dd24 Michael Niedermayer
    s->bitstream_buffer_size=0;
404 de6d9b64 Fabrice Bellard
405 3a3d3a42 Michael Niedermayer
    if (!s->context_initialized) {
406 46fd0de8 Michael Niedermayer
        if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
407 3a3d3a42 Michael Niedermayer
            return -1;
408
    }
409 115329f1 Diego Biurrun
410 755bfeab Diego Biurrun
    /* We need to set current_picture_ptr before reading the header,
411
     * otherwise we cannot store anyting in there */
412 5f194811 Michael Niedermayer
    if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
413
        int i= ff_find_unused_picture(s, 0);
414
        s->current_picture_ptr= &s->picture[i];
415
    }
416 115329f1 Diego Biurrun
417 de6d9b64 Fabrice Bellard
    /* let's go :-) */
418 49fb20cb Aurelien Jacobs
    if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5) {
419 1457ab52 Michael Niedermayer
        ret= ff_wmv2_decode_picture_header(s);
420 49fb20cb Aurelien Jacobs
    } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
421 de6d9b64 Fabrice Bellard
        ret = msmpeg4_decode_picture_header(s);
422 0b60f3bd Michael Niedermayer
    } else if (CONFIG_MPEG4_DECODER && s->h263_pred) {
423 b704e742 Michael Niedermayer
        if(s->avctx->extradata_size && s->picture_number==0){
424
            GetBitContext gb;
425 115329f1 Diego Biurrun
426 68f593b4 Michael Niedermayer
            init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
427 b704e742 Michael Niedermayer
            ret = ff_mpeg4_decode_picture_header(s, &gb);
428
        }
429
        ret = ff_mpeg4_decode_picture_header(s, &s->gb);
430 0b60f3bd Michael Niedermayer
    } else if (CONFIG_H263I_DECODER && s->codec_id == CODEC_ID_H263I) {
431 d7525cba Michael Niedermayer
        ret = ff_intel_h263_decode_picture_header(s);
432 a0b0d753 Michael Niedermayer
    } else if (CONFIG_FLV_DECODER && s->h263_flv) {
433
        ret = ff_flv_decode_picture_header(s);
434 de6d9b64 Fabrice Bellard
    } else {
435
        ret = h263_decode_picture_header(s);
436 488fa19a Michael Niedermayer
    }
437 115329f1 Diego Biurrun
438 160d679c Mike Melanson
    if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_size);
439 ffba1dc0 Michael Niedermayer
440
    /* skip if the header was thrashed */
441
    if (ret < 0){
442 9b879566 Michel Bardiaux
        av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
443 ffba1dc0 Michael Niedermayer
        return -1;
444
    }
445 115329f1 Diego Biurrun
446 4e00e76b Michael Niedermayer
    avctx->has_b_frames= !s->low_delay;
447 115329f1 Diego Biurrun
448 b317567c Michael Niedermayer
    if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
449 2bb6eba2 Aurelien Jacobs
        if(s->stream_codec_tag == AV_RL32("XVID") ||
450
           s->codec_tag == AV_RL32("XVID") || s->codec_tag == AV_RL32("XVIX") ||
451 d3752b15 Michael Niedermayer
           s->codec_tag == AV_RL32("RMP4") ||
452
           s->codec_tag == AV_RL32("SIPP")
453
           )
454 b317567c Michael Niedermayer
            s->xvid_build= 0;
455 541ae140 Michael Niedermayer
#if 0
456 2bb6eba2 Aurelien Jacobs
        if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
457 115329f1 Diego Biurrun
           && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
458 b317567c Michael Niedermayer
            s->xvid_build= 0;
459 541ae140 Michael Niedermayer
#endif
460
    }
461 9b6a5b87 Michael Niedermayer
462 b317567c Michael Niedermayer
    if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
463 2bb6eba2 Aurelien Jacobs
        if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
464 9b6a5b87 Michael Niedermayer
            s->divx_version= 400; //divx 4
465
    }
466 115329f1 Diego Biurrun
467 b317567c Michael Niedermayer
    if(s->xvid_build>=0 && s->divx_version>=0){
468 45cb4767 Michael Niedermayer
        s->divx_version=
469 b317567c Michael Niedermayer
        s->divx_build= -1;
470 45cb4767 Michael Niedermayer
    }
471 4d2858de Michael Niedermayer
472
    if(s->workaround_bugs&FF_BUG_AUTODETECT){
473 2bb6eba2 Aurelien Jacobs
        if(s->codec_tag == AV_RL32("XVIX"))
474 4d2858de Michael Niedermayer
            s->workaround_bugs|= FF_BUG_XVID_ILACE;
475 9b6a5b87 Michael Niedermayer
476 2bb6eba2 Aurelien Jacobs
        if(s->codec_tag == AV_RL32("UMP4")){
477 4d2858de Michael Niedermayer
            s->workaround_bugs|= FF_BUG_UMP4;
478
        }
479 03e93d35 Michael Niedermayer
480 58d1dc0e Lord
        if(s->divx_version>=500 && s->divx_build<1814){
481 03e93d35 Michael Niedermayer
            s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
482
        }
483 7da71a5c Michael Niedermayer
484 58d1dc0e Lord
        if(s->divx_version>502 && s->divx_build<1814){
485 36df8805 Michael Niedermayer
            s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
486
        }
487
488 b317567c Michael Niedermayer
        if(s->xvid_build<=3U)
489 a6a494fb Michael Niedermayer
            s->padding_bug_score= 256*256*256*64;
490 115329f1 Diego Biurrun
491 b317567c Michael Niedermayer
        if(s->xvid_build<=1U)
492 7da71a5c Michael Niedermayer
            s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
493
494 b317567c Michael Niedermayer
        if(s->xvid_build<=12U)
495 9b6a5b87 Michael Niedermayer
            s->workaround_bugs|= FF_BUG_EDGE;
496
497 b317567c Michael Niedermayer
        if(s->xvid_build<=32U)
498 4ccde216 Michael Niedermayer
            s->workaround_bugs|= FF_BUG_DC_CLIP;
499
500 db794953 Michael Niedermayer
#define SET_QPEL_FUNC(postfix1, postfix2) \
501
    s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
502
    s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
503
    s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
504
505 b317567c Michael Niedermayer
        if(s->lavc_build<4653U)
506 db794953 Michael Niedermayer
            s->workaround_bugs|= FF_BUG_STD_QPEL;
507 115329f1 Diego Biurrun
508 b317567c Michael Niedermayer
        if(s->lavc_build<4655U)
509 c40c3482 Michael Niedermayer
            s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
510
511 b317567c Michael Niedermayer
        if(s->lavc_build<4670U){
512 6674a126 Michael Niedermayer
            s->workaround_bugs|= FF_BUG_EDGE;
513
        }
514 115329f1 Diego Biurrun
515 b317567c Michael Niedermayer
        if(s->lavc_build<=4712U)
516 4ccde216 Michael Niedermayer
            s->workaround_bugs|= FF_BUG_DC_CLIP;
517 6674a126 Michael Niedermayer
518 b317567c Michael Niedermayer
        if(s->divx_version>=0)
519 c40c3482 Michael Niedermayer
            s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
520 a61f8265 Michael Niedermayer
//printf("padding_bug_score: %d\n", s->padding_bug_score);
521 d671e2fe Michael Niedermayer
        if(s->divx_version==501 && s->divx_build==20020416)
522
            s->padding_bug_score= 256*256*256*64;
523 1c6dcb0f Michael Niedermayer
524 b317567c Michael Niedermayer
        if(s->divx_version<500U){
525 1c6dcb0f Michael Niedermayer
            s->workaround_bugs|= FF_BUG_EDGE;
526
        }
527 115329f1 Diego Biurrun
528 b317567c Michael Niedermayer
        if(s->divx_version>=0)
529 b44bdf7e Michael Niedermayer
            s->workaround_bugs|= FF_BUG_HPEL_CHROMA;
530 a61f8265 Michael Niedermayer
#if 0
531 4d2858de Michael Niedermayer
        if(s->divx_version==500)
532 d671e2fe Michael Niedermayer
            s->padding_bug_score= 256*256*256*64;
533 4d2858de Michael Niedermayer

534
        /* very ugly XVID padding bug detection FIXME/XXX solve this differently
535 ca74c0a1 Diego Biurrun
         * Let us hope this at least works.
536 4d2858de Michael Niedermayer
         */
537 b317567c Michael Niedermayer
        if(   s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==-1
538 4d2858de Michael Niedermayer
           && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
539
            s->workaround_bugs|= FF_BUG_NO_PADDING;
540 115329f1 Diego Biurrun

541 b317567c Michael Niedermayer
        if(s->lavc_build<4609U) //FIXME not sure about the version num but a 4609 file seems ok
542 4d2858de Michael Niedermayer
            s->workaround_bugs|= FF_BUG_NO_PADDING;
543 a61f8265 Michael Niedermayer
#endif
544 4d2858de Michael Niedermayer
    }
545 115329f1 Diego Biurrun
546 db794953 Michael Niedermayer
    if(s->workaround_bugs& FF_BUG_STD_QPEL){
547
        SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
548
        SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
549
        SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
550
        SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
551
        SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
552
        SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
553
554
        SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
555
        SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
556
        SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
557
        SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
558
        SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
559
        SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
560
    }
561 4d2858de Michael Niedermayer
562 8fc6c2b5 Michael Niedermayer
    if(avctx->debug & FF_DEBUG_BUGS)
563 115329f1 Diego Biurrun
        av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
564 8fc6c2b5 Michael Niedermayer
               s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
565
               s->divx_packed ? "p" : "");
566 115329f1 Diego Biurrun
567 38274ea2 Michael Niedermayer
#if 0 // dump bits per frame / qp / complexity
568
{
569
    static FILE *f=NULL;
570
    if(!f) f=fopen("rate_qp_cplx.txt", "w");
571 32b90921 Michael Niedermayer
    fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
572 38274ea2 Michael Niedermayer
}
573
#endif
574 b442a92f Alex Beregszaszi
575 b250f9c6 Aurelien Jacobs
#if HAVE_MMX
576 c6c98d08 Stefano Sabatini
    if (s->codec_id == CODEC_ID_MPEG4 && s->xvid_build>=0 && avctx->idct_algo == FF_IDCT_AUTO && (av_get_cpu_flags() & AV_CPU_FLAG_MMX)) {
577 ddabdfef Michael Niedermayer
        avctx->idct_algo= FF_IDCT_XVIDMMX;
578 cfeae977 Michael Niedermayer
        avctx->coded_width= 0; // force reinit
579 6baac1af Michael Niedermayer
//        dsputil_init(&s->dsp, avctx);
580
        s->picture_number=0;
581 ee0c00d3 Michael Niedermayer
    }
582 b442a92f Alex Beregszaszi
#endif
583
584 488fa19a Michael Niedermayer
        /* After H263 & mpeg4 header decode we have the height, width,*/
585 9eaa4690 Juanjo
        /* and other parameters. So then we could init the picture   */
586
        /* FIXME: By the way H263 decoder is evolving it should have */
587
        /* an H263EncContext                                         */
588 115329f1 Diego Biurrun
589
    if (   s->width  != avctx->coded_width
590 21adafec Michael Niedermayer
        || s->height != avctx->coded_height) {
591 93a21abd Michael Niedermayer
        /* H.263 could change picture size any time */
592 bcc7125e Michael Niedermayer
        ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
593
        s->parse_context.buffer=0;
594 93a21abd Michael Niedermayer
        MPV_common_end(s);
595 bcc7125e Michael Niedermayer
        s->parse_context= pc;
596 93a21abd Michael Niedermayer
    }
597 488fa19a Michael Niedermayer
    if (!s->context_initialized) {
598 21adafec Michael Niedermayer
        avcodec_set_dimensions(avctx, s->width, s->height);
599 4d2858de Michael Niedermayer
600 4a3d7fbc Michael Niedermayer
        goto retry;
601 de6d9b64 Fabrice Bellard
    }
602 35aa136a Michael Niedermayer
603 71a3dff9 Kostya Shishkov
    if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P || s->codec_id == CODEC_ID_H263I))
604 35aa136a Michael Niedermayer
        s->gob_index = ff_h263_get_gob_height(s);
605 115329f1 Diego Biurrun
606 1e491e29 Michael Niedermayer
    // for hurry_up==5
607
    s->current_picture.pict_type= s->pict_type;
608 9701840b Aurelien Jacobs
    s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
609 347ae22e Michael Niedermayer
610 bb628dae Diego Biurrun
    /* skip B-frames if we don't have reference frames */
611 9701840b Aurelien Jacobs
    if(s->last_picture_ptr==NULL && (s->pict_type==FF_B_TYPE || s->dropable)) return get_consumed_bytes(s, buf_size);
612 2417652e Michael Niedermayer
    /* skip b frames if we are in a hurry */
613 9701840b Aurelien Jacobs
    if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return get_consumed_bytes(s, buf_size);
614
    if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
615
       || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
616 115329f1 Diego Biurrun
       ||  avctx->skip_frame >= AVDISCARD_ALL)
617 8c3eba7c Michael Niedermayer
        return get_consumed_bytes(s, buf_size);
618 347ae22e Michael Niedermayer
    /* skip everything if we are in a hurry>=5 */
619 f8fd15e0 Michael Niedermayer
    if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
620 115329f1 Diego Biurrun
621 6733417a Michael Niedermayer
    if(s->next_p_frame_damaged){
622 9701840b Aurelien Jacobs
        if(s->pict_type==FF_B_TYPE)
623 7866eeff Michael Niedermayer
            return get_consumed_bytes(s, buf_size);
624 6733417a Michael Niedermayer
        else
625
            s->next_p_frame_damaged=0;
626
    }
627
628 9701840b Aurelien Jacobs
    if((s->avctx->flags2 & CODEC_FLAG2_FAST) && s->pict_type==FF_B_TYPE){
629 2833fc46 Loren Merritt
        s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
630
        s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
631 9701840b Aurelien Jacobs
    }else if((!s->no_rounding) || s->pict_type==FF_B_TYPE){
632 a1b13cf5 Loren Merritt
        s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
633 2833fc46 Loren Merritt
        s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
634
    }else{
635 a1b13cf5 Loren Merritt
        s->me.qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
636 2833fc46 Loren Merritt
        s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
637
    }
638
639 d6db1c9c Michael Niedermayer
    if(MPV_frame_start(s, avctx) < 0)
640
        return -1;
641 de6d9b64 Fabrice Bellard
642 70e0c871 NVIDIA Corporation
    if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) {
643 bf9d70d5 Carl Eugen Hoyos
        ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer);
644 70e0c871 NVIDIA Corporation
        goto frame_end;
645
    }
646
647 1468d503 Gwenole Beauchesne
    if (avctx->hwaccel) {
648 bf9d70d5 Carl Eugen Hoyos
        if (avctx->hwaccel->start_frame(avctx, s->gb.buffer, s->gb.buffer_end - s->gb.buffer) < 0)
649 1468d503 Gwenole Beauchesne
            return -1;
650
    }
651
652 46b4feec Michael Niedermayer
    ff_er_frame_start(s);
653 115329f1 Diego Biurrun
654 487bce53 Michael Niedermayer
    //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
655 755bfeab Diego Biurrun
    //which is not available before MPV_frame_start()
656 49fb20cb Aurelien Jacobs
    if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5){
657 9abc7e0f Michael Niedermayer
        ret = ff_wmv2_decode_secondary_picture_header(s);
658
        if(ret<0) return ret;
659
        if(ret==1) goto intrax8_decoded;
660 487bce53 Michael Niedermayer
    }
661
662 de6d9b64 Fabrice Bellard
    /* decode each macroblock */
663 115329f1 Diego Biurrun
    s->mb_x=0;
664 4d2858de Michael Niedermayer
    s->mb_y=0;
665 115329f1 Diego Biurrun
666 4d2858de Michael Niedermayer
    decode_slice(s);
667 487bce53 Michael Niedermayer
    while(s->mb_y<s->mb_height){
668 4d2858de Michael Niedermayer
        if(s->msmpeg4_version){
669 b34397b4 Michael Niedermayer
            if(s->slice_height==0 || s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits)
670 4d2858de Michael Niedermayer
                break;
671
        }else{
672
            if(ff_h263_resync(s)<0)
673
                break;
674 6733417a Michael Niedermayer
        }
675 115329f1 Diego Biurrun
676 28269849 Michael Niedermayer
        if(s->msmpeg4_version<4 && s->h263_pred)
677 4d2858de Michael Niedermayer
            ff_mpeg4_clean_buffers(s);
678 649c00c9 Michael Niedermayer
679 4d2858de Michael Niedermayer
        decode_slice(s);
680 de6d9b64 Fabrice Bellard
    }
681 8b32880c Michael Niedermayer
682 9701840b Aurelien Jacobs
    if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==FF_I_TYPE)
683 49fb20cb Aurelien Jacobs
        if(!CONFIG_MSMPEG4_DECODER || msmpeg4_decode_ext_header(s, buf_size) < 0){
684 1e491e29 Michael Niedermayer
            s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
685
        }
686 115329f1 Diego Biurrun
687 6c587587 Michael Niedermayer
    assert(s->bitstream_buffer_size==0);
688 8ef4f92b Carl Eugen Hoyos
frame_end:
689 eec1c6b9 Michael Niedermayer
    /* divx 5.01+ bistream reorder stuff */
690 6c587587 Michael Niedermayer
    if(s->codec_id==CODEC_ID_MPEG4 && s->divx_packed){
691 6733417a Michael Niedermayer
        int current_pos= get_bits_count(&s->gb)>>3;
692 fb204d4b Michael Niedermayer
        int startcode_found=0;
693 115329f1 Diego Biurrun
694 0ecca7a4 Michael Niedermayer
        if(buf_size - current_pos > 5){
695 6733417a Michael Niedermayer
            int i;
696 7866eeff Michael Niedermayer
            for(i=current_pos; i<buf_size-3; i++){
697 6733417a Michael Niedermayer
                if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
698
                    startcode_found=1;
699
                    break;
700
                }
701
            }
702 fb204d4b Michael Niedermayer
        }
703 b317567c Michael Niedermayer
        if(s->gb.buffer == s->bitstream_buffer && buf_size>7 && s->xvid_build>=0){ //xvid style
704 fb204d4b Michael Niedermayer
            startcode_found=1;
705
            current_pos=0;
706
        }
707
708
        if(startcode_found){
709 238ef6da Reimar Döffinger
            av_fast_malloc(
710
                &s->bitstream_buffer,
711 115329f1 Diego Biurrun
                &s->allocated_bitstream_buffer_size,
712 0ecca7a4 Michael Niedermayer
                buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE);
713 238ef6da Reimar Döffinger
            if (!s->bitstream_buffer)
714
                return AVERROR(ENOMEM);
715 fb204d4b Michael Niedermayer
            memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
716
            s->bitstream_buffer_size= buf_size - current_pos;
717 6733417a Michael Niedermayer
        }
718
    }
719
720 9abc7e0f Michael Niedermayer
intrax8_decoded:
721 46b4feec Michael Niedermayer
    ff_er_frame_end(s);
722 4d2858de Michael Niedermayer
723 f986c8e2 Michael Niedermayer
    if (avctx->hwaccel) {
724
        if (avctx->hwaccel->end_frame(avctx) < 0)
725
            return -1;
726
    }
727
728 de6d9b64 Fabrice Bellard
    MPV_frame_end(s);
729 db794953 Michael Niedermayer
730 db6e7795 Michael Niedermayer
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
731
assert(s->current_picture.pict_type == s->pict_type);
732 9701840b Aurelien Jacobs
    if (s->pict_type == FF_B_TYPE || s->low_delay) {
733 5430b070 Michael Niedermayer
        *pict= *(AVFrame*)s->current_picture_ptr;
734
    } else if (s->last_picture_ptr != NULL) {
735
        *pict= *(AVFrame*)s->last_picture_ptr;
736
    }
737
738
    if(s->last_picture_ptr || s->low_delay){
739
        *data_size = sizeof(AVFrame);
740 0c9bbaec Wolfgang Hesseler
        ff_print_debug_info(s, pict);
741 6f91bcd1 Michael Niedermayer
    }
742 8fc69895 Michael Niedermayer
743 02734c6a Michael Niedermayer
#ifdef PRINT_FRAME_TIME
744 949b1a13 Steve L'Homme
av_log(avctx, AV_LOG_DEBUG, "%"PRId64"\n", rdtsc()-time);
745 02734c6a Michael Niedermayer
#endif
746 d07f9043 Michael Niedermayer
747 7866eeff Michael Niedermayer
    return get_consumed_bytes(s, buf_size);
748 de6d9b64 Fabrice Bellard
}
749
750 e7e2df27 Diego Elio Pettenò
AVCodec ff_h263_decoder = {
751 de6d9b64 Fabrice Bellard
    "h263",
752 72415b2a Stefano Sabatini
    AVMEDIA_TYPE_VIDEO,
753 de6d9b64 Fabrice Bellard
    CODEC_ID_H263,
754
    sizeof(MpegEncContext),
755 1457ab52 Michael Niedermayer
    ff_h263_decode_init,
756 de6d9b64 Fabrice Bellard
    NULL,
757 1457ab52 Michael Niedermayer
    ff_h263_decode_end,
758
    ff_h263_decode_frame,
759 934982c4 Michael Niedermayer
    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
760 7a06ff14 Michael Niedermayer
    .flush= ff_mpeg_flush,
761 0fd0ef79 Carl Eugen Hoyos
    .max_lowres= 3,
762 0ffbc258 Diego Biurrun
    .long_name= NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
763 a4f3fdfa Gwenole Beauchesne
    .pix_fmts= ff_hwaccel_pixfmt_list_420,
764 de6d9b64 Fabrice Bellard
};