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.

181 lines
6.6KB

  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. /**
  19. * @file
  20. * filter for manipulating frame side data
  21. */
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/internal.h"
  24. #include "libavutil/frame.h"
  25. #include "libavutil/opt.h"
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. enum SideDataMode {
  30. SIDEDATA_SELECT,
  31. SIDEDATA_DELETE,
  32. SIDEDATA_NB
  33. };
  34. typedef struct SideDataContext {
  35. const AVClass *class;
  36. int mode;
  37. enum AVFrameSideDataType type;
  38. } SideDataContext;
  39. #define OFFSET(x) offsetof(SideDataContext, x)
  40. #define DEFINE_OPTIONS(filt_name, FLAGS) \
  41. static const AVOption filt_name##_options[] = { \
  42. { "mode", "set a mode of operation", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, SIDEDATA_NB-1, FLAGS, "mode" }, \
  43. { "select", "select frame", 0, AV_OPT_TYPE_CONST, {.i64 = SIDEDATA_SELECT }, 0, 0, FLAGS, "mode" }, \
  44. { "delete", "delete side data", 0, AV_OPT_TYPE_CONST, {.i64 = SIDEDATA_DELETE }, 0, 0, FLAGS, "mode" }, \
  45. { "type", "set side data type", OFFSET(type), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX, FLAGS, "type" }, \
  46. { "PANSCAN", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_PANSCAN }, 0, 0, FLAGS, "type" }, \
  47. { "A53_CC", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_A53_CC }, 0, 0, FLAGS, "type" }, \
  48. { "STEREO3D", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_STEREO3D }, 0, 0, FLAGS, "type" }, \
  49. { "MATRIXENCODING", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_MATRIXENCODING }, 0, 0, FLAGS, "type" }, \
  50. { "DOWNMIX_INFO", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DOWNMIX_INFO }, 0, 0, FLAGS, "type" }, \
  51. { "REPLAYGAIN", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_REPLAYGAIN }, 0, 0, FLAGS, "type" }, \
  52. { "DISPLAYMATRIX", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_DISPLAYMATRIX }, 0, 0, FLAGS, "type" }, \
  53. { "AFD", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_AFD }, 0, 0, FLAGS, "type" }, \
  54. { "MOTION_VECTORS", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_MOTION_VECTORS }, 0, 0, FLAGS, "type" }, \
  55. { "SKIP_SAMPLES", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_SKIP_SAMPLES }, 0, 0, FLAGS, "type" }, \
  56. { "AUDIO_SERVICE_TYPE", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_AUDIO_SERVICE_TYPE }, 0, 0, FLAGS, "type" }, \
  57. { "MASTERING_DISPLAY_METADATA", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_MASTERING_DISPLAY_METADATA }, 0, 0, FLAGS, "type" }, \
  58. { "GOP_TIMECODE", "", 0, AV_OPT_TYPE_CONST, {.i64 = AV_FRAME_DATA_GOP_TIMECODE }, 0, 0, FLAGS, "type" }, \
  59. { NULL } \
  60. }
  61. static av_cold int init(AVFilterContext *ctx)
  62. {
  63. SideDataContext *s = ctx->priv;
  64. if (s->type == -1 && s->mode != SIDEDATA_DELETE) {
  65. av_log(ctx, AV_LOG_ERROR, "Side data type must be set\n");
  66. return AVERROR(EINVAL);
  67. }
  68. return 0;
  69. }
  70. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  71. {
  72. AVFilterContext *ctx = inlink->dst;
  73. AVFilterLink *outlink = ctx->outputs[0];
  74. SideDataContext *s = ctx->priv;
  75. AVFrameSideData *sd = NULL;
  76. if (s->type != -1)
  77. sd = av_frame_get_side_data(frame, s->type);
  78. switch (s->mode) {
  79. case SIDEDATA_SELECT:
  80. if (sd) {
  81. return ff_filter_frame(outlink, frame);
  82. }
  83. break;
  84. case SIDEDATA_DELETE:
  85. if (s->type == -1) {
  86. while (frame->nb_side_data)
  87. av_frame_remove_side_data(frame, frame->side_data[0]->type);
  88. } else if (sd) {
  89. av_frame_remove_side_data(frame, s->type);
  90. }
  91. return ff_filter_frame(outlink, frame);
  92. break;
  93. default:
  94. av_assert0(0);
  95. };
  96. av_frame_free(&frame);
  97. return 0;
  98. }
  99. #if CONFIG_ASIDEDATA_FILTER
  100. DEFINE_OPTIONS(asidedata, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
  101. AVFILTER_DEFINE_CLASS(asidedata);
  102. static const AVFilterPad ainputs[] = {
  103. {
  104. .name = "default",
  105. .type = AVMEDIA_TYPE_AUDIO,
  106. .filter_frame = filter_frame,
  107. },
  108. { NULL }
  109. };
  110. static const AVFilterPad aoutputs[] = {
  111. {
  112. .name = "default",
  113. .type = AVMEDIA_TYPE_AUDIO,
  114. },
  115. { NULL }
  116. };
  117. AVFilter ff_af_asidedata = {
  118. .name = "asidedata",
  119. .description = NULL_IF_CONFIG_SMALL("Manipulate audio frame side data."),
  120. .priv_size = sizeof(SideDataContext),
  121. .priv_class = &asidedata_class,
  122. .init = init,
  123. .inputs = ainputs,
  124. .outputs = aoutputs,
  125. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  126. };
  127. #endif /* CONFIG_ASIDEDATA_FILTER */
  128. #if CONFIG_SIDEDATA_FILTER
  129. DEFINE_OPTIONS(sidedata, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
  130. AVFILTER_DEFINE_CLASS(sidedata);
  131. static const AVFilterPad inputs[] = {
  132. {
  133. .name = "default",
  134. .type = AVMEDIA_TYPE_VIDEO,
  135. .filter_frame = filter_frame,
  136. },
  137. { NULL }
  138. };
  139. static const AVFilterPad outputs[] = {
  140. {
  141. .name = "default",
  142. .type = AVMEDIA_TYPE_VIDEO,
  143. },
  144. { NULL }
  145. };
  146. AVFilter ff_vf_sidedata = {
  147. .name = "sidedata",
  148. .description = NULL_IF_CONFIG_SMALL("Manipulate video frame side data."),
  149. .priv_size = sizeof(SideDataContext),
  150. .priv_class = &sidedata_class,
  151. .init = init,
  152. .inputs = inputs,
  153. .outputs = outputs,
  154. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  155. };
  156. #endif /* CONFIG_SIDEDATA_FILTER */