vf_mp.c 26 KB

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