vf_mp.c 30 KB

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