vf_mp.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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 "libavutil/avassert.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libmpcodecs/vf.h"
  31. #include "libmpcodecs/img_format.h"
  32. #include "libmpcodecs/cpudetect.h"
  33. #include "libmpcodecs/vd_ffmpeg.h"
  34. #include "libmpcodecs/vf_scale.h"
  35. #include "libmpcodecs/libvo/fastmemcpy.h"
  36. #include "libswscale/swscale.h"
  37. //FIXME maybe link the orig in
  38. //XXX: identical pix_fmt must be following with each others
  39. static const struct {
  40. int fmt;
  41. enum PixelFormat pix_fmt;
  42. } conversion_map[] = {
  43. {IMGFMT_ARGB, PIX_FMT_ARGB},
  44. {IMGFMT_BGRA, PIX_FMT_BGRA},
  45. {IMGFMT_BGR24, PIX_FMT_BGR24},
  46. {IMGFMT_BGR16BE, PIX_FMT_RGB565BE},
  47. {IMGFMT_BGR16LE, PIX_FMT_RGB565LE},
  48. {IMGFMT_BGR15BE, PIX_FMT_RGB555BE},
  49. {IMGFMT_BGR15LE, PIX_FMT_RGB555LE},
  50. {IMGFMT_BGR12BE, PIX_FMT_RGB444BE},
  51. {IMGFMT_BGR12LE, PIX_FMT_RGB444LE},
  52. {IMGFMT_BGR8, PIX_FMT_RGB8},
  53. {IMGFMT_BGR4, PIX_FMT_RGB4},
  54. {IMGFMT_BGR1, PIX_FMT_MONOBLACK},
  55. {IMGFMT_RGB1, PIX_FMT_MONOBLACK},
  56. {IMGFMT_RG4B, PIX_FMT_BGR4_BYTE},
  57. {IMGFMT_BG4B, PIX_FMT_RGB4_BYTE},
  58. {IMGFMT_RGB48LE, PIX_FMT_RGB48LE},
  59. {IMGFMT_RGB48BE, PIX_FMT_RGB48BE},
  60. {IMGFMT_ABGR, PIX_FMT_ABGR},
  61. {IMGFMT_RGBA, PIX_FMT_RGBA},
  62. {IMGFMT_RGB24, PIX_FMT_RGB24},
  63. {IMGFMT_RGB16BE, PIX_FMT_BGR565BE},
  64. {IMGFMT_RGB16LE, PIX_FMT_BGR565LE},
  65. {IMGFMT_RGB15BE, PIX_FMT_BGR555BE},
  66. {IMGFMT_RGB15LE, PIX_FMT_BGR555LE},
  67. {IMGFMT_RGB12BE, PIX_FMT_BGR444BE},
  68. {IMGFMT_RGB12LE, PIX_FMT_BGR444LE},
  69. {IMGFMT_RGB8, PIX_FMT_BGR8},
  70. {IMGFMT_RGB4, PIX_FMT_BGR4},
  71. {IMGFMT_BGR8, PIX_FMT_PAL8},
  72. {IMGFMT_YUY2, PIX_FMT_YUYV422},
  73. {IMGFMT_UYVY, PIX_FMT_UYVY422},
  74. {IMGFMT_NV12, PIX_FMT_NV12},
  75. {IMGFMT_NV21, PIX_FMT_NV21},
  76. {IMGFMT_Y800, PIX_FMT_GRAY8},
  77. {IMGFMT_Y8, PIX_FMT_GRAY8},
  78. {IMGFMT_YVU9, PIX_FMT_YUV410P},
  79. {IMGFMT_IF09, PIX_FMT_YUV410P},
  80. {IMGFMT_YV12, PIX_FMT_YUV420P},
  81. {IMGFMT_I420, PIX_FMT_YUV420P},
  82. {IMGFMT_IYUV, PIX_FMT_YUV420P},
  83. {IMGFMT_411P, PIX_FMT_YUV411P},
  84. {IMGFMT_422P, PIX_FMT_YUV422P},
  85. {IMGFMT_444P, PIX_FMT_YUV444P},
  86. {IMGFMT_440P, PIX_FMT_YUV440P},
  87. {IMGFMT_420A, PIX_FMT_YUVA420P},
  88. {IMGFMT_420P16_LE, PIX_FMT_YUV420P16LE},
  89. {IMGFMT_420P16_BE, PIX_FMT_YUV420P16BE},
  90. {IMGFMT_422P16_LE, PIX_FMT_YUV422P16LE},
  91. {IMGFMT_422P16_BE, PIX_FMT_YUV422P16BE},
  92. {IMGFMT_444P16_LE, PIX_FMT_YUV444P16LE},
  93. {IMGFMT_444P16_BE, PIX_FMT_YUV444P16BE},
  94. // YUVJ are YUV formats that use the full Y range and not just
  95. // 16 - 235 (see colorspaces.txt).
  96. // Currently they are all treated the same way.
  97. {IMGFMT_YV12, PIX_FMT_YUVJ420P},
  98. {IMGFMT_422P, PIX_FMT_YUVJ422P},
  99. {IMGFMT_444P, PIX_FMT_YUVJ444P},
  100. {IMGFMT_440P, PIX_FMT_YUVJ440P},
  101. {IMGFMT_XVMC_MOCO_MPEG2, PIX_FMT_XVMC_MPEG2_MC},
  102. {IMGFMT_XVMC_IDCT_MPEG2, PIX_FMT_XVMC_MPEG2_IDCT},
  103. {IMGFMT_VDPAU_MPEG1, PIX_FMT_VDPAU_MPEG1},
  104. {IMGFMT_VDPAU_MPEG2, PIX_FMT_VDPAU_MPEG2},
  105. {IMGFMT_VDPAU_H264, PIX_FMT_VDPAU_H264},
  106. {IMGFMT_VDPAU_WMV3, PIX_FMT_VDPAU_WMV3},
  107. {IMGFMT_VDPAU_VC1, PIX_FMT_VDPAU_VC1},
  108. {IMGFMT_VDPAU_MPEG4, PIX_FMT_VDPAU_MPEG4},
  109. {0, PIX_FMT_NONE}
  110. };
  111. //copied from vf.c
  112. extern const vf_info_t vf_info_1bpp;
  113. extern const vf_info_t vf_info_2xsai;
  114. extern const vf_info_t vf_info_ass;
  115. extern const vf_info_t vf_info_bmovl;
  116. extern const vf_info_t vf_info_crop;
  117. extern const vf_info_t vf_info_decimate;
  118. extern const vf_info_t vf_info_denoise3d;
  119. extern const vf_info_t vf_info_detc;
  120. extern const vf_info_t vf_info_dint;
  121. extern const vf_info_t vf_info_divtc;
  122. extern const vf_info_t vf_info_down3dright;
  123. extern const vf_info_t vf_info_dsize;
  124. extern const vf_info_t vf_info_dvbscale;
  125. extern const vf_info_t vf_info_eq2;
  126. extern const vf_info_t vf_info_eq;
  127. extern const vf_info_t vf_info_expand;
  128. extern const vf_info_t vf_info_field;
  129. extern const vf_info_t vf_info_fil;
  130. extern const vf_info_t vf_info_filmdint;
  131. extern const vf_info_t vf_info_fixpts;
  132. extern const vf_info_t vf_info_flip;
  133. extern const vf_info_t vf_info_format;
  134. extern const vf_info_t vf_info_framestep;
  135. extern const vf_info_t vf_info_fspp;
  136. extern const vf_info_t vf_info_geq;
  137. extern const vf_info_t vf_info_halfpack;
  138. extern const vf_info_t vf_info_harddup;
  139. extern const vf_info_t vf_info_hqdn3d;
  140. extern const vf_info_t vf_info_hue;
  141. extern const vf_info_t vf_info_il;
  142. extern const vf_info_t vf_info_ilpack;
  143. extern const vf_info_t vf_info_ivtc;
  144. extern const vf_info_t vf_info_kerndeint;
  145. extern const vf_info_t vf_info_lavc;
  146. extern const vf_info_t vf_info_lavcdeint;
  147. extern const vf_info_t vf_info_mcdeint;
  148. extern const vf_info_t vf_info_mirror;
  149. extern const vf_info_t vf_info_noformat;
  150. extern const vf_info_t vf_info_noise;
  151. extern const vf_info_t vf_info_ow;
  152. extern const vf_info_t vf_info_palette;
  153. extern const vf_info_t vf_info_perspective;
  154. extern const vf_info_t vf_info_phase;
  155. extern const vf_info_t vf_info_pp7;
  156. extern const vf_info_t vf_info_pp;
  157. extern const vf_info_t vf_info_pullup;
  158. extern const vf_info_t vf_info_qp;
  159. extern const vf_info_t vf_info_rectangle;
  160. extern const vf_info_t vf_info_remove_logo;
  161. extern const vf_info_t vf_info_rotate;
  162. extern const vf_info_t vf_info_sab;
  163. extern const vf_info_t vf_info_scale;
  164. extern const vf_info_t vf_info_screenshot;
  165. extern const vf_info_t vf_info_smartblur;
  166. extern const vf_info_t vf_info_softpulldown;
  167. extern const vf_info_t vf_info_softskip;
  168. extern const vf_info_t vf_info_spp;
  169. extern const vf_info_t vf_info_stereo3d;
  170. extern const vf_info_t vf_info_swapuv;
  171. extern const vf_info_t vf_info_telecine;
  172. extern const vf_info_t vf_info_test;
  173. extern const vf_info_t vf_info_tfields;
  174. extern const vf_info_t vf_info_tile;
  175. extern const vf_info_t vf_info_tinterlace;
  176. extern const vf_info_t vf_info_unsharp;
  177. extern const vf_info_t vf_info_uspp;
  178. extern const vf_info_t vf_info_vo;
  179. extern const vf_info_t vf_info_yadif;
  180. extern const vf_info_t vf_info_yuvcsp;
  181. extern const vf_info_t vf_info_yvu9;
  182. extern const vf_info_t vf_info_zrmjpeg;
  183. static const vf_info_t* const filters[]={
  184. &vf_info_2xsai,
  185. &vf_info_decimate,
  186. &vf_info_denoise3d,
  187. &vf_info_detc,
  188. &vf_info_dint,
  189. &vf_info_divtc,
  190. &vf_info_down3dright,
  191. &vf_info_dsize,
  192. &vf_info_eq2,
  193. &vf_info_eq,
  194. &vf_info_field,
  195. &vf_info_fil,
  196. // &vf_info_filmdint, cmmx.h vd.h ‘opt_screen_size_x’
  197. &vf_info_fixpts,
  198. &vf_info_framestep,
  199. &vf_info_fspp,
  200. &vf_info_geq,
  201. &vf_info_harddup,
  202. &vf_info_hqdn3d,
  203. &vf_info_hue,
  204. &vf_info_il,
  205. &vf_info_ilpack,
  206. &vf_info_ivtc,
  207. &vf_info_kerndeint,
  208. &vf_info_mcdeint,
  209. &vf_info_mirror,
  210. &vf_info_noise,
  211. &vf_info_ow,
  212. &vf_info_palette,
  213. &vf_info_perspective,
  214. &vf_info_phase,
  215. &vf_info_pp7,
  216. &vf_info_pullup,
  217. &vf_info_qp,
  218. &vf_info_rectangle,
  219. &vf_info_remove_logo,
  220. &vf_info_rotate,
  221. &vf_info_sab,
  222. &vf_info_screenshot,
  223. &vf_info_smartblur,
  224. &vf_info_softpulldown,
  225. &vf_info_softskip,
  226. &vf_info_spp,
  227. &vf_info_stereo3d,
  228. &vf_info_swapuv,
  229. &vf_info_telecine,
  230. &vf_info_tile,
  231. &vf_info_tinterlace,
  232. &vf_info_unsharp,
  233. &vf_info_uspp,
  234. &vf_info_yuvcsp,
  235. &vf_info_yvu9,
  236. NULL
  237. };
  238. /*
  239. Unsupported filters
  240. 1bpp
  241. ass
  242. bmovl
  243. crop
  244. dvbscale
  245. flip
  246. expand
  247. format
  248. halfpack
  249. lavc
  250. lavcdeint
  251. noformat
  252. pp
  253. scale
  254. tfields
  255. vo
  256. yadif
  257. zrmjpeg
  258. */
  259. CpuCaps gCpuCaps; //FIXME initialize this so optims work
  260. static void sws_getFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam, SwsFilter **dstFilterParam)
  261. {
  262. static int firstTime=1;
  263. *flags=0;
  264. #if ARCH_X86
  265. if(gCpuCaps.hasMMX)
  266. __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
  267. #endif
  268. if(firstTime)
  269. {
  270. firstTime=0;
  271. *flags= SWS_PRINT_INFO;
  272. }
  273. else if( mp_msg_test(MSGT_VFILTER,MSGL_DBG2) ) *flags= SWS_PRINT_INFO;
  274. switch(SWS_BILINEAR)
  275. {
  276. case 0: *flags|= SWS_FAST_BILINEAR; break;
  277. case 1: *flags|= SWS_BILINEAR; break;
  278. case 2: *flags|= SWS_BICUBIC; break;
  279. case 3: *flags|= SWS_X; break;
  280. case 4: *flags|= SWS_POINT; break;
  281. case 5: *flags|= SWS_AREA; break;
  282. case 6: *flags|= SWS_BICUBLIN; break;
  283. case 7: *flags|= SWS_GAUSS; break;
  284. case 8: *flags|= SWS_SINC; break;
  285. case 9: *flags|= SWS_LANCZOS; break;
  286. case 10:*flags|= SWS_SPLINE; break;
  287. default:*flags|= SWS_BILINEAR; break;
  288. }
  289. *srcFilterParam= NULL;
  290. *dstFilterParam= NULL;
  291. }
  292. //exact copy from vf_scale.c
  293. // will use sws_flags & src_filter (from cmd line)
  294. struct SwsContext *sws_getContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
  295. {
  296. int flags, i;
  297. SwsFilter *dstFilterParam, *srcFilterParam;
  298. enum PixelFormat dfmt, sfmt;
  299. for(i=0; conversion_map[i].fmt && dstFormat != conversion_map[i].fmt; i++);
  300. dfmt= conversion_map[i].pix_fmt;
  301. for(i=0; conversion_map[i].fmt && srcFormat != conversion_map[i].fmt; i++);
  302. sfmt= conversion_map[i].pix_fmt;
  303. if (srcFormat == IMGFMT_RGB8 || srcFormat == IMGFMT_BGR8) sfmt = PIX_FMT_PAL8;
  304. sws_getFlagsAndFilterFromCmdLine(&flags, &srcFilterParam, &dstFilterParam);
  305. return sws_getContext(srcW, srcH, sfmt, dstW, dstH, dfmt, flags , srcFilterParam, dstFilterParam, NULL);
  306. }
  307. typedef struct {
  308. vf_instance_t vf;
  309. vf_instance_t next_vf;
  310. AVFilterContext *avfctx;
  311. int frame_returned;
  312. } MPContext;
  313. void mp_msg(int mod, int lev, const char *format, ... ){
  314. va_list va;
  315. va_start(va, format);
  316. //FIXME convert lev/mod
  317. av_vlog(NULL, AV_LOG_DEBUG, format, va);
  318. va_end(va);
  319. }
  320. int mp_msg_test(int mod, int lev){
  321. return 123;
  322. }
  323. void init_avcodec(void)
  324. {
  325. //we maybe should init but its kinda 1. unneeded 2. a bit inpolite from here
  326. }
  327. //Exact copy of vf.c
  328. void vf_clone_mpi_attributes(mp_image_t* dst, mp_image_t* src){
  329. dst->pict_type= src->pict_type;
  330. dst->fields = src->fields;
  331. dst->qscale_type= src->qscale_type;
  332. if(dst->width == src->width && dst->height == src->height){
  333. dst->qstride= src->qstride;
  334. dst->qscale= src->qscale;
  335. }
  336. }
  337. //Exact copy of vf.c
  338. void vf_next_draw_slice(struct vf_instance *vf,unsigned char** src, int * stride,int w, int h, int x, int y){
  339. if (vf->next->draw_slice) {
  340. vf->next->draw_slice(vf->next,src,stride,w,h,x,y);
  341. return;
  342. }
  343. if (!vf->dmpi) {
  344. mp_msg(MSGT_VFILTER,MSGL_ERR,"draw_slice: dmpi not stored by vf_%s\n", vf->info->name);
  345. return;
  346. }
  347. if (!(vf->dmpi->flags & MP_IMGFLAG_PLANAR)) {
  348. memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+vf->dmpi->bpp/8*x,
  349. src[0], vf->dmpi->bpp/8*w, h, vf->dmpi->stride[0], stride[0]);
  350. return;
  351. }
  352. memcpy_pic(vf->dmpi->planes[0]+y*vf->dmpi->stride[0]+x, src[0],
  353. w, h, vf->dmpi->stride[0], stride[0]);
  354. memcpy_pic(vf->dmpi->planes[1]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[1]+(x>>vf->dmpi->chroma_x_shift),
  355. src[1], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[1], stride[1]);
  356. memcpy_pic(vf->dmpi->planes[2]+(y>>vf->dmpi->chroma_y_shift)*vf->dmpi->stride[2]+(x>>vf->dmpi->chroma_x_shift),
  357. src[2], w>>vf->dmpi->chroma_x_shift, h>>vf->dmpi->chroma_y_shift, vf->dmpi->stride[2], stride[2]);
  358. }
  359. //Exact copy of vf.c
  360. void vf_mpi_clear(mp_image_t* mpi,int x0,int y0,int w,int h){
  361. int y;
  362. if(mpi->flags&MP_IMGFLAG_PLANAR){
  363. y0&=~1;h+=h&1;
  364. if(x0==0 && w==mpi->width){
  365. // full width clear:
  366. memset(mpi->planes[0]+mpi->stride[0]*y0,0,mpi->stride[0]*h);
  367. memset(mpi->planes[1]+mpi->stride[1]*(y0>>mpi->chroma_y_shift),128,mpi->stride[1]*(h>>mpi->chroma_y_shift));
  368. memset(mpi->planes[2]+mpi->stride[2]*(y0>>mpi->chroma_y_shift),128,mpi->stride[2]*(h>>mpi->chroma_y_shift));
  369. } else
  370. for(y=y0;y<y0+h;y+=2){
  371. memset(mpi->planes[0]+x0+mpi->stride[0]*y,0,w);
  372. memset(mpi->planes[0]+x0+mpi->stride[0]*(y+1),0,w);
  373. memset(mpi->planes[1]+(x0>>mpi->chroma_x_shift)+mpi->stride[1]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
  374. memset(mpi->planes[2]+(x0>>mpi->chroma_x_shift)+mpi->stride[2]*(y>>mpi->chroma_y_shift),128,(w>>mpi->chroma_x_shift));
  375. }
  376. return;
  377. }
  378. // packed:
  379. for(y=y0;y<y0+h;y++){
  380. unsigned char* dst=mpi->planes[0]+mpi->stride[0]*y+(mpi->bpp>>3)*x0;
  381. if(mpi->flags&MP_IMGFLAG_YUV){
  382. unsigned int* p=(unsigned int*) dst;
  383. int size=(mpi->bpp>>3)*w/4;
  384. int i;
  385. #if HAVE_BIGENDIAN
  386. #define CLEAR_PACKEDYUV_PATTERN 0x00800080
  387. #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x80008000
  388. #else
  389. #define CLEAR_PACKEDYUV_PATTERN 0x80008000
  390. #define CLEAR_PACKEDYUV_PATTERN_SWAPPED 0x00800080
  391. #endif
  392. if(mpi->flags&MP_IMGFLAG_SWAPPED){
  393. for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
  394. for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN_SWAPPED;
  395. } else {
  396. for(i=0;i<size-3;i+=4) p[i]=p[i+1]=p[i+2]=p[i+3]=CLEAR_PACKEDYUV_PATTERN;
  397. for(;i<size;i++) p[i]=CLEAR_PACKEDYUV_PATTERN;
  398. }
  399. } else
  400. memset(dst,0,(mpi->bpp>>3)*w);
  401. }
  402. }
  403. int vf_next_query_format(struct vf_instance *vf, unsigned int fmt){
  404. return 1;
  405. }
  406. //used by delogo
  407. unsigned int vf_match_csp(vf_instance_t** vfp,const unsigned int* list,unsigned int preferred){
  408. return preferred;
  409. }
  410. mp_image_t* vf_get_image(vf_instance_t* vf, unsigned int outfmt, int mp_imgtype, int mp_imgflag, int w, int h){
  411. MPContext *m= (MPContext*)(((uint8_t*)vf) - offsetof(MPContext, next_vf));
  412. mp_image_t* mpi=NULL;
  413. int w2;
  414. int number = mp_imgtype >> 16;
  415. av_assert0(vf->next == NULL); // all existing filters call this just on next
  416. //vf_dint needs these as it calls vf_get_image() before configuring the output
  417. if(vf->w==0 && w>0) vf->w=w;
  418. if(vf->h==0 && h>0) vf->h=h;
  419. av_assert0(w == -1 || w >= vf->w);
  420. av_assert0(h == -1 || h >= vf->h);
  421. av_assert0(vf->w > 0);
  422. av_assert0(vf->h > 0);
  423. av_log(m->avfctx, AV_LOG_DEBUG, "get_image: %d:%d, vf: %d:%d\n", w,h,vf->w,vf->h);
  424. if (w == -1) w = vf->w;
  425. if (h == -1) h = vf->h;
  426. w2=(mp_imgflag&MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE)?((w+15)&(~15)):w;
  427. // Note: we should call libvo first to check if it supports direct rendering
  428. // and if not, then fallback to software buffers:
  429. switch(mp_imgtype & 0xff){
  430. case MP_IMGTYPE_EXPORT:
  431. if(!vf->imgctx.export_images[0]) vf->imgctx.export_images[0]=new_mp_image(w2,h);
  432. mpi=vf->imgctx.export_images[0];
  433. break;
  434. case MP_IMGTYPE_STATIC:
  435. if(!vf->imgctx.static_images[0]) vf->imgctx.static_images[0]=new_mp_image(w2,h);
  436. mpi=vf->imgctx.static_images[0];
  437. break;
  438. case MP_IMGTYPE_TEMP:
  439. if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
  440. mpi=vf->imgctx.temp_images[0];
  441. break;
  442. case MP_IMGTYPE_IPB:
  443. if(!(mp_imgflag&MP_IMGFLAG_READABLE)){ // B frame:
  444. if(!vf->imgctx.temp_images[0]) vf->imgctx.temp_images[0]=new_mp_image(w2,h);
  445. mpi=vf->imgctx.temp_images[0];
  446. break;
  447. }
  448. case MP_IMGTYPE_IP:
  449. if(!vf->imgctx.static_images[vf->imgctx.static_idx]) vf->imgctx.static_images[vf->imgctx.static_idx]=new_mp_image(w2,h);
  450. mpi=vf->imgctx.static_images[vf->imgctx.static_idx];
  451. vf->imgctx.static_idx^=1;
  452. break;
  453. case MP_IMGTYPE_NUMBERED:
  454. if (number == -1) {
  455. int i;
  456. for (i = 0; i < NUM_NUMBERED_MPI; i++)
  457. if (!vf->imgctx.numbered_images[i] || !vf->imgctx.numbered_images[i]->usage_count)
  458. break;
  459. number = i;
  460. }
  461. if (number < 0 || number >= NUM_NUMBERED_MPI) return NULL;
  462. if (!vf->imgctx.numbered_images[number]) vf->imgctx.numbered_images[number] = new_mp_image(w2,h);
  463. mpi = vf->imgctx.numbered_images[number];
  464. mpi->number = number;
  465. break;
  466. }
  467. if(mpi){
  468. mpi->type=mp_imgtype;
  469. mpi->w=vf->w; mpi->h=vf->h;
  470. // keep buffer allocation status & color flags only:
  471. // mpi->flags&=~(MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE|MP_IMGFLAG_DIRECT);
  472. mpi->flags&=MP_IMGFLAG_ALLOCATED|MP_IMGFLAG_TYPE_DISPLAYED|MP_IMGFLAGMASK_COLORS;
  473. // accept restrictions, draw_slice and palette flags only:
  474. mpi->flags|=mp_imgflag&(MP_IMGFLAGMASK_RESTRICTIONS|MP_IMGFLAG_DRAW_CALLBACK|MP_IMGFLAG_RGB_PALETTE);
  475. if(!vf->draw_slice) mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
  476. if(mpi->width!=w2 || mpi->height!=h){
  477. // printf("vf.c: MPI parameters changed! %dx%d -> %dx%d \n", mpi->width,mpi->height,w2,h);
  478. if(mpi->flags&MP_IMGFLAG_ALLOCATED){
  479. if(mpi->width<w2 || mpi->height<h){
  480. // need to re-allocate buffer memory:
  481. av_free(mpi->planes[0]);
  482. mpi->flags&=~MP_IMGFLAG_ALLOCATED;
  483. mp_msg(MSGT_VFILTER,MSGL_V,"vf.c: have to REALLOCATE buffer memory :(\n");
  484. }
  485. // } else {
  486. } {
  487. mpi->width=w2; mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
  488. mpi->height=h; mpi->chroma_height=(h + (1<<mpi->chroma_y_shift) - 1)>>mpi->chroma_y_shift;
  489. }
  490. }
  491. if(!mpi->bpp) mp_image_setfmt(mpi,outfmt);
  492. if(!(mpi->flags&MP_IMGFLAG_ALLOCATED) && mpi->type>MP_IMGTYPE_EXPORT){
  493. av_assert0(!vf->get_image);
  494. // check libvo first!
  495. if(vf->get_image) vf->get_image(vf,mpi);
  496. if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
  497. // non-direct and not yet allocated image. allocate it!
  498. if (!mpi->bpp) { // no way we can allocate this
  499. mp_msg(MSGT_DECVIDEO, MSGL_FATAL,
  500. "vf_get_image: Tried to allocate a format that can not be allocated!\n");
  501. return NULL;
  502. }
  503. // check if codec prefer aligned stride:
  504. if(mp_imgflag&MP_IMGFLAG_PREFER_ALIGNED_STRIDE){
  505. int align=(mpi->flags&MP_IMGFLAG_PLANAR &&
  506. mpi->flags&MP_IMGFLAG_YUV) ?
  507. (8<<mpi->chroma_x_shift)-1 : 15; // -- maybe FIXME
  508. w2=((w+align)&(~align));
  509. if(mpi->width!=w2){
  510. #if 0
  511. // we have to change width... check if we CAN co it:
  512. int flags=vf->query_format(vf,outfmt); // should not fail
  513. if(!(flags&3)) mp_msg(MSGT_DECVIDEO,MSGL_WARN,"??? vf_get_image{vf->query_format(outfmt)} failed!\n");
  514. // printf("query -> 0x%X \n",flags);
  515. if(flags&VFCAP_ACCEPT_STRIDE){
  516. #endif
  517. mpi->width=w2;
  518. mpi->chroma_width=(w2 + (1<<mpi->chroma_x_shift) - 1)>>mpi->chroma_x_shift;
  519. // }
  520. }
  521. }
  522. mp_image_alloc_planes(mpi);
  523. // printf("clearing img!\n");
  524. vf_mpi_clear(mpi,0,0,mpi->width,mpi->height);
  525. }
  526. }
  527. av_assert0(!vf->start_slice);
  528. if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
  529. if(vf->start_slice) vf->start_slice(vf,mpi);
  530. if(!(mpi->flags&MP_IMGFLAG_TYPE_DISPLAYED)){
  531. mp_msg(MSGT_DECVIDEO,MSGL_V,"*** [%s] %s%s mp_image_t, %dx%dx%dbpp %s %s, %d bytes\n",
  532. "NULL"/*vf->info->name*/,
  533. (mpi->type==MP_IMGTYPE_EXPORT)?"Exporting":
  534. ((mpi->flags&MP_IMGFLAG_DIRECT)?"Direct Rendering":"Allocating"),
  535. (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)?" (slices)":"",
  536. mpi->width,mpi->height,mpi->bpp,
  537. (mpi->flags&MP_IMGFLAG_YUV)?"YUV":((mpi->flags&MP_IMGFLAG_SWAPPED)?"BGR":"RGB"),
  538. (mpi->flags&MP_IMGFLAG_PLANAR)?"planar":"packed",
  539. mpi->bpp*mpi->width*mpi->height/8);
  540. 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",
  541. mpi->imgfmt, mpi->planes[0], mpi->planes[1], mpi->planes[2],
  542. mpi->stride[0], mpi->stride[1], mpi->stride[2],
  543. mpi->chroma_width, mpi->chroma_height, mpi->chroma_x_shift, mpi->chroma_y_shift);
  544. mpi->flags|=MP_IMGFLAG_TYPE_DISPLAYED;
  545. }
  546. mpi->qscale = NULL;
  547. }
  548. mpi->usage_count++;
  549. // printf("\rVF_MPI: %p %p %p %d %d %d \n",
  550. // mpi->planes[0],mpi->planes[1],mpi->planes[2],
  551. // mpi->stride[0],mpi->stride[1],mpi->stride[2]);
  552. return mpi;
  553. }
  554. int vf_next_put_image(struct vf_instance *vf,mp_image_t *mpi, double pts){
  555. MPContext *m= (void*)vf;
  556. AVFilterLink *outlink = m->avfctx->outputs[0];
  557. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  558. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  559. int i;
  560. av_assert0(vf->next);
  561. av_log(m->avfctx, AV_LOG_DEBUG, "vf_next_put_image\n");
  562. if (!pic || !picref)
  563. goto fail;
  564. picref->buf = pic;
  565. picref->buf->please_use_av_free= (void*)av_free;
  566. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  567. goto fail;
  568. pic->w = picref->video->w = mpi->w;
  569. pic->h = picref->video->h = mpi->h;
  570. /* make sure the buffer gets read permission or it's useless for output */
  571. picref->perms = AV_PERM_READ | AV_PERM_REUSE2;
  572. // av_assert0(mpi->flags&MP_IMGFLAG_READABLE);
  573. if(!(mpi->flags&MP_IMGFLAG_PRESERVE))
  574. picref->perms |= AV_PERM_WRITE;
  575. pic->refcount = 1;
  576. picref->type = AVMEDIA_TYPE_VIDEO;
  577. for(i=0; conversion_map[i].fmt && mpi->imgfmt != conversion_map[i].fmt; i++);
  578. pic->format = picref->format = conversion_map[i].pix_fmt;
  579. memcpy(pic->data, mpi->planes, FFMIN(sizeof(pic->data) , sizeof(mpi->planes)));
  580. memcpy(pic->linesize, mpi->stride, FFMIN(sizeof(pic->linesize), sizeof(mpi->stride)));
  581. memcpy(picref->data, pic->data, sizeof(picref->data));
  582. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  583. if(pts != MP_NOPTS_VALUE)
  584. picref->pts= pts * av_q2d(outlink->time_base);
  585. avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  586. avfilter_draw_slice(outlink, 0, picref->video->h, 1);
  587. avfilter_end_frame(outlink);
  588. avfilter_unref_buffer(picref);
  589. m->frame_returned++;
  590. return 1;
  591. fail:
  592. if (picref && picref->video)
  593. av_free(picref->video);
  594. av_free(picref);
  595. av_free(pic);
  596. return 0;
  597. }
  598. int vf_next_config(struct vf_instance *vf,
  599. int width, int height, int d_width, int d_height,
  600. unsigned int voflags, unsigned int outfmt){
  601. av_assert0(width>0 && height>0);
  602. vf->next->w = width; vf->next->h = height;
  603. return 1;
  604. #if 0
  605. int flags=vf->next->query_format(vf->next,outfmt);
  606. if(!flags){
  607. // hmm. colorspace mismatch!!!
  608. //this is fatal for us ATM
  609. return 0;
  610. }
  611. mp_msg(MSGT_VFILTER,MSGL_V,"REQ: flags=0x%X req=0x%X \n",flags,vf->default_reqs);
  612. miss=vf->default_reqs - (flags&vf->default_reqs);
  613. if(miss&VFCAP_ACCEPT_STRIDE){
  614. // vf requires stride support but vf->next doesn't support it!
  615. // let's insert the 'expand' filter, it does the job for us:
  616. vf_instance_t* vf2=vf_open_filter(vf->next,"expand",NULL);
  617. if(!vf2) return 0; // shouldn't happen!
  618. vf->next=vf2;
  619. }
  620. vf->next->w = width; vf->next->h = height;
  621. #endif
  622. return 1;
  623. }
  624. int vf_next_control(struct vf_instance *vf, int request, void* data){
  625. MPContext *m= (void*)vf;
  626. av_log(m->avfctx, AV_LOG_DEBUG, "Received control %d\n", request);
  627. return 0;
  628. }
  629. static int vf_default_query_format(struct vf_instance *vf, unsigned int fmt){
  630. MPContext *m= (void*)vf;
  631. int i;
  632. av_log(m->avfctx, AV_LOG_DEBUG, "query %X\n", fmt);
  633. for(i=0; conversion_map[i].fmt; i++){
  634. if(fmt==conversion_map[i].fmt)
  635. return 1; //we suport all
  636. }
  637. return 0;
  638. }
  639. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  640. {
  641. MPContext *m = ctx->priv;
  642. char name[256];
  643. int i;
  644. m->avfctx= ctx;
  645. if(!args || 1!=sscanf(args, "%255[^:=]", name)){
  646. av_log(ctx, AV_LOG_ERROR, "Invalid parameter.\n");
  647. return AVERROR(EINVAL);
  648. }
  649. args+= strlen(name)+1;
  650. for(i=0; ;i++){
  651. if(!filters[i] || !strcmp(name, filters[i]->name))
  652. break;
  653. }
  654. if(!filters[i]){
  655. av_log(ctx, AV_LOG_ERROR, "Unknown filter %s\n", name);
  656. return AVERROR(EINVAL);
  657. }
  658. av_log(ctx, AV_LOG_WARNING,
  659. "'%s' is a wrapped MPlayer filter (libmpcodecs). This filter may be removed\n"
  660. "once it has been ported to a native libavfilter.\n", name);
  661. memset(&m->vf,0,sizeof(m->vf));
  662. m->vf.info= filters[i];
  663. m->vf.next = &m->next_vf;
  664. m->vf.put_image = vf_next_put_image;
  665. m->vf.config = vf_next_config;
  666. m->vf.query_format= vf_default_query_format;
  667. m->vf.control = vf_next_control;
  668. m->vf.default_caps=VFCAP_ACCEPT_STRIDE;
  669. m->vf.default_reqs=0;
  670. if(m->vf.info->opts)
  671. av_log(ctx, AV_LOG_ERROR, "opts / m_struct_set is unsupported\n");
  672. #if 0
  673. if(vf->info->opts) { // vf_vo get some special argument
  674. const m_struct_t* st = vf->info->opts;
  675. void* vf_priv = m_struct_alloc(st);
  676. int n;
  677. for(n = 0 ; args && args[2*n] ; n++)
  678. m_struct_set(st,vf_priv,args[2*n],args[2*n+1]);
  679. vf->priv = vf_priv;
  680. args = NULL;
  681. } else // Otherwise we should have the '_oldargs_'
  682. if(args && !strcmp(args[0],"_oldargs_"))
  683. args = (char**)args[1];
  684. else
  685. args = NULL;
  686. #endif
  687. if(m->vf.info->vf_open(&m->vf, args)<=0){
  688. av_log(ctx, AV_LOG_ERROR, "vf_open() of %s with arg=%s failed\n", name, args);
  689. return -1;
  690. }
  691. return 0;
  692. }
  693. static int query_formats(AVFilterContext *ctx)
  694. {
  695. AVFilterFormats *avfmts=NULL;
  696. MPContext *m = ctx->priv;
  697. enum PixelFormat lastpixfmt = PIX_FMT_NONE;
  698. int i;
  699. for(i=0; conversion_map[i].fmt; i++){
  700. av_log(ctx, AV_LOG_DEBUG, "query: %X\n", conversion_map[i].fmt);
  701. if(m->vf.query_format(&m->vf, conversion_map[i].fmt)){
  702. av_log(ctx, AV_LOG_DEBUG, "supported,adding\n");
  703. if (conversion_map[i].pix_fmt != lastpixfmt) {
  704. avfilter_add_format(&avfmts, conversion_map[i].pix_fmt);
  705. lastpixfmt = conversion_map[i].pix_fmt;
  706. }
  707. }
  708. }
  709. //We assume all allowed input formats are also allowed output formats
  710. avfilter_set_common_pixel_formats(ctx, avfmts);
  711. return 0;
  712. }
  713. static int config_inprops(AVFilterLink *inlink)
  714. {
  715. MPContext *m = inlink->dst->priv;
  716. int i;
  717. for(i=0; conversion_map[i].fmt && conversion_map[i].pix_fmt != inlink->format; i++);
  718. av_assert0(conversion_map[i].fmt && inlink->w && inlink->h);
  719. m->vf.fmt.have_configured = 1;
  720. m->vf.fmt.orig_height = inlink->h;
  721. m->vf.fmt.orig_width = inlink->w;
  722. m->vf.fmt.orig_fmt = conversion_map[i].fmt;
  723. if(m->vf.config(&m->vf, inlink->w, inlink->h, inlink->w, inlink->h, 0, conversion_map[i].fmt)<=0)
  724. return -1;
  725. return 0;
  726. }
  727. static int config_outprops(AVFilterLink *outlink)
  728. {
  729. MPContext *m = outlink->src->priv;
  730. outlink->w = m->next_vf.w;
  731. outlink->h = m->next_vf.h;
  732. return 0;
  733. }
  734. static int request_frame(AVFilterLink *outlink)
  735. {
  736. MPContext *m = outlink->src->priv;
  737. int ret;
  738. av_log(m->avfctx, AV_LOG_DEBUG, "mp request_frame\n");
  739. for(m->frame_returned=0; !m->frame_returned;){
  740. ret=avfilter_request_frame(outlink->src->inputs[0]);
  741. if(ret<0)
  742. break;
  743. }
  744. av_log(m->avfctx, AV_LOG_DEBUG, "mp request_frame ret=%d\n", ret);
  745. return ret;
  746. }
  747. static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  748. {
  749. }
  750. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  751. {
  752. }
  753. static void end_frame(AVFilterLink *inlink)
  754. {
  755. MPContext *m = inlink->dst->priv;
  756. AVFilterBufferRef *inpic = inlink->cur_buf;
  757. int i;
  758. double pts= MP_NOPTS_VALUE;
  759. mp_image_t* mpi = new_mp_image(inpic->video->w, inpic->video->h);
  760. if(inpic->pts != AV_NOPTS_VALUE)
  761. pts= inpic->pts / av_q2d(inlink->time_base);
  762. for(i=0; conversion_map[i].fmt && conversion_map[i].pix_fmt != inlink->format; i++);
  763. mp_image_setfmt(mpi,conversion_map[i].fmt);
  764. memcpy(mpi->planes, inpic->data, FFMIN(sizeof(inpic->data) , sizeof(mpi->planes)));
  765. memcpy(mpi->stride, inpic->linesize, FFMIN(sizeof(inpic->linesize), sizeof(mpi->stride)));
  766. //FIXME pass interleced & tff flags around
  767. // mpi->flags|=MP_IMGFLAG_ALLOCATED; ?
  768. mpi->flags |= MP_IMGFLAG_READABLE;
  769. if(!(inpic->perms & AV_PERM_WRITE))
  770. mpi->flags |= MP_IMGFLAG_PRESERVE;
  771. if(m->vf.put_image(&m->vf, mpi, pts) == 0){
  772. av_log(m->avfctx, AV_LOG_DEBUG, "put_image() says skip\n");
  773. }
  774. free_mp_image(mpi);
  775. avfilter_unref_buffer(inpic);
  776. }
  777. AVFilter avfilter_vf_mp = {
  778. .name = "mp",
  779. .description = NULL_IF_CONFIG_SMALL("Apply a libmpcodecs filter to the input video."),
  780. .init = init,
  781. .priv_size = sizeof(MPContext),
  782. .query_formats = query_formats,
  783. .inputs = (const AVFilterPad[]) {{ .name = "default",
  784. .type = AVMEDIA_TYPE_VIDEO,
  785. .start_frame = start_frame,
  786. .draw_slice = null_draw_slice,
  787. .end_frame = end_frame,
  788. .config_props = config_inprops,
  789. .min_perms = AV_PERM_READ, },
  790. { .name = NULL}},
  791. .outputs = (const AVFilterPad[]) {{ .name = "default",
  792. .type = AVMEDIA_TYPE_VIDEO,
  793. .request_frame = request_frame,
  794. .config_props = config_outprops, },
  795. { .name = NULL}},
  796. };