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.

376 lines
12KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include "libavutil/imgutils.h"
  21. #include "libavutil/mem.h"
  22. #include "avfilter.h"
  23. #include "internal.h"
  24. #include "video.h"
  25. #ifdef DEBUG
  26. static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  27. {
  28. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  29. perms & AV_PERM_READ ? "r" : "",
  30. perms & AV_PERM_WRITE ? "w" : "",
  31. perms & AV_PERM_PRESERVE ? "p" : "",
  32. perms & AV_PERM_REUSE ? "u" : "",
  33. perms & AV_PERM_REUSE2 ? "U" : "",
  34. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  35. return buf;
  36. }
  37. #endif
  38. static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  39. {
  40. av_unused char buf[16];
  41. av_dlog(ctx,
  42. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  43. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  44. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  45. ref->pts, ref->pos);
  46. if (ref->video) {
  47. av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  48. ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
  49. ref->video->w, ref->video->h,
  50. !ref->video->interlaced ? 'P' : /* Progressive */
  51. ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
  52. ref->video->key_frame,
  53. av_get_picture_type_char(ref->video->pict_type));
  54. }
  55. if (ref->audio) {
  56. av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
  57. ref->audio->channel_layout,
  58. ref->audio->nb_samples,
  59. ref->audio->sample_rate,
  60. ref->audio->planar);
  61. }
  62. av_dlog(ctx, "]%s", end ? "\n" : "");
  63. }
  64. AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  65. {
  66. return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
  67. }
  68. /* TODO: set the buffer's priv member to a context structure for the whole
  69. * filter chain. This will allow for a buffer pool instead of the constant
  70. * alloc & free cycle currently implemented. */
  71. AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  72. {
  73. int linesize[4];
  74. uint8_t *data[4];
  75. AVFilterBufferRef *picref = NULL;
  76. // +2 is needed for swscaler, +16 to be SIMD-friendly
  77. if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
  78. return NULL;
  79. picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
  80. perms, w, h, link->format);
  81. if (!picref) {
  82. av_free(data[0]);
  83. return NULL;
  84. }
  85. return picref;
  86. }
  87. AVFilterBufferRef *
  88. avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
  89. int w, int h, enum AVPixelFormat format)
  90. {
  91. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  92. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  93. if (!pic || !picref)
  94. goto fail;
  95. picref->buf = pic;
  96. picref->buf->free = ff_avfilter_default_free_buffer;
  97. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  98. goto fail;
  99. pic->w = picref->video->w = w;
  100. pic->h = picref->video->h = h;
  101. /* make sure the buffer gets read permission or it's useless for output */
  102. picref->perms = perms | AV_PERM_READ;
  103. pic->refcount = 1;
  104. picref->type = AVMEDIA_TYPE_VIDEO;
  105. pic->format = picref->format = format;
  106. memcpy(pic->data, data, 4*sizeof(data[0]));
  107. memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
  108. memcpy(picref->data, pic->data, sizeof(picref->data));
  109. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  110. pic-> extended_data = pic->data;
  111. picref->extended_data = picref->data;
  112. picref->pts = AV_NOPTS_VALUE;
  113. return picref;
  114. fail:
  115. if (picref && picref->video)
  116. av_free(picref->video);
  117. av_free(picref);
  118. av_free(pic);
  119. return NULL;
  120. }
  121. AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  122. {
  123. AVFilterBufferRef *ret = NULL;
  124. av_unused char buf[16];
  125. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
  126. av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  127. if (link->dstpad->get_video_buffer)
  128. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  129. if (!ret)
  130. ret = ff_default_get_video_buffer(link, perms, w, h);
  131. if (ret)
  132. ret->type = AVMEDIA_TYPE_VIDEO;
  133. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
  134. return ret;
  135. }
  136. int ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  137. {
  138. AVFilterBufferRef *buf_out = avfilter_ref_buffer(picref, ~0);
  139. if (!buf_out)
  140. return AVERROR(ENOMEM);
  141. return ff_start_frame(link->dst->outputs[0], buf_out);
  142. }
  143. // for filters that support (but don't require) outpic==inpic
  144. int ff_inplace_start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  145. {
  146. AVFilterLink *outlink = inlink->dst->outputs[0];
  147. AVFilterBufferRef *outpicref = NULL, *for_next_filter;
  148. int ret = 0;
  149. if ((inpicref->perms & AV_PERM_WRITE) && !(inpicref->perms & AV_PERM_PRESERVE)) {
  150. outpicref = avfilter_ref_buffer(inpicref, ~0);
  151. if (!outpicref)
  152. return AVERROR(ENOMEM);
  153. } else {
  154. outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  155. if (!outpicref)
  156. return AVERROR(ENOMEM);
  157. avfilter_copy_buffer_ref_props(outpicref, inpicref);
  158. outpicref->video->w = outlink->w;
  159. outpicref->video->h = outlink->h;
  160. }
  161. for_next_filter = avfilter_ref_buffer(outpicref, ~0);
  162. if (for_next_filter)
  163. ret = ff_start_frame(outlink, for_next_filter);
  164. else
  165. ret = AVERROR(ENOMEM);
  166. if (ret < 0) {
  167. avfilter_unref_bufferp(&outpicref);
  168. return ret;
  169. }
  170. outlink->out_buf = outpicref;
  171. return 0;
  172. }
  173. static int default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  174. {
  175. AVFilterLink *outlink = NULL;
  176. if (inlink->dst->nb_outputs)
  177. outlink = inlink->dst->outputs[0];
  178. if (outlink) {
  179. AVFilterBufferRef *buf_out;
  180. outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  181. if (!outlink->out_buf)
  182. return AVERROR(ENOMEM);
  183. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  184. buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
  185. if (!buf_out)
  186. return AVERROR(ENOMEM);
  187. return ff_start_frame(outlink, buf_out);
  188. }
  189. return 0;
  190. }
  191. static void clear_link(AVFilterLink *link)
  192. {
  193. avfilter_unref_bufferp(&link->cur_buf);
  194. avfilter_unref_bufferp(&link->src_buf);
  195. avfilter_unref_bufferp(&link->out_buf);
  196. }
  197. /* XXX: should we do the duplicating of the picture ref here, instead of
  198. * forcing the source filter to do it? */
  199. int ff_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  200. {
  201. int (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  202. AVFilterPad *dst = link->dstpad;
  203. int ret, perms = picref->perms;
  204. FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
  205. if (!(start_frame = dst->start_frame))
  206. start_frame = default_start_frame;
  207. if (picref->linesize[0] < 0)
  208. perms |= AV_PERM_NEG_LINESIZES;
  209. /* prepare to copy the picture if it has insufficient permissions */
  210. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  211. av_log(link->dst, AV_LOG_DEBUG,
  212. "frame copy needed (have perms %x, need %x, reject %x)\n",
  213. picref->perms,
  214. link->dstpad->min_perms, link->dstpad->rej_perms);
  215. link->cur_buf = ff_get_video_buffer(link, dst->min_perms, link->w, link->h);
  216. if (!link->cur_buf) {
  217. avfilter_unref_bufferp(&picref);
  218. return AVERROR(ENOMEM);
  219. }
  220. link->src_buf = picref;
  221. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  222. }
  223. else
  224. link->cur_buf = picref;
  225. ret = start_frame(link, link->cur_buf);
  226. if (ret < 0)
  227. clear_link(link);
  228. return ret;
  229. }
  230. int ff_null_end_frame(AVFilterLink *link)
  231. {
  232. return ff_end_frame(link->dst->outputs[0]);
  233. }
  234. static int default_end_frame(AVFilterLink *inlink)
  235. {
  236. AVFilterLink *outlink = NULL;
  237. if (inlink->dst->nb_outputs)
  238. outlink = inlink->dst->outputs[0];
  239. if (outlink) {
  240. return ff_end_frame(outlink);
  241. }
  242. return 0;
  243. }
  244. int ff_end_frame(AVFilterLink *link)
  245. {
  246. int (*end_frame)(AVFilterLink *);
  247. int ret;
  248. if (!(end_frame = link->dstpad->end_frame))
  249. end_frame = default_end_frame;
  250. ret = end_frame(link);
  251. clear_link(link);
  252. return ret;
  253. }
  254. int ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  255. {
  256. return ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  257. }
  258. static int default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  259. {
  260. AVFilterLink *outlink = NULL;
  261. if (inlink->dst->nb_outputs)
  262. outlink = inlink->dst->outputs[0];
  263. if (outlink)
  264. return ff_draw_slice(outlink, y, h, slice_dir);
  265. return 0;
  266. }
  267. int ff_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  268. {
  269. uint8_t *src[4], *dst[4];
  270. int i, j, vsub, ret;
  271. int (*draw_slice)(AVFilterLink *, int, int, int);
  272. FF_DPRINTF_START(NULL, draw_slice); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
  273. /* copy the slice if needed for permission reasons */
  274. if (link->src_buf) {
  275. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  276. vsub = desc->log2_chroma_h;
  277. for (i = 0; i < 4; i++) {
  278. if (link->src_buf->data[i]) {
  279. src[i] = link->src_buf-> data[i] +
  280. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  281. dst[i] = link->cur_buf->data[i] +
  282. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  283. } else
  284. src[i] = dst[i] = NULL;
  285. }
  286. for (i = 0; i < 4; i++) {
  287. int planew =
  288. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  289. if (!src[i]) continue;
  290. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  291. memcpy(dst[i], src[i], planew);
  292. src[i] += link->src_buf->linesize[i];
  293. dst[i] += link->cur_buf->linesize[i];
  294. }
  295. }
  296. }
  297. if (!(draw_slice = link->dstpad->draw_slice))
  298. draw_slice = default_draw_slice;
  299. ret = draw_slice(link, y, h, slice_dir);
  300. if (ret < 0)
  301. clear_link(link);
  302. return ret;
  303. }