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.

778 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. if (link->srcpad->request_frame)
  275. ret = link->srcpad->request_frame(link);
  276. else if (link->src->inputs[0])
  277. ret = ff_request_frame(link->src->inputs[0]);
  278. if (ret == AVERROR_EOF && link->partial_buf) {
  279. AVFrame *pbuf = link->partial_buf;
  280. link->partial_buf = NULL;
  281. ret = ff_filter_frame_framed(link, pbuf);
  282. }
  283. if (ret == AVERROR_EOF)
  284. link->closed = 1;
  285. return ret;
  286. }
  287. int ff_poll_frame(AVFilterLink *link)
  288. {
  289. int i, min = INT_MAX;
  290. if (link->srcpad->poll_frame)
  291. return link->srcpad->poll_frame(link);
  292. for (i = 0; i < link->src->nb_inputs; i++) {
  293. int val;
  294. if (!link->src->inputs[i])
  295. return -1;
  296. val = ff_poll_frame(link->src->inputs[i]);
  297. min = FFMIN(min, val);
  298. }
  299. return min;
  300. }
  301. void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
  302. {
  303. if (pts == AV_NOPTS_VALUE)
  304. return;
  305. link->current_pts = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
  306. /* TODO use duration */
  307. if (link->graph && link->age_index >= 0)
  308. ff_avfilter_graph_update_heap(link->graph, link);
  309. }
  310. int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
  311. {
  312. if(!strcmp(cmd, "ping")){
  313. av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
  314. return 0;
  315. }else if(filter->filter->process_command) {
  316. return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
  317. }
  318. return AVERROR(ENOSYS);
  319. }
  320. #define MAX_REGISTERED_AVFILTERS_NB 256
  321. static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
  322. static int next_registered_avfilter_idx = 0;
  323. AVFilter *avfilter_get_by_name(const char *name)
  324. {
  325. int i;
  326. for (i = 0; registered_avfilters[i]; i++)
  327. if (!strcmp(registered_avfilters[i]->name, name))
  328. return registered_avfilters[i];
  329. return NULL;
  330. }
  331. int avfilter_register(AVFilter *filter)
  332. {
  333. int i;
  334. if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
  335. av_log(NULL, AV_LOG_ERROR,
  336. "Maximum number of registered filters %d reached, "
  337. "impossible to register filter with name '%s'\n",
  338. MAX_REGISTERED_AVFILTERS_NB, filter->name);
  339. return AVERROR(ENOMEM);
  340. }
  341. for(i=0; filter->inputs && filter->inputs[i].name; i++) {
  342. const AVFilterPad *input = &filter->inputs[i];
  343. av_assert0( !input->filter_frame
  344. || (!input->start_frame && !input->end_frame));
  345. }
  346. registered_avfilters[next_registered_avfilter_idx++] = filter;
  347. return 0;
  348. }
  349. AVFilter **av_filter_next(AVFilter **filter)
  350. {
  351. return filter ? ++filter : &registered_avfilters[0];
  352. }
  353. void avfilter_uninit(void)
  354. {
  355. memset(registered_avfilters, 0, sizeof(registered_avfilters));
  356. next_registered_avfilter_idx = 0;
  357. }
  358. static int pad_count(const AVFilterPad *pads)
  359. {
  360. int count;
  361. if (!pads)
  362. return 0;
  363. for(count = 0; pads->name; count ++) pads ++;
  364. return count;
  365. }
  366. static const char *default_filter_name(void *filter_ctx)
  367. {
  368. AVFilterContext *ctx = filter_ctx;
  369. return ctx->name ? ctx->name : ctx->filter->name;
  370. }
  371. static void *filter_child_next(void *obj, void *prev)
  372. {
  373. AVFilterContext *ctx = obj;
  374. if (!prev && ctx->filter && ctx->filter->priv_class)
  375. return ctx->priv;
  376. return NULL;
  377. }
  378. static const AVClass *filter_child_class_next(const AVClass *prev)
  379. {
  380. AVFilter **filter_ptr = NULL;
  381. /* find the filter that corresponds to prev */
  382. while (prev && *(filter_ptr = av_filter_next(filter_ptr)))
  383. if ((*filter_ptr)->priv_class == prev)
  384. break;
  385. /* could not find filter corresponding to prev */
  386. if (prev && !(*filter_ptr))
  387. return NULL;
  388. /* find next filter with specific options */
  389. while (*(filter_ptr = av_filter_next(filter_ptr)))
  390. if ((*filter_ptr)->priv_class)
  391. return (*filter_ptr)->priv_class;
  392. return NULL;
  393. }
  394. static const AVClass avfilter_class = {
  395. .class_name = "AVFilter",
  396. .item_name = default_filter_name,
  397. .version = LIBAVUTIL_VERSION_INT,
  398. .category = AV_CLASS_CATEGORY_FILTER,
  399. .child_next = filter_child_next,
  400. .child_class_next = filter_child_class_next,
  401. };
  402. const AVClass *avfilter_get_class(void)
  403. {
  404. return &avfilter_class;
  405. }
  406. int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
  407. {
  408. AVFilterContext *ret;
  409. *filter_ctx = NULL;
  410. if (!filter)
  411. return AVERROR(EINVAL);
  412. ret = av_mallocz(sizeof(AVFilterContext));
  413. if (!ret)
  414. return AVERROR(ENOMEM);
  415. ret->av_class = &avfilter_class;
  416. ret->filter = filter;
  417. ret->name = inst_name ? av_strdup(inst_name) : NULL;
  418. if (filter->priv_size) {
  419. ret->priv = av_mallocz(filter->priv_size);
  420. if (!ret->priv)
  421. goto err;
  422. }
  423. ret->nb_inputs = pad_count(filter->inputs);
  424. if (ret->nb_inputs ) {
  425. ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
  426. if (!ret->input_pads)
  427. goto err;
  428. memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
  429. ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
  430. if (!ret->inputs)
  431. goto err;
  432. }
  433. ret->nb_outputs = pad_count(filter->outputs);
  434. if (ret->nb_outputs) {
  435. ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
  436. if (!ret->output_pads)
  437. goto err;
  438. memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
  439. ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
  440. if (!ret->outputs)
  441. goto err;
  442. }
  443. #if FF_API_FOO_COUNT
  444. ret->output_count = ret->nb_outputs;
  445. ret->input_count = ret->nb_inputs;
  446. #endif
  447. *filter_ctx = ret;
  448. return 0;
  449. err:
  450. av_freep(&ret->inputs);
  451. av_freep(&ret->input_pads);
  452. ret->nb_inputs = 0;
  453. av_freep(&ret->outputs);
  454. av_freep(&ret->output_pads);
  455. ret->nb_outputs = 0;
  456. av_freep(&ret->priv);
  457. av_free(ret);
  458. return AVERROR(ENOMEM);
  459. }
  460. void avfilter_free(AVFilterContext *filter)
  461. {
  462. int i;
  463. AVFilterLink *link;
  464. if (!filter)
  465. return;
  466. if (filter->filter->uninit)
  467. filter->filter->uninit(filter);
  468. if (filter->filter->shorthand)
  469. av_opt_free(filter->priv);
  470. for (i = 0; i < filter->nb_inputs; i++) {
  471. if ((link = filter->inputs[i])) {
  472. if (link->src)
  473. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  474. ff_formats_unref(&link->in_formats);
  475. ff_formats_unref(&link->out_formats);
  476. ff_formats_unref(&link->in_samplerates);
  477. ff_formats_unref(&link->out_samplerates);
  478. ff_channel_layouts_unref(&link->in_channel_layouts);
  479. ff_channel_layouts_unref(&link->out_channel_layouts);
  480. }
  481. avfilter_link_free(&link);
  482. }
  483. for (i = 0; i < filter->nb_outputs; i++) {
  484. if ((link = filter->outputs[i])) {
  485. if (link->dst)
  486. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  487. ff_formats_unref(&link->in_formats);
  488. ff_formats_unref(&link->out_formats);
  489. ff_formats_unref(&link->in_samplerates);
  490. ff_formats_unref(&link->out_samplerates);
  491. ff_channel_layouts_unref(&link->in_channel_layouts);
  492. ff_channel_layouts_unref(&link->out_channel_layouts);
  493. }
  494. avfilter_link_free(&link);
  495. }
  496. av_freep(&filter->name);
  497. av_freep(&filter->input_pads);
  498. av_freep(&filter->output_pads);
  499. av_freep(&filter->inputs);
  500. av_freep(&filter->outputs);
  501. av_freep(&filter->priv);
  502. while(filter->command_queue){
  503. ff_command_queue_pop(filter);
  504. }
  505. av_free(filter);
  506. }
  507. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  508. {
  509. int ret=0;
  510. if (filter->filter->shorthand) {
  511. av_assert0(filter->priv);
  512. av_assert0(filter->filter->priv_class);
  513. *(const AVClass **)filter->priv = filter->filter->priv_class;
  514. av_opt_set_defaults(filter->priv);
  515. ret = av_opt_set_from_string(filter->priv, args,
  516. filter->filter->shorthand, "=", ":");
  517. if (ret < 0)
  518. return ret;
  519. args = NULL;
  520. }
  521. if (filter->filter->init_opaque)
  522. ret = filter->filter->init_opaque(filter, args, opaque);
  523. else if (filter->filter->init)
  524. ret = filter->filter->init(filter, args);
  525. return ret;
  526. }
  527. const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
  528. {
  529. return pads[pad_idx].name;
  530. }
  531. enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
  532. {
  533. return pads[pad_idx].type;
  534. }
  535. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  536. {
  537. return ff_filter_frame(link->dst->outputs[0], frame);
  538. }
  539. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
  540. {
  541. int (*filter_frame)(AVFilterLink *, AVFrame *);
  542. AVFilterPad *dst = link->dstpad;
  543. AVFrame *out;
  544. int ret;
  545. AVFilterCommand *cmd= link->dst->command_queue;
  546. int64_t pts;
  547. if (link->closed) {
  548. av_frame_free(&frame);
  549. return AVERROR_EOF;
  550. }
  551. if (!(filter_frame = dst->filter_frame))
  552. filter_frame = default_filter_frame;
  553. /* copy the frame if needed */
  554. if (dst->needs_writable && !av_frame_is_writable(frame)) {
  555. av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
  556. /* Maybe use ff_copy_buffer_ref instead? */
  557. switch (link->type) {
  558. case AVMEDIA_TYPE_VIDEO:
  559. out = ff_get_video_buffer(link, link->w, link->h);
  560. break;
  561. case AVMEDIA_TYPE_AUDIO:
  562. out = ff_get_audio_buffer(link, frame->nb_samples);
  563. break;
  564. default: return AVERROR(EINVAL);
  565. }
  566. if (!out) {
  567. av_frame_free(&frame);
  568. return AVERROR(ENOMEM);
  569. }
  570. av_frame_copy_props(out, frame);
  571. switch (link->type) {
  572. case AVMEDIA_TYPE_VIDEO:
  573. av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
  574. frame->format, frame->width, frame->height);
  575. break;
  576. case AVMEDIA_TYPE_AUDIO:
  577. av_samples_copy(out->extended_data, frame->extended_data,
  578. 0, 0, frame->nb_samples,
  579. av_get_channel_layout_nb_channels(frame->channel_layout),
  580. frame->format);
  581. break;
  582. default: return AVERROR(EINVAL);
  583. }
  584. av_frame_free(&frame);
  585. } else
  586. out = frame;
  587. while(cmd && cmd->time <= out->pts * av_q2d(link->time_base)){
  588. av_log(link->dst, AV_LOG_DEBUG,
  589. "Processing command time:%f command:%s arg:%s\n",
  590. cmd->time, cmd->command, cmd->arg);
  591. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  592. ff_command_queue_pop(link->dst);
  593. cmd= link->dst->command_queue;
  594. }
  595. pts = out->pts;
  596. ret = filter_frame(link, out);
  597. ff_update_link_current_pts(link, pts);
  598. return ret;
  599. }
  600. static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFrame *frame)
  601. {
  602. int insamples = frame->nb_samples, inpos = 0, nb_samples;
  603. AVFrame *pbuf = link->partial_buf;
  604. int nb_channels = av_frame_get_channels(frame);
  605. int ret = 0;
  606. /* Handle framing (min_samples, max_samples) */
  607. while (insamples) {
  608. if (!pbuf) {
  609. AVRational samples_tb = { 1, link->sample_rate };
  610. pbuf = ff_get_audio_buffer(link, link->partial_buf_size);
  611. if (!pbuf) {
  612. av_log(link->dst, AV_LOG_WARNING,
  613. "Samples dropped due to memory allocation failure.\n");
  614. return 0;
  615. }
  616. av_frame_copy_props(pbuf, frame);
  617. pbuf->pts = frame->pts +
  618. av_rescale_q(inpos, samples_tb, link->time_base);
  619. pbuf->nb_samples = 0;
  620. }
  621. nb_samples = FFMIN(insamples,
  622. link->partial_buf_size - pbuf->nb_samples);
  623. av_samples_copy(pbuf->extended_data, frame->extended_data,
  624. pbuf->nb_samples, inpos,
  625. nb_samples, nb_channels, link->format);
  626. inpos += nb_samples;
  627. insamples -= nb_samples;
  628. pbuf->nb_samples += nb_samples;
  629. if (pbuf->nb_samples >= link->min_samples) {
  630. ret = ff_filter_frame_framed(link, pbuf);
  631. pbuf = NULL;
  632. }
  633. }
  634. av_frame_free(&frame);
  635. link->partial_buf = pbuf;
  636. return ret;
  637. }
  638. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  639. {
  640. FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
  641. /* Consistency checks */
  642. if (link->type == AVMEDIA_TYPE_VIDEO) {
  643. if (strcmp(link->dst->filter->name, "scale")) {
  644. av_assert1(frame->format == link->format);
  645. av_assert1(frame->width == link->w);
  646. av_assert1(frame->height == link->h);
  647. }
  648. } else {
  649. av_assert1(frame->format == link->format);
  650. av_assert1(av_frame_get_channels(frame) == link->channels);
  651. av_assert1(frame->channel_layout == link->channel_layout);
  652. av_assert1(frame->sample_rate == link->sample_rate);
  653. }
  654. /* Go directly to actual filtering if possible */
  655. if (link->type == AVMEDIA_TYPE_AUDIO &&
  656. link->min_samples &&
  657. (link->partial_buf ||
  658. frame->nb_samples < link->min_samples ||
  659. frame->nb_samples > link->max_samples)) {
  660. return ff_filter_frame_needs_framing(link, frame);
  661. } else {
  662. return ff_filter_frame_framed(link, frame);
  663. }
  664. }