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.

764 lines
24KB

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