00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00027 #include "avfilter.h"
00028 #include "video.h"
00029 #include "formats.h"
00030 #include "internal.h"
00031 #include "libavutil/avassert.h"
00032 #include "libavutil/pixdesc.h"
00033 #include "libavutil/intreadwrite.h"
00034 #include "libavutil/imgutils.h"
00035
00036 #include "libmpcodecs/vf.h"
00037 #include "libmpcodecs/img_format.h"
00038 #include "libmpcodecs/cpudetect.h"
00039 #include "libmpcodecs/vd_ffmpeg.h"
00040 #include "libmpcodecs/vf_scale.h"
00041 #include "libmpcodecs/libvo/fastmemcpy.h"
00042
00043 #include "libswscale/swscale.h"
00044
00045
00046
00047
00048 static const struct {
00049 int fmt;
00050 enum AVPixelFormat pix_fmt;
00051 } conversion_map[] = {
00052 {IMGFMT_ARGB, AV_PIX_FMT_ARGB},
00053 {IMGFMT_BGRA, AV_PIX_FMT_BGRA},
00054 {IMGFMT_BGR24, AV_PIX_FMT_BGR24},
00055 {IMGFMT_BGR16BE, AV_PIX_FMT_RGB565BE},
00056 {IMGFMT_BGR16LE, AV_PIX_FMT_RGB565LE},
00057 {IMGFMT_BGR15BE, AV_PIX_FMT_RGB555BE},
00058 {IMGFMT_BGR15LE, AV_PIX_FMT_RGB555LE},
00059 {IMGFMT_BGR12BE, AV_PIX_FMT_RGB444BE},
00060 {IMGFMT_BGR12LE, AV_PIX_FMT_RGB444LE},
00061 {IMGFMT_BGR8, AV_PIX_FMT_RGB8},
00062 {IMGFMT_BGR4, AV_PIX_FMT_RGB4},
00063 {IMGFMT_BGR1, AV_PIX_FMT_MONOBLACK},
00064 {IMGFMT_RGB1, AV_PIX_FMT_MONOBLACK},
00065 {IMGFMT_RG4B, AV_PIX_FMT_BGR4_BYTE},
00066 {IMGFMT_BG4B, AV_PIX_FMT_RGB4_BYTE},
00067 {IMGFMT_RGB48LE, AV_PIX_FMT_RGB48LE},
00068 {IMGFMT_RGB48BE, AV_PIX_FMT_RGB48BE},
00069 {IMGFMT_ABGR, AV_PIX_FMT_ABGR},
00070 {IMGFMT_RGBA, AV_PIX_FMT_RGBA},
00071 {IMGFMT_RGB24, AV_PIX_FMT_RGB24},
00072 {IMGFMT_RGB16BE, AV_PIX_FMT_BGR565BE},
00073 {IMGFMT_RGB16LE, AV_PIX_FMT_BGR565LE},
00074 {IMGFMT_RGB15BE, AV_PIX_FMT_BGR555BE},
00075 {IMGFMT_RGB15LE, AV_PIX_FMT_BGR555LE},
00076 {IMGFMT_RGB12BE, AV_PIX_FMT_BGR444BE},
00077 {IMGFMT_RGB12LE, AV_PIX_FMT_BGR444LE},
00078 {IMGFMT_RGB8, AV_PIX_FMT_BGR8},
00079 {IMGFMT_RGB4, AV_PIX_FMT_BGR4},
00080 {IMGFMT_BGR8, AV_PIX_FMT_PAL8},
00081 {IMGFMT_YUY2, AV_PIX_FMT_YUYV422},
00082 {IMGFMT_UYVY, AV_PIX_FMT_UYVY422},
00083 {IMGFMT_NV12, AV_PIX_FMT_NV12},
00084 {IMGFMT_NV21, AV_PIX_FMT_NV21},
00085 {IMGFMT_Y800, AV_PIX_FMT_GRAY8},
00086 {IMGFMT_Y8, AV_PIX_FMT_GRAY8},
00087 {IMGFMT_YVU9, AV_PIX_FMT_YUV410P},
00088 {IMGFMT_IF09, AV_PIX_FMT_YUV410P},
00089 {IMGFMT_YV12, AV_PIX_FMT_YUV420P},
00090 {IMGFMT_I420, AV_PIX_FMT_YUV420P},
00091 {IMGFMT_IYUV, AV_PIX_FMT_YUV420P},
00092 {IMGFMT_411P, AV_PIX_FMT_YUV411P},
00093 {IMGFMT_422P, AV_PIX_FMT_YUV422P},
00094 {IMGFMT_444P, AV_PIX_FMT_YUV444P},
00095 {IMGFMT_440P, AV_PIX_FMT_YUV440P},
00096
00097 {IMGFMT_420A, AV_PIX_FMT_YUVA420P},
00098
00099 {IMGFMT_420P16_LE, AV_PIX_FMT_YUV420P16LE},
00100 {IMGFMT_420P16_BE, AV_PIX_FMT_YUV420P16BE},
00101 {IMGFMT_422P16_LE, AV_PIX_FMT_YUV422P16LE},
00102 {IMGFMT_422P16_BE, AV_PIX_FMT_YUV422P16BE},
00103 {IMGFMT_444P16_LE, AV_PIX_FMT_YUV444P16LE},
00104 {IMGFMT_444P16_BE, AV_PIX_FMT_YUV444P16BE},
00105
00106
00107
00108
00109 {IMGFMT_YV12, AV_PIX_FMT_YUVJ420P},
00110 {IMGFMT_422P, AV_PIX_FMT_YUVJ422P},
00111 {IMGFMT_444P, AV_PIX_FMT_YUVJ444P},
00112 {IMGFMT_440P, AV_PIX_FMT_YUVJ440P},
00113
00114 {IMGFMT_XVMC_MOCO_MPEG2, AV_PIX_FMT_XVMC_MPEG2_MC},
00115 {IMGFMT_XVMC_IDCT_MPEG2, AV_PIX_FMT_XVMC_MPEG2_IDCT},
00116 {IMGFMT_VDPAU_MPEG1, AV_PIX_FMT_VDPAU_MPEG1},
00117 {IMGFMT_VDPAU_MPEG2, AV_PIX_FMT_VDPAU_MPEG2},
00118 {IMGFMT_VDPAU_H264, AV_PIX_FMT_VDPAU_H264},
00119 {IMGFMT_VDPAU_WMV3, AV_PIX_FMT_VDPAU_WMV3},
00120 {IMGFMT_VDPAU_VC1, AV_PIX_FMT_VDPAU_VC1},
00121 {IMGFMT_VDPAU_MPEG4, AV_PIX_FMT_VDPAU_MPEG4},
00122 {0, AV_PIX_FMT_NONE}
00123 };
00124
00125 extern const vf_info_t ff_vf_info_detc;
00126 extern const vf_info_t ff_vf_info_dint;
00127 extern const vf_info_t ff_vf_info_divtc;
00128 extern const vf_info_t ff_vf_info_down3dright;
00129 extern const vf_info_t ff_vf_info_dsize;
00130 extern const vf_info_t ff_vf_info_eq2;
00131 extern const vf_info_t ff_vf_info_eq;
00132 extern const vf_info_t ff_vf_info_fil;
00133
00134 extern const vf_info_t ff_vf_info_fspp;
00135 extern const vf_info_t ff_vf_info_harddup;
00136 extern const vf_info_t ff_vf_info_il;
00137 extern const vf_info_t ff_vf_info_ilpack;
00138 extern const vf_info_t ff_vf_info_ivtc;
00139 extern const vf_info_t ff_vf_info_kerndeint;
00140 extern const vf_info_t ff_vf_info_mcdeint;
00141 extern const vf_info_t ff_vf_info_noise;
00142 extern const vf_info_t ff_vf_info_ow;
00143 extern const vf_info_t ff_vf_info_perspective;
00144 extern const vf_info_t ff_vf_info_phase;
00145 extern const vf_info_t ff_vf_info_pp7;
00146 extern const vf_info_t ff_vf_info_pp;
00147 extern const vf_info_t ff_vf_info_pullup;
00148 extern const vf_info_t ff_vf_info_qp;
00149 extern const vf_info_t ff_vf_info_sab;
00150 extern const vf_info_t ff_vf_info_softpulldown;
00151 extern const vf_info_t ff_vf_info_softskip;
00152 extern const vf_info_t ff_vf_info_spp;
00153 extern const vf_info_t ff_vf_info_stereo3d;
00154 extern const vf_info_t ff_vf_info_telecine;
00155 extern const vf_info_t ff_vf_info_tinterlace;
00156 extern const vf_info_t ff_vf_info_unsharp;
00157 extern const vf_info_t ff_vf_info_uspp;
00158
00159
00160 static const vf_info_t* const filters[]={
00161 &ff_vf_info_detc,
00162 &ff_vf_info_dint,
00163 &ff_vf_info_divtc,
00164 &ff_vf_info_down3dright,
00165 &ff_vf_info_dsize,
00166 &ff_vf_info_eq2,
00167 &ff_vf_info_eq,
00168 &ff_vf_info_fil,
00169
00170 &ff_vf_info_fspp,
00171 &ff_vf_info_harddup,
00172 &ff_vf_info_il,
00173 &ff_vf_info_ilpack,
00174 &ff_vf_info_ivtc,
00175 &ff_vf_info_kerndeint,
00176 &ff_vf_info_mcdeint,
00177 &ff_vf_info_noise,
00178 &ff_vf_info_ow,
00179 &ff_vf_info_perspective,
00180 &ff_vf_info_phase,
00181 &ff_vf_info_pp,
00182 &ff_vf_info_pp7,
00183 &ff_vf_info_pullup,
00184 &ff_vf_info_qp,
00185 &ff_vf_info_sab,
00186 &ff_vf_info_softpulldown,
00187 &ff_vf_info_softskip,
00188 &ff_vf_info_spp,
00189 &ff_vf_info_stereo3d,
00190 &ff_vf_info_telecine,
00191 &ff_vf_info_tinterlace,
00192 &ff_vf_info_unsharp,
00193 &ff_vf_info_uspp,
00194
00195 NULL
00196 };
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 CpuCaps ff_gCpuCaps;
00221
00222
00223 static void ff_sws_getFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam, SwsFilter **dstFilterParam)
00224 {
00225 static int firstTime=1;
00226 *flags=0;
00227
00228 #if ARCH_X86
00229 if(ff_gCpuCaps.hasMMX)
00230 __asm__ volatile("emms\n\t"::: "memory");
00231 #endif
00232 if(firstTime)
00233 {
00234 firstTime=0;
00235 *flags= SWS_PRINT_INFO;
00236 }
00237 else if( ff_mp_msg_test(MSGT_VFILTER,MSGL_DBG2) ) *flags= SWS_PRINT_INFO;
00238
00239 switch(SWS_BILINEAR)
00240 {
00241 case 0: *flags|= SWS_FAST_BILINEAR; break;
00242 case 1: *flags|= SWS_BILINEAR; break;
00243 case 2: *flags|= SWS_BICUBIC; break;
00244 case 3: *flags|= SWS_X; break;
00245 case 4: *flags|= SWS_POINT; break;
00246 case 5: *flags|= SWS_AREA; break;
00247 case 6: *flags|= SWS_BICUBLIN; break;
00248 case 7: *flags|= SWS_GAUSS; break;
00249 case 8: *flags|= SWS_SINC; break;
00250 case 9: *flags|= SWS_LANCZOS; break;
00251 case 10:*flags|= SWS_SPLINE; break;
00252 default:*flags|= SWS_BILINEAR; break;
00253 }
00254
00255 *srcFilterParam= NULL;
00256 *dstFilterParam= NULL;
00257 }
00258
00259
00260
00261 struct SwsContext *ff_sws_getContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
00262 {
00263 int flags, i;
00264 SwsFilter *dstFilterParam, *srcFilterParam;
00265 enum AVPixelFormat dfmt, sfmt;
00266
00267 for(i=0; conversion_map[i].fmt && dstFormat != conversion_map[i].fmt; i++);
00268 dfmt= conversion_map[i].pix_fmt;
00269 for(i=0; conversion_map[i].fmt && srcFormat != conversion_map[i].fmt; i++);
00270 sfmt= conversion_map[i].pix_fmt;
00271
00272 if (srcFormat == IMGFMT_RGB8 || srcFormat == IMGFMT_BGR8) sfmt = AV_PIX_FMT_PAL8;
00273 ff_sws_getFlagsAndFilterFromCmdLine(&flags, &srcFilterParam, &dstFilterParam);
00274
00275 return sws_getContext(srcW, srcH, sfmt, dstW, dstH, dfmt, flags , srcFilterParam, dstFilterParam, NULL);
00276 }
00277
00278 typedef struct {
00279 vf_instance_t vf;
00280 vf_instance_t next_vf;
00281 AVFilterContext *avfctx;
00282 int frame_returned;
00283 } MPContext;
00284
00285 void ff_mp_msg(int mod, int lev, const char *format, ... ){
00286 va_list va;
00287 va_start(va, format);
00288
00289 av_vlog(NULL, AV_LOG_DEBUG, format, va);
00290 va_end(va);
00291 }
00292
00293 int ff_mp_msg_test(int mod, int lev){
00294 return 123;
00295 }
00296
00297 void ff_init_avcodec(void)
00298 {
00299
00300 }
00301
00302
00303 void ff_vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
00304 dst->pict_type= src->pict_type;
00305 dst->fields = src->fields;
00306 dst->qscale_type= src->qscale_type;
00307 if(dst->width == src->width && dst->height == src->height){
00308 dst->qstride= src->qstride;
00309 dst->qscale= src->qscale;
00310 }
00311 }
00312
00313
00314 void ff_vf_next_draw_slice(struct vf_instance *vf,unsigned char** src, int * stride,int w, int h, int x, int y){
00315 if (vf->next->draw_slice) {
00316 vf->next->draw_slice(vf->next,src,stride,w,h,x,y);
00317 return;
00318 }
00319 if (!vf->dmpi) {
00320 ff_mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
00321 return;
00322 }
00323 if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
00324 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,
00325 src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);
00326 return;
00327 }
00328 memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],
00329 w, h, vf->dmpi->stride[0], stride[0]);
00330 memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),
00331 src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
00332 memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),
00333 src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
00334 }
00335
00336
00337 void ff_vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
00338 int y;
00339 if(mpi->flags&MP_IMGFLAG_PLANAR){
00340 y0&=~1;h+=h&1;
00341 if(x0==0 && w==mpi->width){
00342
00343 memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
00344 memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
00345 memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
00346 } else
00347 for(y=y0;y<y0+h;y+=2){
00348 memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
00349 memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
00350 memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
00351 memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
00352 }
00353 return;
00354 }
00355
00356 for(y=y0;y<y0+h;y++){
00357 unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
00358 if(mpi->flags&MP_IMGFLAG_YUV){
00359 unsigned int* p=(unsigned int*) dst;
00360 int size=(mpi->bpp>>3)*w/4;
00361 int i;
00362 #if HAVE_BIGENDIAN
00363 #define CLEAR_PACKEDYUV_PATTERN 0x00800080
00364 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
00365 #else
00366 #define CLEAR_PACKEDYUV_PATTERN 0x80008000
00367 #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
00368 #endif
00369 if(mpi->flags&MP_IMGFLAG_SWAPPED){
00370 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
00371 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
00372 } else {
00373 for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
00374 for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
00375 }
00376 } else
00377 memset(dst,0,(mpi->bpp>>3)*w);
00378 }
00379 }
00380
00381 int ff_vf_next_query_format(struct vf_instance *vf, unsigned int fmt){
00382 return 1;
00383 }
00384
00385
00386 unsigned int ff_vf_match_csp(vf_instance_t** vfp,const unsigned int* list,unsigned int preferred){
00387 return preferred;
00388 }
00389
00390 mp_image_t* ff_vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
00391 MPContext *m= (MPContext*)(((uint8_t*)vf) - offsetof(MPContext, next_vf));
00392 mp_image_t* mpi=NULL;
00393 int w2;
00394 int number = mp_imgtype >> 16;
00395
00396 av_assert0(vf->next == NULL);
00397
00398
00399 if(vf->w==0 && w>0) vf->w=w;
00400 if(vf->h==0 && h>0) vf->h=h;
00401
00402 av_assert0(w == -1 || w >= vf->w);
00403 av_assert0(h == -1 || h >= vf->h);
00404 av_assert0(vf->w > 0);
00405 av_assert0(vf->h > 0);
00406
00407 av_log(m->avfctx, AV_LOG_DEBUG, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
00408
00409 if (w == -1) w = vf->w;
00410 if (h == -1) h = vf->h;
00411
00412 w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
00413
00414
00415
00416 switch(mp_imgtype & 0xff){
00417 case MP_IMGTYPE_EXPORT:
00418 if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=ff_new_mp_image(w2,h);
00419 mpi=vf->imgctx.export_images[0];
00420 break;
00421 case MP_IMGTYPE_STATIC:
00422 if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=ff_new_mp_image(w2,h);
00423 mpi=vf->imgctx.static_images[0];
00424 break;
00425 case MP_IMGTYPE_TEMP:
00426 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=ff_new_mp_image(w2,h);
00427 mpi=vf->imgctx.temp_images[0];
00428 break;
00429 case MP_IMGTYPE_IPB:
00430 if(!(mp_imgflag&MP_IMGFLAG_READABLE)){
00431 if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=ff_new_mp_image(w2,h);
00432 mpi=vf->imgctx.temp_images[0];
00433 break;
00434 }
00435 case MP_IMGTYPE_IP:
00436 if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=ff_new_mp_image(w2,h);
00437 mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
00438 vf->imgctx.static_idx^=1;
00439 break;
00440 case MP_IMGTYPE_NUMBERED:
00441 if (number == -1) {
00442 int i;
00443 for (i = 0; i < NUM_NUMBERED_MPI; i++)
00444 if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
00445 break;
00446 number = i;
00447 }
00448 if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
00449 if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = ff_new_mp_image(w2,h);
00450 mpi = vf->imgctx.numbered_images[number];
00451 mpi->number = number;
00452 break;
00453 }
00454 if(mpi){
00455 mpi->type=mp_imgtype;
00456 mpi->w=vf->w; mpi->h=vf->h;
00457
00458
00459 mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
00460
00461 mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK|MP_IMGFLAG_RGB_PALETTE);
00462 if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
00463 if(mpi->width!=w2 || mpi->height!=h){
00464
00465 if(mpi->flags&MP_IMGFLAG_ALLOCATED){
00466 if(mpi->width<w2 || mpi->height<h){
00467
00468 av_free(mpi->planes[0]);
00469 mpi->flags&=~MP_IMGFLAG_ALLOCATED;
00470 ff_mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
00471 }
00472
00473 } {
00474 mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
00475 mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
00476 }
00477 }
00478 if(!mpi->bpp) ff_mp_image_setfmt(mpi,outfmt);
00479 if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
00480
00481 av_assert0(!vf->get_image);
00482
00483 if(vf->get_image) vf->get_image(vf,mpi);
00484
00485 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
00486
00487 if (!mpi->bpp) {
00488 ff_mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
00489 "ff_vf_get_image: Tried to allocate a format that can not be allocated!\n");
00490 return NULL;
00491 }
00492
00493
00494 if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
00495 int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
00496 mpi->flags&MP_IMGFLAG_YUV) ?
00497 (8<<mpi->chroma_x_shift)-1 : 15;
00498 w2=((w+align)&(~align));
00499 if(mpi->width!=w2){
00500 #if 0
00501
00502 int flags=vf->query_format(vf,outfmt);
00503 if(!(flags&3)) ff_mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? ff_vf_get_image{vf->query_format(outfmt)} failed!\n");
00504
00505 if(flags&VFCAP_ACCEPT_STRIDE){
00506 #endif
00507 mpi->width=w2;
00508 mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
00509
00510 }
00511 }
00512
00513 ff_mp_image_alloc_planes(mpi);
00514
00515 ff_vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
00516 }
00517 }
00518 av_assert0(!vf->start_slice);
00519 if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
00520 if(vf->start_slice) vf->start_slice(vf,mpi);
00521 if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
00522 ff_mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
00523 "NULL",
00524 (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
00525 ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
00526 (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
00527 mpi->width,mpi->height,mpi->bpp,
00528 (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
00529 (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
00530 mpi->bpp*mpi->width*mpi->height/8);
00531 ff_mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"(imgfmt: %x, planes: %p,%p,%p strides: %d,%d,%d, chroma: %dx%d, shift: h:%d,v:%d)\n",
00532 mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
00533 mpi->stride[0], mpi->stride[1], mpi->stride[2],
00534 mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
00535 mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
00536 }
00537
00538 mpi->qscale = NULL;
00539 mpi->usage_count++;
00540 }
00541
00542
00543
00544 return mpi;
00545 }
00546
00547
00548 int ff_vf_next_put_image(struct vf_instance *vf,mp_image_t *mpi, double pts){
00549 MPContext *m= (void*)vf;
00550 AVFilterLink *outlink = m->avfctx->outputs[0];
00551 AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
00552 AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
00553 int i;
00554
00555 av_assert0(vf->next);
00556
00557 av_log(m->avfctx, AV_LOG_DEBUG, "ff_vf_next_put_image\n");
00558
00559 if (!pic || !picref)
00560 goto fail;
00561
00562 picref->buf = pic;
00563 picref->buf->free= (void*)av_free;
00564 if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
00565 goto fail;
00566
00567 pic->w = picref->video->w = mpi->w;
00568 pic->h = picref->video->h = mpi->h;
00569
00570
00571 picref->perms = AV_PERM_READ | AV_PERM_REUSE2;
00572
00573 if(!(mpi->flags&MP_IMGFLAG_PRESERVE))
00574 picref->perms |= AV_PERM_WRITE;
00575
00576 pic->refcount = 1;
00577 picref->type = AVMEDIA_TYPE_VIDEO;
00578
00579 for(i=0; conversion_map[i].fmt && mpi->imgfmt != conversion_map[i].fmt; i++);
00580 pic->format = picref->format = conversion_map[i].pix_fmt;
00581
00582 memcpy(pic->data, mpi->planes, FFMIN(sizeof(pic->data) , sizeof(mpi->planes)));
00583 memcpy(pic->linesize, mpi->stride, FFMIN(sizeof(pic->linesize), sizeof(mpi->stride)));
00584 memcpy(picref->data, pic->data, sizeof(picref->data));
00585 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
00586
00587 if(pts != MP_NOPTS_VALUE)
00588 picref->pts= pts * av_q2d(outlink->time_base);
00589
00590 ff_filter_frame(outlink, picref);
00591 m->frame_returned++;
00592
00593 return 1;
00594 fail:
00595 if (picref && picref->video)
00596 av_free(picref->video);
00597 av_free(picref);
00598 av_free(pic);
00599 return 0;
00600 }
00601
00602 int ff_vf_next_config(struct vf_instance *vf,
00603 int width, int height, int d_width, int d_height,
00604 unsigned int voflags, unsigned int outfmt){
00605
00606 av_assert0(width>0 && height>0);
00607 vf->next->w = width; vf->next->h = height;
00608
00609 return 1;
00610 #if 0
00611 int flags=vf->next->query_format(vf->next,outfmt);
00612 if(!flags){
00613
00614
00615 return 0;
00616 }
00617 ff_mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
00618 miss=vf->default_reqs - (flags&vf->default_reqs);
00619 if(miss&VFCAP_ACCEPT_STRIDE){
00620
00621
00622 vf_instance_t* vf2=vf_open_filter(vf->next,"expand",NULL);
00623 if(!vf2) return 0;
00624 vf->next=vf2;
00625 }
00626 vf->next->w = width; vf->next->h = height;
00627 return 1;
00628 #endif
00629 }
00630
00631 int ff_vf_next_control(struct vf_instance *vf, int request, void* data){
00632 MPContext *m= (void*)vf;
00633 av_log(m->avfctx, AV_LOG_DEBUG, "Received control %d\n", request);
00634 return 0;
00635 }
00636
00637 static int vf_default_query_format(struct vf_instance *vf, unsigned int fmt){
00638 MPContext *m= (void*)vf;
00639 int i;
00640 av_log(m->avfctx, AV_LOG_DEBUG, "query %X\n", fmt);
00641
00642 for(i=0; conversion_map[i].fmt; i++){
00643 if(fmt==conversion_map[i].fmt)
00644 return 1;
00645 }
00646 return 0;
00647 }
00648
00649
00650 static av_cold int init(AVFilterContext *ctx, const char *args)
00651 {
00652 MPContext *m = ctx->priv;
00653 char name[256];
00654 int i;
00655
00656 m->avfctx= ctx;
00657
00658 if(!args || 1!=sscanf(args, "%255[^:=]", name)){
00659 av_log(ctx, AV_LOG_ERROR, "Invalid parameter.\n");
00660 return AVERROR(EINVAL);
00661 }
00662 args += strlen(name);
00663 if (args[0] == '=')
00664 args++;
00665
00666 for(i=0; ;i++){
00667 if(!filters[i] || !strcmp(name, filters[i]->name))
00668 break;
00669 }
00670
00671 if(!filters[i]){
00672 av_log(ctx, AV_LOG_ERROR, "Unknown filter %s\n", name);
00673 return AVERROR(EINVAL);
00674 }
00675
00676 av_log(ctx, AV_LOG_WARNING,
00677 "'%s' is a wrapped MPlayer filter (libmpcodecs). This filter may be removed\n"
00678 "once it has been ported to a native libavfilter.\n", name);
00679
00680 memset(&m->vf,0,sizeof(m->vf));
00681 m->vf.info= filters[i];
00682
00683 m->vf.next = &m->next_vf;
00684 m->vf.put_image = ff_vf_next_put_image;
00685 m->vf.config = ff_vf_next_config;
00686 m->vf.query_format= vf_default_query_format;
00687 m->vf.control = ff_vf_next_control;
00688 m->vf.default_caps=VFCAP_ACCEPT_STRIDE;
00689 m->vf.default_reqs=0;
00690 if(m->vf.info->opts)
00691 av_log(ctx, AV_LOG_ERROR, "opts / m_struct_set is unsupported\n");
00692 #if 0
00693 if(vf->info->opts) {
00694 const m_struct_t* st = vf->info->opts;
00695 void* vf_priv = m_struct_alloc(st);
00696 int n;
00697 for(n = 0 ; args && args[2*n] ; n++)
00698 m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
00699 vf->priv = vf_priv;
00700 args = NULL;
00701 } else
00702 if(args && !strcmp(args[0],"_oldargs_"))
00703 args = (char**)args[1];
00704 else
00705 args = NULL;
00706 #endif
00707 if(m->vf.info->vf_open(&m->vf, args)<=0){
00708 av_log(ctx, AV_LOG_ERROR, "vf_open() of %s with arg=%s failed\n", name, args);
00709 return -1;
00710 }
00711
00712 return 0;
00713 }
00714
00715 static av_cold void uninit(AVFilterContext *ctx)
00716 {
00717 MPContext *m = ctx->priv;
00718 vf_instance_t *vf = &m->vf;
00719
00720 while(vf){
00721 vf_instance_t *next = vf->next;
00722 if(vf->uninit)
00723 vf->uninit(vf);
00724 ff_free_mp_image(vf->imgctx.static_images[0]);
00725 ff_free_mp_image(vf->imgctx.static_images[1]);
00726 ff_free_mp_image(vf->imgctx.temp_images[0]);
00727 ff_free_mp_image(vf->imgctx.export_images[0]);
00728 vf = next;
00729 }
00730 }
00731
00732 static int query_formats(AVFilterContext *ctx)
00733 {
00734 AVFilterFormats *avfmts=NULL;
00735 MPContext *m = ctx->priv;
00736 enum AVPixelFormat lastpixfmt = AV_PIX_FMT_NONE;
00737 int i;
00738
00739 for(i=0; conversion_map[i].fmt; i++){
00740 av_log(ctx, AV_LOG_DEBUG, "query: %X\n", conversion_map[i].fmt);
00741 if(m->vf.query_format(&m->vf, conversion_map[i].fmt)){
00742 av_log(ctx, AV_LOG_DEBUG, "supported,adding\n");
00743 if (conversion_map[i].pix_fmt != lastpixfmt) {
00744 ff_add_format(&avfmts, conversion_map[i].pix_fmt);
00745 lastpixfmt = conversion_map[i].pix_fmt;
00746 }
00747 }
00748 }
00749
00750 if (!avfmts)
00751 return -1;
00752
00753
00754 ff_set_common_formats(ctx, avfmts);
00755 return 0;
00756 }
00757
00758 static int config_inprops(AVFilterLink *inlink)
00759 {
00760 MPContext *m = inlink->dst->priv;
00761 int i;
00762 for(i=0; conversion_map[i].fmt && conversion_map[i].pix_fmt != inlink->format; i++);
00763
00764 av_assert0(conversion_map[i].fmt && inlink->w && inlink->h);
00765
00766 m->vf.fmt.have_configured = 1;
00767 m->vf.fmt.orig_height = inlink->h;
00768 m->vf.fmt.orig_width = inlink->w;
00769 m->vf.fmt.orig_fmt = conversion_map[i].fmt;
00770
00771 if(m->vf.config(&m->vf, inlink->w, inlink->h, inlink->w, inlink->h, 0, conversion_map[i].fmt)<=0)
00772 return -1;
00773
00774 return 0;
00775 }
00776
00777 static int config_outprops(AVFilterLink *outlink)
00778 {
00779 MPContext *m = outlink->src->priv;
00780
00781 outlink->w = m->next_vf.w;
00782 outlink->h = m->next_vf.h;
00783
00784 return 0;
00785 }
00786
00787 static int request_frame(AVFilterLink *outlink)
00788 {
00789 MPContext *m = outlink->src->priv;
00790 int ret;
00791
00792 av_log(m->avfctx, AV_LOG_DEBUG, "mp request_frame\n");
00793
00794 for(m->frame_returned=0; !m->frame_returned;){
00795 ret=ff_request_frame(outlink->src->inputs[0]);
00796 if(ret<0)
00797 break;
00798 }
00799
00800 av_log(m->avfctx, AV_LOG_DEBUG, "mp request_frame ret=%d\n", ret);
00801 return ret;
00802 }
00803
00804 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
00805 {
00806 MPContext *m = inlink->dst->priv;
00807 int i;
00808 double pts= MP_NOPTS_VALUE;
00809 mp_image_t* mpi = ff_new_mp_image(inpic->video->w, inpic->video->h);
00810
00811 if(inpic->pts != AV_NOPTS_VALUE)
00812 pts= inpic->pts / av_q2d(inlink->time_base);
00813
00814 for(i=0; conversion_map[i].fmt && conversion_map[i].pix_fmt != inlink->format; i++);
00815 ff_mp_image_setfmt(mpi,conversion_map[i].fmt);
00816
00817 memcpy(mpi->planes, inpic->data, FFMIN(sizeof(inpic->data) , sizeof(mpi->planes)));
00818 memcpy(mpi->stride, inpic->linesize, FFMIN(sizeof(inpic->linesize), sizeof(mpi->stride)));
00819
00820
00821
00822
00823 mpi->flags |= MP_IMGFLAG_READABLE;
00824 if(!(inpic->perms & AV_PERM_WRITE))
00825 mpi->flags |= MP_IMGFLAG_PRESERVE;
00826 if(m->vf.put_image(&m->vf, mpi, pts) == 0){
00827 av_log(m->avfctx, AV_LOG_DEBUG, "put_image() says skip\n");
00828 }else{
00829 avfilter_unref_buffer(inpic);
00830 }
00831 ff_free_mp_image(mpi);
00832 return 0;
00833 }
00834
00835 static const AVFilterPad mp_inputs[] = {
00836 {
00837 .name = "default",
00838 .type = AVMEDIA_TYPE_VIDEO,
00839 .filter_frame = filter_frame,
00840 .config_props = config_inprops,
00841 .min_perms = AV_PERM_READ,
00842 },
00843 { NULL }
00844 };
00845
00846 static const AVFilterPad mp_outputs[] = {
00847 {
00848 .name = "default",
00849 .type = AVMEDIA_TYPE_VIDEO,
00850 .request_frame = request_frame,
00851 .config_props = config_outprops,
00852 },
00853 { NULL }
00854 };
00855
00856 AVFilter avfilter_vf_mp = {
00857 .name = "mp",
00858 .description = NULL_IF_CONFIG_SMALL("Apply a libmpcodecs filter to the input video."),
00859 .init = init,
00860 .uninit = uninit,
00861 .priv_size = sizeof(MPContext),
00862 .query_formats = query_formats,
00863 .inputs = mp_inputs,
00864 .outputs = mp_outputs,
00865 };