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.

371 lines
11KB

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