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.

117 lines
3.3KB

  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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. };
  28. typedef struct RemoveExtradataContext {
  29. const AVClass *class;
  30. int freq;
  31. AVCodecParserContext *parser;
  32. AVCodecContext *avctx;
  33. } RemoveExtradataContext;
  34. static int remove_extradata(AVBSFContext *ctx, AVPacket *out)
  35. {
  36. RemoveExtradataContext *s = ctx->priv_data;
  37. AVPacket *in;
  38. int ret;
  39. ret = ff_bsf_get_packet(ctx, &in);
  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_KEYFRAME && in->flags & AV_PKT_FLAG_KEY)) {
  45. int i = s->parser->parser->split(s->avctx, in->data, in->size);
  46. in->data += i;
  47. in->size -= i;
  48. }
  49. }
  50. av_packet_move_ref(out, in);
  51. av_packet_free(&in);
  52. return 0;
  53. }
  54. static int remove_extradata_init(AVBSFContext *ctx)
  55. {
  56. RemoveExtradataContext *s = ctx->priv_data;
  57. int ret;
  58. s->parser = av_parser_init(ctx->par_in->codec_id);
  59. if (s->parser) {
  60. s->avctx = avcodec_alloc_context3(NULL);
  61. if (!s->avctx)
  62. return AVERROR(ENOMEM);
  63. ret = avcodec_parameters_to_context(s->avctx, ctx->par_in);
  64. if (ret < 0)
  65. return ret;
  66. }
  67. return 0;
  68. }
  69. static void remove_extradata_close(AVBSFContext *ctx)
  70. {
  71. RemoveExtradataContext *s = ctx->priv_data;
  72. avcodec_free_context(&s->avctx);
  73. av_parser_close(s->parser);
  74. }
  75. #define OFFSET(x) offsetof(RemoveExtradataContext, x)
  76. static const AVOption options[] = {
  77. { "freq", NULL, OFFSET(freq), AV_OPT_TYPE_INT, { .i64 = REMOVE_FREQ_KEYFRAME }, REMOVE_FREQ_KEYFRAME, REMOVE_FREQ_ALL, 0, "freq" },
  78. { "keyframe", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_KEYFRAME }, .unit = "freq" },
  79. { "all", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_ALL }, .unit = "freq" },
  80. { NULL },
  81. };
  82. static const AVClass remove_extradata_class = {
  83. .class_name = "remove_extradata",
  84. .item_name = av_default_item_name,
  85. .option = options,
  86. .version = LIBAVUTIL_VERSION_INT,
  87. };
  88. const AVBitStreamFilter ff_remove_extradata_bsf = {
  89. .name = "remove_extra",
  90. .priv_data_size = sizeof(RemoveExtradataContext),
  91. .priv_class = &remove_extradata_class,
  92. .init = remove_extradata_init,
  93. .close = remove_extradata_close,
  94. .filter = remove_extradata,
  95. };