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.

118 lines
3.7KB

  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  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. #include "libavutil/log.h"
  21. #include "libavutil/opt.h"
  22. #include "avcodec.h"
  23. #include "bsf.h"
  24. enum RemoveFreq {
  25. REMOVE_FREQ_KEYFRAME,
  26. REMOVE_FREQ_ALL,
  27. REMOVE_FREQ_NONKEYFRAME,
  28. };
  29. typedef struct RemoveExtradataContext {
  30. const AVClass *class;
  31. int freq;
  32. AVCodecParserContext *parser;
  33. AVCodecContext *avctx;
  34. } RemoveExtradataContext;
  35. static int remove_extradata(AVBSFContext *ctx, AVPacket *pkt)
  36. {
  37. RemoveExtradataContext *s = ctx->priv_data;
  38. int ret;
  39. ret = ff_bsf_get_packet_ref(ctx, pkt);
  40. if (ret < 0)
  41. return ret;
  42. if (s->parser && s->parser->parser->split) {
  43. if (s->freq == REMOVE_FREQ_ALL ||
  44. (s->freq == REMOVE_FREQ_NONKEYFRAME && !(pkt->flags & AV_PKT_FLAG_KEY)) ||
  45. (s->freq == REMOVE_FREQ_KEYFRAME && pkt->flags & AV_PKT_FLAG_KEY)) {
  46. int i = s->parser->parser->split(s->avctx, pkt->data, pkt->size);
  47. pkt->data += i;
  48. pkt->size -= i;
  49. }
  50. }
  51. return 0;
  52. }
  53. static int remove_extradata_init(AVBSFContext *ctx)
  54. {
  55. RemoveExtradataContext *s = ctx->priv_data;
  56. int ret;
  57. s->parser = av_parser_init(ctx->par_in->codec_id);
  58. if (s->parser) {
  59. s->avctx = avcodec_alloc_context3(NULL);
  60. if (!s->avctx)
  61. return AVERROR(ENOMEM);
  62. ret = avcodec_parameters_to_context(s->avctx, ctx->par_in);
  63. if (ret < 0)
  64. return ret;
  65. }
  66. return 0;
  67. }
  68. static void remove_extradata_close(AVBSFContext *ctx)
  69. {
  70. RemoveExtradataContext *s = ctx->priv_data;
  71. avcodec_free_context(&s->avctx);
  72. av_parser_close(s->parser);
  73. }
  74. #define OFFSET(x) offsetof(RemoveExtradataContext, x)
  75. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
  76. static const AVOption options[] = {
  77. { "freq", NULL, OFFSET(freq), AV_OPT_TYPE_INT, { .i64 = REMOVE_FREQ_KEYFRAME }, REMOVE_FREQ_KEYFRAME, REMOVE_FREQ_NONKEYFRAME, FLAGS, "freq" },
  78. { "k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_NONKEYFRAME }, .flags = FLAGS, .unit = "freq" },
  79. { "keyframe", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_KEYFRAME }, .flags = FLAGS, .unit = "freq" },
  80. { "e", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_ALL }, .flags = FLAGS, .unit = "freq" },
  81. { "all", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_ALL }, .flags = FLAGS, .unit = "freq" },
  82. { NULL },
  83. };
  84. static const AVClass remove_extradata_class = {
  85. .class_name = "remove_extradata",
  86. .item_name = av_default_item_name,
  87. .option = options,
  88. .version = LIBAVUTIL_VERSION_INT,
  89. };
  90. const AVBitStreamFilter ff_remove_extradata_bsf = {
  91. .name = "remove_extra",
  92. .priv_data_size = sizeof(RemoveExtradataContext),
  93. .priv_class = &remove_extradata_class,
  94. .init = remove_extradata_init,
  95. .close = remove_extradata_close,
  96. .filter = remove_extradata,
  97. };