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.

765 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. ff_filter_frame_framed(link, pbuf);
  281. return 0;
  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. for (i = 0; i < filter->nb_inputs; i++) {
  469. if ((link = filter->inputs[i])) {
  470. if (link->src)
  471. link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
  472. ff_formats_unref(&link->in_formats);
  473. ff_formats_unref(&link->out_formats);
  474. ff_formats_unref(&link->in_samplerates);
  475. ff_formats_unref(&link->out_samplerates);
  476. ff_channel_layouts_unref(&link->in_channel_layouts);
  477. ff_channel_layouts_unref(&link->out_channel_layouts);
  478. }
  479. avfilter_link_free(&link);
  480. }
  481. for (i = 0; i < filter->nb_outputs; i++) {
  482. if ((link = filter->outputs[i])) {
  483. if (link->dst)
  484. link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
  485. ff_formats_unref(&link->in_formats);
  486. ff_formats_unref(&link->out_formats);
  487. ff_formats_unref(&link->in_samplerates);
  488. ff_formats_unref(&link->out_samplerates);
  489. ff_channel_layouts_unref(&link->in_channel_layouts);
  490. ff_channel_layouts_unref(&link->out_channel_layouts);
  491. }
  492. avfilter_link_free(&link);
  493. }
  494. av_freep(&filter->name);
  495. av_freep(&filter->input_pads);
  496. av_freep(&filter->output_pads);
  497. av_freep(&filter->inputs);
  498. av_freep(&filter->outputs);
  499. av_freep(&filter->priv);
  500. while(filter->command_queue){
  501. ff_command_queue_pop(filter);
  502. }
  503. av_free(filter);
  504. }
  505. int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
  506. {
  507. int ret=0;
  508. if (filter->filter->init_opaque)
  509. ret = filter->filter->init_opaque(filter, args, opaque);
  510. else if (filter->filter->init)
  511. ret = filter->filter->init(filter, args);
  512. return ret;
  513. }
  514. const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
  515. {
  516. return pads[pad_idx].name;
  517. }
  518. enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
  519. {
  520. return pads[pad_idx].type;
  521. }
  522. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  523. {
  524. return ff_filter_frame(link->dst->outputs[0], frame);
  525. }
  526. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
  527. {
  528. int (*filter_frame)(AVFilterLink *, AVFrame *);
  529. AVFilterPad *dst = link->dstpad;
  530. AVFrame *out;
  531. int ret;
  532. AVFilterCommand *cmd= link->dst->command_queue;
  533. int64_t pts;
  534. if (link->closed) {
  535. av_frame_free(&frame);
  536. return AVERROR_EOF;
  537. }
  538. if (!(filter_frame = dst->filter_frame))
  539. filter_frame = default_filter_frame;
  540. /* copy the frame if needed */
  541. if (dst->needs_writable && !av_frame_is_writable(frame)) {
  542. av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
  543. /* Maybe use ff_copy_buffer_ref instead? */
  544. switch (link->type) {
  545. case AVMEDIA_TYPE_VIDEO:
  546. out = ff_get_video_buffer(link, link->w, link->h);
  547. break;
  548. case AVMEDIA_TYPE_AUDIO:
  549. out = ff_get_audio_buffer(link, frame->nb_samples);
  550. break;
  551. default: return AVERROR(EINVAL);
  552. }
  553. if (!out) {
  554. av_frame_free(&frame);
  555. return AVERROR(ENOMEM);
  556. }
  557. av_frame_copy_props(out, frame);
  558. switch (link->type) {
  559. case AVMEDIA_TYPE_VIDEO:
  560. av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
  561. frame->format, frame->width, frame->height);
  562. break;
  563. case AVMEDIA_TYPE_AUDIO:
  564. av_samples_copy(out->extended_data, frame->extended_data,
  565. 0, 0, frame->nb_samples,
  566. av_get_channel_layout_nb_channels(frame->channel_layout),
  567. frame->format);
  568. break;
  569. default: return AVERROR(EINVAL);
  570. }
  571. av_frame_free(&frame);
  572. } else
  573. out = frame;
  574. while(cmd && cmd->time <= out->pts * av_q2d(link->time_base)){
  575. av_log(link->dst, AV_LOG_DEBUG,
  576. "Processing command time:%f command:%s arg:%s\n",
  577. cmd->time, cmd->command, cmd->arg);
  578. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  579. ff_command_queue_pop(link->dst);
  580. cmd= link->dst->command_queue;
  581. }
  582. pts = out->pts;
  583. ret = filter_frame(link, out);
  584. ff_update_link_current_pts(link, pts);
  585. return ret;
  586. }
  587. static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFrame *frame)
  588. {
  589. int insamples = frame->nb_samples, inpos = 0, nb_samples;
  590. AVFrame *pbuf = link->partial_buf;
  591. int nb_channels = av_frame_get_channels(frame);
  592. int ret = 0;
  593. /* Handle framing (min_samples, max_samples) */
  594. while (insamples) {
  595. if (!pbuf) {
  596. AVRational samples_tb = { 1, link->sample_rate };
  597. pbuf = ff_get_audio_buffer(link, link->partial_buf_size);
  598. if (!pbuf) {
  599. av_log(link->dst, AV_LOG_WARNING,
  600. "Samples dropped due to memory allocation failure.\n");
  601. return 0;
  602. }
  603. av_frame_copy_props(pbuf, frame);
  604. pbuf->pts = frame->pts +
  605. av_rescale_q(inpos, samples_tb, link->time_base);
  606. pbuf->nb_samples = 0;
  607. }
  608. nb_samples = FFMIN(insamples,
  609. link->partial_buf_size - pbuf->nb_samples);
  610. av_samples_copy(pbuf->extended_data, frame->extended_data,
  611. pbuf->nb_samples, inpos,
  612. nb_samples, nb_channels, link->format);
  613. inpos += nb_samples;
  614. insamples -= nb_samples;
  615. pbuf->nb_samples += nb_samples;
  616. if (pbuf->nb_samples >= link->min_samples) {
  617. ret = ff_filter_frame_framed(link, pbuf);
  618. pbuf = NULL;
  619. }
  620. }
  621. av_frame_free(&frame);
  622. link->partial_buf = pbuf;
  623. return ret;
  624. }
  625. int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
  626. {
  627. FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
  628. /* Consistency checks */
  629. if (link->type == AVMEDIA_TYPE_VIDEO) {
  630. if (strcmp(link->dst->filter->name, "scale")) {
  631. av_assert1(frame->format == link->format);
  632. av_assert1(frame->width == link->w);
  633. av_assert1(frame->height == link->h);
  634. }
  635. } else {
  636. av_assert1(frame->format == link->format);
  637. av_assert1(av_frame_get_channels(frame) == link->channels);
  638. av_assert1(frame->channel_layout == link->channel_layout);
  639. av_assert1(frame->sample_rate == link->sample_rate);
  640. }
  641. /* Go directly to actual filtering if possible */
  642. if (link->type == AVMEDIA_TYPE_AUDIO &&
  643. link->min_samples &&
  644. (link->partial_buf ||
  645. frame->nb_samples < link->min_samples ||
  646. frame->nb_samples > link->max_samples)) {
  647. return ff_filter_frame_needs_framing(link, frame);
  648. } else {
  649. return ff_filter_frame_framed(link, frame);
  650. }
  651. }