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

1003 lines
32KB

  1. /*
  2. * filter layer
  3. * Copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /* #define DEBUG */
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/rational.h"
  24. #include "libavutil/audioconvert.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. unsigned avfilter_version(void) {
  31. av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
  32. return LIBAVFILTER_VERSION_INT;
  33. }
  34. const char *avfilter_configuration(void)
  35. {
  36. return FFMPEG_CONFIGURATION;
  37. }
  38. const char *avfilter_license(void)
  39. {
  40. #define LICENSE_PREFIX "libavfilter license: "
  41. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  42. }
  43. static void command_queue_pop(AVFilterContext *filter)
  44. {
  45. AVFilterCommand *c= filter->command_queue;
  46. av_freep(&c->arg);
  47. av_freep(&c->command);
  48. filter->command_queue= c->next;
  49. av_free(c);
  50. }
  51. AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
  52. {
  53. AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
  54. if (!ret)
  55. return NULL;
  56. *ret = *ref;
  57. if (ref->type == AVMEDIA_TYPE_VIDEO) {
  58. ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
  59. if (!ret->video) {
  60. av_free(ret);
  61. return NULL;
  62. }
  63. *ret->video = *ref->video;
  64. ret->extended_data = ret->data;
  65. } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
  66. ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
  67. if (!ret->audio) {
  68. av_free(ret);
  69. return NULL;
  70. }
  71. *ret->audio = *ref->audio;
  72. if (ref->extended_data != ref->data) {
  73. int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
  74. if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
  75. nb_channels))) {
  76. av_freep(&ret->audio);
  77. av_freep(&ret);
  78. return NULL;
  79. }
  80. memcpy(ret->extended_data, ref->extended_data,
  81. sizeof(*ret->extended_data) * nb_channels);
  82. } else
  83. ret->extended_data = ret->data;
  84. }
  85. ret->perms &= pmask;
  86. ret->buf->refcount ++;
  87. return ret;
  88. }
  89. static void free_pool(AVFilterPool *pool)
  90. {
  91. int i;
  92. av_assert0(pool->refcount > 0);
  93. for (i = 0; i < POOL_SIZE; i++) {
  94. if (pool->pic[i]) {
  95. AVFilterBufferRef *picref = pool->pic[i];
  96. /* free buffer: picrefs stored in the pool are not
  97. * supposed to contain a free callback */
  98. av_assert0(!picref->buf->refcount);
  99. av_freep(&picref->buf->data[0]);
  100. av_freep(&picref->buf);
  101. av_freep(&picref->audio);
  102. av_freep(&picref->video);
  103. av_freep(&pool->pic[i]);
  104. pool->count--;
  105. }
  106. }
  107. pool->draining = 1;
  108. if (!--pool->refcount) {
  109. av_assert0(!pool->count);
  110. av_free(pool);
  111. }
  112. }
  113. static void store_in_pool(AVFilterBufferRef *ref)
  114. {
  115. int i;
  116. AVFilterPool *pool= ref->buf->priv;
  117. av_assert0(ref->buf->data[0]);
  118. av_assert0(pool->refcount>0);
  119. if (pool->count == POOL_SIZE) {
  120. AVFilterBufferRef *ref1 = pool->pic[0];
  121. av_freep(&ref1->video);
  122. av_freep(&ref1->audio);
  123. av_freep(&ref1->buf->data[0]);
  124. av_freep(&ref1->buf);
  125. av_free(ref1);
  126. memmove(&pool->pic[0], &pool->pic[1], sizeof(void*)*(POOL_SIZE-1));
  127. pool->count--;
  128. pool->pic[POOL_SIZE-1] = NULL;
  129. }
  130. for (i = 0; i < POOL_SIZE; i++) {
  131. if (!pool->pic[i]) {
  132. pool->pic[i] = ref;
  133. pool->count++;
  134. break;
  135. }
  136. }
  137. if (pool->draining) {
  138. free_pool(pool);
  139. } else
  140. --pool->refcount;
  141. }
  142. void avfilter_unref_buffer(AVFilterBufferRef *ref)
  143. {
  144. if (!ref)
  145. return;
  146. av_assert0(ref->buf->refcount > 0);
  147. if (!(--ref->buf->refcount)) {
  148. if (!ref->buf->free) {
  149. store_in_pool(ref);
  150. return;
  151. }
  152. ref->buf->free(ref->buf);
  153. }
  154. if (ref->extended_data != ref->data)
  155. av_freep(&ref->extended_data);
  156. av_freep(&ref->video);
  157. av_freep(&ref->audio);
  158. av_free(ref);
  159. }
  160. void avfilter_unref_bufferp(AVFilterBufferRef **ref)
  161. {
  162. avfilter_unref_buffer(*ref);
  163. *ref = NULL;
  164. }
  165. void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  166. AVFilterPad **pads, AVFilterLink ***links,
  167. AVFilterPad *newpad)
  168. {
  169. unsigned i;
  170. idx = FFMIN(idx, *count);
  171. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  172. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  173. memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
  174. memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
  175. memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
  176. (*links)[idx] = NULL;
  177. (*count)++;
  178. for (i = idx+1; i < *count; i++)
  179. if (*links[i])
  180. (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
  181. }
  182. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  183. AVFilterContext *dst, unsigned dstpad)
  184. {
  185. AVFilterLink *link;
  186. if (src->output_count <= srcpad || dst->input_count <= dstpad ||
  187. src->outputs[srcpad] || dst->inputs[dstpad])
  188. return -1;
  189. if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
  190. av_log(src, AV_LOG_ERROR,
  191. "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
  192. src->name, srcpad, dst->name, dstpad);
  193. return AVERROR(EINVAL);
  194. }
  195. src->outputs[srcpad] =
  196. dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
  197. link->src = src;
  198. link->dst = dst;
  199. link->srcpad = &src->output_pads[srcpad];
  200. link->dstpad = &dst->input_pads[dstpad];
  201. link->type = src->output_pads[srcpad].type;
  202. assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
  203. link->format = -1;
  204. return 0;
  205. }
  206. void avfilter_link_free(AVFilterLink **link)
  207. {
  208. if (!*link)
  209. return;
  210. if ((*link)->pool)
  211. free_pool((*link)->pool);
  212. av_freep(link);
  213. }
  214. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  215. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
  216. {
  217. int ret;
  218. unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
  219. av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
  220. "between the filter '%s' and the filter '%s'\n",
  221. filt->name, link->src->name, link->dst->name);
  222. link->dst->inputs[dstpad_idx] = NULL;
  223. if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
  224. /* failed to link output filter to new filter */
  225. link->dst->inputs[dstpad_idx] = link;
  226. return ret;
  227. }
  228. /* re-hookup the link to the new destination filter we inserted */
  229. link->dst = filt;
  230. link->dstpad = &filt->input_pads[filt_srcpad_idx];
  231. filt->inputs[filt_srcpad_idx] = link;
  232. /* if any information on supported media formats already exists on the
  233. * link, we need to preserve that */
  234. if (link->out_formats)
  235. avfilter_formats_changeref(&link->out_formats,
  236. &filt->outputs[filt_dstpad_idx]->out_formats);
  237. if (link->out_chlayouts)
  238. avfilter_formats_changeref(&link->out_chlayouts,
  239. &filt->outputs[filt_dstpad_idx]->out_chlayouts);
  240. if (link->out_packing)
  241. avfilter_formats_changeref(&link->out_packing,
  242. &filt->outputs[filt_dstpad_idx]->out_packing);
  243. return 0;
  244. }
  245. int avfilter_config_links(AVFilterContext *filter)
  246. {
  247. int (*config_link)(AVFilterLink *);
  248. unsigned i;
  249. int ret;
  250. for (i = 0; i < filter->input_count; i ++) {
  251. AVFilterLink *link = filter->inputs[i];
  252. AVFilterLink *inlink = link->src->input_count ?
  253. link->src->inputs[0] : NULL;
  254. if (!link) continue;
  255. link->current_pts = AV_NOPTS_VALUE;
  256. switch (link->init_state) {
  257. case AVLINK_INIT:
  258. continue;
  259. case AVLINK_STARTINIT:
  260. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  261. return 0;
  262. case AVLINK_UNINIT:
  263. link->init_state = AVLINK_STARTINIT;
  264. if ((ret = avfilter_config_links(link->src)) < 0)
  265. return ret;
  266. if (!(config_link = link->srcpad->config_props)) {
  267. if (link->src->input_count != 1) {
  268. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  269. "with more than one input "
  270. "must set config_props() "
  271. "callbacks on all outputs\n");
  272. return AVERROR(EINVAL);
  273. }
  274. } else if ((ret = config_link(link)) < 0)
  275. return ret;
  276. switch (link->type) {
  277. case AVMEDIA_TYPE_VIDEO:
  278. if (!link->time_base.num && !link->time_base.den)
  279. link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
  280. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  281. link->sample_aspect_ratio = inlink ?
  282. inlink->sample_aspect_ratio : (AVRational){1,1};
  283. if (inlink) {
  284. if (!link->w)
  285. link->w = inlink->w;
  286. if (!link->h)
  287. link->h = inlink->h;
  288. } else if (!link->w || !link->h) {
  289. av_log(link->src, AV_LOG_ERROR,
  290. "Video source filters must set their output link's "
  291. "width and height\n");
  292. return AVERROR(EINVAL);
  293. }
  294. break;
  295. case AVMEDIA_TYPE_AUDIO:
  296. if (inlink) {
  297. if (!link->sample_rate)
  298. link->sample_rate = inlink->sample_rate;
  299. if (!link->time_base.num && !link->time_base.den)
  300. link->time_base = inlink->time_base;
  301. } else if (!link->sample_rate) {
  302. av_log(link->src, AV_LOG_ERROR,
  303. "Audio source filters must set their output link's "
  304. "sample_rate\n");
  305. return AVERROR(EINVAL);
  306. }
  307. if (!link->time_base.num && !link->time_base.den)
  308. link->time_base = (AVRational) {1, link->sample_rate};
  309. }
  310. if ((config_link = link->dstpad->config_props))
  311. if ((ret = config_link(link)) < 0)
  312. return ret;
  313. link->init_state = AVLINK_INIT;
  314. }
  315. }
  316. return 0;
  317. }
  318. static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  319. {
  320. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  321. perms & AV_PERM_READ ? "r" : "",
  322. perms & AV_PERM_WRITE ? "w" : "",
  323. perms & AV_PERM_PRESERVE ? "p" : "",
  324. perms & AV_PERM_REUSE ? "u" : "",
  325. perms & AV_PERM_REUSE2 ? "U" : "",
  326. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  327. return buf;
  328. }
  329. static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  330. {
  331. av_unused char buf[16];
  332. av_dlog(ctx,
  333. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  334. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  335. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  336. ref->pts, ref->pos);
  337. if (ref->video) {
  338. av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  339. ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
  340. ref->video->w, ref->video->h,
  341. !ref->video->interlaced ? 'P' : /* Progressive */
  342. ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
  343. ref->video->key_frame,
  344. av_get_picture_type_char(ref->video->pict_type));
  345. }
  346. if (ref->audio) {
  347. av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
  348. ref->audio->channel_layout,
  349. ref->audio->nb_samples,
  350. ref->audio->sample_rate,
  351. ref->audio->planar);
  352. }
  353. av_dlog(ctx, "]%s", end ? "\n" : "");
  354. }
  355. static void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
  356. {
  357. if (link->type == AVMEDIA_TYPE_VIDEO) {
  358. av_dlog(ctx,
  359. "link[%p s:%dx%d fmt:%s %s->%s]%s",
  360. link, link->w, link->h,
  361. av_pix_fmt_descriptors[link->format].name,
  362. link->src ? link->src->filter->name : "",
  363. link->dst ? link->dst->filter->name : "",
  364. end ? "\n" : "");
  365. } else {
  366. char buf[128];
  367. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  368. av_dlog(ctx,
  369. "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
  370. link, (int)link->sample_rate, buf,
  371. av_get_sample_fmt_name(link->format),
  372. link->src ? link->src->filter->name : "",
  373. link->dst ? link->dst->filter->name : "",
  374. end ? "\n" : "");
  375. }
  376. }
  377. #define FF_DPRINTF_START(ctx, func) av_dlog(NULL, "%-16s: ", #func)
  378. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  379. {
  380. AVFilterBufferRef *ret = NULL;
  381. av_unused char buf[16];
  382. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
  383. av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  384. if (link->dstpad->get_video_buffer)
  385. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  386. if (!ret)
  387. ret = avfilter_default_get_video_buffer(link, perms, w, h);
  388. if (ret)
  389. ret->type = AVMEDIA_TYPE_VIDEO;
  390. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
  391. return ret;
  392. }
  393. AVFilterBufferRef *
  394. avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
  395. int w, int h, enum PixelFormat format)
  396. {
  397. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  398. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  399. if (!pic || !picref)
  400. goto fail;
  401. picref->buf = pic;
  402. picref->buf->free = ff_avfilter_default_free_buffer;
  403. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  404. goto fail;
  405. pic->w = picref->video->w = w;
  406. pic->h = picref->video->h = h;
  407. /* make sure the buffer gets read permission or it's useless for output */
  408. picref->perms = perms | AV_PERM_READ;
  409. pic->refcount = 1;
  410. picref->type = AVMEDIA_TYPE_VIDEO;
  411. pic->format = picref->format = format;
  412. memcpy(pic->data, data, 4*sizeof(data[0]));
  413. memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
  414. memcpy(picref->data, pic->data, sizeof(picref->data));
  415. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  416. pic-> extended_data = pic->data;
  417. picref->extended_data = picref->data;
  418. return picref;
  419. fail:
  420. if (picref && picref->video)
  421. av_free(picref->video);
  422. av_free(picref);
  423. av_free(pic);
  424. return NULL;
  425. }
  426. AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link,
  427. int perms, int nb_samples)
  428. {
  429. AVFilterBufferRef *ret = NULL;
  430. if (link->dstpad->get_audio_buffer)
  431. ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
  432. if (!ret)
  433. ret = avfilter_default_get_audio_buffer(link, perms, nb_samples);
  434. if (ret)
  435. ret->type = AVMEDIA_TYPE_AUDIO;
  436. return ret;
  437. }
  438. AVFilterBufferRef *
  439. avfilter_get_audio_buffer_ref_from_arrays(uint8_t *data[8], int linesize[8], int perms,
  440. int nb_samples, enum AVSampleFormat sample_fmt,
  441. uint64_t channel_layout, int planar)
  442. {
  443. AVFilterBuffer *samples = av_mallocz(sizeof(AVFilterBuffer));
  444. AVFilterBufferRef *samplesref = av_mallocz(sizeof(AVFilterBufferRef));
  445. if (!samples || !samplesref)
  446. goto fail;
  447. samplesref->buf = samples;
  448. samplesref->buf->free = ff_avfilter_default_free_buffer;
  449. if (!(samplesref->audio = av_mallocz(sizeof(AVFilterBufferRefAudioProps))))
  450. goto fail;
  451. samplesref->audio->nb_samples = nb_samples;
  452. samplesref->audio->channel_layout = channel_layout;
  453. samplesref->audio->planar = planar;
  454. /* make sure the buffer gets read permission or it's useless for output */
  455. samplesref->perms = perms | AV_PERM_READ;
  456. samples->refcount = 1;
  457. samplesref->type = AVMEDIA_TYPE_AUDIO;
  458. samplesref->format = sample_fmt;
  459. memcpy(samples->data, data, sizeof(samples->data));
  460. memcpy(samples->linesize, linesize, sizeof(samples->linesize));
  461. memcpy(samplesref->data, data, sizeof(samplesref->data));
  462. memcpy(samplesref->linesize, linesize, sizeof(samplesref->linesize));
  463. return samplesref;
  464. fail:
  465. if (samplesref && samplesref->audio)
  466. av_freep(&samplesref->audio);
  467. av_freep(&samplesref);
  468. av_freep(&samples);
  469. return NULL;
  470. }
  471. AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_arrays_alt(uint8_t **data,
  472. int linesize, int perms,
  473. int nb_samples,
  474. enum AVSampleFormat sample_fmt,
  475. uint64_t channel_layout)
  476. {
  477. int planes;
  478. AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
  479. AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
  480. if (!samples || !samplesref)
  481. goto fail;
  482. samplesref->buf = samples;
  483. samplesref->buf->free = ff_avfilter_default_free_buffer;
  484. if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
  485. goto fail;
  486. samplesref->audio->nb_samples = nb_samples;
  487. samplesref->audio->channel_layout = channel_layout;
  488. samplesref->audio->planar = av_sample_fmt_is_planar(sample_fmt);
  489. planes = samplesref->audio->planar ? av_get_channel_layout_nb_channels(channel_layout) : 1;
  490. /* make sure the buffer gets read permission or it's useless for output */
  491. samplesref->perms = perms | AV_PERM_READ;
  492. samples->refcount = 1;
  493. samplesref->type = AVMEDIA_TYPE_AUDIO;
  494. samplesref->format = sample_fmt;
  495. memcpy(samples->data, data,
  496. FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
  497. memcpy(samplesref->data, samples->data, sizeof(samples->data));
  498. samples->linesize[0] = samplesref->linesize[0] = linesize;
  499. if (planes > FF_ARRAY_ELEMS(samples->data)) {
  500. samples-> extended_data = av_mallocz(sizeof(*samples->extended_data) *
  501. planes);
  502. samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
  503. planes);
  504. if (!samples->extended_data || !samplesref->extended_data)
  505. goto fail;
  506. memcpy(samples-> extended_data, data, sizeof(*data)*planes);
  507. memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
  508. } else {
  509. samples->extended_data = samples->data;
  510. samplesref->extended_data = samplesref->data;
  511. }
  512. return samplesref;
  513. fail:
  514. if (samples && samples->extended_data != samples->data)
  515. av_freep(&samples->extended_data);
  516. if (samplesref) {
  517. av_freep(&samplesref->audio);
  518. if (samplesref->extended_data != samplesref->data)
  519. av_freep(&samplesref->extended_data);
  520. }
  521. av_freep(&samplesref);
  522. av_freep(&samples);
  523. return NULL;
  524. }
  525. int avfilter_request_frame(AVFilterLink *link)
  526. {
  527. FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
  528. if (link->srcpad->request_frame)
  529. return link->srcpad->request_frame(link);
  530. else if (link->src->inputs[0])
  531. return avfilter_request_frame(link->src->inputs[0]);
  532. else return -1;
  533. }
  534. int avfilter_poll_frame(AVFilterLink *link)
  535. {
  536. int i, min = INT_MAX;
  537. if (link->srcpad->poll_frame)
  538. return link->srcpad->poll_frame(link);
  539. for (i = 0; i < link->src->input_count; i++) {
  540. int val;
  541. if (!link->src->inputs[i])
  542. return -1;
  543. val = avfilter_poll_frame(link->src->inputs[i]);
  544. min = FFMIN(min, val);
  545. }
  546. return min;
  547. }
  548. static void update_link_current_pts(AVFilterLink *link, int64_t pts)
  549. {
  550. if (pts == AV_NOPTS_VALUE)
  551. return;
  552. link->current_pts = pts; /* TODO use duration */
  553. if (link->graph && link->age_index >= 0)
  554. ff_avfilter_graph_update_heap(link->graph, link);
  555. }
  556. /* XXX: should we do the duplicating of the picture ref here, instead of
  557. * forcing the source filter to do it? */
  558. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  559. {
  560. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  561. AVFilterPad *dst = link->dstpad;
  562. int perms = picref->perms;
  563. AVFilterCommand *cmd= link->dst->command_queue;
  564. FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
  565. if (!(start_frame = dst->start_frame))
  566. start_frame = avfilter_default_start_frame;
  567. if (picref->linesize[0] < 0)
  568. perms |= AV_PERM_NEG_LINESIZES;
  569. /* prepare to copy the picture if it has insufficient permissions */
  570. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  571. av_log(link->dst, AV_LOG_DEBUG,
  572. "frame copy needed (have perms %x, need %x, reject %x)\n",
  573. picref->perms,
  574. link->dstpad->min_perms, link->dstpad->rej_perms);
  575. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  576. link->src_buf = picref;
  577. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  578. }
  579. else
  580. link->cur_buf = picref;
  581. while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
  582. av_log(link->dst, AV_LOG_DEBUG,
  583. "Processing command time:%f command:%s arg:%s\n",
  584. cmd->time, cmd->command, cmd->arg);
  585. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  586. command_queue_pop(link->dst);
  587. cmd= link->dst->command_queue;
  588. }
  589. start_frame(link, link->cur_buf);
  590. update_link_current_pts(link, link->cur_buf->pts);
  591. }
  592. void avfilter_end_frame(AVFilterLink *link)
  593. {
  594. void (*end_frame)(AVFilterLink *);
  595. if (!(end_frame = link->dstpad->end_frame))
  596. end_frame = avfilter_default_end_frame;
  597. end_frame(link);
  598. /* unreference the source picture if we're feeding the destination filter
  599. * a copied version dues to permission issues */
  600. if (link->src_buf) {
  601. avfilter_unref_buffer(link->src_buf);
  602. link->src_buf = NULL;
  603. }
  604. }
  605. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  606. {
  607. uint8_t *src[4], *dst[4];
  608. int i, j, vsub;
  609. void (*draw_slice)(AVFilterLink *, int, int, int);
  610. FF_DPRINTF_START(NULL, draw_slice); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
  611. /* copy the slice if needed for permission reasons */
  612. if (link->src_buf) {
  613. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  614. for (i = 0; i < 4; i++) {
  615. if (link->src_buf->data[i]) {
  616. src[i] = link->src_buf-> data[i] +
  617. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  618. dst[i] = link->cur_buf->data[i] +
  619. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  620. } else
  621. src[i] = dst[i] = NULL;
  622. }
  623. for (i = 0; i < 4; i++) {
  624. int planew =
  625. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  626. if (!src[i]) continue;
  627. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  628. memcpy(dst[i], src[i], planew);
  629. src[i] += link->src_buf->linesize[i];
  630. dst[i] += link->cur_buf->linesize[i];
  631. }
  632. }
  633. }
  634. if (!(draw_slice = link->dstpad->draw_slice))
  635. draw_slice = avfilter_default_draw_slice;
  636. draw_slice(link, y, h, slice_dir);
  637. }
  638. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  639. {
  640. if(!strcmp(cmd, "ping")){
  641. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  642. return 0;
  643. }else if(filter->filter->process_command) {
  644. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  645. }
  646. return AVERROR(ENOSYS);
  647. }
  648. void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  649. {
  650. void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
  651. AVFilterPad *dst = link->dstpad;
  652. int i;
  653. int64_t pts;
  654. FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
  655. if (!(filter_samples = dst->filter_samples))
  656. filter_samples = avfilter_default_filter_samples;
  657. /* prepare to copy the samples if the buffer has insufficient permissions */
  658. if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
  659. dst->rej_perms & samplesref->perms) {
  660. av_log(link->dst, AV_LOG_DEBUG,
  661. "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
  662. samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
  663. link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
  664. samplesref->audio->nb_samples);
  665. link->cur_buf->pts = samplesref->pts;
  666. link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
  667. /* Copy actual data into new samples buffer */
  668. for (i = 0; samplesref->data[i] && i < 8; i++)
  669. memcpy(link->cur_buf->data[i], samplesref->data[i], samplesref->linesize[0]);
  670. avfilter_unref_buffer(samplesref);
  671. } else
  672. link->cur_buf = samplesref;
  673. pts = link->cur_buf->pts;
  674. filter_samples(link, link->cur_buf);
  675. update_link_current_pts(link, pts);
  676. }
  677. #define MAX_REGISTERED_AVFILTERS_NB 128
  678. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  679. static int next_registered_avfilter_idx = 0;
  680. AVFilter *avfilter_get_by_name(const char *name)
  681. {
  682. int i;
  683. for (i = 0; registered_avfilters[i]; i++)
  684. if (!strcmp(registered_avfilters[i]->name, name))
  685. return registered_avfilters[i];
  686. return NULL;
  687. }
  688. int avfilter_register(AVFilter *filter)
  689. {
  690. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
  691. av_log(NULL, AV_LOG_ERROR,
  692. "Maximum number of registered filters %d reached, "
  693. "impossible to register filter with name '%s'\n",
  694. MAX_REGISTERED_AVFILTERS_NB, filter->name);
  695. return AVERROR(ENOMEM);
  696. }
  697. registered_avfilters[next_registered_avfilter_idx++] = filter;
  698. return 0;
  699. }
  700. AVFilter **av_filter_next(AVFilter **filter)
  701. {
  702. return filter ? ++filter : &registered_avfilters[0];
  703. }
  704. void avfilter_uninit(void)
  705. {
  706. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  707. next_registered_avfilter_idx = 0;
  708. }
  709. static int pad_count(const AVFilterPad *pads)
  710. {
  711. int count;
  712. for(count = 0; pads->name; count ++) pads ++;
  713. return count;
  714. }
  715. static const char *filter_name(void *p)
  716. {
  717. AVFilterContext *filter = p;
  718. return filter->filter->name;
  719. }
  720. static const AVClass avfilter_class = {
  721. "AVFilter",
  722. filter_name,
  723. NULL,
  724. LIBAVUTIL_VERSION_INT,
  725. };
  726. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  727. {
  728. AVFilterContext *ret;
  729. *filter_ctx = NULL;
  730. if (!filter)
  731. return AVERROR(EINVAL);
  732. ret = av_mallocz(sizeof(AVFilterContext));
  733. if (!ret)
  734. return AVERROR(ENOMEM);
  735. ret->av_class = &avfilter_class;
  736. ret->filter = filter;
  737. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  738. if (filter->priv_size) {
  739. ret->priv = av_mallocz(filter->priv_size);
  740. if (!ret->priv)
  741. goto err;
  742. }
  743. ret->input_count = pad_count(filter->inputs);
  744. if (ret->input_count) {
  745. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
  746. if (!ret->input_pads)
  747. goto err;
  748. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
  749. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
  750. if (!ret->inputs)
  751. goto err;
  752. }
  753. ret->output_count = pad_count(filter->outputs);
  754. if (ret->output_count) {
  755. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
  756. if (!ret->output_pads)
  757. goto err;
  758. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
  759. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
  760. if (!ret->outputs)
  761. goto err;
  762. }
  763. *filter_ctx = ret;
  764. return 0;
  765. err:
  766. av_freep(&ret->inputs);
  767. av_freep(&ret->input_pads);
  768. ret->input_count = 0;
  769. av_freep(&ret->outputs);
  770. av_freep(&ret->output_pads);
  771. ret->output_count = 0;
  772. av_freep(&ret->priv);
  773. av_free(ret);
  774. return AVERROR(ENOMEM);
  775. }
  776. void avfilter_free(AVFilterContext *filter)
  777. {
  778. int i;
  779. AVFilterLink *link;
  780. if (!filter)
  781. return;
  782. if (filter->filter->uninit)
  783. filter->filter->uninit(filter);
  784. for (i = 0; i < filter->input_count; i++) {
  785. if ((link = filter->inputs[i])) {
  786. if (link->src)
  787. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  788. avfilter_formats_unref(&link->in_formats);
  789. avfilter_formats_unref(&link->out_formats);
  790. }
  791. avfilter_link_free(&link);
  792. }
  793. for (i = 0; i < filter->output_count; i++) {
  794. if ((link = filter->outputs[i])) {
  795. if (link->dst)
  796. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  797. avfilter_formats_unref(&link->in_formats);
  798. avfilter_formats_unref(&link->out_formats);
  799. }
  800. avfilter_link_free(&link);
  801. }
  802. av_freep(&filter->name);
  803. av_freep(&filter->input_pads);
  804. av_freep(&filter->output_pads);
  805. av_freep(&filter->inputs);
  806. av_freep(&filter->outputs);
  807. av_freep(&filter->priv);
  808. while(filter->command_queue){
  809. command_queue_pop(filter);
  810. }
  811. av_free(filter);
  812. }
  813. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  814. {
  815. int ret=0;
  816. if (filter->filter->init)
  817. ret = filter->filter->init(filter, args, opaque);
  818. return ret;
  819. }
  820. void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
  821. {
  822. // copy common properties
  823. dst->pts = src->pts;
  824. dst->pos = src->pos;
  825. switch (src->type) {
  826. case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
  827. case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
  828. default: break;
  829. }
  830. }