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.

790 lines
25KB

  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. #include "libavutil/avassert.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/channel_layout.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "libavutil/rational.h"
  29. #include "libavutil/samplefmt.h"
  30. #include "audio.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. #include "audio.h"
  35. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame);
  36. void ff_tlog_ref(void *ctx, AVFrame *ref, int end)
  37. {
  38. av_unused char buf[16];
  39. ff_tlog(ctx,
  40. "ref[%p buf:%p data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  41. ref, ref->buf, ref->data[0],
  42. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  43. ref->pts, av_frame_get_pkt_pos(ref));
  44. if (ref->width) {
  45. ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  46. ref->sample_aspect_ratio.num, ref->sample_aspect_ratio.den,
  47. ref->width, ref->height,
  48. !ref->interlaced_frame ? 'P' : /* Progressive */
  49. ref->top_field_first ? 'T' : 'B', /* Top / Bottom */
  50. ref->key_frame,
  51. av_get_picture_type_char(ref->pict_type));
  52. }
  53. if (ref->nb_samples) {
  54. ff_tlog(ctx, " cl:%"PRId64"d n:%d r:%d",
  55. ref->channel_layout,
  56. ref->nb_samples,
  57. ref->sample_rate);
  58. }
  59. ff_tlog(ctx, "]%s", end ? "\n" : "");
  60. }
  61. unsigned avfilter_version(void) {
  62. av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
  63. return LIBAVFILTER_VERSION_INT;
  64. }
  65. const char *avfilter_configuration(void)
  66. {
  67. return FFMPEG_CONFIGURATION;
  68. }
  69. const char *avfilter_license(void)
  70. {
  71. #define LICENSE_PREFIX "libavfilter license: "
  72. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  73. }
  74. void ff_command_queue_pop(AVFilterContext *filter)
  75. {
  76. AVFilterCommand *c= filter->command_queue;
  77. av_freep(&c->arg);
  78. av_freep(&c->command);
  79. filter->command_queue= c->next;
  80. av_free(c);
  81. }
  82. void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
  83. AVFilterPad **pads, AVFilterLink ***links,
  84. AVFilterPad *newpad)
  85. {
  86. unsigned i;
  87. idx = FFMIN(idx, *count);
  88. *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
  89. *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
  90. memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
  91. memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
  92. memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
  93. (*links)[idx] = NULL;
  94. (*count)++;
  95. for (i = idx+1; i < *count; i++)
  96. if (*links[i])
  97. (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
  98. }
  99. int avfilter_link(AVFilterContext *src, unsigned srcpad,
  100. AVFilterContext *dst, unsigned dstpad)
  101. {
  102. AVFilterLink *link;
  103. if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
  104. src->outputs[srcpad] || dst->inputs[dstpad])
  105. return -1;
  106. if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
  107. av_log(src, AV_LOG_ERROR,
  108. "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
  109. src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
  110. dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
  111. return AVERROR(EINVAL);
  112. }
  113. src->outputs[srcpad] =
  114. dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
  115. link->src = src;
  116. link->dst = dst;
  117. link->srcpad = &src->output_pads[srcpad];
  118. link->dstpad = &dst->input_pads[dstpad];
  119. link->type = src->output_pads[srcpad].type;
  120. av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
  121. link->format = -1;
  122. return 0;
  123. }
  124. void avfilter_link_free(AVFilterLink **link)
  125. {
  126. if (!*link)
  127. return;
  128. av_frame_free(&(*link)->partial_buf);
  129. av_freep(link);
  130. }
  131. int avfilter_link_get_channels(AVFilterLink *link)
  132. {
  133. return link->channels;
  134. }
  135. void avfilter_link_set_closed(AVFilterLink *link, int closed)
  136. {
  137. link->closed = closed;
  138. }
  139. int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
  140. unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
  141. {
  142. int ret;
  143. unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
  144. av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
  145. "between the filter '%s' and the filter '%s'\n",
  146. filt->name, link->src->name, link->dst->name);
  147. link->dst->inputs[dstpad_idx] = NULL;
  148. if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
  149. /* failed to link output filter to new filter */
  150. link->dst->inputs[dstpad_idx] = link;
  151. return ret;
  152. }
  153. /* re-hookup the link to the new destination filter we inserted */
  154. link->dst = filt;
  155. link->dstpad = &filt->input_pads[filt_srcpad_idx];
  156. filt->inputs[filt_srcpad_idx] = link;
  157. /* if any information on supported media formats already exists on the
  158. * link, we need to preserve that */
  159. if (link->out_formats)
  160. ff_formats_changeref(&link->out_formats,
  161. &filt->outputs[filt_dstpad_idx]->out_formats);
  162. if (link->out_samplerates)
  163. ff_formats_changeref(&link->out_samplerates,
  164. &filt->outputs[filt_dstpad_idx]->out_samplerates);
  165. if (link->out_channel_layouts)
  166. ff_channel_layouts_changeref(&link->out_channel_layouts,
  167. &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
  168. return 0;
  169. }
  170. int avfilter_config_links(AVFilterContext *filter)
  171. {
  172. int (*config_link)(AVFilterLink *);
  173. unsigned i;
  174. int ret;
  175. for (i = 0; i < filter->nb_inputs; i ++) {
  176. AVFilterLink *link = filter->inputs[i];
  177. AVFilterLink *inlink;
  178. if (!link) continue;
  179. inlink = link->src->nb_inputs ? link->src->inputs[0] : NULL;
  180. link->current_pts = AV_NOPTS_VALUE;
  181. switch (link->init_state) {
  182. case AVLINK_INIT:
  183. continue;
  184. case AVLINK_STARTINIT:
  185. av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
  186. return 0;
  187. case AVLINK_UNINIT:
  188. link->init_state = AVLINK_STARTINIT;
  189. if ((ret = avfilter_config_links(link->src)) < 0)
  190. return ret;
  191. if (!(config_link = link->srcpad->config_props)) {
  192. if (link->src->nb_inputs != 1) {
  193. av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
  194. "with more than one input "
  195. "must set config_props() "
  196. "callbacks on all outputs\n");
  197. return AVERROR(EINVAL);
  198. }
  199. } else if ((ret = config_link(link)) < 0) {
  200. av_log(link->src, AV_LOG_ERROR,
  201. "Failed to configure output pad on %s\n",
  202. link->src->name);
  203. return ret;
  204. }
  205. switch (link->type) {
  206. case AVMEDIA_TYPE_VIDEO:
  207. if (!link->time_base.num && !link->time_base.den)
  208. link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
  209. if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
  210. link->sample_aspect_ratio = inlink ?
  211. inlink->sample_aspect_ratio : (AVRational){1,1};
  212. if (inlink && !link->frame_rate.num && !link->frame_rate.den)
  213. link->frame_rate = inlink->frame_rate;
  214. if (inlink) {
  215. if (!link->w)
  216. link->w = inlink->w;
  217. if (!link->h)
  218. link->h = inlink->h;
  219. } else if (!link->w || !link->h) {
  220. av_log(link->src, AV_LOG_ERROR,
  221. "Video source filters must set their output link's "
  222. "width and height\n");
  223. return AVERROR(EINVAL);
  224. }
  225. break;
  226. case AVMEDIA_TYPE_AUDIO:
  227. if (inlink) {
  228. if (!link->time_base.num && !link->time_base.den)
  229. link->time_base = inlink->time_base;
  230. }
  231. if (!link->time_base.num && !link->time_base.den)
  232. link->time_base = (AVRational) {1, link->sample_rate};
  233. }
  234. if ((config_link = link->dstpad->config_props))
  235. if ((ret = config_link(link)) < 0) {
  236. av_log(link->src, AV_LOG_ERROR,
  237. "Failed to configure input pad on %s\n",
  238. link->dst->name);
  239. return ret;
  240. }
  241. link->init_state = AVLINK_INIT;
  242. }
  243. }
  244. return 0;
  245. }
  246. void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
  247. {
  248. if (link->type == AVMEDIA_TYPE_VIDEO) {
  249. ff_tlog(ctx,
  250. "link[%p s:%dx%d fmt:%s %s->%s]%s",
  251. link, link->w, link->h,
  252. av_get_pix_fmt_name(link->format),
  253. link->src ? link->src->filter->name : "",
  254. link->dst ? link->dst->filter->name : "",
  255. end ? "\n" : "");
  256. } else {
  257. char buf[128];
  258. av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
  259. ff_tlog(ctx,
  260. "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
  261. link, (int)link->sample_rate, buf,
  262. av_get_sample_fmt_name(link->format),
  263. link->src ? link->src->filter->name : "",
  264. link->dst ? link->dst->filter->name : "",
  265. end ? "\n" : "");
  266. }
  267. }
  268. int ff_request_frame(AVFilterLink *link)
  269. {
  270. int ret = -1;
  271. FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
  272. if (link->closed)
  273. return AVERROR_EOF;
  274. av_assert0(!link->frame_requested);
  275. link->frame_requested = 1;
  276. while (link->frame_requested) {
  277. if (link->srcpad->request_frame)
  278. ret = link->srcpad->request_frame(link);
  279. else if (link->src->inputs[0])
  280. ret = ff_request_frame(link->src->inputs[0]);
  281. if (ret == AVERROR_EOF && link->partial_buf) {
  282. AVFrame *pbuf = link->partial_buf;
  283. link->partial_buf = NULL;
  284. ret = ff_filter_frame_framed(link, pbuf);
  285. }
  286. if (ret < 0) {
  287. link->frame_requested = 0;
  288. if (ret == AVERROR_EOF)
  289. link->closed = 1;
  290. } else {
  291. av_assert0(!link->frame_requested ||
  292. link->flags & FF_LINK_FLAG_REQUEST_LOOP);
  293. }
  294. }
  295. return ret;
  296. }
  297. int ff_poll_frame(AVFilterLink *link)
  298. {
  299. int i, min = INT_MAX;
  300. if (link->srcpad->poll_frame)
  301. return link->srcpad->poll_frame(link);
  302. for (i = 0; i < link->src->nb_inputs; i++) {
  303. int val;
  304. if (!link->src->inputs[i])
  305. return -1;
  306. val = ff_poll_frame(link->src->inputs[i]);
  307. min = FFMIN(min, val);
  308. }
  309. return min;
  310. }
  311. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
  312. {
  313. if (pts == AV_NOPTS_VALUE)
  314. return;
  315. link->current_pts = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
  316. /* TODO use duration */
  317. if (link->graph && link->age_index >= 0)
  318. ff_avfilter_graph_update_heap(link->graph, link);
  319. }
  320. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  321. {
  322. if(!strcmp(cmd, "ping")){
  323. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  324. return 0;
  325. }else if(filter->filter->process_command) {
  326. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  327. }
  328. return AVERROR(ENOSYS);
  329. }
  330. #define MAX_REGISTERED_AVFILTERS_NB 256
  331. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  332. static int next_registered_avfilter_idx = 0;
  333. AVFilter *avfilter_get_by_name(const char *name)
  334. {
  335. int i;
  336. for (i = 0; registered_avfilters[i]; i++)
  337. if (!strcmp(registered_avfilters[i]->name, name))
  338. return registered_avfilters[i];
  339. return NULL;
  340. }
  341. int avfilter_register(AVFilter *filter)
  342. {
  343. int i;
  344. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
  345. av_log(NULL, AV_LOG_ERROR,
  346. "Maximum number of registered filters %d reached, "
  347. "impossible to register filter with name '%s'\n",
  348. MAX_REGISTERED_AVFILTERS_NB, filter->name);
  349. return AVERROR(ENOMEM);
  350. }
  351. for(i=0; filter->inputs && filter->inputs[i].name; i++) {
  352. const AVFilterPad *input = &filter->inputs[i];
  353. av_assert0( !input->filter_frame
  354. || (!input->start_frame && !input->end_frame));
  355. }
  356. registered_avfilters[next_registered_avfilter_idx++] = filter;
  357. return 0;
  358. }
  359. AVFilter **av_filter_next(AVFilter **filter)
  360. {
  361. return filter ? ++filter : &registered_avfilters[0];
  362. }
  363. void avfilter_uninit(void)
  364. {
  365. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  366. next_registered_avfilter_idx = 0;
  367. }
  368. static int pad_count(const AVFilterPad *pads)
  369. {
  370. int count;
  371. if (!pads)
  372. return 0;
  373. for(count = 0; pads->name; count ++) pads ++;
  374. return count;
  375. }
  376. static const char *default_filter_name(void *filter_ctx)
  377. {
  378. AVFilterContext *ctx = filter_ctx;
  379. return ctx->name ? ctx->name : ctx->filter->name;
  380. }
  381. static void *filter_child_next(void *obj, void *prev)
  382. {
  383. AVFilterContext *ctx = obj;
  384. if (!prev && ctx->filter && ctx->filter->priv_class)
  385. return ctx->priv;
  386. return NULL;
  387. }
  388. static const AVClass *filter_child_class_next(const AVClass *prev)
  389. {
  390. AVFilter **filter_ptr = NULL;
  391. /* find the filter that corresponds to prev */
  392. while (prev && *(filter_ptr = av_filter_next(filter_ptr)))
  393. if ((*filter_ptr)->priv_class == prev)
  394. break;
  395. /* could not find filter corresponding to prev */
  396. if (prev && !(*filter_ptr))
  397. return NULL;
  398. /* find next filter with specific options */
  399. while (*(filter_ptr = av_filter_next(filter_ptr)))
  400. if ((*filter_ptr)->priv_class)
  401. return (*filter_ptr)->priv_class;
  402. return NULL;
  403. }
  404. static const AVClass avfilter_class = {
  405. .class_name = "AVFilter",
  406. .item_name = default_filter_name,
  407. .version = LIBAVUTIL_VERSION_INT,
  408. .category = AV_CLASS_CATEGORY_FILTER,
  409. .child_next = filter_child_next,
  410. .child_class_next = filter_child_class_next,
  411. };
  412. const AVClass *avfilter_get_class(void)
  413. {
  414. return &avfilter_class;
  415. }
  416. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  417. {
  418. AVFilterContext *ret;
  419. *filter_ctx = NULL;
  420. if (!filter)
  421. return AVERROR(EINVAL);
  422. ret = av_mallocz(sizeof(AVFilterContext));
  423. if (!ret)
  424. return AVERROR(ENOMEM);
  425. ret->av_class = &avfilter_class;
  426. ret->filter = filter;
  427. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  428. if (filter->priv_size) {
  429. ret->priv = av_mallocz(filter->priv_size);
  430. if (!ret->priv)
  431. goto err;
  432. }
  433. ret->nb_inputs = pad_count(filter->inputs);
  434. if (ret->nb_inputs ) {
  435. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
  436. if (!ret->input_pads)
  437. goto err;
  438. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
  439. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
  440. if (!ret->inputs)
  441. goto err;
  442. }
  443. ret->nb_outputs = pad_count(filter->outputs);
  444. if (ret->nb_outputs) {
  445. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
  446. if (!ret->output_pads)
  447. goto err;
  448. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
  449. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
  450. if (!ret->outputs)
  451. goto err;
  452. }
  453. #if FF_API_FOO_COUNT
  454. ret->output_count = ret->nb_outputs;
  455. ret->input_count = ret->nb_inputs;
  456. #endif
  457. *filter_ctx = ret;
  458. return 0;
  459. err:
  460. av_freep(&ret->inputs);
  461. av_freep(&ret->input_pads);
  462. ret->nb_inputs = 0;
  463. av_freep(&ret->outputs);
  464. av_freep(&ret->output_pads);
  465. ret->nb_outputs = 0;
  466. av_freep(&ret->priv);
  467. av_free(ret);
  468. return AVERROR(ENOMEM);
  469. }
  470. void avfilter_free(AVFilterContext *filter)
  471. {
  472. int i;
  473. AVFilterLink *link;
  474. if (!filter)
  475. return;
  476. if (filter->filter->uninit)
  477. filter->filter->uninit(filter);
  478. if (filter->filter->shorthand)
  479. av_opt_free(filter->priv);
  480. for (i = 0; i < filter->nb_inputs; i++) {
  481. if ((link = filter->inputs[i])) {
  482. if (link->src)
  483. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  484. ff_formats_unref(&link->in_formats);
  485. ff_formats_unref(&link->out_formats);
  486. ff_formats_unref(&link->in_samplerates);
  487. ff_formats_unref(&link->out_samplerates);
  488. ff_channel_layouts_unref(&link->in_channel_layouts);
  489. ff_channel_layouts_unref(&link->out_channel_layouts);
  490. }
  491. avfilter_link_free(&link);
  492. }
  493. for (i = 0; i < filter->nb_outputs; i++) {
  494. if ((link = filter->outputs[i])) {
  495. if (link->dst)
  496. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  497. ff_formats_unref(&link->in_formats);
  498. ff_formats_unref(&link->out_formats);
  499. ff_formats_unref(&link->in_samplerates);
  500. ff_formats_unref(&link->out_samplerates);
  501. ff_channel_layouts_unref(&link->in_channel_layouts);
  502. ff_channel_layouts_unref(&link->out_channel_layouts);
  503. }
  504. avfilter_link_free(&link);
  505. }
  506. av_freep(&filter->name);
  507. av_freep(&filter->input_pads);
  508. av_freep(&filter->output_pads);
  509. av_freep(&filter->inputs);
  510. av_freep(&filter->outputs);
  511. av_freep(&filter->priv);
  512. while(filter->command_queue){
  513. ff_command_queue_pop(filter);
  514. }
  515. av_free(filter);
  516. }
  517. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  518. {
  519. int ret=0;
  520. if (filter->filter->shorthand) {
  521. av_assert0(filter->priv);
  522. av_assert0(filter->filter->priv_class);
  523. *(const AVClass **)filter->priv = filter->filter->priv_class;
  524. av_opt_set_defaults(filter->priv);
  525. ret = av_opt_set_from_string(filter->priv, args,
  526. filter->filter->shorthand, "=", ":");
  527. if (ret < 0)
  528. return ret;
  529. args = NULL;
  530. }
  531. if (filter->filter->init_opaque)
  532. ret = filter->filter->init_opaque(filter, args, opaque);
  533. else if (filter->filter->init)
  534. ret = filter->filter->init(filter, args);
  535. return ret;
  536. }
  537. const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
  538. {
  539. return pads[pad_idx].name;
  540. }
  541. enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
  542. {
  543. return pads[pad_idx].type;
  544. }
  545. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  546. {
  547. return ff_filter_frame(link->dst->outputs[0], frame);
  548. }
  549. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
  550. {
  551. int (*filter_frame)(AVFilterLink *, AVFrame *);
  552. AVFilterPad *dst = link->dstpad;
  553. AVFrame *out;
  554. int ret;
  555. AVFilterCommand *cmd= link->dst->command_queue;
  556. int64_t pts;
  557. if (link->closed) {
  558. av_frame_free(&frame);
  559. return AVERROR_EOF;
  560. }
  561. if (!(filter_frame = dst->filter_frame))
  562. filter_frame = default_filter_frame;
  563. /* copy the frame if needed */
  564. if (dst->needs_writable && !av_frame_is_writable(frame)) {
  565. av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
  566. /* Maybe use ff_copy_buffer_ref instead? */
  567. switch (link->type) {
  568. case AVMEDIA_TYPE_VIDEO:
  569. out = ff_get_video_buffer(link, link->w, link->h);
  570. break;
  571. case AVMEDIA_TYPE_AUDIO:
  572. out = ff_get_audio_buffer(link, frame->nb_samples);
  573. break;
  574. default: return AVERROR(EINVAL);
  575. }
  576. if (!out) {
  577. av_frame_free(&frame);
  578. return AVERROR(ENOMEM);
  579. }
  580. av_frame_copy_props(out, frame);
  581. switch (link->type) {
  582. case AVMEDIA_TYPE_VIDEO:
  583. av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
  584. frame->format, frame->width, frame->height);
  585. break;
  586. case AVMEDIA_TYPE_AUDIO:
  587. av_samples_copy(out->extended_data, frame->extended_data,
  588. 0, 0, frame->nb_samples,
  589. av_get_channel_layout_nb_channels(frame->channel_layout),
  590. frame->format);
  591. break;
  592. default: return AVERROR(EINVAL);
  593. }
  594. av_frame_free(&frame);
  595. } else
  596. out = frame;
  597. while(cmd && cmd->time <= out->pts * av_q2d(link->time_base)){
  598. av_log(link->dst, AV_LOG_DEBUG,
  599. "Processing command time:%f command:%s arg:%s\n",
  600. cmd->time, cmd->command, cmd->arg);
  601. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  602. ff_command_queue_pop(link->dst);
  603. cmd= link->dst->command_queue;
  604. }
  605. pts = out->pts;
  606. ret = filter_frame(link, out);
  607. link->frame_requested = 0;
  608. ff_update_link_current_pts(link, pts);
  609. return ret;
  610. }
  611. static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFrame *frame)
  612. {
  613. int insamples = frame->nb_samples, inpos = 0, nb_samples;
  614. AVFrame *pbuf = link->partial_buf;
  615. int nb_channels = av_frame_get_channels(frame);
  616. int ret = 0;
  617. link->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  618. /* Handle framing (min_samples, max_samples) */
  619. while (insamples) {
  620. if (!pbuf) {
  621. AVRational samples_tb = { 1, link->sample_rate };
  622. pbuf = ff_get_audio_buffer(link, link->partial_buf_size);
  623. if (!pbuf) {
  624. av_log(link->dst, AV_LOG_WARNING,
  625. "Samples dropped due to memory allocation failure.\n");
  626. return 0;
  627. }
  628. av_frame_copy_props(pbuf, frame);
  629. pbuf->pts = frame->pts +
  630. av_rescale_q(inpos, samples_tb, link->time_base);
  631. pbuf->nb_samples = 0;
  632. }
  633. nb_samples = FFMIN(insamples,
  634. link->partial_buf_size - pbuf->nb_samples);
  635. av_samples_copy(pbuf->extended_data, frame->extended_data,
  636. pbuf->nb_samples, inpos,
  637. nb_samples, nb_channels, link->format);
  638. inpos += nb_samples;
  639. insamples -= nb_samples;
  640. pbuf->nb_samples += nb_samples;
  641. if (pbuf->nb_samples >= link->min_samples) {
  642. ret = ff_filter_frame_framed(link, pbuf);
  643. pbuf = NULL;
  644. }
  645. }
  646. av_frame_free(&frame);
  647. link->partial_buf = pbuf;
  648. return ret;
  649. }
  650. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  651. {
  652. FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
  653. /* Consistency checks */
  654. if (link->type == AVMEDIA_TYPE_VIDEO) {
  655. if (strcmp(link->dst->filter->name, "scale")) {
  656. av_assert1(frame->format == link->format);
  657. av_assert1(frame->width == link->w);
  658. av_assert1(frame->height == link->h);
  659. }
  660. } else {
  661. av_assert1(frame->format == link->format);
  662. av_assert1(av_frame_get_channels(frame) == link->channels);
  663. av_assert1(frame->channel_layout == link->channel_layout);
  664. av_assert1(frame->sample_rate == link->sample_rate);
  665. }
  666. /* Go directly to actual filtering if possible */
  667. if (link->type == AVMEDIA_TYPE_AUDIO &&
  668. link->min_samples &&
  669. (link->partial_buf ||
  670. frame->nb_samples < link->min_samples ||
  671. frame->nb_samples > link->max_samples)) {
  672. return ff_filter_frame_needs_framing(link, frame);
  673. } else {
  674. return ff_filter_frame_framed(link, frame);
  675. }
  676. }