You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

845 lines
28KB

  1. /*
  2. * Copyright (c) 2011 Michael Niedermayer
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Parts of this file have been stolen from mplayer
  21. */
  22. /**
  23. * @file
  24. */
  25. #include "avfilter.h"
  26. #include "video.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libmpcodecs/vf.h"
  34. #include "libmpcodecs/img_format.h"
  35. #include "libmpcodecs/cpudetect.h"
  36. #include "libmpcodecs/av_helpers.h"
  37. #include "libmpcodecs/vf_scale.h"
  38. #include "libmpcodecs/libvo/fastmemcpy.h"
  39. #include "libswscale/swscale.h"
  40. //FIXME maybe link the orig in
  41. //XXX: identical pix_fmt must be following with each others
  42. static const struct {
  43. int fmt;
  44. enum AVPixelFormat pix_fmt;
  45. } conversion_map[] = {
  46. {IMGFMT_ARGB, AV_PIX_FMT_ARGB},
  47. {IMGFMT_BGRA, AV_PIX_FMT_BGRA},
  48. {IMGFMT_BGR24, AV_PIX_FMT_BGR24},
  49. {IMGFMT_BGR16BE, AV_PIX_FMT_RGB565BE},
  50. {IMGFMT_BGR16LE, AV_PIX_FMT_RGB565LE},
  51. {IMGFMT_BGR15BE, AV_PIX_FMT_RGB555BE},
  52. {IMGFMT_BGR15LE, AV_PIX_FMT_RGB555LE},
  53. {IMGFMT_BGR12BE, AV_PIX_FMT_RGB444BE},
  54. {IMGFMT_BGR12LE, AV_PIX_FMT_RGB444LE},
  55. {IMGFMT_BGR8, AV_PIX_FMT_RGB8},
  56. {IMGFMT_BGR4, AV_PIX_FMT_RGB4},
  57. {IMGFMT_BGR1, AV_PIX_FMT_MONOBLACK},
  58. {IMGFMT_RGB1, AV_PIX_FMT_MONOBLACK},
  59. {IMGFMT_RG4B, AV_PIX_FMT_BGR4_BYTE},
  60. {IMGFMT_BG4B, AV_PIX_FMT_RGB4_BYTE},
  61. {IMGFMT_RGB48LE, AV_PIX_FMT_RGB48LE},
  62. {IMGFMT_RGB48BE, AV_PIX_FMT_RGB48BE},
  63. {IMGFMT_ABGR, AV_PIX_FMT_ABGR},
  64. {IMGFMT_RGBA, AV_PIX_FMT_RGBA},
  65. {IMGFMT_RGB24, AV_PIX_FMT_RGB24},
  66. {IMGFMT_RGB16BE, AV_PIX_FMT_BGR565BE},
  67. {IMGFMT_RGB16LE, AV_PIX_FMT_BGR565LE},
  68. {IMGFMT_RGB15BE, AV_PIX_FMT_BGR555BE},
  69. {IMGFMT_RGB15LE, AV_PIX_FMT_BGR555LE},
  70. {IMGFMT_RGB12BE, AV_PIX_FMT_BGR444BE},
  71. {IMGFMT_RGB12LE, AV_PIX_FMT_BGR444LE},
  72. {IMGFMT_RGB8, AV_PIX_FMT_BGR8},
  73. {IMGFMT_RGB4, AV_PIX_FMT_BGR4},
  74. {IMGFMT_BGR8, AV_PIX_FMT_PAL8},
  75. {IMGFMT_YUY2, AV_PIX_FMT_YUYV422},
  76. {IMGFMT_UYVY, AV_PIX_FMT_UYVY422},
  77. {IMGFMT_NV12, AV_PIX_FMT_NV12},
  78. {IMGFMT_NV21, AV_PIX_FMT_NV21},
  79. {IMGFMT_Y800, AV_PIX_FMT_GRAY8},
  80. {IMGFMT_Y8, AV_PIX_FMT_GRAY8},
  81. {IMGFMT_YVU9, AV_PIX_FMT_YUV410P},
  82. {IMGFMT_IF09, AV_PIX_FMT_YUV410P},
  83. {IMGFMT_YV12, AV_PIX_FMT_YUV420P},
  84. {IMGFMT_I420, AV_PIX_FMT_YUV420P},
  85. {IMGFMT_IYUV, AV_PIX_FMT_YUV420P},
  86. {IMGFMT_411P, AV_PIX_FMT_YUV411P},
  87. {IMGFMT_422P, AV_PIX_FMT_YUV422P},
  88. {IMGFMT_444P, AV_PIX_FMT_YUV444P},
  89. {IMGFMT_440P, AV_PIX_FMT_YUV440P},
  90. {IMGFMT_420A, AV_PIX_FMT_YUVA420P},
  91. {IMGFMT_420P16_LE, AV_PIX_FMT_YUV420P16LE},
  92. {IMGFMT_420P16_BE, AV_PIX_FMT_YUV420P16BE},
  93. {IMGFMT_422P16_LE, AV_PIX_FMT_YUV422P16LE},
  94. {IMGFMT_422P16_BE, AV_PIX_FMT_YUV422P16BE},
  95. {IMGFMT_444P16_LE, AV_PIX_FMT_YUV444P16LE},
  96. {IMGFMT_444P16_BE, AV_PIX_FMT_YUV444P16BE},
  97. // YUVJ are YUV formats that use the full Y range and not just
  98. // 16 - 235 (see colorspaces.txt).
  99. // Currently they are all treated the same way.
  100. {IMGFMT_YV12, AV_PIX_FMT_YUVJ420P},
  101. {IMGFMT_422P, AV_PIX_FMT_YUVJ422P},
  102. {IMGFMT_444P, AV_PIX_FMT_YUVJ444P},
  103. {IMGFMT_440P, AV_PIX_FMT_YUVJ440P},
  104. {IMGFMT_XVMC_MOCO_MPEG2, AV_PIX_FMT_XVMC_MPEG2_MC},
  105. {IMGFMT_XVMC_IDCT_MPEG2, AV_PIX_FMT_XVMC_MPEG2_IDCT},
  106. {IMGFMT_VDPAU_MPEG1, AV_PIX_FMT_VDPAU_MPEG1},
  107. {IMGFMT_VDPAU_MPEG2, AV_PIX_FMT_VDPAU_MPEG2},
  108. {IMGFMT_VDPAU_H264, AV_PIX_FMT_VDPAU_H264},
  109. {IMGFMT_VDPAU_WMV3, AV_PIX_FMT_VDPAU_WMV3},
  110. {IMGFMT_VDPAU_VC1, AV_PIX_FMT_VDPAU_VC1},
  111. {IMGFMT_VDPAU_MPEG4, AV_PIX_FMT_VDPAU_MPEG4},
  112. {0, AV_PIX_FMT_NONE}
  113. };
  114. extern const vf_info_t ff_vf_info_detc;
  115. extern const vf_info_t ff_vf_info_dint;
  116. extern const vf_info_t ff_vf_info_divtc;
  117. extern const vf_info_t ff_vf_info_down3dright;
  118. extern const vf_info_t ff_vf_info_eq2;
  119. extern const vf_info_t ff_vf_info_eq;
  120. extern const vf_info_t ff_vf_info_fil;
  121. //extern const vf_info_t ff_vf_info_filmdint;
  122. extern const vf_info_t ff_vf_info_fspp;
  123. extern const vf_info_t ff_vf_info_ilpack;
  124. extern const vf_info_t ff_vf_info_ivtc;
  125. extern const vf_info_t ff_vf_info_mcdeint;
  126. extern const vf_info_t ff_vf_info_noise;
  127. extern const vf_info_t ff_vf_info_ow;
  128. extern const vf_info_t ff_vf_info_perspective;
  129. extern const vf_info_t ff_vf_info_phase;
  130. extern const vf_info_t ff_vf_info_pp7;
  131. extern const vf_info_t ff_vf_info_pullup;
  132. extern const vf_info_t ff_vf_info_qp;
  133. extern const vf_info_t ff_vf_info_sab;
  134. extern const vf_info_t ff_vf_info_softpulldown;
  135. extern const vf_info_t ff_vf_info_spp;
  136. extern const vf_info_t ff_vf_info_telecine;
  137. extern const vf_info_t ff_vf_info_tinterlace;
  138. extern const vf_info_t ff_vf_info_uspp;
  139. static const vf_info_t* const filters[]={
  140. &ff_vf_info_detc,
  141. &ff_vf_info_dint,
  142. &ff_vf_info_divtc,
  143. &ff_vf_info_down3dright,
  144. &ff_vf_info_eq2,
  145. &ff_vf_info_eq,
  146. &ff_vf_info_fil,
  147. // &ff_vf_info_filmdint, cmmx.h vd.h ‘opt_screen_size_x’
  148. &ff_vf_info_fspp,
  149. &ff_vf_info_ilpack,
  150. &ff_vf_info_ivtc,
  151. &ff_vf_info_mcdeint,
  152. &ff_vf_info_noise,
  153. &ff_vf_info_ow,
  154. &ff_vf_info_perspective,
  155. &ff_vf_info_phase,
  156. &ff_vf_info_pp7,
  157. &ff_vf_info_pullup,
  158. &ff_vf_info_qp,
  159. &ff_vf_info_sab,
  160. &ff_vf_info_softpulldown,
  161. &ff_vf_info_spp,
  162. &ff_vf_info_telecine,
  163. &ff_vf_info_tinterlace,
  164. &ff_vf_info_uspp,
  165. NULL
  166. };
  167. /*
  168. Unsupported filters
  169. 1bpp
  170. ass
  171. bmovl
  172. crop
  173. dvbscale
  174. flip
  175. expand
  176. format
  177. halfpack
  178. lavc
  179. lavcdeint
  180. noformat
  181. pp
  182. scale
  183. tfields
  184. vo
  185. yadif
  186. zrmjpeg
  187. */
  188. CpuCaps ff_gCpuCaps; //FIXME initialize this so optims work
  189. enum AVPixelFormat ff_mp2ff_pix_fmt(int mp){
  190. int i;
  191. for(i=0; conversion_map[i].fmt && mp != conversion_map[i].fmt; i++)
  192. ;
  193. return mp == conversion_map[i].fmt ? conversion_map[i].pix_fmt : AV_PIX_FMT_NONE;
  194. }
  195. static void ff_sws_getFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam, SwsFilter **dstFilterParam)
  196. {
  197. static int firstTime=1;
  198. *flags=0;
  199. #if ARCH_X86
  200. if(ff_gCpuCaps.hasMMX)
  201. __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
  202. #endif
  203. if(firstTime)
  204. {
  205. firstTime=0;
  206. *flags= SWS_PRINT_INFO;
  207. }
  208. else if( ff_mp_msg_test(MSGT_VFILTER,MSGL_DBG2) ) *flags= SWS_PRINT_INFO;
  209. switch(SWS_BILINEAR)
  210. {
  211. case 0: *flags|= SWS_FAST_BILINEAR; break;
  212. case 1: *flags|= SWS_BILINEAR; break;
  213. case 2: *flags|= SWS_BICUBIC; break;
  214. case 3: *flags|= SWS_X; break;
  215. case 4: *flags|= SWS_POINT; break;
  216. case 5: *flags|= SWS_AREA; break;
  217. case 6: *flags|= SWS_BICUBLIN; break;
  218. case 7: *flags|= SWS_GAUSS; break;
  219. case 8: *flags|= SWS_SINC; break;
  220. case 9: *flags|= SWS_LANCZOS; break;
  221. case 10:*flags|= SWS_SPLINE; break;
  222. default:*flags|= SWS_BILINEAR; break;
  223. }
  224. *srcFilterParam= NULL;
  225. *dstFilterParam= NULL;
  226. }
  227. //exact copy from vf_scale.c
  228. // will use sws_flags & src_filter (from cmd line)
  229. struct SwsContext *ff_sws_getContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
  230. {
  231. int flags, i;
  232. SwsFilter *dstFilterParam, *srcFilterParam;
  233. enum AVPixelFormat dfmt, sfmt;
  234. for(i=0; conversion_map[i].fmt && dstFormat != conversion_map[i].fmt; i++);
  235. dfmt= conversion_map[i].pix_fmt;
  236. for(i=0; conversion_map[i].fmt && srcFormat != conversion_map[i].fmt; i++);
  237. sfmt= conversion_map[i].pix_fmt;
  238. if (srcFormat == IMGFMT_RGB8 || srcFormat == IMGFMT_BGR8) sfmt = AV_PIX_FMT_PAL8;
  239. ff_sws_getFlagsAndFilterFromCmdLine(&flags, &srcFilterParam, &dstFilterParam);
  240. return sws_getContext(srcW, srcH, sfmt, dstW, dstH, dfmt, flags , srcFilterParam, dstFilterParam, NULL);
  241. }
  242. typedef struct {
  243. vf_instance_t vf;
  244. vf_instance_t next_vf;
  245. AVFilterContext *avfctx;
  246. int frame_returned;
  247. } MPContext;
  248. void ff_mp_msg(int mod, int lev, const char *format, ... ){
  249. va_list va;
  250. va_start(va, format);
  251. //FIXME convert lev/mod
  252. av_vlog(NULL, AV_LOG_DEBUG, format, va);
  253. va_end(va);
  254. }
  255. int ff_mp_msg_test(int mod, int lev){
  256. return 123;
  257. }
  258. void ff_init_avcodec(void)
  259. {
  260. //we maybe should init but its kinda 1. unneeded 2. a bit inpolite from here
  261. }
  262. //Exact copy of vf.c
  263. void ff_vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
  264. dst->pict_type= src->pict_type;
  265. dst->fields = src->fields;
  266. dst->qscale_type= src->qscale_type;
  267. if(dst->width == src->width && dst->height == src->height){
  268. dst->qstride= src->qstride;
  269. dst->qscale= src->qscale;
  270. }
  271. }
  272. //Exact copy of vf.c
  273. void ff_vf_next_draw_slice(struct vf_instance *vf,unsigned char** src, int * stride,int w, int h, int x, int y){
  274. if (vf->next->draw_slice) {
  275. vf->next->draw_slice(vf->next,src,stride,w,h,x,y);
  276. return;
  277. }
  278. if (!vf->dmpi) {
  279. ff_mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
  280. return;
  281. }
  282. if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
  283. memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,
  284. src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);
  285. return;
  286. }
  287. memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],
  288. w, h, vf->dmpi->stride[0], stride[0]);
  289. memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),
  290. src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
  291. memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),
  292. src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
  293. }
  294. //Exact copy of vf.c
  295. void ff_vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
  296. int y;
  297. if(mpi->flags&MP_IMGFLAG_PLANAR){
  298. y0&=~1;h+=h&1;
  299. if(x0==0 && w==mpi->width){
  300. // full width clear:
  301. memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
  302. memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
  303. memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
  304. } else
  305. for(y=y0;y<y0+h;y+=2){
  306. memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
  307. memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
  308. memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
  309. memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
  310. }
  311. return;
  312. }
  313. // packed:
  314. for(y=y0;y<y0+h;y++){
  315. unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
  316. if(mpi->flags&MP_IMGFLAG_YUV){
  317. unsigned int* p=(unsigned int*) dst;
  318. int size=(mpi->bpp>>3)*w/4;
  319. int i;
  320. #if HAVE_BIGENDIAN
  321. #define CLEAR_PACKEDYUV_PATTERN 0x00800080
  322. #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
  323. #else
  324. #define CLEAR_PACKEDYUV_PATTERN 0x80008000
  325. #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
  326. #endif
  327. if(mpi->flags&MP_IMGFLAG_SWAPPED){
  328. for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
  329. for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
  330. } else {
  331. for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
  332. for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
  333. }
  334. } else
  335. memset(dst,0,(mpi->bpp>>3)*w);
  336. }
  337. }
  338. int ff_vf_next_query_format(struct vf_instance *vf, unsigned int fmt){
  339. return 1;
  340. }
  341. //used by delogo
  342. unsigned int ff_vf_match_csp(vf_instance_t** vfp,const unsigned int* list,unsigned int preferred){
  343. return preferred;
  344. }
  345. mp_image_t* ff_vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
  346. MPContext *m= (MPContext*)(((uint8_t*)vf) - offsetof(MPContext, next_vf));
  347. mp_image_t* mpi=NULL;
  348. int w2;
  349. int number = mp_imgtype >> 16;
  350. av_assert0(vf->next == NULL); // all existing filters call this just on next
  351. //vf_dint needs these as it calls ff_vf_get_image() before configuring the output
  352. if(vf->w==0 && w>0) vf->w=w;
  353. if(vf->h==0 && h>0) vf->h=h;
  354. av_assert0(w == -1 || w >= vf->w);
  355. av_assert0(h == -1 || h >= vf->h);
  356. av_assert0(vf->w > 0);
  357. av_assert0(vf->h > 0);
  358. av_log(m->avfctx, AV_LOG_DEBUG, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
  359. if (w == -1) w = vf->w;
  360. if (h == -1) h = vf->h;
  361. w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
  362. // Note: we should call libvo first to check if it supports direct rendering
  363. // and if not, then fallback to software buffers:
  364. switch(mp_imgtype & 0xff){
  365. case MP_IMGTYPE_EXPORT:
  366. if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=ff_new_mp_image(w2,h);
  367. mpi=vf->imgctx.export_images[0];
  368. break;
  369. case MP_IMGTYPE_STATIC:
  370. if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=ff_new_mp_image(w2,h);
  371. mpi=vf->imgctx.static_images[0];
  372. break;
  373. case MP_IMGTYPE_TEMP:
  374. if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=ff_new_mp_image(w2,h);
  375. mpi=vf->imgctx.temp_images[0];
  376. break;
  377. case MP_IMGTYPE_IPB:
  378. if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
  379. if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=ff_new_mp_image(w2,h);
  380. mpi=vf->imgctx.temp_images[0];
  381. break;
  382. }
  383. case MP_IMGTYPE_IP:
  384. if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=ff_new_mp_image(w2,h);
  385. mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
  386. vf->imgctx.static_idx^=1;
  387. break;
  388. case MP_IMGTYPE_NUMBERED:
  389. if (number == -1) {
  390. int i;
  391. for (i = 0; i < NUM_NUMBERED_MPI; i++)
  392. if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
  393. break;
  394. number = i;
  395. }
  396. if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
  397. if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = ff_new_mp_image(w2,h);
  398. mpi = vf->imgctx.numbered_images[number];
  399. mpi->number = number;
  400. break;
  401. }
  402. if(mpi){
  403. mpi->type=mp_imgtype;
  404. mpi->w=vf->w; mpi->h=vf->h;
  405. // keep buffer allocation status & color flags only:
  406. // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
  407. mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
  408. // accept restrictions, draw_slice and palette flags only:
  409. mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK|MP_IMGFLAG_RGB_PALETTE);
  410. if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
  411. if(mpi->width!=w2 || mpi->height!=h){
  412. // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
  413. if(mpi->flags&MP_IMGFLAG_ALLOCATED){
  414. if(mpi->width<w2 || mpi->height<h){
  415. // need to re-allocate buffer memory:
  416. av_free(mpi->planes[0]);
  417. mpi->flags&=~MP_IMGFLAG_ALLOCATED;
  418. ff_mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
  419. }
  420. // } else {
  421. } {
  422. mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
  423. mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
  424. }
  425. }
  426. if(!mpi->bpp) ff_mp_image_setfmt(mpi,outfmt);
  427. if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
  428. av_assert0(!vf->get_image);
  429. // check libvo first!
  430. if(vf->get_image) vf->get_image(vf,mpi);
  431. if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
  432. // non-direct and not yet allocated image. allocate it!
  433. if (!mpi->bpp) { // no way we can allocate this
  434. ff_mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
  435. "ff_vf_get_image: Tried to allocate a format that can not be allocated!\n");
  436. return NULL;
  437. }
  438. // check if codec prefer aligned stride:
  439. if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
  440. int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
  441. mpi->flags&MP_IMGFLAG_YUV) ?
  442. (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
  443. w2=((w+align)&(~align));
  444. if(mpi->width!=w2){
  445. #if 0
  446. // we have to change width... check if we CAN co it:
  447. int flags=vf->query_format(vf,outfmt); // should not fail
  448. if(!(flags&3)) ff_mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? ff_vf_get_image{vf->query_format(outfmt)} failed!\n");
  449. // printf("query -> 0x%X \n",flags);
  450. if(flags&VFCAP_ACCEPT_STRIDE){
  451. #endif
  452. mpi->width=w2;
  453. mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
  454. // }
  455. }
  456. }
  457. ff_mp_image_alloc_planes(mpi);
  458. // printf("clearing img!\n");
  459. ff_vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
  460. }
  461. }
  462. av_assert0(!vf->start_slice);
  463. if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
  464. if(vf->start_slice) vf->start_slice(vf,mpi);
  465. if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
  466. ff_mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
  467. "NULL"/*vf->info->name*/,
  468. (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
  469. ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
  470. (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
  471. mpi->width,mpi->height,mpi->bpp,
  472. (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
  473. (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
  474. mpi->bpp*mpi->width*mpi->height/8);
  475. 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",
  476. mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
  477. mpi->stride[0], mpi->stride[1], mpi->stride[2],
  478. mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
  479. mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
  480. }
  481. mpi->qscale = NULL;
  482. mpi->usage_count++;
  483. }
  484. // printf("\rVF_MPI: %p %p %p %d %d %d \n",
  485. // mpi->planes[0],mpi->planes[1],mpi->planes[2],
  486. // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
  487. return mpi;
  488. }
  489. static void dummy_free(void *opaque, uint8_t *data){}
  490. int ff_vf_next_put_image(struct vf_instance *vf,mp_image_t *mpi, double pts){
  491. MPContext *m= (void*)vf;
  492. AVFilterLink *outlink = m->avfctx->outputs[0];
  493. AVFrame *picref = av_frame_alloc();
  494. int i;
  495. av_assert0(vf->next);
  496. av_log(m->avfctx, AV_LOG_DEBUG, "ff_vf_next_put_image\n");
  497. if (!picref)
  498. goto fail;
  499. picref->width = mpi->w;
  500. picref->height = mpi->h;
  501. picref->type = AVMEDIA_TYPE_VIDEO;
  502. for(i=0; conversion_map[i].fmt && mpi->imgfmt != conversion_map[i].fmt; i++);
  503. picref->format = conversion_map[i].pix_fmt;
  504. memcpy(picref->linesize, mpi->stride, FFMIN(sizeof(picref->linesize), sizeof(mpi->stride)));
  505. for(i=0; i<4 && mpi->stride[i]; i++){
  506. picref->buf[i] = av_buffer_create(mpi->planes[i], mpi->stride[i], dummy_free, NULL,
  507. (mpi->flags & MP_IMGFLAG_PRESERVE) ? AV_BUFFER_FLAG_READONLY : 0);
  508. if (!picref->buf[i])
  509. goto fail;
  510. picref->data[i] = picref->buf[i]->data;
  511. }
  512. if(pts != MP_NOPTS_VALUE)
  513. picref->pts= pts * av_q2d(outlink->time_base);
  514. ff_filter_frame(outlink, picref);
  515. m->frame_returned++;
  516. return 1;
  517. fail:
  518. av_frame_free(&picref);
  519. return 0;
  520. }
  521. int ff_vf_next_config(struct vf_instance *vf,
  522. int width, int height, int d_width, int d_height,
  523. unsigned int voflags, unsigned int outfmt){
  524. av_assert0(width>0 && height>0);
  525. vf->next->w = width; vf->next->h = height;
  526. return 1;
  527. #if 0
  528. int flags=vf->next->query_format(vf->next,outfmt);
  529. if(!flags){
  530. // hmm. colorspace mismatch!!!
  531. //this is fatal for us ATM
  532. return 0;
  533. }
  534. ff_mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
  535. miss=vf->default_reqs - (flags&vf->default_reqs);
  536. if(miss&VFCAP_ACCEPT_STRIDE){
  537. // vf requires stride support but vf->next doesn't support it!
  538. // let's insert the 'expand' filter, it does the job for us:
  539. vf_instance_t* vf2=vf_open_filter(vf->next,"expand",NULL);
  540. if(!vf2) return 0; // shouldn't happen!
  541. vf->next=vf2;
  542. }
  543. vf->next->w = width; vf->next->h = height;
  544. return 1;
  545. #endif
  546. }
  547. int ff_vf_next_control(struct vf_instance *vf, int request, void* data){
  548. MPContext *m= (void*)vf;
  549. av_log(m->avfctx, AV_LOG_DEBUG, "Received control %d\n", request);
  550. return 0;
  551. }
  552. static int vf_default_query_format(struct vf_instance *vf, unsigned int fmt){
  553. MPContext *m= (void*)vf;
  554. int i;
  555. av_log(m->avfctx, AV_LOG_DEBUG, "query %X\n", fmt);
  556. for(i=0; conversion_map[i].fmt; i++){
  557. if(fmt==conversion_map[i].fmt)
  558. return 1; //we suport all
  559. }
  560. return 0;
  561. }
  562. static av_cold int init(AVFilterContext *ctx, const char *args)
  563. {
  564. MPContext *m = ctx->priv;
  565. char name[256];
  566. int i;
  567. m->avfctx= ctx;
  568. if(!args || 1!=sscanf(args, "%255[^:=]", name)){
  569. av_log(ctx, AV_LOG_ERROR, "Invalid parameter.\n");
  570. return AVERROR(EINVAL);
  571. }
  572. args += strlen(name);
  573. if (args[0] == '=')
  574. args++;
  575. for(i=0; ;i++){
  576. if(!filters[i] || !strcmp(name, filters[i]->name))
  577. break;
  578. }
  579. if(!filters[i]){
  580. av_log(ctx, AV_LOG_ERROR, "Unknown filter %s\n", name);
  581. return AVERROR(EINVAL);
  582. }
  583. av_log(ctx, AV_LOG_WARNING,
  584. "'%s' is a wrapped MPlayer filter (libmpcodecs). This filter may be removed\n"
  585. "once it has been ported to a native libavfilter.\n", name);
  586. memset(&m->vf,0,sizeof(m->vf));
  587. m->vf.info= filters[i];
  588. m->vf.next = &m->next_vf;
  589. m->vf.put_image = ff_vf_next_put_image;
  590. m->vf.config = ff_vf_next_config;
  591. m->vf.query_format= vf_default_query_format;
  592. m->vf.control = ff_vf_next_control;
  593. m->vf.default_caps=VFCAP_ACCEPT_STRIDE;
  594. m->vf.default_reqs=0;
  595. if(m->vf.info->opts)
  596. av_log(ctx, AV_LOG_ERROR, "opts / m_struct_set is unsupported\n");
  597. #if 0
  598. if(vf->info->opts) { // vf_vo get some special argument
  599. const m_struct_t* st = vf->info->opts;
  600. void* vf_priv = m_struct_alloc(st);
  601. int n;
  602. for(n = 0 ; args && args[2*n] ; n++)
  603. m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
  604. vf->priv = vf_priv;
  605. args = NULL;
  606. } else // Otherwise we should have the '_oldargs_'
  607. if(args && !strcmp(args[0],"_oldargs_"))
  608. args = (char**)args[1];
  609. else
  610. args = NULL;
  611. #endif
  612. if(m->vf.info->vf_open(&m->vf, (char*)args)<=0){
  613. av_log(ctx, AV_LOG_ERROR, "vf_open() of %s with arg=%s failed\n", name, args);
  614. return -1;
  615. }
  616. return 0;
  617. }
  618. static av_cold void uninit(AVFilterContext *ctx)
  619. {
  620. MPContext *m = ctx->priv;
  621. vf_instance_t *vf = &m->vf;
  622. while(vf){
  623. vf_instance_t *next = vf->next;
  624. if(vf->uninit)
  625. vf->uninit(vf);
  626. ff_free_mp_image(vf->imgctx.static_images[0]);
  627. ff_free_mp_image(vf->imgctx.static_images[1]);
  628. ff_free_mp_image(vf->imgctx.temp_images[0]);
  629. ff_free_mp_image(vf->imgctx.export_images[0]);
  630. vf = next;
  631. }
  632. }
  633. static int query_formats(AVFilterContext *ctx)
  634. {
  635. AVFilterFormats *avfmts=NULL;
  636. MPContext *m = ctx->priv;
  637. enum AVPixelFormat lastpixfmt = AV_PIX_FMT_NONE;
  638. int i;
  639. for(i=0; conversion_map[i].fmt; i++){
  640. av_log(ctx, AV_LOG_DEBUG, "query: %X\n", conversion_map[i].fmt);
  641. if(m->vf.query_format(&m->vf, conversion_map[i].fmt)){
  642. av_log(ctx, AV_LOG_DEBUG, "supported,adding\n");
  643. if (conversion_map[i].pix_fmt != lastpixfmt) {
  644. ff_add_format(&avfmts, conversion_map[i].pix_fmt);
  645. lastpixfmt = conversion_map[i].pix_fmt;
  646. }
  647. }
  648. }
  649. if (!avfmts)
  650. return -1;
  651. //We assume all allowed input formats are also allowed output formats
  652. ff_set_common_formats(ctx, avfmts);
  653. return 0;
  654. }
  655. static int config_inprops(AVFilterLink *inlink)
  656. {
  657. MPContext *m = inlink->dst->priv;
  658. int i;
  659. for(i=0; conversion_map[i].fmt && conversion_map[i].pix_fmt != inlink->format; i++);
  660. av_assert0(conversion_map[i].fmt && inlink->w && inlink->h);
  661. m->vf.fmt.have_configured = 1;
  662. m->vf.fmt.orig_height = inlink->h;
  663. m->vf.fmt.orig_width = inlink->w;
  664. m->vf.fmt.orig_fmt = conversion_map[i].fmt;
  665. if(m->vf.config(&m->vf, inlink->w, inlink->h, inlink->w, inlink->h, 0, conversion_map[i].fmt)<=0)
  666. return -1;
  667. return 0;
  668. }
  669. static int config_outprops(AVFilterLink *outlink)
  670. {
  671. MPContext *m = outlink->src->priv;
  672. outlink->w = m->next_vf.w;
  673. outlink->h = m->next_vf.h;
  674. return 0;
  675. }
  676. static int request_frame(AVFilterLink *outlink)
  677. {
  678. MPContext *m = outlink->src->priv;
  679. int ret;
  680. av_log(m->avfctx, AV_LOG_DEBUG, "mp request_frame\n");
  681. for(m->frame_returned=0; !m->frame_returned;){
  682. ret=ff_request_frame(outlink->src->inputs[0]);
  683. if(ret<0)
  684. break;
  685. }
  686. av_log(m->avfctx, AV_LOG_DEBUG, "mp request_frame ret=%d\n", ret);
  687. return ret;
  688. }
  689. static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
  690. {
  691. MPContext *m = inlink->dst->priv;
  692. int i;
  693. double pts= MP_NOPTS_VALUE;
  694. mp_image_t* mpi = ff_new_mp_image(inpic->width, inpic->height);
  695. if(inpic->pts != AV_NOPTS_VALUE)
  696. pts= inpic->pts / av_q2d(inlink->time_base);
  697. for(i=0; conversion_map[i].fmt && conversion_map[i].pix_fmt != inlink->format; i++);
  698. ff_mp_image_setfmt(mpi,conversion_map[i].fmt);
  699. memcpy(mpi->planes, inpic->data, FFMIN(sizeof(inpic->data) , sizeof(mpi->planes)));
  700. memcpy(mpi->stride, inpic->linesize, FFMIN(sizeof(inpic->linesize), sizeof(mpi->stride)));
  701. //FIXME pass interleced & tff flags around
  702. // mpi->flags|=MP_IMGFLAG_ALLOCATED; ?
  703. mpi->flags |= MP_IMGFLAG_READABLE;
  704. if(!av_frame_is_writable(inpic))
  705. mpi->flags |= MP_IMGFLAG_PRESERVE;
  706. if(m->vf.put_image(&m->vf, mpi, pts) == 0){
  707. av_log(m->avfctx, AV_LOG_DEBUG, "put_image() says skip\n");
  708. }else{
  709. av_frame_free(&inpic);
  710. }
  711. ff_free_mp_image(mpi);
  712. return 0;
  713. }
  714. static const AVFilterPad mp_inputs[] = {
  715. {
  716. .name = "default",
  717. .type = AVMEDIA_TYPE_VIDEO,
  718. .filter_frame = filter_frame,
  719. .config_props = config_inprops,
  720. },
  721. { NULL }
  722. };
  723. static const AVFilterPad mp_outputs[] = {
  724. {
  725. .name = "default",
  726. .type = AVMEDIA_TYPE_VIDEO,
  727. .request_frame = request_frame,
  728. .config_props = config_outprops,
  729. },
  730. { NULL }
  731. };
  732. AVFilter avfilter_vf_mp = {
  733. .name = "mp",
  734. .description = NULL_IF_CONFIG_SMALL("Apply a libmpcodecs filter to the input video."),
  735. .init = init,
  736. .uninit = uninit,
  737. .priv_size = sizeof(MPContext),
  738. .query_formats = query_formats,
  739. .inputs = mp_inputs,
  740. .outputs = mp_outputs,
  741. };