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.

204 lines
7.4KB

  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/opt.h"
  27. int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
  28. {
  29. dst->pts = src->pts;
  30. dst->pos = av_frame_get_pkt_pos(src);
  31. dst->format = src->format;
  32. av_dict_free(&dst->metadata);
  33. av_dict_copy(&dst->metadata, av_frame_get_metadata(src), 0);
  34. switch (dst->type) {
  35. case AVMEDIA_TYPE_VIDEO:
  36. dst->video->w = src->width;
  37. dst->video->h = src->height;
  38. dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
  39. dst->video->interlaced = src->interlaced_frame;
  40. dst->video->top_field_first = src->top_field_first;
  41. dst->video->key_frame = src->key_frame;
  42. dst->video->pict_type = src->pict_type;
  43. av_freep(&dst->video->qp_table);
  44. dst->video->qp_table_linesize = 0;
  45. if (src->qscale_table) {
  46. int qsize = src->qstride ? src->qstride * ((src->height+15)/16) : (src->width+15)/16;
  47. dst->video->qp_table = av_malloc(qsize);
  48. if (!dst->video->qp_table)
  49. return AVERROR(ENOMEM);
  50. dst->video->qp_table_linesize = src->qstride;
  51. dst->video->qp_table_size = qsize;
  52. memcpy(dst->video->qp_table, src->qscale_table, qsize);
  53. }
  54. break;
  55. case AVMEDIA_TYPE_AUDIO:
  56. dst->audio->sample_rate = src->sample_rate;
  57. dst->audio->channel_layout = src->channel_layout;
  58. dst->audio->channels = src->channels;
  59. if(src->channels < av_get_channel_layout_nb_channels(src->channel_layout)) {
  60. av_log(NULL, AV_LOG_ERROR, "libavfilter does not support this channel layout\n");
  61. return AVERROR(EINVAL);
  62. }
  63. break;
  64. default:
  65. return AVERROR(EINVAL);
  66. }
  67. return 0;
  68. }
  69. AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
  70. int perms)
  71. {
  72. AVFilterBufferRef *picref =
  73. avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
  74. frame->width, frame->height,
  75. frame->format);
  76. if (!picref)
  77. return NULL;
  78. if (avfilter_copy_frame_props(picref, frame) < 0) {
  79. picref->buf->data[0] = NULL;
  80. avfilter_unref_bufferp(&picref);
  81. }
  82. return picref;
  83. }
  84. AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
  85. int perms)
  86. {
  87. AVFilterBufferRef *samplesref;
  88. int channels = av_frame_get_channels(frame);
  89. int64_t layout = av_frame_get_channel_layout(frame);
  90. if(av_frame_get_channels(frame) > 8) // libavfilter does not suport more than 8 channels FIXME, remove once libavfilter is fixed
  91. return NULL;
  92. if (layout && av_get_channel_layout_nb_channels(layout) != av_frame_get_channels(frame)) {
  93. av_log(0, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
  94. return NULL;
  95. }
  96. samplesref = avfilter_get_audio_buffer_ref_from_arrays_channels(
  97. (uint8_t **)frame->data, frame->linesize[0], perms,
  98. frame->nb_samples, frame->format, channels, layout);
  99. if (!samplesref)
  100. return NULL;
  101. if (avfilter_copy_frame_props(samplesref, frame) < 0) {
  102. samplesref->buf->data[0] = NULL;
  103. avfilter_unref_bufferp(&samplesref);
  104. }
  105. return samplesref;
  106. }
  107. AVFilterBufferRef *avfilter_get_buffer_ref_from_frame(enum AVMediaType type,
  108. const AVFrame *frame,
  109. int perms)
  110. {
  111. switch (type) {
  112. case AVMEDIA_TYPE_VIDEO:
  113. return avfilter_get_video_buffer_ref_from_frame(frame, perms);
  114. case AVMEDIA_TYPE_AUDIO:
  115. return avfilter_get_audio_buffer_ref_from_frame(frame, perms);
  116. default:
  117. return NULL;
  118. }
  119. }
  120. int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
  121. {
  122. int planes, nb_channels;
  123. if (!dst)
  124. return AVERROR(EINVAL);
  125. /* abort in case the src is NULL and dst is not, avoid inconsistent state in dst */
  126. av_assert0(src);
  127. memcpy(dst->data, src->data, sizeof(dst->data));
  128. memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
  129. dst->pts = src->pts;
  130. dst->format = src->format;
  131. av_frame_set_pkt_pos(dst, src->pos);
  132. switch (src->type) {
  133. case AVMEDIA_TYPE_VIDEO:
  134. av_assert0(src->video);
  135. dst->width = src->video->w;
  136. dst->height = src->video->h;
  137. dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
  138. dst->interlaced_frame = src->video->interlaced;
  139. dst->top_field_first = src->video->top_field_first;
  140. dst->key_frame = src->video->key_frame;
  141. dst->pict_type = src->video->pict_type;
  142. break;
  143. case AVMEDIA_TYPE_AUDIO:
  144. av_assert0(src->audio);
  145. nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
  146. planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
  147. if (planes > FF_ARRAY_ELEMS(dst->data)) {
  148. dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
  149. if (!dst->extended_data)
  150. return AVERROR(ENOMEM);
  151. memcpy(dst->extended_data, src->extended_data,
  152. planes * sizeof(*dst->extended_data));
  153. } else
  154. dst->extended_data = dst->data;
  155. dst->nb_samples = src->audio->nb_samples;
  156. av_frame_set_sample_rate (dst, src->audio->sample_rate);
  157. av_frame_set_channel_layout(dst, src->audio->channel_layout);
  158. av_frame_set_channels (dst, src->audio->channels);
  159. break;
  160. default:
  161. return AVERROR(EINVAL);
  162. }
  163. return 0;
  164. }
  165. #ifdef FF_API_FILL_FRAME
  166. int avfilter_fill_frame_from_audio_buffer_ref(AVFrame *frame,
  167. const AVFilterBufferRef *samplesref)
  168. {
  169. return avfilter_copy_buf_props(frame, samplesref);
  170. }
  171. int avfilter_fill_frame_from_video_buffer_ref(AVFrame *frame,
  172. const AVFilterBufferRef *picref)
  173. {
  174. return avfilter_copy_buf_props(frame, picref);
  175. }
  176. int avfilter_fill_frame_from_buffer_ref(AVFrame *frame,
  177. const AVFilterBufferRef *ref)
  178. {
  179. return avfilter_copy_buf_props(frame, ref);
  180. }
  181. #endif