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.

160 lines
4.3KB

  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. #include "bsf_internal.h"
  19. #include "cbs_bsf.h"
  20. static int cbs_bsf_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
  21. {
  22. CBSBSFContext *ctx = bsf->priv_data;
  23. CodedBitstreamFragment *frag = &ctx->fragment;
  24. uint8_t *side_data;
  25. buffer_size_t side_data_size;
  26. int err;
  27. side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
  28. &side_data_size);
  29. if (!side_data_size)
  30. return 0;
  31. err = ff_cbs_read(ctx->input, frag, side_data, side_data_size);
  32. if (err < 0) {
  33. av_log(bsf, AV_LOG_ERROR,
  34. "Failed to read extradata from packet side data.\n");
  35. return err;
  36. }
  37. err = ctx->type->update_fragment(bsf, NULL, frag);
  38. if (err < 0)
  39. return err;
  40. err = ff_cbs_write_fragment_data(ctx->output, frag);
  41. if (err < 0) {
  42. av_log(bsf, AV_LOG_ERROR,
  43. "Failed to write extradata into packet side data.\n");
  44. return err;
  45. }
  46. side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
  47. frag->data_size);
  48. if (!side_data)
  49. return AVERROR(ENOMEM);
  50. memcpy(side_data, frag->data, frag->data_size);
  51. ff_cbs_fragment_reset(frag);
  52. return 0;
  53. }
  54. int ff_cbs_bsf_generic_filter(AVBSFContext *bsf, AVPacket *pkt)
  55. {
  56. CBSBSFContext *ctx = bsf->priv_data;
  57. CodedBitstreamFragment *frag = &ctx->fragment;
  58. int err;
  59. err = ff_bsf_get_packet_ref(bsf, pkt);
  60. if (err < 0)
  61. return err;
  62. err = cbs_bsf_update_side_data(bsf, pkt);
  63. if (err < 0)
  64. goto fail;
  65. err = ff_cbs_read_packet(ctx->input, frag, pkt);
  66. if (err < 0) {
  67. av_log(bsf, AV_LOG_ERROR, "Failed to read %s from packet.\n",
  68. ctx->type->fragment_name);
  69. goto fail;
  70. }
  71. if (frag->nb_units == 0) {
  72. av_log(bsf, AV_LOG_ERROR, "No %s found in packet.\n",
  73. ctx->type->unit_name);
  74. err = AVERROR_INVALIDDATA;
  75. goto fail;
  76. }
  77. err = ctx->type->update_fragment(bsf, pkt, frag);
  78. if (err < 0)
  79. goto fail;
  80. err = ff_cbs_write_packet(ctx->output, pkt, frag);
  81. if (err < 0) {
  82. av_log(bsf, AV_LOG_ERROR, "Failed to write %s into packet.\n",
  83. ctx->type->fragment_name);
  84. goto fail;
  85. }
  86. err = 0;
  87. fail:
  88. ff_cbs_fragment_reset(frag);
  89. if (err < 0)
  90. av_packet_unref(pkt);
  91. return err;
  92. }
  93. int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type)
  94. {
  95. CBSBSFContext *ctx = bsf->priv_data;
  96. CodedBitstreamFragment *frag = &ctx->fragment;
  97. int err;
  98. ctx->type = type;
  99. err = ff_cbs_init(&ctx->input, type->codec_id, bsf);
  100. if (err < 0)
  101. return err;
  102. err = ff_cbs_init(&ctx->output, type->codec_id, bsf);
  103. if (err < 0)
  104. return err;
  105. if (bsf->par_in->extradata) {
  106. err = ff_cbs_read_extradata(ctx->input, frag, bsf->par_in);
  107. if (err < 0) {
  108. av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
  109. goto fail;
  110. }
  111. err = type->update_fragment(bsf, NULL, frag);
  112. if (err < 0)
  113. goto fail;
  114. err = ff_cbs_write_extradata(ctx->output, bsf->par_out, frag);
  115. if (err < 0) {
  116. av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
  117. goto fail;
  118. }
  119. }
  120. err = 0;
  121. fail:
  122. ff_cbs_fragment_reset(frag);
  123. return err;
  124. }
  125. void ff_cbs_bsf_generic_close(AVBSFContext *bsf)
  126. {
  127. CBSBSFContext *ctx = bsf->priv_data;
  128. ff_cbs_fragment_free(&ctx->fragment);
  129. ff_cbs_close(&ctx->input);
  130. ff_cbs_close(&ctx->output);
  131. }