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.

127 lines
3.4KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdio.h>
  19. #include "libavutil/avstring.h"
  20. #include "libavutil/common.h"
  21. #include "libavutil/log.h"
  22. #include "bsf.h"
  23. #include "cbs.h"
  24. typedef struct TraceHeadersContext {
  25. CodedBitstreamContext cbc;
  26. } TraceHeadersContext;
  27. static int trace_headers_init(AVBSFContext *bsf)
  28. {
  29. TraceHeadersContext *ctx = bsf->priv_data;
  30. int err;
  31. err = ff_cbs_init(&ctx->cbc, bsf->par_in->codec_id, bsf);
  32. if (err < 0)
  33. return err;
  34. ctx->cbc.trace_enable = 1;
  35. ctx->cbc.trace_level = AV_LOG_INFO;
  36. if (bsf->par_in->extradata) {
  37. CodedBitstreamFragment ps;
  38. av_log(bsf, AV_LOG_INFO, "Extradata\n");
  39. err = ff_cbs_read_extradata(&ctx->cbc, &ps, bsf->par_in);
  40. if (err < 0) {
  41. av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
  42. return err;
  43. }
  44. ff_cbs_fragment_uninit(&ctx->cbc, &ps);
  45. }
  46. return 0;
  47. }
  48. static void trace_headers_close(AVBSFContext *bsf)
  49. {
  50. TraceHeadersContext *ctx = bsf->priv_data;
  51. ff_cbs_close(&ctx->cbc);
  52. }
  53. static int trace_headers(AVBSFContext *bsf, AVPacket *out)
  54. {
  55. TraceHeadersContext *ctx = bsf->priv_data;
  56. CodedBitstreamFragment au;
  57. AVPacket *in;
  58. char tmp[256] = { 0 };
  59. int err;
  60. err = ff_bsf_get_packet(bsf, &in);
  61. if (err < 0)
  62. return err;
  63. if (in->flags & AV_PKT_FLAG_KEY)
  64. av_strlcat(tmp, ", key frame", sizeof(tmp));
  65. if (in->flags & AV_PKT_FLAG_CORRUPT)
  66. av_strlcat(tmp, ", corrupt", sizeof(tmp));
  67. if (in->pts != AV_NOPTS_VALUE)
  68. av_strlcatf(tmp, sizeof(tmp), ", pts %"PRId64, in->pts);
  69. else
  70. av_strlcat(tmp, ", no pts", sizeof(tmp));
  71. if (in->dts != AV_NOPTS_VALUE)
  72. av_strlcatf(tmp, sizeof(tmp), ", dts %"PRId64, in->dts);
  73. else
  74. av_strlcat(tmp, ", no dts", sizeof(tmp));
  75. if (in->duration > 0)
  76. av_strlcatf(tmp, sizeof(tmp), ", duration %"PRId64, in->duration);
  77. av_log(bsf, AV_LOG_INFO, "Packet: %d bytes%s.\n", in->size, tmp);
  78. err = ff_cbs_read_packet(&ctx->cbc, &au, in);
  79. if (err < 0)
  80. return err;
  81. ff_cbs_fragment_uninit(&ctx->cbc, &au);
  82. av_packet_move_ref(out, in);
  83. av_packet_free(&in);
  84. return 0;
  85. }
  86. static const enum AVCodecID trace_headers_codec_ids[] = {
  87. AV_CODEC_ID_H264,
  88. AV_CODEC_ID_HEVC,
  89. AV_CODEC_ID_MPEG2VIDEO,
  90. AV_CODEC_ID_NONE,
  91. };
  92. const AVBitStreamFilter ff_trace_headers_bsf = {
  93. .name = "trace_headers",
  94. .priv_data_size = sizeof(TraceHeadersContext),
  95. .init = &trace_headers_init,
  96. .close = &trace_headers_close,
  97. .filter = &trace_headers,
  98. .codec_ids = trace_headers_codec_ids,
  99. };