cbs_bsf.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifndef AVCODEC_CBS_BSF_H
  19. #define AVCODEC_CBS_BSF_H
  20. #include "libavutil/log.h"
  21. #include "libavutil/opt.h"
  22. #include "bsf.h"
  23. #include "codec_id.h"
  24. #include "cbs.h"
  25. #include "packet.h"
  26. typedef struct CBSBSFType {
  27. enum AVCodecID codec_id;
  28. // Name of a frame fragment in this codec (e.g. "access unit",
  29. // "temporal unit").
  30. const char *fragment_name;
  31. // Name of a unit for this BSF, for use in error messages (e.g.
  32. // "NAL unit", "OBU").
  33. const char *unit_name;
  34. // Update the content of a fragment with whatever metadata changes
  35. // are desired. The associated AVPacket is provided so that any side
  36. // data associated with the fragment can be inspected or edited. If
  37. // pkt is NULL, then an extradata header fragment is being updated.
  38. int (*update_fragment)(AVBSFContext *bsf, AVPacket *pkt,
  39. CodedBitstreamFragment *frag);
  40. } CBSBSFType;
  41. // Common structure for all generic CBS BSF users. An instance of this
  42. // structure must be the first member of the BSF private context (to be
  43. // pointed to by AVBSFContext.priv_data).
  44. typedef struct CBSBSFContext {
  45. const AVClass *class;
  46. const CBSBSFType *type;
  47. CodedBitstreamContext *input;
  48. CodedBitstreamContext *output;
  49. CodedBitstreamFragment fragment;
  50. } CBSBSFContext;
  51. /**
  52. * Initialise generic CBS BSF setup.
  53. *
  54. * Creates the input and output CBS instances, and applies the filter to
  55. * the extradata on the input codecpar if any is present.
  56. *
  57. * Since it calls the update_fragment() function immediately to deal with
  58. * extradata, this should be called after any codec-specific setup is done
  59. * (probably at the end of the FFBitStreamFilter.init function).
  60. */
  61. int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type);
  62. /**
  63. * Close a generic CBS BSF instance.
  64. *
  65. * If no other deinitialisation is required then this function can be used
  66. * directly as FFBitStreamFilter.close.
  67. */
  68. void ff_cbs_bsf_generic_close(AVBSFContext *bsf);
  69. /**
  70. * Filter operation for CBS BSF.
  71. *
  72. * Reads the input packet into a CBS fragment, calls update_fragment() on
  73. * it, then writes the result to an output packet. If the input packet
  74. * has AV_PKT_DATA_NEW_EXTRADATA side-data associated with it then it does
  75. * the same thing to that new extradata to form the output side-data first.
  76. *
  77. * If the BSF does not do anything else then this function can be used
  78. * directly as FFBitStreamFilter.filter.
  79. */
  80. int ff_cbs_bsf_generic_filter(AVBSFContext *bsf, AVPacket *pkt);
  81. // Options for element manipulation.
  82. enum {
  83. // Pass this element through unchanged.
  84. BSF_ELEMENT_PASS,
  85. // Insert this element, replacing any existing instances of it.
  86. // Associated values may be provided explicitly (as additional options)
  87. // or implicitly (either as side data or deduced from other parts of
  88. // the stream).
  89. BSF_ELEMENT_INSERT,
  90. // Remove this element if it appears in the stream.
  91. BSF_ELEMENT_REMOVE,
  92. // Extract this element to side data, so that further manipulation
  93. // can happen elsewhere.
  94. BSF_ELEMENT_EXTRACT,
  95. };
  96. #define BSF_ELEMENT_OPTIONS_PIR(name, help, field, opt_flags) \
  97. { name, help, OFFSET(field), AV_OPT_TYPE_INT, \
  98. { .i64 = BSF_ELEMENT_PASS }, \
  99. BSF_ELEMENT_PASS, BSF_ELEMENT_REMOVE, opt_flags, name }, \
  100. { "pass", NULL, 0, AV_OPT_TYPE_CONST, \
  101. { .i64 = BSF_ELEMENT_PASS }, .flags = opt_flags, .unit = name }, \
  102. { "insert", NULL, 0, AV_OPT_TYPE_CONST, \
  103. { .i64 = BSF_ELEMENT_INSERT }, .flags = opt_flags, .unit = name }, \
  104. { "remove", NULL, 0, AV_OPT_TYPE_CONST, \
  105. { .i64 = BSF_ELEMENT_REMOVE }, .flags = opt_flags, .unit = name }
  106. #define BSF_ELEMENT_OPTIONS_PIRE(name, help, field, opt_flags) \
  107. { name, help, OFFSET(field), AV_OPT_TYPE_INT, \
  108. { .i64 = BSF_ELEMENT_PASS }, \
  109. BSF_ELEMENT_PASS, BSF_ELEMENT_EXTRACT, opt_flags, name }, \
  110. { "pass", NULL, 0, AV_OPT_TYPE_CONST, \
  111. { .i64 = BSF_ELEMENT_PASS }, .flags = opt_flags, .unit = name }, \
  112. { "insert", NULL, 0, AV_OPT_TYPE_CONST, \
  113. { .i64 = BSF_ELEMENT_INSERT }, .flags = opt_flags, .unit = name }, \
  114. { "remove", NULL, 0, AV_OPT_TYPE_CONST, \
  115. { .i64 = BSF_ELEMENT_REMOVE }, .flags = opt_flags, .unit = name }, \
  116. { "extract", NULL, 0, AV_OPT_TYPE_CONST, \
  117. { .i64 = BSF_ELEMENT_EXTRACT }, .flags = opt_flags, .unit = name } \
  118. #endif /* AVCODEC_CBS_BSF_H */