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.

386 lines
13KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. 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. static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
  34. {
  35. av_unused char buf[16];
  36. av_dlog(ctx,
  37. "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
  38. ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
  39. ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
  40. ref->pts, ref->pos);
  41. if (ref->video) {
  42. av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
  43. ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
  44. ref->video->w, ref->video->h,
  45. !ref->video->interlaced ? 'P' : /* Progressive */
  46. ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
  47. ref->video->key_frame,
  48. av_get_picture_type_char(ref->video->pict_type));
  49. }
  50. if (ref->audio) {
  51. av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d",
  52. ref->audio->channel_layout,
  53. ref->audio->nb_samples,
  54. ref->audio->sample_rate);
  55. }
  56. av_dlog(ctx, "]%s", end ? "\n" : "");
  57. }
  58. AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  59. {
  60. return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
  61. }
  62. AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  63. {
  64. int linesize[4];
  65. uint8_t *data[4];
  66. int i;
  67. AVFilterBufferRef *picref = NULL;
  68. AVFilterPool *pool = link->pool;
  69. if (pool) {
  70. for (i = 0; i < POOL_SIZE; i++) {
  71. picref = pool->pic[i];
  72. if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
  73. AVFilterBuffer *pic = picref->buf;
  74. pool->pic[i] = NULL;
  75. pool->count--;
  76. picref->video->w = w;
  77. picref->video->h = h;
  78. picref->perms = perms | AV_PERM_READ;
  79. picref->format = link->format;
  80. pic->refcount = 1;
  81. memcpy(picref->data, pic->data, sizeof(picref->data));
  82. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  83. pool->refcount++;
  84. return picref;
  85. }
  86. }
  87. } else {
  88. pool = link->pool = av_mallocz(sizeof(AVFilterPool));
  89. pool->refcount = 1;
  90. }
  91. // align: +2 is needed for swscaler, +16 to be SIMD-friendly
  92. if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
  93. return NULL;
  94. picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
  95. perms, w, h, link->format);
  96. if (!picref) {
  97. av_free(data[0]);
  98. return NULL;
  99. }
  100. memset(data[0], 128, i);
  101. picref->buf->priv = pool;
  102. picref->buf->free = NULL;
  103. pool->refcount++;
  104. return picref;
  105. }
  106. AVFilterBufferRef *
  107. avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
  108. int w, int h, enum PixelFormat format)
  109. {
  110. AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
  111. AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
  112. if (!pic || !picref)
  113. goto fail;
  114. picref->buf = pic;
  115. picref->buf->free = ff_avfilter_default_free_buffer;
  116. if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
  117. goto fail;
  118. pic->w = picref->video->w = w;
  119. pic->h = picref->video->h = h;
  120. /* make sure the buffer gets read permission or it's useless for output */
  121. picref->perms = perms | AV_PERM_READ;
  122. pic->refcount = 1;
  123. picref->type = AVMEDIA_TYPE_VIDEO;
  124. pic->format = picref->format = format;
  125. memcpy(pic->data, data, 4*sizeof(data[0]));
  126. memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
  127. memcpy(picref->data, pic->data, sizeof(picref->data));
  128. memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
  129. pic-> extended_data = pic->data;
  130. picref->extended_data = picref->data;
  131. picref->pts = AV_NOPTS_VALUE;
  132. return picref;
  133. fail:
  134. if (picref && picref->video)
  135. av_free(picref->video);
  136. av_free(picref);
  137. av_free(pic);
  138. return NULL;
  139. }
  140. AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  141. {
  142. AVFilterBufferRef *ret = NULL;
  143. av_unused char buf[16];
  144. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
  145. av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
  146. if (link->dstpad->get_video_buffer)
  147. ret = link->dstpad->get_video_buffer(link, perms, w, h);
  148. if (!ret)
  149. ret = ff_default_get_video_buffer(link, perms, w, h);
  150. if (ret)
  151. ret->type = AVMEDIA_TYPE_VIDEO;
  152. FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
  153. return ret;
  154. }
  155. void ff_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  156. {
  157. avfilter_start_frame(link->dst->outputs[0], picref);
  158. }
  159. static void default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  160. {
  161. AVFilterLink *outlink = NULL;
  162. if (inlink->dst->output_count)
  163. outlink = inlink->dst->outputs[0];
  164. if (outlink) {
  165. outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  166. avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
  167. avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  168. }
  169. }
  170. /* XXX: should we do the duplicating of the picture ref here, instead of
  171. * forcing the source filter to do it? */
  172. void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  173. {
  174. void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
  175. AVFilterPad *dst = link->dstpad;
  176. int perms = picref->perms;
  177. AVFilterCommand *cmd= link->dst->command_queue;
  178. FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
  179. if (!(start_frame = dst->start_frame))
  180. start_frame = default_start_frame;
  181. if (picref->linesize[0] < 0)
  182. perms |= AV_PERM_NEG_LINESIZES;
  183. /* prepare to copy the picture if it has insufficient permissions */
  184. if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
  185. av_log(link->dst, AV_LOG_DEBUG,
  186. "frame copy needed (have perms %x, need %x, reject %x)\n",
  187. picref->perms,
  188. link->dstpad->min_perms, link->dstpad->rej_perms);
  189. link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
  190. link->src_buf = picref;
  191. avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
  192. /* copy palette if required */
  193. if (av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)
  194. memcpy(link->cur_buf->data[1], link->src_buf-> data[1], AVPALETTE_SIZE);
  195. }
  196. else
  197. link->cur_buf = picref;
  198. while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
  199. av_log(link->dst, AV_LOG_DEBUG,
  200. "Processing command time:%f command:%s arg:%s\n",
  201. cmd->time, cmd->command, cmd->arg);
  202. avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
  203. ff_command_queue_pop(link->dst);
  204. cmd= link->dst->command_queue;
  205. }
  206. start_frame(link, link->cur_buf);
  207. ff_update_link_current_pts(link, link->cur_buf->pts);
  208. }
  209. void ff_null_end_frame(AVFilterLink *link)
  210. {
  211. avfilter_end_frame(link->dst->outputs[0]);
  212. }
  213. static void default_end_frame(AVFilterLink *inlink)
  214. {
  215. AVFilterLink *outlink = NULL;
  216. if (inlink->dst->output_count)
  217. outlink = inlink->dst->outputs[0];
  218. avfilter_unref_buffer(inlink->cur_buf);
  219. inlink->cur_buf = NULL;
  220. if (outlink) {
  221. if (outlink->out_buf) {
  222. avfilter_unref_buffer(outlink->out_buf);
  223. outlink->out_buf = NULL;
  224. }
  225. avfilter_end_frame(outlink);
  226. }
  227. }
  228. void avfilter_end_frame(AVFilterLink *link)
  229. {
  230. void (*end_frame)(AVFilterLink *);
  231. if (!(end_frame = link->dstpad->end_frame))
  232. end_frame = default_end_frame;
  233. end_frame(link);
  234. /* unreference the source picture if we're feeding the destination filter
  235. * a copied version dues to permission issues */
  236. if (link->src_buf) {
  237. avfilter_unref_buffer(link->src_buf);
  238. link->src_buf = NULL;
  239. }
  240. }
  241. void ff_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  242. {
  243. avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
  244. }
  245. static void default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  246. {
  247. AVFilterLink *outlink = NULL;
  248. if (inlink->dst->output_count)
  249. outlink = inlink->dst->outputs[0];
  250. if (outlink)
  251. avfilter_draw_slice(outlink, y, h, slice_dir);
  252. }
  253. void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  254. {
  255. uint8_t *src[4], *dst[4];
  256. int i, j, vsub;
  257. void (*draw_slice)(AVFilterLink *, int, int, int);
  258. 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);
  259. /* copy the slice if needed for permission reasons */
  260. if (link->src_buf) {
  261. vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
  262. for (i = 0; i < 4; i++) {
  263. if (link->src_buf->data[i]) {
  264. src[i] = link->src_buf-> data[i] +
  265. (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
  266. dst[i] = link->cur_buf->data[i] +
  267. (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
  268. } else
  269. src[i] = dst[i] = NULL;
  270. }
  271. for (i = 0; i < 4; i++) {
  272. int planew =
  273. av_image_get_linesize(link->format, link->cur_buf->video->w, i);
  274. if (!src[i]) continue;
  275. for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
  276. memcpy(dst[i], src[i], planew);
  277. src[i] += link->src_buf->linesize[i];
  278. dst[i] += link->cur_buf->linesize[i];
  279. }
  280. }
  281. }
  282. if (!(draw_slice = link->dstpad->draw_slice))
  283. draw_slice = default_draw_slice;
  284. draw_slice(link, y, h, slice_dir);
  285. }
  286. #if FF_API_FILTERS_PUBLIC
  287. AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  288. {
  289. return ff_default_get_video_buffer(link, perms, w, h);
  290. }
  291. void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  292. {
  293. default_start_frame(inlink, picref);
  294. }
  295. void avfilter_default_end_frame(AVFilterLink *inlink)
  296. {
  297. default_end_frame(inlink);
  298. }
  299. void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  300. {
  301. default_draw_slice(inlink, y, h, slice_dir);
  302. }
  303. AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
  304. {
  305. return ff_null_get_video_buffer(link, perms, w, h);
  306. }
  307. void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  308. {
  309. ff_null_start_frame(link, picref);
  310. }
  311. void avfilter_null_end_frame(AVFilterLink *link)
  312. {
  313. ff_null_end_frame(link);
  314. }
  315. void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  316. {
  317. ff_null_draw_slice(link, y, h, slice_dir);
  318. }
  319. #endif