avcodec.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2011 Stefano Sabatini | stefasab at gmail.com
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * libavcodec/libavfilter gluing utilities
  23. */
  24. #include "avcodec.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/opt.h"
  28. #if FF_API_AVFILTERBUFFER
  29. AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
  30. int perms)
  31. {
  32. AVFilterBufferRef *picref =
  33. avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
  34. frame->width, frame->height,
  35. frame->format);
  36. if (!picref)
  37. return NULL;
  38. if (avfilter_copy_frame_props(picref, frame) < 0) {
  39. picref->buf->data[0] = NULL;
  40. avfilter_unref_bufferp(&picref);
  41. }
  42. return picref;
  43. }
  44. AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
  45. int perms)
  46. {
  47. AVFilterBufferRef *samplesref;
  48. int channels = av_frame_get_channels(frame);
  49. int64_t layout = av_frame_get_channel_layout(frame);
  50. if (layout && av_get_channel_layout_nb_channels(layout) != av_frame_get_channels(frame)) {
  51. av_log(NULL, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
  52. return NULL;
  53. }
  54. samplesref = avfilter_get_audio_buffer_ref_from_arrays_channels(
  55. (uint8_t **)frame->extended_data, frame->linesize[0], perms,
  56. frame->nb_samples, frame->format, channels, layout);
  57. if (!samplesref)
  58. return NULL;
  59. if (avfilter_copy_frame_props(samplesref, frame) < 0) {
  60. samplesref->buf->data[0] = NULL;
  61. avfilter_unref_bufferp(&samplesref);
  62. }
  63. return samplesref;
  64. }
  65. AVFilterBufferRef *avfilter_get_buffer_ref_from_frame(enum AVMediaType type,
  66. const AVFrame *frame,
  67. int perms)
  68. {
  69. switch (type) {
  70. case AVMEDIA_TYPE_VIDEO:
  71. return avfilter_get_video_buffer_ref_from_frame(frame, perms);
  72. case AVMEDIA_TYPE_AUDIO:
  73. return avfilter_get_audio_buffer_ref_from_frame(frame, perms);
  74. default:
  75. return NULL;
  76. }
  77. }
  78. int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
  79. {
  80. int planes, nb_channels;
  81. if (!dst)
  82. return AVERROR(EINVAL);
  83. /* abort in case the src is NULL and dst is not, avoid inconsistent state in dst */
  84. av_assert0(src);
  85. memcpy(dst->data, src->data, sizeof(dst->data));
  86. memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
  87. dst->pts = src->pts;
  88. dst->format = src->format;
  89. av_frame_set_pkt_pos(dst, src->pos);
  90. switch (src->type) {
  91. case AVMEDIA_TYPE_VIDEO:
  92. av_assert0(src->video);
  93. dst->width = src->video->w;
  94. dst->height = src->video->h;
  95. dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
  96. dst->interlaced_frame = src->video->interlaced;
  97. dst->top_field_first = src->video->top_field_first;
  98. dst->key_frame = src->video->key_frame;
  99. dst->pict_type = src->video->pict_type;
  100. break;
  101. case AVMEDIA_TYPE_AUDIO:
  102. av_assert0(src->audio);
  103. nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
  104. planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
  105. if (planes > FF_ARRAY_ELEMS(dst->data)) {
  106. dst->extended_data = av_mallocz_array(planes, sizeof(*dst->extended_data));
  107. if (!dst->extended_data)
  108. return AVERROR(ENOMEM);
  109. memcpy(dst->extended_data, src->extended_data,
  110. planes * sizeof(*dst->extended_data));
  111. } else
  112. dst->extended_data = dst->data;
  113. dst->nb_samples = src->audio->nb_samples;
  114. av_frame_set_sample_rate (dst, src->audio->sample_rate);
  115. av_frame_set_channel_layout(dst, src->audio->channel_layout);
  116. av_frame_set_channels (dst, src->audio->channels);
  117. break;
  118. default:
  119. return AVERROR(EINVAL);
  120. }
  121. return 0;
  122. }
  123. #endif