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.

309 lines
10KB

  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. #ifdef DEBUG
  22. static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
  23. {
  24. snprintf(buf, buf_size, "%s%s%s%s%s%s",
  25. perms & AV_PERM_READ ? "r" : "",
  26. perms & AV_PERM_WRITE ? "w" : "",
  27. perms & AV_PERM_PRESERVE ? "p" : "",
  28. perms & AV_PERM_REUSE ? "u" : "",
  29. perms & AV_PERM_REUSE2 ? "U" : "",
  30. perms & AV_PERM_NEG_LINESIZES ? "n" : "");
  31. return buf;
  32. }
  33. #endif
  34. static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  35. {
  36. av_unused char buf[16];
  37. av_dlog(ctx,
  38. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  39. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  40. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  41. ref->pts, ref->pos);
  42. if (ref->video) {
  43. av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  44. ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
  45. ref->video->w, ref->video->h,
  46. !ref->video->interlaced ? 'P' : /* Progressive */
  47. ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
  48. ref->video->key_frame,
  49. av_get_picture_type_char(ref->video->pict_type));
  50. }
  51. if (ref->audio) {
  52. av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
  53. ref->audio->channel_layout,
  54. ref->audio->nb_samples,
  55. ref->audio->sample_rate,
  56. ref->audio->planar);
  57. }
  58. av_dlog(ctx, "]%s", end ? "\n" : "");
  59. }
  60. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  61. {
  62. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  63. }
  64. /* TODO: set the buffer's priv member to a context structure for the whole
  65. * filter chain. This will allow for a buffer pool instead of the constant
  66. * alloc & free cycle currently implemented. */
  67. AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  68. {
  69. int linesize[4];
  70. uint8_t *data[4];
  71. AVFilterBufferRef *picref = NULL;
  72. // +2 is needed for swscaler, +16 to be SIMD-friendly
  73. if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
  74. return NULL;
  75. picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
  76. perms, w, h, link->format);
  77. if (!picref) {
  78. av_free(data[0]);
  79. return NULL;
  80. }
  81. return picref;
  82. }
  83. AVFilterBufferRef *
  84. avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
  85. int w, int h, enum PixelFormat format)
  86. {
  87. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  88. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  89. if (!pic || !picref)
  90. goto fail;
  91. picref->buf = pic;
  92. picref->buf->free = ff_avfilter_default_free_buffer;
  93. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  94. goto fail;
  95. pic->w = picref->video->w = w;
  96. pic->h = picref->video->h = h;
  97. /* make sure the buffer gets read permission or it's useless for output */
  98. picref->perms = perms | AV_PERM_READ;
  99. pic->refcount = 1;
  100. picref->type = AVMEDIA_TYPE_VIDEO;
  101. pic->format = picref->format = format;
  102. memcpy(pic->data, data, 4*sizeof(data[0]));
  103. memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
  104. memcpy(picref->data, pic->data, sizeof(picref->data));
  105. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  106. pic-> extended_data = pic->data;
  107. picref->extended_data = picref->data;
  108. return picref;
  109. fail:
  110. if (picref && picref->video)
  111. av_free(picref->video);
  112. av_free(picref);
  113. av_free(pic);
  114. return NULL;
  115. }
  116. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  117. {
  118. AVFilterBufferRef *ret = NULL;
  119. av_unused char buf[16];
  120. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
  121. av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  122. if (link->dstpad->get_video_buffer)
  123. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  124. if (!ret)
  125. ret = avfilter_default_get_video_buffer(link, perms, w, h);
  126. if (ret)
  127. ret->type = AVMEDIA_TYPE_VIDEO;
  128. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
  129. return ret;
  130. }
  131. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  132. {
  133. avfilter_start_frame(link->dst->outputs[0], picref);
  134. }
  135. void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  136. {
  137. AVFilterLink *outlink = NULL;
  138. if (inlink->dst->output_count)
  139. outlink = inlink->dst->outputs[0];
  140. if (outlink) {
  141. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  142. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  143. avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  144. }
  145. }
  146. /* XXX: should we do the duplicating of the picture ref here, instead of
  147. * forcing the source filter to do it? */
  148. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  149. {
  150. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  151. AVFilterPad *dst = link->dstpad;
  152. int perms = picref->perms;
  153. FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
  154. if (!(start_frame = dst->start_frame))
  155. start_frame = avfilter_default_start_frame;
  156. if (picref->linesize[0] < 0)
  157. perms |= AV_PERM_NEG_LINESIZES;
  158. /* prepare to copy the picture if it has insufficient permissions */
  159. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  160. av_log(link->dst, AV_LOG_DEBUG,
  161. "frame copy needed (have perms %x, need %x, reject %x)\n",
  162. picref->perms,
  163. link->dstpad->min_perms, link->dstpad->rej_perms);
  164. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  165. link->src_buf = picref;
  166. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  167. }
  168. else
  169. link->cur_buf = picref;
  170. start_frame(link, link->cur_buf);
  171. }
  172. void avfilter_null_end_frame(AVFilterLink *link)
  173. {
  174. avfilter_end_frame(link->dst->outputs[0]);
  175. }
  176. void avfilter_default_end_frame(AVFilterLink *inlink)
  177. {
  178. AVFilterLink *outlink = NULL;
  179. if (inlink->dst->output_count)
  180. outlink = inlink->dst->outputs[0];
  181. avfilter_unref_buffer(inlink->cur_buf);
  182. inlink->cur_buf = NULL;
  183. if (outlink) {
  184. if (outlink->out_buf) {
  185. avfilter_unref_buffer(outlink->out_buf);
  186. outlink->out_buf = NULL;
  187. }
  188. avfilter_end_frame(outlink);
  189. }
  190. }
  191. void avfilter_end_frame(AVFilterLink *link)
  192. {
  193. void (*end_frame)(AVFilterLink *);
  194. if (!(end_frame = link->dstpad->end_frame))
  195. end_frame = avfilter_default_end_frame;
  196. end_frame(link);
  197. /* unreference the source picture if we're feeding the destination filter
  198. * a copied version dues to permission issues */
  199. if (link->src_buf) {
  200. avfilter_unref_buffer(link->src_buf);
  201. link->src_buf = NULL;
  202. }
  203. }
  204. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  205. {
  206. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  207. }
  208. void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  209. {
  210. AVFilterLink *outlink = NULL;
  211. if (inlink->dst->output_count)
  212. outlink = inlink->dst->outputs[0];
  213. if (outlink)
  214. avfilter_draw_slice(outlink, y, h, slice_dir);
  215. }
  216. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  217. {
  218. uint8_t *src[4], *dst[4];
  219. int i, j, vsub;
  220. void (*draw_slice)(AVFilterLink *, int, int, int);
  221. 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);
  222. /* copy the slice if needed for permission reasons */
  223. if (link->src_buf) {
  224. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  225. for (i = 0; i < 4; i++) {
  226. if (link->src_buf->data[i]) {
  227. src[i] = link->src_buf-> data[i] +
  228. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  229. dst[i] = link->cur_buf->data[i] +
  230. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  231. } else
  232. src[i] = dst[i] = NULL;
  233. }
  234. for (i = 0; i < 4; i++) {
  235. int planew =
  236. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  237. if (!src[i]) continue;
  238. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  239. memcpy(dst[i], src[i], planew);
  240. src[i] += link->src_buf->linesize[i];
  241. dst[i] += link->cur_buf->linesize[i];
  242. }
  243. }
  244. }
  245. if (!(draw_slice = link->dstpad->draw_slice))
  246. draw_slice = avfilter_default_draw_slice;
  247. draw_slice(link, y, h, slice_dir);
  248. }