avcodec.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. switch (dst->type) {
  33. case AVMEDIA_TYPE_VIDEO:
  34. dst->video->w = src->width;
  35. dst->video->h = src->height;
  36. dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
  37. dst->video->interlaced = src->interlaced_frame;
  38. dst->video->top_field_first = src->top_field_first;
  39. dst->video->key_frame = src->key_frame;
  40. dst->video->pict_type = src->pict_type;
  41. av_freep(&dst->video->qp_table);
  42. dst->video->qp_table_linesize = 0;
  43. if (src->qscale_table) {
  44. int qsize = src->qstride ? src->qstride * ((src->height+15)/16) : (src->width+15)/16;
  45. dst->video->qp_table = av_malloc(qsize);
  46. if (!dst->video->qp_table)
  47. return AVERROR(ENOMEM);
  48. dst->video->qp_table_linesize = src->qstride;
  49. dst->video->qp_table_size = qsize;
  50. memcpy(dst->video->qp_table, src->qscale_table, qsize);
  51. }
  52. break;
  53. case AVMEDIA_TYPE_AUDIO:
  54. dst->audio->sample_rate = src->sample_rate;
  55. dst->audio->channel_layout = src->channel_layout;
  56. break;
  57. default:
  58. return AVERROR(EINVAL);
  59. }
  60. return 0;
  61. }
  62. AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
  63. int perms)
  64. {
  65. AVFilterBufferRef *picref =
  66. avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
  67. frame->width, frame->height,
  68. frame->format);
  69. if (!picref)
  70. return NULL;
  71. avfilter_copy_frame_props(picref, frame);
  72. return picref;
  73. }
  74. AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
  75. int perms)
  76. {
  77. AVFilterBufferRef *samplesref =
  78. avfilter_get_audio_buffer_ref_from_arrays((uint8_t **)frame->data, frame->linesize[0], perms,
  79. frame->nb_samples, frame->format,
  80. av_frame_get_channel_layout(frame));
  81. if (!samplesref)
  82. return NULL;
  83. avfilter_copy_frame_props(samplesref, frame);
  84. return samplesref;
  85. }
  86. AVFilterBufferRef *avfilter_get_buffer_ref_from_frame(enum AVMediaType type,
  87. const AVFrame *frame,
  88. int perms)
  89. {
  90. switch (type) {
  91. case AVMEDIA_TYPE_VIDEO:
  92. return avfilter_get_video_buffer_ref_from_frame(frame, perms);
  93. case AVMEDIA_TYPE_AUDIO:
  94. return avfilter_get_audio_buffer_ref_from_frame(frame, perms);
  95. default:
  96. return NULL;
  97. }
  98. }
  99. int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
  100. {
  101. int planes, nb_channels;
  102. if (!dst)
  103. return AVERROR(EINVAL);
  104. /* abort in case the src is NULL and dst is not, avoid inconsistent state in dst */
  105. av_assert0(src);
  106. memcpy(dst->data, src->data, sizeof(dst->data));
  107. memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
  108. dst->pts = src->pts;
  109. dst->format = src->format;
  110. av_frame_set_pkt_pos(dst, src->pos);
  111. switch (src->type) {
  112. case AVMEDIA_TYPE_VIDEO:
  113. av_assert0(src->video);
  114. dst->width = src->video->w;
  115. dst->height = src->video->h;
  116. dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
  117. dst->interlaced_frame = src->video->interlaced;
  118. dst->top_field_first = src->video->top_field_first;
  119. dst->key_frame = src->video->key_frame;
  120. dst->pict_type = src->video->pict_type;
  121. break;
  122. case AVMEDIA_TYPE_AUDIO:
  123. av_assert0(src->audio);
  124. nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
  125. planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
  126. if (planes > FF_ARRAY_ELEMS(dst->data)) {
  127. dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
  128. if (!dst->extended_data)
  129. return AVERROR(ENOMEM);
  130. memcpy(dst->extended_data, src->extended_data,
  131. planes * sizeof(dst->extended_data));
  132. } else
  133. dst->extended_data = dst->data;
  134. dst->nb_samples = src->audio->nb_samples;
  135. av_frame_set_sample_rate (dst, src->audio->sample_rate);
  136. av_frame_set_channel_layout(dst, src->audio->channel_layout);
  137. break;
  138. default:
  139. return AVERROR(EINVAL);
  140. }
  141. return 0;
  142. }
  143. #ifdef FF_API_FILL_FRAME
  144. int avfilter_fill_frame_from_audio_buffer_ref(AVFrame *frame,
  145. const AVFilterBufferRef *samplesref)
  146. {
  147. return avfilter_copy_buf_props(frame, samplesref);
  148. }
  149. int avfilter_fill_frame_from_video_buffer_ref(AVFrame *frame,
  150. const AVFilterBufferRef *picref)
  151. {
  152. return avfilter_copy_buf_props(frame, picref);
  153. }
  154. int avfilter_fill_frame_from_buffer_ref(AVFrame *frame,
  155. const AVFilterBufferRef *ref)
  156. {
  157. return avfilter_copy_buf_props(frame, ref);
  158. }
  159. #endif