~ubuntu-dev/mplayer/upstream-ubuntu

1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <assert.h>
4
#include <time.h>
5
6
#include "config.h"
7
#include "mp_msg.h"
8
#include "help_mp.h"
9
4 by Mario Limonciello
Update to 1.0rc2
10
#include "libavutil/common.h"
11
#include "libavutil/intreadwrite.h"
12
#include "mpbswap.h"
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
13
14
#include "vd_internal.h"
15
16
static vd_info_t info = {
17
	"FFmpeg's libavcodec codec family",
18
	"ffmpeg",
19
	"A'rpi",
20
	"A'rpi, Michael, Alex",
21
	"native codecs (http://ffmpeg.sf.net/)"
22
};
23
24
LIBVD_EXTERN(ffmpeg)
25
26
#ifdef USE_LIBAVCODEC_SO
27
#include <ffmpeg/avcodec.h>
28
#else
29
#include "libavcodec/avcodec.h"
30
#endif
31
32
#ifdef HAVE_XVMC
33
#include "xvmc_render.h"
34
#endif
35
36
int avcodec_inited=0;
37
38
typedef struct {
39
    AVCodecContext *avctx;
40
    AVFrame *pic;
2 by Reinhard Tartler
upgrade to pre8
41
    enum PixelFormat pix_fmt;
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
42
    int do_slices;
43
    int do_dr1;
44
    int vo_inited;
45
    int best_csp;
46
    int b_age;
47
    int ip_age[2];
48
    int qp_stat[32];
49
    double qp_sum;
50
    double inv_qp_sum;
51
    int ip_count;
52
    int b_count;
4 by Mario Limonciello
Update to 1.0rc2
53
    AVRational last_sample_aspect_ratio;
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
54
} vd_ffmpeg_ctx;
55
2 by Reinhard Tartler
upgrade to pre8
56
//#ifdef USE_LIBPOSTPROC
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
57
//unsigned int lavc_pp=0;
58
//#endif
59
60
#include "m_option.h"
61
62
static int get_buffer(AVCodecContext *avctx, AVFrame *pic);
63
static void release_buffer(AVCodecContext *avctx, AVFrame *pic);
4 by Mario Limonciello
Update to 1.0rc2
64
65
#ifdef HAVE_XVMC
66
static enum PixelFormat get_format(struct AVCodecContext * avctx,
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
67
                                   const enum PixelFormat * pix_fmt);
68
static int mc_get_buffer(AVCodecContext *avctx, AVFrame *pic);
69
static void mc_release_buffer(AVCodecContext *avctx, AVFrame *pic);
70
static void mc_render_slice(struct AVCodecContext *s,
71
                	AVFrame *src, int offset[4],
72
                	int y, int type, int height);
73
#endif
74
75
static int lavc_param_workaround_bugs= FF_BUG_AUTODETECT;
76
static int lavc_param_error_resilience=2;
77
static int lavc_param_error_concealment=3;
78
static int lavc_param_gray=0;
79
static int lavc_param_vstats=0;
80
static int lavc_param_idct_algo=0;
81
static int lavc_param_debug=0;
82
static int lavc_param_vismv=0;
83
static int lavc_param_skip_top=0;
84
static int lavc_param_skip_bottom=0;
85
static int lavc_param_fast=0;
86
static int lavc_param_lowres=0;
87
static char *lavc_param_lowres_str=NULL;
88
static char *lavc_param_skip_loop_filter_str = NULL;
89
static char *lavc_param_skip_idct_str = NULL;
90
static char *lavc_param_skip_frame_str = NULL;
91
static int lavc_param_threads=1;
92
static int lavc_param_bitexact=0;
93
94
m_option_t lavc_decode_opts_conf[]={
95
	{"bug", &lavc_param_workaround_bugs, CONF_TYPE_INT, CONF_RANGE, -1, 999999, NULL},
96
	{"er", &lavc_param_error_resilience, CONF_TYPE_INT, CONF_RANGE, 0, 99, NULL},
97
	{"gray", &lavc_param_gray, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL},
98
	{"idct", &lavc_param_idct_algo, CONF_TYPE_INT, CONF_RANGE, 0, 99, NULL},
99
	{"ec", &lavc_param_error_concealment, CONF_TYPE_INT, CONF_RANGE, 0, 99, NULL},
100
	{"vstats", &lavc_param_vstats, CONF_TYPE_FLAG, 0, 0, 1, NULL},
101
	{"debug", &lavc_param_debug, CONF_TYPE_INT, CONF_RANGE, 0, 9999999, NULL},
102
	{"vismv", &lavc_param_vismv, CONF_TYPE_INT, CONF_RANGE, 0, 9999999, NULL},
103
	{"st", &lavc_param_skip_top, CONF_TYPE_INT, CONF_RANGE, 0, 999, NULL},
104
	{"sb", &lavc_param_skip_bottom, CONF_TYPE_INT, CONF_RANGE, 0, 999, NULL},
105
#ifdef CODEC_FLAG2_FAST
106
        {"fast", &lavc_param_fast, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG2_FAST, NULL},
107
#endif
108
	{"lowres", &lavc_param_lowres_str, CONF_TYPE_STRING, 0, 0, 0, NULL},
109
	{"skiploopfilter", &lavc_param_skip_loop_filter_str, CONF_TYPE_STRING, 0, 0, 0, NULL},
110
	{"skipidct", &lavc_param_skip_idct_str, CONF_TYPE_STRING, 0, 0, 0, NULL},
111
	{"skipframe", &lavc_param_skip_frame_str, CONF_TYPE_STRING, 0, 0, 0, NULL},
112
        {"threads", &lavc_param_threads, CONF_TYPE_INT, CONF_RANGE, 1, 8, NULL},
113
        {"bitexact", &lavc_param_bitexact, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_BITEXACT, NULL},
114
	{NULL, NULL, 0, 0, 0, 0, NULL}
115
};
116
117
static enum AVDiscard str2AVDiscard(char *str) {
118
  if (!str)
119
    return AVDISCARD_DEFAULT;
120
  if (strcasecmp(str, "none") == 0)
121
    return AVDISCARD_NONE;
122
  if (strcasecmp(str, "default") == 0)
123
    return AVDISCARD_DEFAULT;
124
  if (strcasecmp(str, "nonref") == 0)
125
    return AVDISCARD_NONREF;
126
  if (strcasecmp(str, "bidir") == 0)
127
    return AVDISCARD_BIDIR;
128
  if (strcasecmp(str, "nonkey") == 0)
129
    return AVDISCARD_NONKEY;
130
  if (strcasecmp(str, "all") == 0)
131
    return AVDISCARD_ALL;
132
  mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Unknown discard value %s\n", str);
133
  return AVDISCARD_DEFAULT;
134
}
135
136
// to set/get/query special features/parameters
137
static int control(sh_video_t *sh,int cmd,void* arg,...){
138
    vd_ffmpeg_ctx *ctx = sh->context;
139
    AVCodecContext *avctx = ctx->avctx;
140
    switch(cmd){
141
    case VDCTRL_QUERY_FORMAT:
142
        {
143
            int format =(*((int*)arg));
144
	    if( format == ctx->best_csp ) return CONTROL_TRUE;//supported
145
	// possible conversions:
146
	    switch( format ){
147
        case IMGFMT_YV12:
148
        case IMGFMT_IYUV:
149
        case IMGFMT_I420:
150
	    // "converted" using pointer/stride modification
151
	    if(avctx->pix_fmt==PIX_FMT_YUV420P) return CONTROL_TRUE;// u/v swap
152
	    if(avctx->pix_fmt==PIX_FMT_YUV422P && !ctx->do_dr1) return CONTROL_TRUE;// half stride
153
	    break;
154
#ifdef HAVE_XVMC
155
        case IMGFMT_XVMC_IDCT_MPEG2:
156
        case IMGFMT_XVMC_MOCO_MPEG2:
157
            if(avctx->pix_fmt==PIX_FMT_XVMC_MPEG2_IDCT) return CONTROL_TRUE;
158
#endif
159
	}
160
        return CONTROL_FALSE;
161
        }
162
    break;
163
    case VDCTRL_RESYNC_STREAM:
164
        avcodec_flush_buffers(avctx);
3 by William Grant
Update to 1.0rc1.
165
	return CONTROL_TRUE;
166
    case VDCTRL_QUERY_UNSEEN_FRAMES:
167
	return avctx->has_b_frames + 10;
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
168
    }
169
    return CONTROL_UNKNOWN;
170
}
171
4 by Mario Limonciello
Update to 1.0rc2
172
void mp_msp_av_log_callback(void* ptr, int level, const char* fmt, va_list vl)
173
{
174
    static int print_prefix=1;
175
    AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
176
    int type= MSGT_FIXME;
177
    int mp_level;
178
    char buf[256];
179
180
    switch(level){
181
    case AV_LOG_DEBUG:  mp_level= MSGL_V   ; break;
182
    case AV_LOG_INFO :  mp_level= MSGL_INFO; break;
183
    case AV_LOG_ERROR:  mp_level= MSGL_ERR ; break;
184
    default          :  mp_level= MSGL_ERR ; break;
185
    }
186
187
    if (!mp_msg_test(type, mp_level)) return;
188
189
    if(ptr){
190
        if(!strcmp(avc->class_name, "AVCodecContext")){
191
            AVCodecContext * s= ptr;
192
            if(s->codec){
193
                if(s->codec->type == CODEC_TYPE_AUDIO){
194
                    if(s->codec->decode)
195
                        type= MSGT_DECAUDIO;
196
                }else if(s->codec->type == CODEC_TYPE_VIDEO){
197
                    if(s->codec->decode)
198
                        type= MSGT_DECVIDEO;
199
                }
200
                //FIXME subtitles, encoders (what msgt for them? there is no appropriate ...)
201
            }
202
        }else if(!strcmp(avc->class_name, "AVFormatContext")){
203
#if 0 //needs libavformat include FIXME iam too lazy to do this cleanly,probably the whole should be moved out of this file ...
204
            AVFormatContext * s= ptr;
205
            if(s->iformat)
206
                type= MSGT_DEMUXER;
207
            else if(s->oformat)
208
                type= MSGT_MUXER;
209
#endif
210
        }
211
    }
212
213
    if(print_prefix && avc) {
214
        mp_msg(type, mp_level, "[%s @ %p]", avc->item_name(ptr), avc);
215
    }
216
217
    print_prefix= strchr(fmt, '\n') != NULL;
218
    vsnprintf(buf, sizeof(buf), fmt, vl);
219
    mp_msg(type, mp_level, buf);
220
}
221
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
222
// init driver
223
static int init(sh_video_t *sh){
224
    AVCodecContext *avctx;
225
    vd_ffmpeg_ctx *ctx;
226
    AVCodec *lavc_codec;
227
    int lowres_w=0;
228
    int do_vis_debug= lavc_param_vismv || (lavc_param_debug&(FF_DEBUG_VIS_MB_TYPE|FF_DEBUG_VIS_QP));
229
230
    if(!avcodec_inited){
231
      avcodec_init();
232
      avcodec_register_all();
233
      avcodec_inited=1;
4 by Mario Limonciello
Update to 1.0rc2
234
      av_log_set_callback(mp_msp_av_log_callback);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
235
    }
236
237
    ctx = sh->context = malloc(sizeof(vd_ffmpeg_ctx));
238
    if (!ctx)
239
	return(0);
240
    memset(ctx, 0, sizeof(vd_ffmpeg_ctx));
241
    
242
    lavc_codec = (AVCodec *)avcodec_find_decoder_by_name(sh->codec->dll);
243
    if(!lavc_codec){
244
	mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MissingLAVCcodec,sh->codec->dll);
4 by Mario Limonciello
Update to 1.0rc2
245
        uninit(sh);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
246
	return 0;
247
    }
248
249
    if(vd_use_slices && (lavc_codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND) && !do_vis_debug)
250
	ctx->do_slices=1;
251
 
4 by Mario Limonciello
Update to 1.0rc2
252
    if(lavc_codec->capabilities&CODEC_CAP_DR1 && !do_vis_debug && lavc_codec->id != CODEC_ID_H264 && lavc_codec->id != CODEC_ID_INTERPLAY_VIDEO && lavc_codec->id != CODEC_ID_ROQ)
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
253
	ctx->do_dr1=1;
254
    ctx->b_age= ctx->ip_age[0]= ctx->ip_age[1]= 256*256*256*64;
255
    ctx->ip_count= ctx->b_count= 0;
256
257
    ctx->pic = avcodec_alloc_frame();
258
    ctx->avctx = avcodec_alloc_context();
259
    avctx = ctx->avctx;
260
261
#ifdef HAVE_XVMC
262
263
#ifdef CODEC_CAP_HWACCEL
264
    if(lavc_codec->capabilities & CODEC_CAP_HWACCEL){
265
#else
266
    if(lavc_codec->id == CODEC_ID_MPEG2VIDEO_XVMC){
4 by Mario Limonciello
Update to 1.0rc2
267
#endif /* CODEC_CAP_HWACCEL */
2 by Reinhard Tartler
upgrade to pre8
268
        mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_MPCODECS_XVMCAcceleratedCodec);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
269
        assert(ctx->do_dr1);//these are must to!
270
        assert(ctx->do_slices); //it is (vo_)ffmpeg bug if this fails
271
        avctx->flags|= CODEC_FLAG_EMU_EDGE;//do i need that??!!
272
        avctx->get_format= get_format;//for now only this decoder will use it
273
        avctx->get_buffer= mc_get_buffer;
274
        avctx->release_buffer= mc_release_buffer;
275
        avctx->draw_horiz_band = mc_render_slice;
276
        avctx->slice_flags=SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
277
    }else
4 by Mario Limonciello
Update to 1.0rc2
278
#endif /* HAVE_XVMC */
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
279
    if(ctx->do_dr1){
280
        avctx->flags|= CODEC_FLAG_EMU_EDGE; 
281
        avctx->get_buffer= get_buffer;
282
        avctx->release_buffer= release_buffer;
283
        avctx->reget_buffer= get_buffer;
284
    }
285
286
#ifdef CODEC_FLAG_NOT_TRUNCATED
287
    avctx->flags|= CODEC_FLAG_NOT_TRUNCATED;
288
#endif
289
    avctx->flags|= lavc_param_bitexact;
290
    
291
    avctx->width = sh->disp_w;
292
    avctx->height= sh->disp_h;
293
    avctx->workaround_bugs= lavc_param_workaround_bugs;
294
    avctx->error_resilience= lavc_param_error_resilience;
295
    if(lavc_param_gray) avctx->flags|= CODEC_FLAG_GRAY;
296
#ifdef CODEC_FLAG2_FAST
297
    avctx->flags2|= lavc_param_fast;
298
#endif
299
    avctx->codec_tag= sh->format;
300
    avctx->stream_codec_tag= sh->video.fccHandler;
301
    avctx->idct_algo= lavc_param_idct_algo;
302
    avctx->error_concealment= lavc_param_error_concealment;
303
    avctx->debug= lavc_param_debug;
304
    if (lavc_param_debug)
305
        av_log_set_level(AV_LOG_DEBUG);
306
    avctx->debug_mv= lavc_param_vismv;
307
    avctx->skip_top   = lavc_param_skip_top;
308
    avctx->skip_bottom= lavc_param_skip_bottom;
309
    if(lavc_param_lowres_str != NULL)
310
    {
311
        sscanf(lavc_param_lowres_str, "%d,%d", &lavc_param_lowres, &lowres_w);
312
        if(lavc_param_lowres < 1 || lavc_param_lowres > 16 || (lowres_w > 0 && avctx->width < lowres_w))
313
            lavc_param_lowres = 0;
314
        avctx->lowres = lavc_param_lowres;
315
    }
316
    avctx->skip_loop_filter = str2AVDiscard(lavc_param_skip_loop_filter_str);
317
    avctx->skip_idct = str2AVDiscard(lavc_param_skip_idct_str);
318
    avctx->skip_frame = str2AVDiscard(lavc_param_skip_frame_str);
319
    mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"libavcodec.size: %d x %d\n",avctx->width,avctx->height);
2 by Reinhard Tartler
upgrade to pre8
320
    switch (sh->format) {
3 by William Grant
Update to 1.0rc1.
321
    case mmioFOURCC('S','V','Q','3'):
322
    /* SVQ3 extradata can show up as sh->ImageDesc if demux_mov is used, or
323
       in the phony AVI header if demux_lavf is used. The first case is
324
       handled here; the second case falls through to the next section. */
325
	if (sh->ImageDesc) {
326
	    avctx->extradata_size = (*(int*)sh->ImageDesc) - sizeof(int);
327
	    avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
328
	    memcpy(avctx->extradata, ((int*)sh->ImageDesc)+1, avctx->extradata_size);
329
	    break;
330
	}
331
	/* fallthrough */
332
2 by Reinhard Tartler
upgrade to pre8
333
    case mmioFOURCC('A','V','R','n'):
334
    case mmioFOURCC('M','J','P','G'):
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
335
    /* AVRn stores huffman table in AVI header */
336
    /* Pegasus MJPEG stores it also in AVI header, but it uses the common
337
       MJPG fourcc :( */
2 by Reinhard Tartler
upgrade to pre8
338
	if (!sh->bih || sh->bih->biSize <= sizeof(BITMAPINFOHEADER))
339
	    break;
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
340
	avctx->flags |= CODEC_FLAG_EXTERN_HUFF;
341
	avctx->extradata_size = sh->bih->biSize-sizeof(BITMAPINFOHEADER);
342
	avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
3 by William Grant
Update to 1.0rc1.
343
	memcpy(avctx->extradata, sh->bih+1, avctx->extradata_size);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
344
345
#if 0
346
	{
347
	    int x;
348
	    uint8_t *p = avctx->extradata;
349
	    
350
	    for (x=0; x<avctx->extradata_size; x++)
2 by Reinhard Tartler
upgrade to pre8
351
		mp_msg(MSGT_DECVIDEO, MSGL_INFO,"[%x] ", p[x]);
352
	    mp_msg(MSGT_DECVIDEO, MSGL_INFO,"\n");
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
353
	}
354
#endif
2 by Reinhard Tartler
upgrade to pre8
355
	break;
356
357
    case mmioFOURCC('R', 'V', '1', '0'):
358
    case mmioFOURCC('R', 'V', '1', '3'):
359
    case mmioFOURCC('R', 'V', '2', '0'):
360
    case mmioFOURCC('R', 'V', '3', '0'):
361
    case mmioFOURCC('R', 'V', '4', '0'):
4 by Mario Limonciello
Update to 1.0rc2
362
        if(sh->bih->biSize<sizeof(*sh->bih)+8){
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
363
            /* only 1 packet per frame & sub_id from fourcc */
4 by Mario Limonciello
Update to 1.0rc2
364
            avctx->extradata_size= 8;
365
            avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
366
	    ((uint32_t*)avctx->extradata)[0] = 0;
367
	    ((uint32_t*)avctx->extradata)[1] =
368
        	(sh->format == mmioFOURCC('R', 'V', '1', '3')) ? 0x10003001 : 0x10000000;
369
        } else {
370
	    /* has extra slice header (demux_rm or rm->avi streamcopy) */
4 by Mario Limonciello
Update to 1.0rc2
371
	    avctx->extradata_size = sh->bih->biSize-sizeof(BITMAPINFOHEADER);
372
	    avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
373
	    memcpy(avctx->extradata, sh->bih+1, avctx->extradata_size);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
374
	}
4 by Mario Limonciello
Update to 1.0rc2
375
	avctx->sub_id= AV_RB32(avctx->extradata+4);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
376
377
//        printf("%X %X %d %d\n", extrahdr[0], extrahdr[1]);
2 by Reinhard Tartler
upgrade to pre8
378
        break;
379
380
    default:
381
	if (!sh->bih || sh->bih->biSize <= sizeof(BITMAPINFOHEADER))
382
	    break;
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
383
	avctx->extradata_size = sh->bih->biSize-sizeof(BITMAPINFOHEADER);
384
	avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
385
	memcpy(avctx->extradata, sh->bih+1, avctx->extradata_size);
2 by Reinhard Tartler
upgrade to pre8
386
	break;
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
387
    }
388
    /* Pass palette to codec */
389
    if (sh->bih && (sh->bih->biBitCount <= 8)) {
3 by William Grant
Update to 1.0rc1.
390
        avctx->palctrl = calloc(1,sizeof(AVPaletteControl));
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
391
        avctx->palctrl->palette_changed = 1;
392
        if (sh->bih->biSize-sizeof(BITMAPINFOHEADER))
393
            /* Palette size in biSize */
394
            memcpy(avctx->palctrl->palette, sh->bih+1,
4 by Mario Limonciello
Update to 1.0rc2
395
                   FFMIN(sh->bih->biSize-sizeof(BITMAPINFOHEADER), AVPALETTE_SIZE));
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
396
        else
397
            /* Palette size in biClrUsed */
398
            memcpy(avctx->palctrl->palette, sh->bih+1,
4 by Mario Limonciello
Update to 1.0rc2
399
                   FFMIN(sh->bih->biClrUsed * 4, AVPALETTE_SIZE));
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
400
	}
3 by William Grant
Update to 1.0rc1.
401
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
402
    if(sh->bih)
403
	avctx->bits_per_sample= sh->bih->biBitCount;
404
405
    if(lavc_param_threads > 1)
406
        avcodec_thread_init(avctx, lavc_param_threads);
407
    /* open it */
408
    if (avcodec_open(avctx, lavc_codec) < 0) {
409
        mp_msg(MSGT_DECVIDEO,MSGL_ERR, MSGTR_CantOpenCodec);
4 by Mario Limonciello
Update to 1.0rc2
410
        uninit(sh);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
411
        return 0;
412
    }
413
    mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: libavcodec init OK!\n");
414
    return 1; //mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12);
415
}
416
417
// uninit driver
418
static void uninit(sh_video_t *sh){
419
    vd_ffmpeg_ctx *ctx = sh->context;
420
    AVCodecContext *avctx = ctx->avctx;
421
    
422
    if(lavc_param_vstats){
423
        int i;
424
        for(i=1; i<32; i++){
2 by Reinhard Tartler
upgrade to pre8
425
            mp_msg(MSGT_DECVIDEO, MSGL_INFO,"QP: %d, count: %d\n", i, ctx->qp_stat[i]);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
426
        }
2 by Reinhard Tartler
upgrade to pre8
427
        mp_msg(MSGT_DECVIDEO, MSGL_INFO,MSGTR_MPCODECS_ArithmeticMeanOfQP, 
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
428
            ctx->qp_sum / avctx->coded_frame->coded_picture_number,
429
            1.0/(ctx->inv_qp_sum / avctx->coded_frame->coded_picture_number)
430
            );
431
    }
432
4 by Mario Limonciello
Update to 1.0rc2
433
    if (avctx) {
434
        if (avctx->codec && avcodec_close(avctx) < 0)
435
            mp_msg(MSGT_DECVIDEO,MSGL_ERR, MSGTR_CantCloseCodec);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
436
4 by Mario Limonciello
Update to 1.0rc2
437
        av_freep(&avctx->extradata);
438
        av_freep(&avctx->palctrl);
439
        av_freep(&avctx->slice_offset);
440
    }
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
441
442
    av_freep(&avctx);
443
    av_freep(&ctx->pic);
444
    if (ctx)
445
	free(ctx);
446
}
447
448
static void draw_slice(struct AVCodecContext *s,
449
                	AVFrame *src, int offset[4],
450
                	int y, int type, int height){
451
    sh_video_t * sh = s->opaque;
4 by Mario Limonciello
Update to 1.0rc2
452
    uint8_t *source[3]= {src->data[0] + offset[0], src->data[1] + offset[1], src->data[2] + offset[2]};
453
#if 0
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
454
    int start=0, i;
455
    int width= s->width;
456
    int skip_stride= ((width<<lavc_param_lowres)+15)>>4;
457
    uint8_t *skip= &s->coded_frame->mbskip_table[(y>>4)*skip_stride];
458
    int threshold= s->coded_frame->age;
459
    if(s->pict_type!=B_TYPE){
460
        for(i=0; i*16<width+16; i++){ 
461
            if(i*16>=width || skip[i]>=threshold){
462
                if(start==i) start++;
463
                else{
464
                    uint8_t *src2[3]= {src[0] + start*16, 
465
                                     src[1] + start*8, 
466
                                     src[2] + start*8};
467
//printf("%2d-%2d x %d\n", start, i, y);
468
                    mpcodecs_draw_slice (sh,src2, stride, (i-start)*16, height, start*16, y);
469
                    start= i+1;
470
                }
471
            }       
472
        }
473
    }else
474
#endif
475
    if (y < sh->disp_h) {
476
        mpcodecs_draw_slice (sh, source, src->linesize, sh->disp_w, (y+height)<=sh->disp_h?height:sh->disp_h-y, 0, y);
477
    }
478
}
479
480
481
static int init_vo(sh_video_t *sh, enum PixelFormat pix_fmt){
482
    vd_ffmpeg_ctx *ctx = sh->context;
483
    AVCodecContext *avctx = ctx->avctx;
484
    float aspect= av_q2d(avctx->sample_aspect_ratio) * avctx->width / avctx->height;
485
    int width, height;
486
487
    width = avctx->width;
488
    height = avctx->height;
489
490
    // HACK!
491
    // if sh->ImageDesc is non-NULL, it means we decode QuickTime(tm) video.
492
    // use dimensions from BIH to avoid black borders at the right and bottom.
493
    if (sh->bih && sh->ImageDesc) {
494
	width = sh->bih->biWidth>>lavc_param_lowres;
495
	height = sh->bih->biHeight>>lavc_param_lowres;
496
    }
497
    
498
     // it is possible another vo buffers to be used after vo config()
499
     // lavc reset its buffers on width/heigh change but not on aspect change!!!
4 by Mario Limonciello
Update to 1.0rc2
500
    if (av_cmp_q(avctx->sample_aspect_ratio, ctx->last_sample_aspect_ratio) ||
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
501
	width != sh->disp_w  ||
502
	height != sh->disp_h ||
2 by Reinhard Tartler
upgrade to pre8
503
	pix_fmt != ctx->pix_fmt ||
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
504
	!ctx->vo_inited)
505
    {
506
	mp_msg(MSGT_DECVIDEO, MSGL_V, "[ffmpeg] aspect_ratio: %f\n", aspect);
4 by Mario Limonciello
Update to 1.0rc2
507
	if (sh->aspect == 0 ||
508
	    av_cmp_q(avctx->sample_aspect_ratio, 
509
	             ctx->last_sample_aspect_ratio))
510
	    sh->aspect = aspect;
511
	ctx->last_sample_aspect_ratio = avctx->sample_aspect_ratio;
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
512
	sh->disp_w = width;
513
	sh->disp_h = height;
2 by Reinhard Tartler
upgrade to pre8
514
	ctx->pix_fmt = pix_fmt;
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
515
	switch(pix_fmt){
516
	// YUVJ are YUV formats that use the full Y range and not just
517
	// 16 - 235 (see colorspaces.txt).
518
	// Currently they are all treated the same way.
519
	case PIX_FMT_YUV410P: ctx->best_csp=IMGFMT_YVU9;break; //svq1
520
	case PIX_FMT_YUVJ420P:
521
	case PIX_FMT_YUV420P: ctx->best_csp=IMGFMT_YV12;break; //mpegs
522
	case PIX_FMT_YUVJ422P:
523
	case PIX_FMT_YUV422P: ctx->best_csp=IMGFMT_422P;break; //mjpeg / huffyuv
524
	case PIX_FMT_YUVJ444P:
525
	case PIX_FMT_YUV444P: ctx->best_csp=IMGFMT_444P;break; //photo jpeg
526
	case PIX_FMT_YUV411P: ctx->best_csp=IMGFMT_411P;break; //dv ntsc
4 by Mario Limonciello
Update to 1.0rc2
527
	case PIX_FMT_YUYV422: ctx->best_csp=IMGFMT_YUY2;break; //huffyuv perhaps in the future
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
528
	case PIX_FMT_RGB24 :  ctx->best_csp=IMGFMT_RGB24;break; //qtrle
4 by Mario Limonciello
Update to 1.0rc2
529
	case PIX_FMT_RGB32:   ctx->best_csp=IMGFMT_BGR32;break; //huffyuv / mjpeg
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
530
	case PIX_FMT_BGR24 :  ctx->best_csp=IMGFMT_BGR24;break; //8bps
531
	case PIX_FMT_RGB555:  ctx->best_csp=IMGFMT_BGR15;break; //rpza,cram
532
	case PIX_FMT_RGB565:  ctx->best_csp=IMGFMT_BGR16;break; //4xm
2 by Reinhard Tartler
upgrade to pre8
533
	case PIX_FMT_GRAY8:   ctx->best_csp=IMGFMT_Y800;break; // gray jpeg
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
534
	case PIX_FMT_PAL8:    ctx->best_csp=IMGFMT_BGR8;break; //8bps,mrle,cram
535
#ifdef HAVE_XVMC
536
        case PIX_FMT_XVMC_MPEG2_MC:ctx->best_csp=IMGFMT_XVMC_MOCO_MPEG2;break;
537
        case PIX_FMT_XVMC_MPEG2_IDCT:ctx->best_csp=IMGFMT_XVMC_IDCT_MPEG2;break;
538
#endif
539
	default:
540
	    ctx->best_csp=0;
541
	}
542
    	if (!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h, ctx->best_csp))
543
    		return -1;
544
	ctx->vo_inited = 1;
545
    }
546
    return 0;
547
}
548
549
static int get_buffer(AVCodecContext *avctx, AVFrame *pic){
550
    sh_video_t * sh = avctx->opaque;
551
    vd_ffmpeg_ctx *ctx = sh->context;
552
    mp_image_t* mpi=NULL;
553
    int flags= MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE;
554
    int type= MP_IMGTYPE_IPB;
555
    int width= avctx->width;
556
    int height= avctx->height;
557
    int align=15;
558
//printf("get_buffer %d %d %d\n", pic->reference, ctx->ip_count, ctx->b_count);
559
    if(avctx->pix_fmt == PIX_FMT_YUV410P)
560
        align=63; //yes seriously, its really needed (16x16 chroma blocks in SVQ1 -> 64x64)
561
562
  if (pic->buffer_hints) {
563
    mp_msg(MSGT_DECVIDEO,MSGL_DBG2, "Buffer hints: %u\n", pic->buffer_hints);
564
    type = MP_IMGTYPE_TEMP;
565
    if (pic->buffer_hints & FF_BUFFER_HINTS_READABLE)
566
        flags |= MP_IMGFLAG_READABLE;
567
    if (pic->buffer_hints & FF_BUFFER_HINTS_PRESERVE) {
568
        type = MP_IMGTYPE_STATIC;
569
        flags |= MP_IMGFLAG_PRESERVE;
570
    }
571
    if (pic->buffer_hints & FF_BUFFER_HINTS_REUSABLE) {
572
        type = MP_IMGTYPE_STATIC;
573
        flags |= MP_IMGFLAG_PRESERVE;
574
    }
575
    flags|=(!avctx->hurry_up && ctx->do_slices) ?
576
            MP_IMGFLAG_DRAW_CALLBACK:0;
577
    mp_msg(MSGT_DECVIDEO,MSGL_DBG2, type == MP_IMGTYPE_STATIC ? "using STATIC\n" : "using TEMP\n");
578
  } else {
579
    if(!pic->reference){
580
        ctx->b_count++;
581
        flags|=(!avctx->hurry_up && ctx->do_slices) ?
582
                MP_IMGFLAG_DRAW_CALLBACK:0;
583
    }else{
584
        ctx->ip_count++;
585
        flags|= MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE
586
                | (ctx->do_slices ? MP_IMGFLAG_DRAW_CALLBACK : 0);
587
    }
588
  }
589
590
    if(init_vo(sh,avctx->pix_fmt) < 0){
591
        avctx->release_buffer= avcodec_default_release_buffer;
592
        avctx->get_buffer= avcodec_default_get_buffer;
593
        return avctx->get_buffer(avctx, pic);
594
    }
3 by William Grant
Update to 1.0rc1.
595
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
596
  if (!pic->buffer_hints) {
597
    if(ctx->b_count>1 || ctx->ip_count>2){
2 by Reinhard Tartler
upgrade to pre8
598
        mp_msg(MSGT_DECVIDEO, MSGL_WARN, MSGTR_MPCODECS_DRIFailure);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
599
600
        ctx->do_dr1=0; //FIXME
601
        avctx->get_buffer= avcodec_default_get_buffer;
602
        return avctx->get_buffer(avctx, pic);
603
    }
604
605
    if(avctx->has_b_frames){
606
        type= MP_IMGTYPE_IPB;
607
    }else{
608
        type= MP_IMGTYPE_IP;
609
    }
610
    mp_msg(MSGT_DECVIDEO,MSGL_DBG2, type== MP_IMGTYPE_IPB ? "using IPB\n" : "using IP\n");
611
  }
612
613
    mpi= mpcodecs_get_image(sh,type, flags,
614
			(width+align)&(~align), (height+align)&(~align));
615
616
    // ok, let's see what did we get:
617
    if(  mpi->flags&MP_IMGFLAG_DRAW_CALLBACK &&
618
       !(mpi->flags&MP_IMGFLAG_DIRECT)){
619
	// nice, filter/vo likes draw_callback :)
620
	avctx->draw_horiz_band= draw_slice;
621
    } else
622
	avctx->draw_horiz_band= NULL;
623
624
	// Palette support: libavcodec copies palette to *data[1]
625
	if (mpi->bpp == 8)
626
		mpi->planes[1] = av_malloc(AVPALETTE_SIZE);
627
628
    pic->data[0]= mpi->planes[0];
629
    pic->data[1]= mpi->planes[1];
630
    pic->data[2]= mpi->planes[2];
631
632
#if 0    
633
    assert(mpi->width >= ((width +align)&(~align)));
634
    assert(mpi->height >= ((height+align)&(~align)));
635
    assert(mpi->stride[0] >= mpi->width);
636
    if(mpi->imgfmt==IMGFMT_I420 || mpi->imgfmt==IMGFMT_YV12 || mpi->imgfmt==IMGFMT_IYUV){
637
        const int y_size= mpi->stride[0] * (mpi->h-1) + mpi->w;
638
        const int c_size= mpi->stride[1] * ((mpi->h>>1)-1) + (mpi->w>>1);
639
        
640
        assert(mpi->planes[0] > mpi->planes[1] || mpi->planes[0] + y_size <= mpi->planes[1]);
641
        assert(mpi->planes[0] > mpi->planes[2] || mpi->planes[0] + y_size <= mpi->planes[2]);
642
        assert(mpi->planes[1] > mpi->planes[0] || mpi->planes[1] + c_size <= mpi->planes[0]);
643
        assert(mpi->planes[1] > mpi->planes[2] || mpi->planes[1] + c_size <= mpi->planes[2]);
644
        assert(mpi->planes[2] > mpi->planes[0] || mpi->planes[2] + c_size <= mpi->planes[0]);
645
        assert(mpi->planes[2] > mpi->planes[1] || mpi->planes[2] + c_size <= mpi->planes[1]);
646
    }
647
#endif
648
649
    /* Note, some (many) codecs in libavcodec must have stride1==stride2 && no changes between frames
650
     * lavc will check that and die with an error message, if its not true
651
     */
652
    pic->linesize[0]= mpi->stride[0];
653
    pic->linesize[1]= mpi->stride[1];
654
    pic->linesize[2]= mpi->stride[2];
655
656
    pic->opaque = mpi;
657
//printf("%X\n", (int)mpi->planes[0]);
658
#if 0
659
if(mpi->flags&MP_IMGFLAG_DIRECT)
660
    printf("D");
661
else if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
662
    printf("S");
663
else
664
    printf(".");
665
#endif
666
    if(pic->reference){
667
        pic->age= ctx->ip_age[0];
668
        
669
        ctx->ip_age[0]= ctx->ip_age[1]+1;
670
        ctx->ip_age[1]= 1;
671
        ctx->b_age++;
672
    }else{
673
        pic->age= ctx->b_age;
674
    
675
        ctx->ip_age[0]++;
676
        ctx->ip_age[1]++;
677
        ctx->b_age=1;
678
    }
679
    pic->type= FF_BUFFER_TYPE_USER;
680
    return 0;
681
}
682
683
static void release_buffer(struct AVCodecContext *avctx, AVFrame *pic){
684
    mp_image_t* mpi= pic->opaque;
685
    sh_video_t * sh = avctx->opaque;
686
    vd_ffmpeg_ctx *ctx = sh->context;
687
    int i;
688
689
//printf("release buffer %d %d %d\n", mpi ? mpi->flags&MP_IMGFLAG_PRESERVE : -99, ctx->ip_count, ctx->b_count); 
690
691
  if(ctx->ip_count <= 2 && ctx->b_count<=1){
692
    if(mpi->flags&MP_IMGFLAG_PRESERVE)
693
        ctx->ip_count--;
694
    else
695
        ctx->b_count--;
696
  }
697
698
	// Palette support: free palette buffer allocated in get_buffer
699
	if ( mpi && (mpi->bpp == 8))
700
		av_freep(&mpi->planes[1]);
701
702
    if(pic->type!=FF_BUFFER_TYPE_USER){
703
        avcodec_default_release_buffer(avctx, pic);
704
        return;
705
    }
706
707
    for(i=0; i<4; i++){
708
        pic->data[i]= NULL;
709
    }
710
//printf("R%X %X\n", pic->linesize[0], pic->data[0]);
711
}
712
713
// copypaste from demux_real.c - it should match to get it working!
714
//FIXME put into some header
715
typedef struct dp_hdr_s {
716
    uint32_t chunks;	// number of chunks
717
    uint32_t timestamp; // timestamp from packet header
718
    uint32_t len;	// length of actual data
719
    uint32_t chunktab;	// offset to chunk offset array
720
} dp_hdr_t;
721
4 by Mario Limonciello
Update to 1.0rc2
722
void swap_palette(void *pal) {
723
    int i;
724
    uint32_t *p = pal;
725
    for (i = 0; i < AVPALETTE_COUNT; i++)
726
        p[i] = le2me_32(p[i]);
727
}
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
728
729
// decode a frame
730
static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
731
    int got_picture=0;
732
    int ret;
733
    vd_ffmpeg_ctx *ctx = sh->context;
734
    AVFrame *pic= ctx->pic;
735
    AVCodecContext *avctx = ctx->avctx;
736
    mp_image_t* mpi=NULL;
737
    int dr1= ctx->do_dr1;
738
739
    if(len<=0) return NULL; // skipped frame
740
741
//ffmpeg interlace (mpeg2) bug have been fixed. no need of -noslices
742
    if (!dr1)
743
    avctx->draw_horiz_band=NULL;
744
    avctx->opaque=sh;
745
    if(ctx->vo_inited && !(flags&3) && !dr1){
746
	mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, MP_IMGFLAG_PRESERVE |
747
	    (ctx->do_slices?MP_IMGFLAG_DRAW_CALLBACK:0),
748
	    sh->disp_w, sh->disp_h);
749
	if(mpi && mpi->flags&MP_IMGFLAG_DRAW_CALLBACK){
750
	    // vd core likes slices!
751
	    avctx->draw_horiz_band=draw_slice;
752
	}
753
    }
754
755
    avctx->hurry_up=(flags&3)?((flags&2)?2:1):0;
756
4 by Mario Limonciello
Update to 1.0rc2
757
    if(sh->ds->demuxer->type != DEMUXER_TYPE_LAVF)
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
758
    if(   sh->format == mmioFOURCC('R', 'V', '1', '0')
759
       || sh->format == mmioFOURCC('R', 'V', '1', '3')
760
       || sh->format == mmioFOURCC('R', 'V', '2', '0')
761
       || sh->format == mmioFOURCC('R', 'V', '3', '0')
762
       || sh->format == mmioFOURCC('R', 'V', '4', '0'))
4 by Mario Limonciello
Update to 1.0rc2
763
    if(sh->bih->biSize>=sizeof(*sh->bih)+8){
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
764
        int i;
765
        dp_hdr_t *hdr= (dp_hdr_t*)data;
4 by Mario Limonciello
Update to 1.0rc2
766
        uint32_t *offsets = (uint32_t*)(data + hdr->chunktab) + 1;
767
        char *end = data + len;
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
768
769
        if(avctx->slice_offset==NULL) 
770
            avctx->slice_offset= av_malloc(sizeof(int)*1000);
771
        
772
//        for(i=0; i<25; i++) printf("%02X ", ((uint8_t*)data)[i]);
773
        
4 by Mario Limonciello
Update to 1.0rc2
774
        avctx->slice_count= FFMIN(hdr->chunks+1, 1000);
775
        for(i=0; i<avctx->slice_count && end >= &offsets[2*i+1]; i++)
776
            avctx->slice_offset[i]= offsets[2*i];
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
777
	len=hdr->len;
778
        data+= sizeof(dp_hdr_t);
779
    }
780
781
    mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "vd_ffmpeg data: %04x, %04x, %04x, %04x\n",
782
           ((int *)data)[0], ((int *)data)[1], ((int *)data)[2], ((int *)data)[3]);
783
    ret = avcodec_decode_video(avctx, pic,
784
	     &got_picture, data, len);
785
786
    dr1= ctx->do_dr1;
787
    if(ret<0) mp_msg(MSGT_DECVIDEO,MSGL_WARN, "Error while decoding frame!\n");
788
//printf("repeat: %d\n", pic->repeat_pict);
789
//-- vstats generation
790
    while(lavc_param_vstats){ // always one time loop
791
        static FILE *fvstats=NULL;
792
        char filename[20];
793
        static long long int all_len=0;
794
        static int frame_number=0;
795
        static double all_frametime=0.0;
796
        AVFrame *pic= avctx->coded_frame;
797
	double quality=0.0;
798
799
        if(!fvstats) {
800
            time_t today2;
801
            struct tm *today;
802
            today2 = time(NULL);
803
            today = localtime(&today2);
804
            sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
805
                today->tm_min, today->tm_sec);
806
            fvstats = fopen(filename,"w");
807
            if(!fvstats) {
808
                perror("fopen");
809
                lavc_param_vstats=0; // disable block
810
                break;
811
                /*exit(1);*/
812
            }
813
        }
814
815
	// average MB quantizer
816
	{
817
	    int x, y;
818
	    int w = ((avctx->width  << lavc_param_lowres)+15) >> 4;
819
	    int h = ((avctx->height << lavc_param_lowres)+15) >> 4;
820
	    int8_t *q = pic->qscale_table;
821
	    for( y = 0; y < h; y++ ) {
822
		for( x = 0; x < w; x++ )
823
		    quality += (double)*(q+x);
824
		q += pic->qstride;
825
	    }
826
	    quality /= w * h;
827
	}
828
829
        all_len+=len;
830
        all_frametime+=sh->frametime;
831
        fprintf(fvstats, "frame= %5d q= %2.2f f_size= %6d s_size= %8.0fkB ",
832
            ++frame_number, quality, len, (double)all_len/1024);
833
        fprintf(fvstats, "time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
834
           all_frametime, (double)(len*8)/sh->frametime/1000.0,
835
           (double)(all_len*8)/all_frametime/1000.0);
836
	switch(pic->pict_type){
837
	case FF_I_TYPE:
838
            fprintf(fvstats, "type= I\n");
839
	    break;
840
	case FF_P_TYPE:
841
            fprintf(fvstats, "type= P\n");
842
	    break;
843
	case FF_S_TYPE:
844
            fprintf(fvstats, "type= S\n");
845
	    break;
846
	case FF_B_TYPE:
847
            fprintf(fvstats, "type= B\n");
848
	    break;
849
	default:
850
            fprintf(fvstats, "type= ? (%d)\n", pic->pict_type);
851
	    break;
852
	}
853
        
854
        ctx->qp_stat[(int)(quality+0.5)]++;
855
        ctx->qp_sum += quality;
856
        ctx->inv_qp_sum += 1.0/(double)quality;
857
        
858
        break;
859
    }
860
//--
861
862
    if(!got_picture) return NULL;	// skipped image
863
864
    if(init_vo(sh,avctx->pix_fmt) < 0) return NULL;
865
866
    if(dr1 && pic->opaque){
867
        mpi= (mp_image_t*)pic->opaque;
868
    }
869
        
870
    if(!mpi)
871
    mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, MP_IMGFLAG_PRESERVE,
872
	avctx->width, avctx->height);
873
    if(!mpi){	// temporary!
2 by Reinhard Tartler
upgrade to pre8
874
	mp_msg(MSGT_DECVIDEO, MSGL_WARN, MSGTR_MPCODECS_CouldntAllocateImageForCodec);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
875
	return NULL;
876
    }
877
    
878
    if(!dr1){
879
        mpi->planes[0]=pic->data[0];
880
        mpi->planes[1]=pic->data[1];
881
        mpi->planes[2]=pic->data[2];
882
        mpi->stride[0]=pic->linesize[0];
883
        mpi->stride[1]=pic->linesize[1];
884
        mpi->stride[2]=pic->linesize[2];
885
    }
886
    
887
    if (!mpi->planes[0])
888
	return NULL;
889
890
    if(avctx->pix_fmt==PIX_FMT_YUV422P && mpi->chroma_y_shift==1){
891
	// we have 422p but user wants 420p
892
	mpi->stride[1]*=2;
893
	mpi->stride[2]*=2;
894
    }
895
    
4 by Mario Limonciello
Update to 1.0rc2
896
#ifdef WORDS_BIGENDIAN
897
    // FIXME: this might cause problems for buffers with FF_BUFFER_HINTS_PRESERVE
898
    if (mpi->bpp == 8)
899
        swap_palette(mpi->planes[1]);
900
#endif
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
901
/* to comfirm with newer lavc style */
902
    mpi->qscale =pic->qscale_table;
903
    mpi->qstride=pic->qstride;
904
    mpi->pict_type=pic->pict_type;
905
    mpi->qscale_type= pic->qscale_type;
906
    mpi->fields = MP_IMGFIELD_ORDERED;
907
    if(pic->interlaced_frame) mpi->fields |= MP_IMGFIELD_INTERLACED;
908
    if(pic->top_field_first ) mpi->fields |= MP_IMGFIELD_TOP_FIRST;    
909
    if(pic->repeat_pict == 1) mpi->fields |= MP_IMGFIELD_REPEAT_FIRST;
3 by William Grant
Update to 1.0rc1.
910
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
911
    return mpi;
912
}
913
4 by Mario Limonciello
Update to 1.0rc2
914
#ifdef HAVE_XVMC
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
915
static enum PixelFormat get_format(struct AVCodecContext * avctx, 
916
                                    const enum PixelFormat * fmt){
917
sh_video_t * sh = avctx->opaque;
918
int i;
919
920
    if(avctx->xvmc_acceleration){
4 by Mario Limonciello
Update to 1.0rc2
921
        vd_ffmpeg_ctx *ctx = sh->context;
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
922
        avctx->get_buffer= mc_get_buffer;
923
        avctx->release_buffer= mc_release_buffer;
924
        avctx->draw_horiz_band = mc_render_slice;
2 by Reinhard Tartler
upgrade to pre8
925
        mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_MPCODECS_XVMCAcceleratedMPEG2);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
926
        assert(ctx->do_dr1);//these are must to!
927
        assert(ctx->do_slices); //it is (vo_)ffmpeg bug if this fails
928
        avctx->flags|= CODEC_FLAG_EMU_EDGE;//do i need that??!!
929
        avctx->slice_flags=SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
930
    }
931
    for(i=0;fmt[i]!=-1;i++){
2 by Reinhard Tartler
upgrade to pre8
932
        mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_MPCODECS_TryingPixfmt,i);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
933
        if( init_vo(sh,fmt[i]) >= 0)
934
	    return fmt[i];
935
    }
936
    return fmt[0];
937
}
938
939
static int mc_get_buffer(AVCodecContext *avctx, AVFrame *pic){
940
    sh_video_t * sh = avctx->opaque;
941
    vd_ffmpeg_ctx *ctx = sh->context;
942
    mp_image_t* mpi=NULL;
943
    xvmc_render_state_t * render;
944
    int flags= MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE| 
945
               MP_IMGFLAG_DRAW_CALLBACK;
946
    
947
//  printf("vd_ffmpeg::mc_get_buffer (xvmc) %d %d %d\n", pic->reference, ctx->ip_count, ctx->b_count);
948
    if(!avctx->xvmc_acceleration){
2 by Reinhard Tartler
upgrade to pre8
949
        mp_msg(MSGT_DECVIDEO, MSGL_INFO, MSGTR_MPCODECS_McGetBufferShouldWorkOnlyWithXVMC);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
950
        assert(0);
951
        exit(1);
952
//        return -1;//!!fixme check error conditions
953
    }
954
    assert(avctx->draw_horiz_band == mc_render_slice);
955
    assert(avctx->release_buffer == mc_release_buffer);
2 by Reinhard Tartler
upgrade to pre8
956
    if( mp_msg_test(MSGT_DECVIDEO,MSGL_DBG5) )
957
        mp_msg(MSGT_DECVIDEO, MSGL_DBG5, "vd_ffmpeg::mc_get_buffer\n");
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
958
959
    if(init_vo(sh,avctx->pix_fmt) < 0){
2 by Reinhard Tartler
upgrade to pre8
960
        mp_msg(MSGT_DECVIDEO, MSGL_WARN, MSGTR_MPCODECS_UnexpectedInitVoError);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
961
        exit(1);
962
//        return -1;//!!fixme check error conditions
963
    }
964
965
966
967
    if(!pic->reference){
968
        ctx->b_count++;
969
    }else{
970
        ctx->ip_count++;
971
        flags|= MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE;
972
    }
973
974
    mpi= mpcodecs_get_image(sh, MP_IMGTYPE_IPB,flags ,
975
                            avctx->width, avctx->height);
976
    if(mpi==NULL){
2 by Reinhard Tartler
upgrade to pre8
977
        mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_MPCODECS_UnrecoverableErrorRenderBuffersNotTaken);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
978
        assert(0);
979
        exit(1);
980
//        return -1;//!!fixme check error conditions in ffmpeg
981
    };
982
    
983
    if( (mpi->flags & MP_IMGFLAG_DIRECT) == 0){
2 by Reinhard Tartler
upgrade to pre8
984
        mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_MPCODECS_OnlyBuffersAllocatedByVoXvmcAllowed);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
985
        assert(0);
986
        exit(1);
987
//        return -1;//!!fixme check error conditions in ffmpeg
988
    }
989
    
990
    pic->data[0]= mpi->planes[0];
991
    pic->data[1]= mpi->planes[1];
992
    pic->data[2]= mpi->planes[2];
993
994
995
    /* Note, some (many) codecs in libavcodec must have stride1==stride2 && no changes between frames
996
     * lavc will check that and die with an error message, if its not true
997
     */
998
    pic->linesize[0]= mpi->stride[0];
999
    pic->linesize[1]= mpi->stride[1];
1000
    pic->linesize[2]= mpi->stride[2];
1001
1002
    pic->opaque = mpi;
1003
1004
    if(pic->reference){
1005
    //I or P frame
1006
        pic->age= ctx->ip_age[0];
1007
        
1008
        ctx->ip_age[0]= ctx->ip_age[1]+1;
1009
        ctx->ip_age[1]= 1;
1010
        ctx->b_age++;
1011
    }else{
1012
    //B frame
1013
        pic->age= ctx->b_age;
1014
    
1015
        ctx->ip_age[0]++;
1016
        ctx->ip_age[1]++;
1017
        ctx->b_age=1;
1018
    }
1019
1020
    pic->type= FF_BUFFER_TYPE_USER;
1021
1022
    render=(xvmc_render_state_t*)mpi->priv;//same as data[2]
2 by Reinhard Tartler
upgrade to pre8
1023
    if( mp_msg_test(MSGT_DECVIDEO,MSGL_DBG5) )
1024
        mp_msg(MSGT_DECVIDEO, MSGL_DBG5, "vd_ffmpeg::mc_get_buffer (render=%p)\n",render);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
1025
    assert(render != 0);
1026
    assert(render->magic == MP_XVMC_RENDER_MAGIC);
1027
    render->state |= MP_XVMC_STATE_PREDICTION;
1028
    return 0;
1029
}
1030
1031
1032
static void mc_release_buffer(AVCodecContext *avctx, AVFrame *pic){
1033
    mp_image_t* mpi= pic->opaque;
1034
    sh_video_t * sh = avctx->opaque;
1035
    vd_ffmpeg_ctx *ctx = sh->context;
1036
    xvmc_render_state_t * render;
1037
    int i;
1038
1039
1040
    if(ctx->ip_count <= 2 && ctx->b_count<=1){
1041
        if(mpi->flags&MP_IMGFLAG_PRESERVE)
1042
            ctx->ip_count--;
1043
        else
1044
            ctx->b_count--;
1045
    }
1046
1047
//printf("R%X %X\n", pic->linesize[0], pic->data[0]);
1048
//mark the surface as not requared for prediction
1049
    render=(xvmc_render_state_t*)pic->data[2];//same as mpi->priv
2 by Reinhard Tartler
upgrade to pre8
1050
    if( mp_msg_test(MSGT_DECVIDEO,MSGL_DBG5) )
1051
        mp_msg(MSGT_DECVIDEO, MSGL_DBG5, "vd_ffmpeg::mc_release_buffer (render=%p)\n",render);
1 by Reinhard Tartler
imported mplayer_0.99+1.0pre7try2+cvs20060117
1052
    assert(render!=NULL);
1053
    assert(render->magic==MP_XVMC_RENDER_MAGIC);
1054
    render->state&=~MP_XVMC_STATE_PREDICTION;
1055
    for(i=0; i<4; i++){
1056
        pic->data[i]= NULL;
1057
    }
1058
}
1059
1060
static void mc_render_slice(struct AVCodecContext *s,
1061
                	AVFrame *src, int offset[4],
1062
                	int y, int type, int height){
1063
int width= s->width;
1064
sh_video_t * sh = s->opaque;
1065
uint8_t *source[3]= {src->data[0], src->data[1], src->data[2]};
1066
    
1067
    assert(src->linesize[0]==0 && src->linesize[1]==0 && src->linesize[2]==0);
1068
    assert(offset[0]==0 && offset[1]==0 && offset[2]==0);
1069
    
1070
    mpcodecs_draw_slice (sh, source, src->linesize, width, height, 0, y);
1071
1072
}
1073
1074
#endif // HAVE_XVMC