vf_mp.c 30 KB

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